Trading Automation with Interactive Broker API, Python and Docker
Crypto trading has been all the rage over the past few years, and people tend to forget that there are many many more trading opportunities in the real world than in the crypto world. Unfortunately, the real world is often using outdated tech and it can be a real pain to finally manage to perform automatic actions on the stock exchanges.
Here, we are going to quickly set up docker containers that will allow you to automate your trading strategy in the real world, using Interactive Broker API. Interactive Broker is one of the most famous and versatile platforms for trading. Its API is quite clunky but thanks to the software popularity, people have developed nice wrappers around it.
An important note: I’ve tried many systems and after a few days of exploration, I found out that Interactive Broker is the most practical one, allows you to perform all kinds of actions, is available in the USA and in Europe. All other tools were either USA-only, either limited in terms of features scope (ex: limited to Forex).
So, how do we go about it? The first thing is to get Interactive Broker (IB) API to run in headless mode, inside a docker container. Originally, IB is a trading platform with a GUI, and the API is online when the GUI is open. If you want to automate stuff, it’s better to have something running headless in a Docker container on a Linux server. In order to achieve that, we used Dockerfile from https://github.com/ryankennedyio/ib-docker and we then modified 1 or 2 things such as using a docker-compose.yml, and adding a few comments.
Then, we use PostgreSQL to store data we retrieve from the API, and Pgweb to visualise the DB content (useful for debugging). We then use Python Celery to run periodic tasks (fetch stock market data every X min), Celery flower to visualise the queue, and Grafana to explore our data and get nice charts.
Here is the full docker-compose.yml :
You’ll find the full github repo here, with instructions to get started:
Important disclaimer:
- You cannot perform high-frequency trading with this setup. Interactive Broker is a broker, which means you’re not talking directly to the exchanges. Moreover, your internet connection is probably too slow compared to the big boys, and none of this code is optimized for speed.
- You will probably not get rich with automated trading! Brokers usually sell their customers order books to bigger fishes before executing them! The optimal use-case for this tutorial is to automate mundane and repetitive trading actions in a context of a long-term trading strategy. If I reformulate: if you don’t already have a winning trading strategy, automation won’t give you one!
Originally published at http://fruty.io on July 30, 2019.