Skip to content

Framework for backtesting quantitative trading strategies, allowing easy data visualisation, performance comparison and analysis.

Notifications You must be signed in to change notification settings

0xTDF/Quant-Trading-Strategy-Backtesting-Framework

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 

Repository files navigation

MIT License LinkedIn

Framework for backtesting quantitative trading strategies

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.

Real-World Use-Cases

💰 Develop and backtest trading strategies

🏦 Develop highly customised indicators

💲 Compare and analyse quant strategies

Development-Goals

🧰 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

Built With

Getting Started

  1. Obtain price data.

  2. 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)
  1. 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
  1. 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)
  1. Run the back test using:
  back = cerebro.run()

License

Distributed under the MIT License. See LICENSE for more information.

Contact

Twitter - @TraderTDF

LinkedIn - https://www.linkedin.com/in/RAMWatson/

Project Link: https://github.com/Elisik/Quant-Trading-Strategy-Backtesting-Framework

About

Framework for backtesting quantitative trading strategies, allowing easy data visualisation, performance comparison and analysis.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages