Multi time frame

I am trying to code a multi time frame NinjaScript.

on a 30 min time frame, filter is +di(13) > -di(13)

entry on the 5 min time frame when +di(13) crosses above -di(13) and adx(8) < 25

exit either rsi crosses above 70 or crosses below 30

profit target 55
stop loss 21

I know that the following objects need to be added.
Add(PeriodType.Minute, 5);
Add(PeriodType.Minute, 30;

Would the 5 min then be BarsArray[1] and the 30 min be BarsArray[2]? I thought that if no BarArray was referenced that it BarsArray would use the 5 min and BarsArray[1] would use the 30 min?

Has anyone tried to code a multi time frame script or have any advice?

Thx,

Ken

Ken,
You are on the right track, but you do not “Add” both. There is a base instrument, and then you add one. So the base may be 30 min, and then you Add the 5 min. The chart must be displaying the base instrument, I believe, and this corresponds to BarsInProgress==0 (I actually chart both the input series and the trading series on one chart so that is why I’m not sure what the requirement is). So you end up with BarsArray[0] for 30 min, and BarsArray[1] for 5 min. When you EnterLong it will be to BarsArray[1], for example:

EnterLong( 1, DefaultQuantity, “Long1”);

This can quickly get very confusing, as OnBarUpdate is called for every timeframe. So usually you will put code at the start of OnBarUpdate like:

if( BarsInProgress > 0 )
return;

Now I’m not saying this is exactly right for your system, but it may be. What this should do is effectively keep your logic using the 30 min bars for calculations. When the bars are from the 5 min. timeframe, the OnBarUpdate simply ignores them (and returns). Then on the 30 min bars, it will decide to enter a trade, and the trade will be placed against the 5 min timeframe bars, as the first argument to EnterLong is “1”.

I am not that fond of multi-timeframe strategies because of the complexity it adds. I’d be curious why you need two timeframes? For example, if you simply use the 5 min series, then adjust your logic to calculate the 30 min. statistics off of the 5 min bars?

Or maybe switch the order and make the 5 min the base series, and “Add” the 30 min. Then you don’t have to get into doing anything special with the Entries / Exits. Then you would use “if (BarsInProgress == 1){ do some calculations with 30 min bar } else { do 5 min calculations }”.

This is just a start, but I’m now thinking this last case is closest to what you need. Make the 5 min the “base” then “Add” the 30, and then do different calculations based on BarsInProgress.

You can check the basic @SampleMultiTimeFrame.cs, but just keep in mind that this basic strategy is using 3 time frames:

  • the time frame of your main data serie (what you have on your chart or what you’ve configured in the “Strategies” tab)
  • 5 min (BarsArray[1])
  • 15 min (BarsArray[2])