Today I'd like to share a notebook in our Research environment that shows how to run backtests of your algo across various input parameters, and then plot the results in various heatmaps to enable visualizing of how sensitive the algo is to small changes in the input parameters.
You can step through the notebook one cell at a time, or just do "Run All..." If you go the route of "Run All..." it may take upwards of 30 minutes for the whole notebook to complete because I have it setup to run 25 total backtests, varying the values across two parameters over five values each.
As well, it is worth mentioning, that your algo must be written in Zipline, completely in the Research environment, in order to do this at present time. The notebook I'm sharing here is a basic implementation of a pair trading algo, and you can freely modify the following inputs:
- the 2 stocks in the pair
- start and end dates for the backtest
- Z-score entry/exit criteria (e.g. +/- 1.0 standard deviation) for which to enter and exit the trades based on how much the pair's spread has diverged
- the lookback number of days to use for computing the hedge ratio (e.g. the # of days used in the regression)
- the lookback number of days for calculating the Z-score for determining whether the pair is diverging
The parameters over which I'm testing the sensitivity of in this example are:
- the lookback number of days to use for computing the hedge ratio (e.g. the # of days used in the regression)
- the lookback number of days for calculating the Z-score for determining whether the pair is diverging
By varying each of the inputs and viewing the resulting heatmaps I can see whether basing the spread calculation over shorter or longer timeframes results in more profitable trades (based on days used in the hedge ratio regression calculation); as well, I can see whether I should trigger a trade based on shorter or longer term divergences (based on the days used to compute the Z-score).
Held "constant" in the backtest are the following:
- trades are entered when the pair's spread diverges by more than +/- 1.0 standard deviations (Z-scores),
- trades are exited when the spread converges to 0.0 Z-scores.
Free free to change the values to whatever you wish if you prefer. As well, the for() loop that runs all of the backtests can be easily modified to run over these entry and exit z-score parameters instead.
After you've run your simulations, over many different pairs of stocks, and encounter encouraging results, you can simply clone the algo I've shared below in the next reply (which is the Q Backtester equivalent of the Zipline algo used in this research notebook), quickly modify the stocks, and parameters to what you've researched, and then papertrade it live or enter it in an upcoming contest.
Happy Researching!