Quantitative trading is a type of market strategy that relies on mathematical and statistical models to identify and execute opportunities and trades.
This project aims to serve as a framework for developing and backtesting trading strategies, allowing for easy data visualisation and strategy performance comparison.
Presented in the script as a demonstration, an extremely basic, and likely unprofitable, simple moving average crossover strategy is provided. Said strategy buys when the 10-day moving average crosses the 20-day moving average, and sells when the reverse occurs.
Building upon this framework, much more complex, robust, and profitable strategies can be built, tested, and optimised.
๐ฐ Develop and backtest trading strategies
๐ฆ Develop highly customised indicators
๐ฒ Compare and analyse quant strategies
๐งฐ Develop a framework for backtesting trading strategies
โ๏ธ Deep dive into Backtrader's library and understand both it's capabilities and limitations.
๐งพ Further develop knowledge of Matplotlib
๐ค Backtest a simple moving average crossover trading strategy
-
Create a signal generator within the init function of the strategy class:
# signal generator
def __init__(self):
ma_fast = bt.ind.SMA(period = 10)
ma_slow = bt.ind.SMA(period = 20)
self.crossover = bt.ind.CrossOver(ma_fast, ma_slow)
- Create buy and sell orders based upon the previously generate signals. For example:
# executes order from the signals
def next(self):
if not self.position: # if not already in a position
if self.crossover > 0: # if 10-day moving average crosses above 20-day moving average
self.buy() # take a long position
elif self.crossover < 0: # if 10-day moving average crosses below 20-day moving average
self.close() # close long position
- Initialise the backtesting engine (Cerebro). Add the price data and strategy before setting inital conditions such as account size and risk amount per trade etc.
cerebro = bt.Cerebro()
# adds data to engine
cerebro.adddata(data)
# adds strategy to engine
cerebro.addstrategy(MaCrossStrategy)
# sets starting capital
cerebro.broker.setcash(1000.0)
# sets size per trade
cerebro.addsizer(bt.sizers.PercentSizer, percents = 10)
- Run the back test using:
back = cerebro.run()
Distributed under the MIT License. See LICENSE
for more information.
Twitter - @TraderTDF
LinkedIn - https://www.linkedin.com/in/RAMWatson/
Project Link: https://github.com/Elisik/Quant-Trading-Strategy-Backtesting-Framework