In a recent series of articles by Kevin Davey, a method of combining multiple trading strategies into one single strategy was presented. Kevin’s concept is a great way to tackle the issues of trading multiple systems on the same instrument. Kevin’s technique is to create a “summing” strategy where there is one single buy or sell signal and the number of contracts are based upon which strategy has been triggered. Traditionally, I code systems together into one single strategy where the system will have multiple entry and exit points. My technique has its own unique set of problems and I think Kevin’s “summing” concept is better. If you’ve not read Kevin’s articles you can find the links below.
In the original article series the final summing strategy embeds the code for both strategies in-line. This is fine for simple concepts but often I like to move functionality into a function.
Functions are blocks of code which are contained within their own file. Traditionally, you can’t put a strategy within a function because buy/sell signals don’t work within this structure. However, because the actual buy/sell signals have been stripped from the two strategies, both our momentum and trend following strategy can be easily converted into a function.
Here is the Momentum strategy converted into a function called, KJD_Momentum_Strategy_Example:
As you can see, this function contains all the code for the momentum strategy example. The function returns the “FinalPosition” which is the current position of the strategy. This function can now be called within the main summation strategy, as seen below:
So this single line of code can now be placed within the main summation strategy. Why would we do this? First, it cleans up the code for the main summation strategy. The main summation code only deals with determining the number of contracts and placing the trades. In our example, the trade logic for the momentum strategy is contained within its own file. This increases the readability of all the code. Granted, our example momentum strategy is very simple but this technique would be even more important with a more complicated strategies.
Second, this provides more flexibility in regards to code reuse. Maybe we trade both of these two strategies (momentum and trend) together on the oil futures but also discover they work great on the Euro futures. Great, we simply make a copy of our summation strategy that can trade it on the Euro. But could we not do that with the original code? Yes, but what happens if you want to make a slight change on the momentum strategy due to a recently discovered bug? Or maybe you wanted to add a new feature to the momentum strategy? Well, you would have to change the code in two locations. One for the oil futures and again for the euro futures.
If you have your strategies as functions there is only one place to change the code and all summation strategies that utilize that function will be using the modified code. This saves you time and possible headaches!
Our next modification is an obvious next choice. Our momentum strategy that we placed within a function has particular values which can be viewed as an input parameter. For our momentum strategy we have a look-back value and a stop loss value. Let’s convert this into an input for the function. We do this by declaring two input values. See the code below. Notice that I removed in StopLoss$ and Lookback variables and moved them to the “Input:” section of the code.
We then must pass in the stop loss value and look-back period when we call the function within our summation strategy:
The obvious benefit here is we can now use the momentum function within different summation strategies yet use unique stop loss values and look-back periods. We use the look-back value of five for trading oil futures but discover that using 15 on the euro futures works even better. We’re still using the same momentum code for each summation strategy, but we now have the ability to modify the look-back period for each time we use it.
The other benefit here is we can optimize over these values within the summation strategy.
So there you have it. A way to improve the summation strategy for clarity, code reuse, and maintenance. As Kevin pointed out in his original series, try to build your strategies with the summation concept in mind so down the road if you wish to combine your strategies together, it will be much easier to do. I would also like to add, build your strategies as functions and you’ll be on your way to building more flexible and maintainable code.
Jeff is the founder of System Trader Success – a website and mission to empowering the retail trader with the proper knowledge and tools to become a profitable trader the world of quantitative/automated trading.
Please log in again. The login page will open in a new window. After logging in you can close it and return to this page.