Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

AddDataSeries Offset by number of bars from secondary time frame

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

    AddDataSeries Offset by number of bars from secondary time frame

    Hi, I am new to NT scripting.

    I have some indicators I built for another trading platform and I am trying to learn how to port them over to work on NT8.

    Test scenario:
    Chart set to 60 min aggregation
    Secondary data series set to daily aggregation
    Check if any given high from the secondary time frame is a higher high than one day to the left and one day to the right
    (very similar to the Swing indicator included with NT8)

    Goal:
    When this pattern is identified on a specific bar the code needs to place the plot over the entire day in which this condition is true. (Just like the Swing indicator does but it only works on the primary series time frame). I have included a screenshot which is very important for describing exactly what this code does and the behavior I am trying to enforce.

    Here is a section of code I have built to test this out and try to find a solution:

    First, there are two data series that are initialized using the BarsArray[1]. They are listed in the "State.DataLoaded" of the "State.Confige" sections of the indicator class. As follows:

    Code:
    timeFrameOne = new Series<double>(BarsArray[1]);
    swingHigh = new Series<bool>(BarsArray[1]);
    For someone brand new to NinjaScript, the assumption is that each of these data series should contain only one place holder for each "day" on the chart. So when we apply an index to read it's value: timeFrameOne[1] this should be the previous "day" since BarsArray[1] is set to daily aggregation period. It should NOT be the previous hour (primary time frame of the chart). In practice, I have proven this not to work as I expected.

    Code:
    if (BarsInProgress == 1)
    {
    
    // This works, except it does not shift the plots two days to the left.
        if (High[0] < High[1] & High[1] > High[2])
        {
            timeFrameOne[0] = High[1];
            swingHigh[0] = true;
        }
        else
        {
            timeFrameOne[0] = Close[0];
            swingHigh[0] = false;
        }
    
    }
    
    if (BarsInProgress == 0)
        {
            if (timeFrameOne[0] != 0)
        {
            Value[0] = timeFrameOne[0];
        }
        else
        {
            Value[0] = Value[1];
        }
    
    }
    That code is placed in the "OnBarUpdate()" event call.

    I would expect that when you adjust the following line...

    Code:
    timeFrameOne[2] = High[1];
    ...to include an index value of [2], that it would shift the plot exactly 2 "days" to the left. Why? Because the placement of that line of code shows that the secondary bar series is the current context. And using an index value of [2] should shift the bar two bars from the secondary bar series. But in reality it is shifting the plot 2 bars from the primary bar series (2 hours instead of two days)

    I have tried just about every combination of code elements In can imagine. This is just the version that demonstrates what I am trying to accomplish in the clearest terms.

    Now I know you can simply change the index value to 14 hours to the left using an index value of [14]. But when you switch the chart to a 15 min or 5 min time frame these plot do not adapt and the plots are not centered where I intended.

    FYI. I really need to have a solution that works with a plot statement. I cannot use a drawing object placed at the desired location unless I can use that drawing object as an anchor point from which to adjust my plot values.

    Hope that makes sense. Thanks in advance for any assistance or suggestions you have.

    Click image for larger version  Name:	Screen Shot 2021-08-22 at 12.20.38 PM.png Views:	10 Size:	123.5 KB ID:	1168601
    Last edited by 618Fib; 08-22-2021, 07:48 PM.

    #2
    Hello 618Fib,

    Using a barsAgo index of [2] would be two bars ago. If you want to set a value from 2 bars ago, you would use that index on the plot series.

    MyPlot[2] = MyValue;

    On a 60 minute chart that would be 2 bars or 120 minutes. Are you trying to shift 2 days or 120 minutes?

    If you want a barsAgo index from 2 days ago on the primary series you can use CurrentBar - Bars.GetBar(Times[1].AddDays(-2)).
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_ChelseaB View Post
      Hello 618Fib,

      Using a barsAgo index of [2] would be two bars ago. If you want to set a value from 2 bars ago, you would use that index on the plot series.

      MyPlot[2] = MyValue;

      On a 60 minute chart that would be 2 bars or 120 minutes. Are you trying to shift 2 days or 120 minutes?

      If you want a barsAgo index from 2 days ago on the primary series you can use CurrentBar - Bars.GetBar(Times[1].AddDays(-2)).
      https://ninjatrader.com/support/help...nt8/getbar.htm
      Thanks for that link Chelsea B.
      I will check out those details tomorrow morning and post back here if that gives me the solution I have been trying to achieve.

      Comment


        #4
        Quick update. I manage to get this working and it now plots a single triangle over the first bar of the day in which the pattern is confirmed. Problem I am trying to work out now is how to carry that value forward using recursion in another plot. I use recursion all the time on other platforms but can't get it working on NT8. Are there any tricks I should know about to build recursive variables? For example if previous bar contains a value but the current bar contains "n/a", how can I an if/then/else statement to test for that condition and assign a recursive value to the current bar? I'm exhausted from trying to figure this out so I don't have the strength to post the current version of my test code. Perhaps tomorrow I can do that before getting back to work on this. But I hope someone will help me out with some suggestions before then. Thanks!

        Comment


          #5
          Hello 618Fib,

          What do you mean by recursive variables?

          Recursion would be something that calls itself. A variable is not a method that can call itself. A recursive method could call itself...

          If you want to know if a series has a value set on the current bar, you can check this with Series.IsValidDataPoint().
          https://ninjatrader.com/support/help...ddatapoint.htm

          You can assign the previous bars value if CurrentBar is at least 1.
          if (!MySeries.IsValidDataPoint(0) && CurrentBar > 0)
          MySeries[0] = MySeries[1];

          However, I do not know what you mean by assign a recursive value as the value cannot call itself.
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            Originally posted by NinjaTrader_ChelseaB View Post
            Hello 618Fib,

            What do you mean by recursive variables?

            Recursion would be something that calls itself. A variable is not a method that can call itself. A recursive method could call itself...

            If you want to know if a series has a value set on the current bar, you can check this with Series.IsValidDataPoint().
            https://ninjatrader.com/support/help...ddatapoint.htm

            You can assign the previous bars value if CurrentBar is at least 1.
            if (!MySeries.IsValidDataPoint(0) && CurrentBar > 0)
            MySeries[0] = MySeries[1];

            However, I do not know what you mean by assign a recursive value as the value cannot call itself.
            Thanks again Chelsea B. for providing some helpful tips. It seems we agree on the definition of recursion but I may have used some language that lead you to believe I was speaking of something else. For the context of my recent inquiry, recursion is where the current bar's value is based on the value of that same variable on a previous bar. Seems that your solution which includes "MySeries[0] = MySeries[1]" is exactly what I was speaking about. And the ".IsValidDataPoint(0)" also provides the other piece of this puzzle I was missing. Which is how to tell if a value in any element of a series is a valid value for the given datatype. This should do the trick. I will test it out and add a new comment to this post to confirm.
            Thanks!

            Comment


              #7
              Hello 618Fib,

              Below is a link to an educational site on recursion.


              (Google also has a funny easter egg that searching the word recursion suggests to you 'did you mean recursion?' and loads the same page over and over)

              But, yes you can set the current value to the previous value and push that value forward. Below is a link to an example.
              Chelsea B.NinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
              0 responses
              580 views
              0 likes
              Last Post Geovanny Suaza  
              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
              0 responses
              335 views
              1 like
              Last Post Geovanny Suaza  
              Started by Mindset, 02-09-2026, 11:44 AM
              0 responses
              102 views
              0 likes
              Last Post Mindset
              by Mindset
               
              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
              0 responses
              554 views
              1 like
              Last Post Geovanny Suaza  
              Started by RFrosty, 01-28-2026, 06:49 PM
              0 responses
              552 views
              1 like
              Last Post RFrosty
              by RFrosty
               
              Working...
              X