Hi All,

Inspired by the amazing series "From Research To Production" by Sherry Yang and Jack Simonson, I wanted to share with the community my implementation of a simple Long-Short Moving Average Crossover using the Algorithm Framework and a Research file with explanation of the Alpha logic.

There are already open-source implementations of this strategy of course, but the educational purpose of this algorithm is to show how seamlessly we can move from research to production with almost no changes in code. Also, I'm updating indicators "manually" using history calls on purpose, instead of using the QuantConnect Update method for indicators, as otherwise I couldn't replicate the logic in the research notebook.

ALGORITHM FRAMEWORK MODULES:

  1. Universe: Manual input of tickers.
  2. Alpha: Go Long when the Short Period Moving Average crosses above the Long Period Moving Average, and go Short when it crosses below.
  3. Portfolio: Equally Weighted portfolio (investing the same amounts in each security). It includes optional inputs for portfolio rebalancing.
  4. Execution: Immediate Execution with Market Orders (using a custom model Immediate 'Execution Model With Logs').

A couple of notes I'd like to share on these modules:

  • The concept of Insights in the Alpha model. Normally and with few exceptions when using Minute data, the way I like to think about these insights is as a prediction that I update every trading bar (as opposed to giving a longer, uncertain period) for another extra bar until my model tells me that signal is no longer valid. As you can see, when we get a Long signal we emit InsightDirection.Up for one bar and then continue to update this prediction until we get a Short signal, and repeat the process. We use InsightDirection.Flat to stay in cash when there are no trading signals (in this algo that only happens from the launch of the backtest until it gets the first signal).
  • In order to handle this way of sending Insights, I slightly modified the open-source code for the EqualWeightingPortfolioConstructionModel. This custom version will only rebalance the portfolio if:
    • It's time to rebalance the portfolio. You can control the number of trading days between rebalances with the rebalancingParam input in the main.py script.
    • OR there are Up/Down insights for securities that are not in the portfolio yet.
    • OR there are changes in direction for actively invested securities.
  • Finally, this project uses a custom implementation of the open-source Immediate Execution Model that includes many different logs of information about number of shares traded, average holding prices, profit and profit percent for both long and short positions. If you like that module you can simply plug it into any of your existing strategies that use market orders. This is a good example of the modularity of the Framework!

RESEARCH NOTEBOOK:

As mentioned above, this project includes a Research file with explanation of the Alpha logic. I tried to even 'replicate' the event-driven data flow of the Alpha module happening within the backtesting environment by looping through the dataframe of historical data in the research environment to update indicators and get trading signals, so we can see exactly what's happening behind the scenes of an algo. A few notes on this research notebook:

  • The class SymbolData that stores the different indicator calculations for each symbol can be copied into the Alpha model with no changes in code.
  • Then we're iterating through each row in the dataframe of historical data to update indicators and signals. You can see how this code is almost identical to the one found in the Alpha model. Of course, in the Alpha module the iterations through new data points happen behind the scenes, but this is pretty much what it's doing.
  • At the end I'm just plotting the indicators and buy/sell signals so we can compare with the results from the backtesting plots. They should be the same.
  • Play with different tickers, start/end dates and parameters for the moving averages on both the research notebook and the backtesting main.py script to check they both give the same plots!

OTHER FEATURES:

This project also includes some other features that might be of interest for beginners:

  • Potting indicators in backtesting.
  • Organizing code in a certain way with calculations per symbol stored in a separate Python class.

Merry Christmas!

Emilio

InnoQuantivity.com

Author