February 29


Trading Multiple Strategies With The Same Instrument – Part 2

By Kevin Davey

February 29, 2016

Automated Trading Development, EasyLanguage, Kevin Davey, portfolio management

In the previous article, I discussed the need for trading multiple strategies with the same instrument in the same account. This type of capability is very important to a trader who wishes to trade a diversified portfolio, while at the same time efficiently managing margin. Unfortunately, one of the limitations of Tradestation, my trading platform of choice, is that this capability is very tough to achieve.

As you will recall from the earlier article, we discussed six different options to trade multiple strategies with the same instrument. In this article, we will layout the process and the code to create a summing strategy, which was Option number 6. The steps to take in such an approach are as follows:

  • Create a typical strategy, using standard Tradestation reserved words such as “setstoploss”
  • Transform this typical strategy into a “summing” strategy. An example of how a summing strategy works is shown in Figure 1

Figure 1 – Example of a Summing Strategy

Today’s article will focus on the first three steps, and the final article in this series will focus on the fourth step.

For the baseline normal strategy we will just use the simple strategy described in part 1 – a simple momentum strategy with a stoploss. This is typical of a very simple strategy. We will not include a profit target, but the conversion process from a typical strategy to a summing strategy could easily handle this. This simple strategy produces the equity curve shown in Figure 2.

Figure 2 – Original Momentum Strategy Equity Curve

This is the curve we will try to mimic with the summing strategy. It is important to note that we say “mimic” and not “duplicate.” For example, for various reasons it is not possible to correctly use Tradestation stoploss functions when combining multiple strategies. So, we will attempt to get close to the original strategy, but the summing strategy performance may be better, or it may be worse. That is the price we must pay to reach the end objective.

In looking at the original strategy, the entry – a simple “buy the next bar at market” – should be fairly easy to implement. The stop loss order will be a bit more difficult. This is because the stop loss is active the whole bar, and the new strategy will only act at end of bars.

To keep the conversion simple, I am going to use “sell next bar at market” to replicate the stops. This will mean there is no stop in the market when you actually trade it live. This could be a deal breaker for some people, but in reality the difference between using actual stop losses and simply exiting the next bar at market is not troublesome in most strategies. If it is significant, the pseudo stop price can be adjusted to more closely match the original strategy. Again, though, we are giving up the power of the stoploss function to be able to more easily create the summing strategy.

Figure 3 shows the original strategy created for step 1.

Figure 3 – Original Strategy Code, Momentum Strategy

Below shows the summing strategy created for step 2.


Summing strategy, designed to combine with other strategies
By Kevin J. Davey


If close>close[5] then buy next bar at market;
If close<close[5] then sellshort next bar at market;

input: stopl(1000),ncons01(1);
var: PositionAtEnd01(0),LongEntry(0),ShortEntry(0);
//this is the current position we should be at the start of the bar

//Entry rule

//If Condition1 then begin Condition1 is your buy signal

If close>close[5] then begin
PositionAtEnd01=1; //should be long at open of next bar
If PositionAtEnd01[1]<>1 then LongEntry=Close-stopl/BigPointValue; //reset longentry only if New trade

// If Condition2 then begin Condition2 is your sellshort signal
If close<close[5] then begin
PositionAtEnd01=-1; //should be short at open of next bar
If PositionAtEnd01[1]<>-1 then ShortEntry=Close+stopl/BigPointValue; //reset shortentry only if New trade

//now check current position and see if pseudo stop was hit- if it was, then make sure position at open of next bar is flat
If (positionAtEnd01[1]=1 ) and Close<=LongEntry then PositionAtEnd01=0;
If (positionAtEnd01[1]=-1 ) and Close>=ShortEntry then PositionAtEnd01=0;

//now at this point, we have three possibilities
//PositionAtEnd01=1 means at open of next bar, we should be LONG ncons01 contracts
//PositionAtEnd01=-1 means at open of next bar, we should be SHORT ncons01 contracts
//PositionAtEnd01=0 means at open of next bar, we should be FLAT

//we have to figure out how many long or short we should be

If PositionAtEnd01=1 then totalcons=ncons01;
If PositionAtEnd01=-1 then totalcons=-ncons01;

//so now totalcons should be the final position, after all is said and done

CurrentCons=CurrentContracts*Marketposition; //this is our current position at the close of the current bar

//print(date,” “,totalcons,” “,currentcons);

//first figure out how many contracts to buy or sell to get to right NET LONG position

If TotalCons>0 then begin
If currentcons<=0 then constobuy= totalcons;
If currentcons>0 then begin
//we are long, but have to decrease our long position (but still stay long)
if totalconscurrentcons then constobuy= totalcons-currentcons;

buy constobuy contracts next bar at market;

//next figure out how many contracts to buy or sell to get to right NET SHORT position

If TotalCons=0 then constosellshort= -totalcons;

If currentconscurrentcons then begin
//we are short, but have to decrease our short position (but still stay Short)
Buytocover (currentcontracts+totalcons) contracts total next bar at market;

If totalcons end;

sellshort constosellshort contracts next bar at market;

//if we should be flat overall, then exit all existing contracts

If TotalCons=0 then Sell All contracts next bar at market;
If TotalCons=0 then BuyToCover All contracts next bar at market;

By examining the code, it should be pretty clear what is going on – order statements like “buy next bar at market” are replaced with counters/summing variables, and at the end of the code, the counters/summing variables are converted back into buy and sell statements.

Some of the code for the bottom section looks overly complex, and it is that way by necessity. Tradestation has some weird quirks with order placement that must be accounted for. For example, let’s say you have a strategy with two separate buy statements triggered, corresponding to different conditions. So, now you’d be long two contracts, one from each entry. If you later wanted to exit (sell) only one of these contracts, a statement such as “sell 1 contract next bar at market;” will actually sell one contract from each entry.

So, the architecture of Tradestation makes keeping track of orders and where they come from pretty difficult. Thus, the code at the bottom of the summing strategy is complicated. In the end, the accuracy of this code can be determined by comparing the individual summing strategies with the single “master” summing strategy. That will be demonstrated in part 3.

Figure 4 shows the equity curve for each of the strategies. As you can see, the results are fairly similar – close enough for us to accept the summing strategy of Figure 4 in lieu of the original strategy of Figure 3.

Figure 4 – Comparison of Original and Summing Strategies

Eventually, as the trader gets more comfortable with this conversion process, he or she can start out from the beginning with the summing strategy, bypassing the original strategy completely. If this strategy produces acceptable performance results, there really is no need to create the original strategy. A sample template for an individual summing strategy is shown in the code at the bottom of the article.

Now that we can convert a “normal” strategy into a format that can work with a summing strategy, we can combine multiple strategies into one large strategy. That will be tackled in the final part of this article series.

If you would like to learn more about building trading systems be sure to get a copy of my latest book, Building Winning Algorithmic Trading Systems.

Kevin Davey

About the author

Kevin Davey is a professional trader and a top performing systems developer. Kevin is the author of “Building Algorithmic Trading Systems: A Trader's Journey From Data Mining to Monte Carlo Simulation to Live Trading” (Wiley Trading, 2014.) . He generated triple digit annual returns 148 percent, 107 percent, and 112 percent in three consecutive World Cup of Futures Trading Championships® using algorithmic trading systems.

His web site, www.kjtradingsystems.com, provides trading mentoring, trading signals, and free trading videos and articles. He writes extensively in industry publications such as Futures Magazine and Active Trader and was featured as a “Market Master” in the book The Universal Principles of Successful Trading by Brent Penfold (Wiley, 2010).
Active in social media, Kevin has over 15,000 Twitter followers. An aerospace engineer and MBA by background, he has been an independent trader for over 20 years. Kevin continues to trade full time and develop algorithmic trading strategies.

  • Thanks for the great article. Have you thought of using Portfolio Trader on Mulitcharts as that is supposedly automatically in some ways creating the super strategy.

  • Thanks for the comment Shane, I appreciate it!

    I have never tried Multicharts to do this, so I can’t comment on how good their approach is. Tradestation is admittedly weak in this area, but still for platform of choice.


  • Thanks for sharing this Kevin. I’d tried using the named entries approach but found you can’t rely on it, so it’s good to see a way round the issue – other than opening a separate brokerage account for every strategy!

    • Hi Peter –

      I wish the way I presented was foolproof, and worked in all situations. Of course, it doesn’t. I actually use it for a few strategies, and it seems to work properly.

      Good Luck!

  • Thank you for this great article. I wonder about your quote “If it is significant, the pseudo stop price can be adjusted to more closely match the original strategy”. Is it possible to elaborate a little bit on that?

    • Sure, here is what I meant…

      Let’s say your original strategy had a stop loss of $1000. With this approach, now you exit next bar only if the loss is above $XXXX – the pseudo stop. So $XXXX might be $1000, or it might be better to make it smaller (or larger), so that you get as close performance wise to the original $1000 real stop. So what I am saying is that the new pseudo-stop dollar amount does not have to be $1000. Hope this helps

      • Kevin thank you very much for the reply. I think that the amount of capital loss when the pseudo stop is hit is not a fixed value as with the classical SL, because it is evaluated on the opening of next bar. In that sense you cannot mimic the performance of a system with a SL. I would suggest to add a safety SL in the summation signal, just to avoid losses in an extreme event. So, when the summed position is not flat one could feel protected using a classical SL.

          • Instead of the pseudo stop, wouldn’t it be possible to have two stop orders in the market at all times, with a specified number of contracts for each?

  • Great article. Very good material.

    I’m new to algo trading, but wouldn’t the ‘Portfolio Maestro’ tool from TradeStation solve all this issues?

  • {"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

    Learn To Code & Build Strategies
    Using EasyLanguage.