Parameter optimization is an important part of developing any trading strategy. However, not long after optimization the strategy falls short because of changes in market dynamics.
Is re-optimization the answer? If the answer is a resounding yes, then the obvious question is “well, how often do we optimize?”; is every week too much? What about every month?
Clearly, this is not going to be a one size fits all type of investigation, but from my analysis, for a strategy on a daily time-frame we should optimize every 6–12 weeks. Here is an example where I re-optimize a moving average crossover trading strategy on the S&P500 index:
Besides the absolute classic buy-and-hold approach, the best performer was re-optimizing every 12 weeks; however, I’d argue that the 6 week re-optimization does rather well too! In all circumstances, the optimized strategies come out on top of the classic moving average crossover strategy (labelled as the “Baseline”).
Please tell me about the strategy this uses!
Sure, I realise the details above are left somewhat fuzzy. The strategy is a moving average crossover strategy, where we:
- Enter a long position if the faster moving average is above the slower moving average (indicating a short-term bullish sentiment).
- Close the position when the faster moving average moves below the slower moving average.
The traditional parameters for this are the 10 and 20 period simple moving averages. Here is a quick image summary for a trade on SPY:
OK! How do you optimize the strategy?
I used a Genetic Algorithm — which takes its roots from the theory of natural selection. The algorithm I wrote myself in Python, and is actually the subject of one of my articles if you’d like to read and find out more:
The work performed for this study was basically to write some wrapper functionality which performs the following sequential tasks:
- Take the last n weeks of price data for a set of companies, and optimize the moving average strategy for them (not including the SPY).
- Forward test this strategy on the SPY by trading the next m weeks (I used m = n/2) using the optimized strategy.
The above process was repeated from 2017 till the present day (November 2022). The one small caveat is that I assumed that any trades would be closed at the end of the m week forward-testing period; so even if a buy signal was still active, the trade was terminated (for code simplicity).
After each testing period, the code returns the multiple of your investment after this trading period (i.e. 0.9 indicates that you lost 10%); this allows the use of a cumulative product to determine the compounded growth from 2017 to November 2022.
Wait, what companies did you use to optimize?
Since we are trading the SPY, I decided to use the current 7 biggest constituents (found here). My hypothesis is that since the SPY is sort of an average of many stocks, optimising over the largest few will give a good idea of how to trade the index fund.
I’m still not convinced my hypothesis is 100% solid, that’s a plan for future testing/exploration 😅
Can this be improved further?
Of course! This was a quick study to estimate how long one should wait until you re-optimize a trading strategy, improvements could take the form of:
- Testing different training/testing periods (I only used 3, primarily due to the long computational time).
- Improving the speed of the optimization (or writing more medium articles so I can pay for a better computer…)
- Implement different trading strategies (e.g. a Bollinger band one)
- Maybe to implement different optimization methods!
And so on! I’d be super keen to hear if anyone out there has ideas for this.
Can I see the code, pretty please?
Oh well, since you asked so nicely! The full code can be found here. 🙂
So it appears as if we do indeed need to re-optimize, and fairly frequently. If anything, I feel like this code has given me a ballpark understanding on how long we should wait until re-optimization; now my question is, “should I optimize the re-optimization period?”… the fun never ends! 😎
⚠️Please note that this article is not a suggestion to use my code to develop your own strategies. You should always do your own testing/validation before using and/or trusting anything on the internet!
Thank you for reading, I hope you enjoyed the article! Please feel free to connect with me on LinkedIn, I’d love to hear if/how you use the code🙂
If you are thinking getting a medium account, then please consider supporting me and thousands of other writers by signing up for a membership. For full disclosure, signing up through this link grants me a portion of your membership fee, at no additional cost to you (a win-win for sure!).
Or if you’d like another way to support my content creation, then you could
Because I work a full-time job, go to the gym, trade, and write on Medium, I use a lot of coffee!