• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.


No announcement yet.

Partner 728x90


Adding two different time frames to strategy.

  • Filter
  • Time
  • Show
Clear All
new posts

    Adding two different time frames to strategy.


    Do you have a sample code of how to add two different time series to strategy and plot them?

    I am trying to create a strategy to compare two different time periods.

    For example, if close[0] > EMA(100) on 5 minute chart
    and if close[0] > EMA(100) on 10 minute chart
    then go long

    and vice versa.

    I'm trying to add both of them to strategy and possibly show them on the chart but I don't think I'm doing it right and getting various errors. I am looking for some sample where I can reference.

    Hello priceisking,

    Thanks for your post.

    We do not have an example of this.

    The best way to start something like this is with a fresh strategy and the very best way to get all the structure you need is to use the Strategy Wizard (not the strategy builder). Here is a link to the wizard in the help guide for clarity: https://ninjatrader.com/support/help...?ns_wizard.htm

    You access the strategy wizard in the Ninjascript editor through the "+" tab at the bottom left of the Ninjascript editor screen, left click and select "New strategy" In the "additional Data" window, click Add and then click "Use primary instrument" and change the "value to "10" to add the 10-minute data series, click OK. Click Next, click next, add any inputs you want and this will create the structures for your for the properties., click next when done. In the Plots and Lines, add the two plots you want, click next and finish. You will now have a structure including the two plots and all their code along with any properties they need.

    To see the plots then you next have to create and assign the two EMAs to their data series. You declare these as private at the public class level, for example:

    public class YourStrategyName: Strategy
    private EMA ema5; // for the chart bars, 5 minute series
    private EMA ema10;// for the added 10 minute series

    In the OnStateChange() method you will have already State == State.Configure which should already show AddDataSeries(...); Just below that you would add: ema5 = EMA(100); This will create the private indicator ema5 with a 100 period based on the chart bars (5 minute bars).

    To add the higher time frame EMA we need to add State== State.DataLoaded and inside of that we would add ema10 = EMA(Closes[1], 100); which will create the 100 period EMA based on Closes[1] which is the close values of the first added data series.

    Your States of Configure and Dataloaded should look like:

    else if (State == State.Configure)
    AddDataSeries(Data.BarsPeriodType.Minute, 10); // add the 10 minute series
    ema5 = EMA(100); // create the 5 minute 100 period EMA
    else if (State == State.DataLoaded)
    ema10 = EMA(Closes[1], 100); //create the 10 minute 100 period EMA

    Then to output to the plots, you would have something like this:

    protected override void OnBarUpdate()
    if (CurrentBars[0] < 1 || CurrentBars[1] < 1) return; // ensure minimu bars in each series before proceeding

    if (BarsInProgress != 0) return; // only plot when the chart bars call OnBarUpdate

    your5minuteplotname[0] = ema5[0]; // send the current value of the 5 minute EMA(100) to the 5 minute plot
    your10minuteplotname[0] = ema10[0]; // send the current value of the 10 minute EMA(100) to the 10 minute plot.

    For your strategy entry logic, you would use the ema5 and ema10 values, something like:

    if (Close[0] > ema5[0] && Close[0] > ema10[0])
    // do something

    Paul H.NinjaTrader Customer Service


      Thank you, Paul. I will go through this and revert back with any questions. Appreciate it.


      Latest Posts


      Topics Statistics Last Post
      Started by Leeroy_Jenkins, Yesterday, 09:17 AM
      1 response
      Last Post NinjaTrader_Melissa  
      Started by chaitunt, 03-29-2020, 08:26 PM
      1 response
      Last Post NinjaTrader_ChrisL  
      Started by mbcmbc, Yesterday, 11:01 PM
      2 responses
      Last Post mbcmbc
      by mbcmbc
      Started by Zuhier Dourgham, Yesterday, 03:55 AM
      1 response
      Last Post NinjaTrader_Jim  
      Started by b16_aln, Today, 12:47 AM
      6 responses
      Last Post NinjaTrader_Jesse