Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Adding two different time frames to strategy.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Adding two different time frames to strategy.

    Hi,

    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.


    #2
    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

    Comment


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

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Waxavi, Today, 02:10 AM
      0 responses
      1 view
      0 likes
      Last Post Waxavi
      by Waxavi
       
      Started by TradeForge, Today, 02:09 AM
      0 responses
      2 views
      0 likes
      Last Post TradeForge  
      Started by Waxavi, Today, 02:00 AM
      0 responses
      2 views
      0 likes
      Last Post Waxavi
      by Waxavi
       
      Started by elirion, Today, 01:36 AM
      0 responses
      4 views
      0 likes
      Last Post elirion
      by elirion
       
      Started by gentlebenthebear, Today, 01:30 AM
      0 responses
      4 views
      0 likes
      Last Post gentlebenthebear  
      Working...
      X