Improving The Summing Strategy

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.

For this article I wanted to take a look at Kevin’s example a bit more closely and see if we can improve upon it. This is going to be a technical article on programming where we’ll take a concept and attempt to maximize it for flexibility and readability. So fair warning! If you’re not into programming this article may bore the hell out of you.
Kevin may have very well implemented very similar changes to his own personal strategies, but if not hopefully Kevin and you, the reader of this article, will find the modifications helpful.
For this article I’m going to start with the code provided from Kevin’s third article, Trading Multiple Strategies With The Same Instrument – Part 3. This code includes the summation strategy which contains two very simple strategies. The first strategy is a momentum-based strategy while the second strategy is a trend-based strategy.
As a reminder the strategies look something like this:
Momentum Strategy
If close>close[5] then buy next bar at market;
If close<close[5] then sellshort next bar at market;
setstoploss(1000);
Trend Strategy
If Close > average(Close,20) then buy next bar at market;
If Close < average(Close,20) then sellshort next bar at market;
setstoploss(1000);

Convert Each Strategy To  Function

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:

MomentumFunction1

click to enlarge

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:

MomentumCall_1

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!

Make Parameters Dynamic

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.

click to enlarge

click to enlarge

We then must pass in the stop loss value and look-back period when we call the function within our summation strategy:

Click to enlarge

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.

Conclusion

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.

Downloads

Download All Files For TradeStation (ELD)
Download TradeStation WorkSpace (TSW)
Download the two strategy functions (Text)
Download the main summation strategy (Text)

About the Author Jeff Swanson

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.

follow me on:
>