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

multiple time frame

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

    multiple time frame

    Hello

    I am working on an indicator that will plot information from multiple time frames.

    I have read through the multiple time frame information in the NT support docs. But I still have one question.

    For example, I would like the indicator to plot information from the 240 minute, daily, weekly, and monthly time frames, regardless which time frame the chart is currently on.

    so, of course, I will have to add the following in the initialize section:

    Code:
    Add(PeriodType.Minute, 240);
    Add(PeriodType.Day, 1);
    Add(PeriodType.Week, 1);
    Add(PeriodType.Month, 1);
    But my question is, what happens if the chart is on a Daily Chart or Weekly Chart? Will I need to put that into the code somehow? Will just this cover what I need regardless of the chart time frame is?

    #2
    Hello jg123, and thank you for your question.

    The primary concern when working with multiple data series, is that each of your data series has data. That is to say, that if you are working with a 240 minute data series, data may not be available for longer time periods such as daily, weekly, or monthly.

    To make sure that you have data available for each of your time series, you can use this code.

    Code:
    [FONT=Courier New]
    protected override void OnBarUpdate()
    {
      foreach(int CurrentBarForSeries in CurrentBars)
      {
        if (CurrentBarForSeries < 1)
        {
          return;
        }
      }
    
      // the rest of your OnBarUpdate code here
    }[/FONT]
    Please let us know if there are any other ways we can help.
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      Smaple Multi Time frame Question

      Hello,

      Below is a snippett of the Sample Multi Time Frame Strategy that comes in NT 7:

      What is the significance of the check on line 3 ? I had noticed that if it is removed it produces a significant change in the PNL result. Could you provide a link or detailed description of line 3's purpose ?

      1. if (BarsInProgress != 0)
      return;

      2. // Checks if the 5 period SMA is above the 50 period SMA on both the 5 and 15 minute time frames

      3. if (SMA(BarsArray[1], 5)[0] > SMA(BarsArray[1], 50)[0] && SMA(BarsArray[2], 5)[0] > SMA(BarsArray[2], 50)[0])

      4. {
      // Checks for a cross above condition of the 5 and 50 period SMA on the primary Bars object and enters long
      if (CrossAbove(SMA(5), SMA(50), 1))
      EnterLong(1000, "SMA");
      }


      5. // Checks for a cross below condition of the 5 and 15 period SMA on the 15 minute time frame and exits long
      if (CrossBelow(SMA(BarsArray[2], 5), SMA(BarsArray[2], 50), 1))
      ExitLong(1000);

      Comment


        #4
        Thank you for your question 208275. The 3rd line of code is explained in the comment in the 2nd line. The 3rd line checks the 5 period SMA against the 50 period SMA, for both the secondary and tertiary data series.

        I believe it may help if we use some different identifiers so that this code is self descriptive.

        Code:
        Bars fiveMinuteBars = BarsArray[1];
        Bars fifteenMinuteBars = BarsArray[2];
        bool fivePeriodAboveFiftyPeriodForFiveMinuteBars = SMA(fiveMinuteBars, 5)[0] > SMA(fifteenMinuteBars, 50)[0];
        bool fivePeriodAboveFiftyPeriodForFifteenMinuteBars = SMA(fifteenMinuteBars, 5)[0] > SMA(fifteenMinuteBars, 50)[0];
        
        if (fivePeriodAboveFiftyPeriodForFiveMinuteBars && fivePeriodAboveFiftyPeriodForFifteenMinuteBars)
        Please let us know if there are any other ways we can help.
        Jessica P.NinjaTrader Customer Service

        Comment


          #5
          Jessica,

          Thanks for the reply. I think I understand what the code is doing ( as referenced by the commented out lines in line # 2 ) What I don't understand is why the reference is made and where exactly to use brackets. An additional reply by Jesse stated that :

          if (SMA(BarsArray[1], 5)[0] > SMA(BarsArray[1], 50)[0] && SMA(BarsArray[2], 5)[0] > SMA(BarsArray[2], 50)[0])


          The significance of this line would be that this is one of the two the conditions to EnterLong. If this condition is removed, the strategy would essentially try to EnterLong each time the condition if (CrossAbove(SMA(5), SMA(50), 1)) becomes true.



          What I don't understand is why this statement isn't wrapped with ( nested ? ) curly braces ? It is to be considered so why isn't it wrapped with line 4 ? So, as I understand all of this, this is how this logic would unfold :
          Line 3 checks if the 5 SMA is above the 50 SMA on the 5 & 15 multi series charts. If this is TRUE then line 4 is evaluated and if it is TRUE then a long position would be processed on the primary series chart; let's say a 30 minute chart. If line 3 were removed and if line 4 were TRUE then it would EnterLong (CrossAbove(SMA(5), SMA(50), 1)) on the primary series chart ( 30 minute ).



          Additionally, I would only need to use nested brackets if I wanted to process a condition on the primary series ( 30 minute ). All other series evaluations would be inside the OnBarUpdate brackets.
          EXAMPLE - Line 5 is not nested as it is referencing the 15 minute ( the 3rd series ). Do I understand all of this correctly ? I hope this all makes sense. Just trying to understand all the nuances in multi series programming.


          Thank You !

          Comment


            #6
            Thank you for clarifying. You are correct in that each of the main, 5 minute, and 15 minute series each have to experience a 5 v 50 SMA crossover in order to enter long. I am providing equivalent code using a for loop which I am hoping will clear up this example.

            Code:
            [FONT=Courier New]
            // We only want to process events on our primary Bars object (index = 0) which
            // is set when adding the strategy to a chart
            if (BarsInProgress != 0)
            {
                return;
            }
            
            // Checks  if the 5 period SMA is above the 50 period SMA
            // on the primary bars and the 5 and 15 minute time frames
            bool passed = true;
            for (int curSeries = 0; curSeries < 3; curSeries++)
            {
                // curSeries == 0 : primary bars
                // curSeries == 1 : 5 minute timeframe
                // curSeries == 2 : 15 minute timeframe
                if ( ! (SMA(BarsArray[curSeries], 5)[0] > SMA(BarsArray[curSeries], 50)[0]) )
                {
                    passed = false;
                }
            }
            if (passed)
            {
                EnterLong(1000, "SMA");
            }[/FONT]
            Jessica P.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by bmartz, 03-12-2024, 06:12 AM
            5 responses
            32 views
            0 likes
            Last Post NinjaTrader_Zachary  
            Started by Aviram Y, Today, 05:29 AM
            4 responses
            13 views
            0 likes
            Last Post Aviram Y  
            Started by algospoke, 04-17-2024, 06:40 PM
            3 responses
            28 views
            0 likes
            Last Post NinjaTrader_Jesse  
            Started by gentlebenthebear, Today, 01:30 AM
            1 response
            8 views
            0 likes
            Last Post NinjaTrader_Jesse  
            Started by cls71, Today, 04:45 AM
            1 response
            7 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Working...
            X