Join the Quant Scientist Newsletter

Gain access to exclusive tools that Wall Street's Elite don't want you to have. Don't miss the next issue...

Join 11,500+ Quant Scientists learning one article at a time

Join 11,500+ Quant Scientists learning one article at a time

Automating Trades with Interactive Brokers Trader Workstation and Python

Using Python to Automate Trades with Interactive Brokers

July 28, 20233 min read

Today, we are sharing some of the progress we've been making on the course. Our goal is to take you end-to-end from installing the Python Quant Scientist Stack all the way through submitting trades. And today, we are sharing a prototype that covers:

  • Creating a stock scanner

  • Identifying a list of equites to trade

  • Using quantitative portfolio construction to build a volatility targeted portfolio using our proprietary Python Quant Scientist Stack

  • Calculate optimal weights based on portfolio value

  • BONUS: Automatically send those trades to our Interactive Brokers Trader Workstation from Python 👇

Interactive Brokers Screen

Join the Quant Scientist Newsletter (and Get the Code)

Want exclusive access to the FULL codebase for this Quant Science tutorial plus dozens more? Then:

Quant Scientist Newsletter - Get the Code

Free Video Tutorial

This post comes with an 8 minute video tutorial showcasing everything we cover in this tutorial.

Step 1: we create a quick screener

We'll use openbb to import a list of stock tickers. The screener will search for stocks with new high prices greater than 15 and within the USA. We then collect the stock prices from our desired time range. And we convert from prices to percentage returns.

openbb screener

This gives us the stock returns for 119 equities.

Stock Returns from our Scanner

Step 2: Using Riskfolio

We'll use Riskfolio to do computational analysis and produce a constrained portfolio.

Riskfolio Constrained Portfolio

This produces the portfolio weights for our Volatility Weighted Portfolio.

Riskfolio Portfolio Weights

Step 3: Compute the Number of Shares to Trade

Next, we just need to get the number of shares to trade which we can see in the 4th column ("shares").

Number of Shares to Trade

Now we just need to iterate through each of these to place the trades. We will walk through this in the course, but just recognize that we can convert the portfolio's shares to Interactive Brokers contracts and place the orders.

Step 4: Connect to the Interactive Brokers API

The last step is to connect to the Interactive Brokers (IB) API. This will allow us to place the trades via IB Contracts.

Connect to Interactive Brokers API

We can quickly check that we are now connect to the Interactive Brokers Trader Workstation via the API connection.

Interactive Brokers Trader Workstation (API Connection)

Step 5: Submit the Orders

The last thing we do is submit the orders to Interactive Brokers. We run this Python code:

Submit Orders to Interactive Brokers via Python API

We have just submitted 119 orders. Not all of the orders will be filled, but we can see that the grayed out assets have been filled along with their fill price.

Interactive Brokers Orders Being Filled

Conclusions

This is what we are working on. The idea is to take you from Ground Zero all the way to where you can automatically submit trades, manage risk, and use Python with Interactive Brokers all from start to finish.

Are you feeling lost when trying to learn Algorithmic Trading?

There's nothing worse than going at this alone--

Learning Python is tough.

❌ Learning Trading is tough.

Learning Math & Stats is tough.

It's no wonder why it's easy to feel lost.

And all of this increases the likelihood you will fail (not to mention lose money in the process). Protect your future.

Python for Algorithmic Trading Course

👉 Join 800+ future Quant Scientists on our Python for Algorithmic Trading Course Waitlist: https://learn.quantscience.io/python-algorithmic-trading-course-waitlist

button course waitlist

investingstockspythonalgorithmic tradinginteractive brokers
Matt is a Data Science expert with over 18 years working in business and 10+ years as a Data Scientist, Consultant, and Trainer. Matt has built Business Science, a successful educational platform with similar goals to Quant Science, but focused on developing Data Scientists in business, marketing, and finance disciplines.

Matt Dancho

Matt is a Data Science expert with over 18 years working in business and 10+ years as a Data Scientist, Consultant, and Trainer. Matt has built Business Science, a successful educational platform with similar goals to Quant Science, but focused on developing Data Scientists in business, marketing, and finance disciplines.

Back to Blog

Start Your Journey To Becoming A Quant Today!

Join the Quant Scientist Newsletter

Gain access to exclusive tools that Wall Street's Elite don't want you to have. Don't miss the next issue...

Join 11,500+ Quant Scientists learning one article at a time

Join 11,500+ Quant Scientists learning one article at a time

© 2024 Quant Science - All Rights Reserved

Cohort 6 launches Wednesday, October 2nd at 10AM EST