code, python

How to Create an Algo Trading Bot

Not long ago, Quantopian allowed you to live trade algorithms developed with their software. Sadly they no longer let you do that. It was a painful process but I’ve figured out how to use Interactive Brokers API to create an algorithmic trading bot and run it on a server.

In this post I’ll cover how to setup the API.

Set Up Your Account

I evaluated a few other brokers and the API for IB is excellent compared to the undocumented piles of junk with minimal open source packages other brokers have.

First up you need an account with Interactive Brokers (IB). Then download Trader Workstation (TWS) and IB Gateway.

I also recommend you create a paper trading account by logging into Account Management -> Settings -> Account Settings and then click the gear next to “Paper Trading Account”.

Hello World with Trades

If you have any problems with any steps feel free to leave a comment and I’ll help you out

Install Python API Clients

We’re going to use ib_insync, an unofficial IB API client wrapper, to simplify API access. Use Python 3.6 and follow their install steps but when they say to download the official IB API, make sure you select the “Latest” IB API (at least 9.73 to support python, NOT 9.72)

Install the official API by running “python build install” in your ib_insync python environment after unzipping the API. DO NOT USE PIP.

Set Up TWS API Settings

Open Trader Workstation on your computer and login with your paper trading account (unless you’re feeling adventurous).

Click on the wrench icon in the upper right

TWS Settings

Click on API and then settings in the dialog that opens up. Your settings should look like this

API Settings(make sure Read-Only API is deselected and Enable ActiveX and Socket Clients is enabled). Also make note of your port, you’ll use this number later when we run ib_insync)

And that’s all you need to do with TWS (you could also use the more lightweight IB Gateway but I prefer using TWS so I can see monitor the trading algorithm easier on my local machine).

Connect to Interactive Brokers with ib_insync

Follow along ib_insync’s example notebooks and be sure to read their main API documentation page regarding the ib_insync event loop.

ib_insync orders notebook
An excerpt from the ib_insync notebook for placing orders

Next Steps

Now that you can interact with a broker’s API your next step should be to create an algorithm. Figure out what signals you need, create a control loop, and start testing!

Once you feel confident with your algorithm you should test it out on a server, write up coming soon.

If you can’t wait for me to write that post and are feeling like an 31$$7 h4x0r go download IB Controller¬†on a server, configure your account settings, and run it with xrfb-run. I had to setup X-11 forwarding with xming on my machine to get IB gateway and TWS to install and it was quite a pain but YMMV.

6 thoughts on “How to Create an Algo Trading Bot

  1. Hi !

    Great job…

    First read your May’21 blog on the basic pair trade backtester.

    Good that I started with IB after a long break. They still are unmatched with professional tools, that ThinkOrSwim cannot do. Never mind the bad IB customer service !

    So can you link to a couple of trading bots ready made code I could use with my paper trade account, that I am using now for options spreads ( risk defined)?

Leave a Reply

Your email address will not be published. Required fields are marked *