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.
Below is the structure of quanttrader live trading module. The entry point is live_engine.py in examples folder.
To install quanttrader.
Step one: pip install
pip install quanttrader
Or alternatively, download source code or checkout using git, then include the downloaded path in PYTHONPATH.
Step two: copy two files
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
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.
if (k.full_symbol == self.symbols) & (self.ticks > self.tick_trigger_threshold):
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.
if k.price > self.ema: # buy at bid
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,
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.