QTrader: A Light Event-Driven Algorithmic Trading Engine
- 4 minsQTrader is a light and flexible event-driven algorithmic trading engine that can be used to backtest strategies, and seamlessly switch to live trading without any pain.
Key features
- Completely same code for backtesting/simulation/live trading
- Currently only support equity trading
Quick install
You may run the folllowing command to install QTrader immediately:
Get the data ready
QTrader supports 1 minute bar data at the moment. What you need to prepare is CSV files with file names in the format of “[symbol]-yyyy-mm-dd.csv”
And you should specify the path of data folder in qtrader.config.config.py
. For example, set
DATA_PATH = "./qtrader/examples/data"
and put all your CSV files to the following folder:
{DATA_PATH}/k_line/K_1M
How to implement a strategy
To implement a strategy is simple in QTrader. A strategy needs to implement init_strategy and on_bar methods in BaseStrategy. Here is a quick sample:
from qtrader.core.strategy import BaseStrategy
How to record anything I want
QTrader provides a module named BacktestRecorder to record variables during backtesting. By default, it saves datetime and portfolio_value every timestep.
If you want to record additional variables (let’s say it is called var), you need to write a method called get_var in your strategy:
And initialize your BacktestRecorder with the same vairable var=[]
(if you want to record every timestep) or var=None
(if you want to record only the last updated value):
Run a backtest
Now we are ready to run a backtest. Here is a sample of running a backtest in QTrader:
Live trading
Ok, your strategy looks good now. How can you put it to live trading? In QTrader it is extremely easy to switch from backtest mode to simulation or live trading mode. What you need to modify is just two lines:
Here you go! Enjoy the trading.
Important Notice: In the demo sample, the live trading mode will keep sending orders, please be aware of the risk when running it.