• 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.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Multiple Timeframe Issue

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

    Multiple Timeframe Issue

    I use multiple time frames in my trading between 2 and 13 minutes. I have written a custom strategy that evaluates certain conditions to determine whether to place a Buy or Sell arrow on my charts.

    I wish to call a custom indicator into this strategy. And I desire that its calculations be based on a 30-minute input series.

    I have added the following code in the Initialize() section to introduce the additional input series:

    protected override void Initialize()
    {

    // Add a 30 minute Bars object - BarsInProgress index = 1
    Add(PeriodType.Minute, 30);

    CalculateOnBarClose = true;
    }

    To determine whether the distance between Low[0] of the time frame of my chart (say, for example, 5 minutes) and the VAH of my custom Market Profile indicator, which requires the 30-minute time frame, I have written the following:

    if(Math.Abs(Low[0] - TTZMPX(30, false, OpenHour, OpenMinute, 68.27, TTZMPX_VAMethods.VWTPO, SessionLength, 300, false, TTZMPX_VAPlotStyle.Both, true).VAH[0]) > 2 * Tock)

    Paint an arrow.....

    The first argument of TTZMPX is supposed to be the IDataSeries. I realize using 30 in my code is not correct.

    My question is, how do I instruct in my coding, so that NinjaTrader will use the 30-minute input series when comparing to TTZMPX? That is, what is proper syntax to accomplish this?

    If I do not specify which series to use, it will use the time frame of my chart, which I do not want. I want this calculation to use the 30-minute VAH when comparing to Low[0] of my chart's time frame.
    Last edited by Longhornmark; 05-20-2019, 07:09 AM.

    #2
    Hello Longhornmark,

    Thanks for your post.

    You would need to specify the added data series in your indicator line like this: TTZMPX(BarsArray[1], false, OpenHour,....

    I highly recommend that you review the ninjascript help guide on Multi time/series as there are some critical concepts you will need to review.
    Critical concepts are what bars are actually referenced with using CalculateOnBarClose = true/false.
    Considering that both data series will call OnBarUpdate() and that will change references to the calling data series and how to prevent/isolate.
    Ensuring that a correct number of bars are loaded before accessing in both data series.
    https://ninjatrader.com/support/help...nstruments.htm
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Thanks, Paul. I understand your advice.

      I have amended my strategy with the following in OnBarUpdate(), which I believe might relieve me of some future trouble otherwise:

      protected override void Initialize()
      {
      // Add a 30 minute Bars object - BarsInProgress index = 1

      Add(PeriodType.Minute, 30);

      CalculateOnBarClose = true;
      }

      /// <summary>
      /// Called on each bar update event (incoming tick)
      /// </summary>
      protected override void OnBarUpdate()
      {
      // Checks to ensure all Bars objects contain enough bars before beginning:

      if (CurrentBars[0] <= BarsRequired
      || CurrentBars[1] <= BarsRequired)
      return;

      My understanding is that if my code references, say, Low[0], NinjaTrader will interpret that to mean Low{0][0], which I understand to mean Low[0] of the primary Input Series.

      But if my code references Low[1][0], it will use the Low[0] of Input Series[1].

      Is my understanding of all the above correct?

      Comment


        #4
        Hello Longhornmark,

        Thanks for your reply.

        When using Low[0] and OnBarUpdate() is called by the 30-minute bars, Low[0] will point to the 30 minute bars low. You can use the plural Lows[0][0] if you want to keep that particular value to the chart bars low

        I don't know entirely what you are doing (and I do not need to know) but you likely would want to segment your code so that when the 30 minute bar closes that it does not cause your code to run, this can be done early in OnBarUpdate() by adding: if (BarsInProgress != 0) return; // only process from here on down when called by the chart bars
        Reference: https://ninjatrader.com/support/help...inprogress.htm You code can still access the 30-minute data, all this does is prevent the references from shifting and having your code execute twice at the same time (5-minute bar close and 30-minute bar close at the bottom/top of the hour)
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by Longhornmark View Post
          My understanding is that if my code references, say, Low[0], NinjaTrader will interpret that to mean Lows[0][0], which I understand to mean Low[0] of the primary Input Series.
          [EDIT: Note the 's' above is required, I added it for you.]

          Not necessarily, the answer really depends on the BarsInProgress context.

          Low[0] will mean Lows[0][0] when the BarsInProgress context is 0.
          Low[0] will mean Lows[1][0] when the BarsInProgress context is 1.

          See that?
          Using Low[0] is like a builtin shortcut for Lows[BarsInProgress][0].

          Originally posted by Longhornmark View Post
          But if my code references Lows[1][0], it will use the Low[0] of Input Series[1].
          Well now, that is absolutely true, because you are explicitly specifying the BIP value.

          Originally posted by Longhornmark View Post
          Is my understanding of all the above correct?
          Not quite, you need to understand that OnBarUpdate is called with BarsInProgress == 0,
          which means the primary input series, so all your single index references to DataSeries
          Open, High, Low, Close, Volume, Time, etc, are automatically mapped to input series '0',
          which is the primary bars on your chart. That is, using a single index like High[0] really
          means Highs[0][0] -- the BarsInProgress context has mapped "High" to mean "Highs[0]".

          Later, when OnBarUpdate is called again when BarsInProgress == 1, this is your secondary
          data series form your Add() in Initialize -- all the single index references are automatically
          mapped to input series [1] -- that is, High[0] automatically maps Highs[1][0] when the
          BarsInProgress context is 1.

          [EDIT: key phrase here is automatically mapped, this is a critical concept.]

          That's why NT support suggested you do something like this,

          Code:
          protected override void OnBarUpdate()
          {
              if (BarsInProgress == 0)
              {
                  ... process primary bar series
                  ... here Low[0] automatically means Lows[0][0]
                  ... here you must use Lows[1][0] to access the secondary bar series
              }
              else if (BarsInProgress == 1)
              {
                  ... process secondary bar series
                  ... here Low[0] automatically means Lows[1][0]
                  ... here you must use Lows[0][0] to access the primary bar series
              }
          }
          Of course, you can bypass all of this (or mix and match techniques) by using Lows[0][0]
          and Lows[1][0] to explicitly access a specific input series any time you want.

          Btw, I highlighted all the 's' -- do not forget those 's' letters.

          Last edited by bltdavid; 05-20-2019, 03:05 PM.

          Comment


            #6
            the above explanation should make a great addition to Ninja MTF documentation.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by johnnybegoode, Today, 08:00 PM
            2 responses
            4 views
            0 likes
            Last Post johnnybegoode  
            Started by digibob, Today, 03:00 PM
            1 response
            6 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Started by yeshujbp, Today, 02:50 PM
            1 response
            7 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Started by ntbone, Today, 01:25 AM
            3 responses
            11 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Started by fredfred, 02-06-2018, 10:14 AM
            18 responses
            1,111 views
            0 likes
            Last Post yunakhan1  
            Working...
            X