Quantitative Trading and Systematic Investing

Letian Wang Blog on Quant Trading and Portfolio Management

0%

Live Trading with Interactive Brokers Native Python API

Introduction

This post domonstrates how to use quanttrader to connect to Interactive Brokers and do live algorithmic trade. Quanttrader is pure Python and the brokerage API is also native Python so in total the solution is 100% Python. Interactive Brokers is a popular brokerage among quant traders thanks to its powerful and robust Application Programming Interface (API). A few years ago, I open sourced a trading system with connection to IB C# API. Now with IB's new Native Python API library, it is a good idea to build strategies in Python in order to leverage Python's machine learning toolkits.

The demo video is located here on Youtube.

For quanttrader backtest, check out this post.

Code Structure

Below is the structure of quanttrader live trading module. The entry point is live_engine.py in examples folder.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
+-- brokerage
| +-- ib_brokerage.py
+-- data
| +-- tick_event.py
+-- event
| +-- live_event_engine.py
+-- gui
| +-- ui_main_window.py
+-- order
| +-- fill_event.py
| +-- order_event.py
| +-- order_manager.py
+-- strategy
| +-- strategy_manager.py
+-- examples
| +-- live_engine.py
| +-- config_live.yaml
| +-- download_historical_data_from_ib.py

Installation

To install quanttrader.

Step one: pip install

1
pip install quanttrader

Or alternatively, download source code or checkout using git, then include the downloaded path in PYTHONPATH.

Step two: copy two files

download/copy two files from examples folder, namely live_engine.py and config_live.yaml. After that, just execute the python file,

1
python live_engine.py

Executing live_engine.py without example strategies, the strategy control tab will be empty.

Step three. copy or create two examples files as described in the next section

The files used in step 2 and 3 are zipped in starter_kit for convenience. Alternatively, after pip install, just download and unzip starter_kit.

Example Strategies

In examples/strategy folder there are two naive strategies, order_per_interval_strategy.py and moving_average_cross_strategy.py. The former simply sends out a market order every n-tick bar where n can be configured in config_live.yaml.

1
2
3
4
5
6
if (k.full_symbol == self.symbols[0]) & (self.ticks > self.tick_trigger_threshold):
o = OrderEvent()
self.place_order(o)
self.ticks = 0
else:
self.ticks += 1

The latter is a simple EMA strategy that sends out buy order when price crosses moving average from below and sell order when price crosses moving average from above. Irregular interval EMA is explained in this post.

1
2
3
4
5
6
7
8
if k.price > self.ema:    # buy at bid
current_pos = int(self._position_manager.get_position_size(symbol))
o = OrderEvent()
self.place_order(o)
else: # flip to short
current_pos = int(self._position_manager.get_position_size(symbol))
o = OrderEvent()
self.place_order(o)

Create your own strategy in the strategy folder and configurate it in config.yaml, then it will show up in the strategy tab.

Tick Data Recording

When you run live_engine.py, it creates two folders, log and tick, if they do not exist. Log files tells all the important things happened during trading sesssion; while tick data enables session replay as well as strategy backtest.

Intraday data is important to quant trading. Sometimes commercial data vendors are too expensive to retail traders. On the other hand, I would argue that recorded data might be better in the sense that it may not be the most acurate one, but it is more pertinent to your trading strategies because it is what you actually observe or reeceive under your trading constraints. After all, why pay a premium to premium data vendors if you don't collocate your strategies with direct market access.

Historical Data Download

IB also nicely provides down to 1 second historical bar data up to 180 days. To download historical bar data from IB, use download_historical_data_from_ib.py in example folder,

1
python download_historical_data_from_ib.py

Happy trading!

DISCLAIMER: This post is for the purpose of research and backtest only. The author doesn't promise any future profits and doesn't take responsibility for any trading losses.