A basic trading model on Interactive Brokers' API dealing with high-frequency data studies.
19 Jun 2019
ibpy
library is dropped in favour of the newer ib_insync
library.ib_insync
, compatible with Python 3.7. Includes various code cleanup.matplotlib
charting in favour of headless running inside Docker.14 Jun 2019
Merged pull request from: https://github.com/chicago-joe/IB_PairsTrading_Algo
Thanks to chicago-joe for updating to work with Python 3.
As this is only a compatibility update, there are many outdated components and the trading model is quite unlikely to be working as intended.
8 Jun 2015
You can choose to run this model in your console OR in Docker.
Steps to run the trading model on your command line:
Within a Python 3.7 environment, install the requirements:
pip install -r requirements.txt
In IB Trader Workstation (TWS), go to Configuration > Api > Settings and:
Update main.py
with the required parameters and run the model with the command:
python main.py
This step is optional. You can choose to deploy one or several instances of these algos on a remote machine for execution using Docker.
A Docker container helps to automatically build your running environment and isolate changes, all in just a few simple commands!
To run this trading model in headless mode:
In TWS, ensure that remote API connections are accepted and the Docker machine's IP is added to Trusted IPs.
Ensure your machine has docker and docker-compose installed. Build the image with this command:
docker-compose build
Update the parameters in docker-compose.yml
. I've set the TWS_HOST
value in my environment variables. This is the IP address of the remote machine running TWS. Or, you can just manually enter the IP address value directly. Then, run the image as a container instance:
docker-compose up
To run in headless mode, simply add the detached command -d
, like this:
docker-compose up -d
In headless mode, you would have to start and stop the containers manually.
At the present moment, this model utilizes statistical arbitrage incorporating these methodologies:
Other functions:
And greatly inspired by these papers:
And book:
I published a book titled 'Mastering Python for Finance - Second Edition', discussing additional algorithmic trading ideas, statistical analysis, machine learning and deep learning, which you might find it useful. It is available on major sales channels including Amazon, Safari Online and Barnes & Noble, in paperback, Kindle and ebook. Get it from:
Source codes and table of contents on GitHub:
Topics covered with source codes:
If you would like a FREE review copy, drop me an email at jamesmawm@gmail.com.
Some ideas that you can extend this model for better results:
Sure, I had some questions "how is this high-frequency" or "not for UHFT" or "this is not front-running". Let's take a closer look at these definitions:
This models aims to incorporate the above two functions and present a simplistic view to traders who wish to automate their trades, get started in Python trading or use a free trading platform.
I write software in my free time. One of them for trading futures was simply called 'The Gateway'. It is a C# application that exposes a socket and public API method calls for interfacing Python with futures markets including CME, CBOT, NYSE, Eurex and ICE. Targets the T4 API.
More information on GitHub: https://github.com/hftstrat/The-Gateway-code-samples or view on the website.