Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Analyzing ATR indicator in secondary data series

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

    Analyzing ATR indicator in secondary data series

    I am running my strategy on 1-minute bar chart. When the strategy is first loaded I have this code to include the daily chart of the same symbol.

    Code:
    if (State == State.Configure)
    {
        AddDataSeries(Data.BarsPeriodType.Day, 1);  // add secondary day bar
    }
    Then in Historical life cycle I want to perform some analysis on the daily data series using the ATR indicator.

    Code:
    if (State == State.Historical)
    {
        Print(ATR(BarsArray[1], 14)[0]);
    }
    What I want to do is find the min and max of the ATR value on the daily chart for the past one year (365 days). I put the print statement just to test what I see and I get an exception with that code.

    Exception message:
    "accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart."

    How do I achieve what I want just once on strategy initialization?

    #2
    Hello jiminssy,

    Thank you for writing in.

    Before checking for the ATR value on your secondary series, you'll want to make sure that at least once daily bar exists first. Without any sort of checks, the code is going to try to access the daily bar even if one doesn't exist.

    More information about this subject can be found here: http://ninjatrader.com/support/forum...ead.php?t=3170

    While the information above is referring to a single-series script, you can easily replace CurrentBar with CurrentBars[1] to check the CurrentBar value of your secondary series: https://ninjatrader.com/support/help...urrentbars.htm

    Because CurrentBar starts at the value of 0, you can check that you have at least one bar by doing the following before attempting to access the ATR value:

    Code:
    // if we do not have at least one bar elapse in our secondary series, stop evaluating the rest of the code
    protected override void OnBarUpdate()
    {
         if (CurrentBars[1] < 0)
              return;
    
         // the BarsInProgress check will only allow the print to occur every time the secondary series calls OnBarUpdate()
         if (State == State.Historical && BarsInProgress == 1)
              Print(ATR(BarsArray[1], 14)[0]);
    }
    Please, let us know if we may be of further assistance.
    Zachary G.NinjaTrader Customer Service

    Comment


      #3
      Based on the code supplied by you, OnBarUpdate gets called even for State.Historical? Is there any state I can use to perform strategy initializations? This is what I want to do.

      1. load two data series. 1-minute and 1-day.
      2. upon loading all data, find the min and max of ATR value in the last 365 days from the 1-day data series.

      This initialization should happen only once on strategy load and before any real-time OnBarUpdate() is called. Why do I have to check to see if bars exist in 1-day data series if they are already all loaded in this case? I am not too familiar with NinjaTrader's strategy life cycles so if you could help me, I would appreciate it much.

      Comment


        #4
        Hello jiminssy,

        OnBarUpdate() is called both historically and in real-time. This is how historical orders are generated when adding a strategy to a chart, for example, or through a backtest.

        The OnBarUpdate() method is called for each bar contained in both of your data series. Let's pretend you are running your script on a 1-minute chart. On the very first minute bar (think of the 1st bar on the chart from left to right) the value of "ATR value of daily bar" (ATR(BarsArray[1], 14)[0]) does not yet exist (because the daily bar does not yet exist at this point) and your indicator/strategy will not work and throw an exception to the Control Center Log tab "Index was out of range...".

        Finding the minimum and maximum ATR values can be done with the MIN() and MAX() method calls.


        Zachary G.NinjaTrader Customer Service

        Comment


          #5
          Oh so when strategy is first loaded onto the 1-minute chart, OnBarUpdate() is called for both data series for all the bars from left to right?

          Comment


            #6
            Hello jiminssy,

            This is correct. OnBarUpdate() is called upon the close of each historical bar on your chart. Therefore, on the first minute bar, a day bar has not yet closed and when you try to access that data on the first minute bar, you're going to run into that run-time error since the data you're trying to get doesn't exist at that point.
            Zachary G.NinjaTrader Customer Service

            Comment


              #7
              Thank you very much. This is everything I needed.

              Comment


                #8
                One last thing is when the strategy calls OnBarUpdate() while in State.Historical, how do you check if I am on the right most bar for the secondary data series?

                Comment


                  #9
                  Hello jiminssy,

                  Can you please define right-most bar?

                  Are you asking how to check if your script is evaluating the secondary series' final historical bar?

                  You can do this:

                  Code:
                  if (CurrentBars[1] == BarsArray[1].Count - 2)
                       // do something
                  Zachary G.NinjaTrader Customer Service

                  Comment


                    #10
                    Yes I am referring to the most recent bar in the second data series when I meant right most bar.

                    Comment


                      #11
                      Hello jiminssy,

                      You can do this:

                      Code:
                      if (CurrentBars[1] == BarsArray[1].Count - 1)
                           // do something
                      This if statement will evaluate true if the strategy is evaluating the last bar in the series.
                      Zachary G.NinjaTrader Customer Service

                      Comment


                        #12
                        It seems like

                        if (CurrentBars[1] == BarsArray[1].Count - 1)

                        is incorrect. I can get it to work with

                        if (CurrentBars[1] == BarsArray[1].Count - 2)

                        Could you verify with me which is correct to use in finding the most recent (right most) bar in the data series?

                        Comment


                          #13
                          Hello jiminssy,

                          You would use Count - 2 if your indicator's Calculate property is set to Calculate.OnBarClose in State.SetDefaults (note that this is going to be the previous bar, not the currently building bar).

                          You would use Count - 1 if your indicator's Calculate property is set to Calculate.OnEachTick or Calculate.OnPriceChange (this is going to be the currently building bar).
                          Zachary G.NinjaTrader Customer Service

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                          0 responses
                          563 views
                          0 likes
                          Last Post Geovanny Suaza  
                          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                          0 responses
                          329 views
                          1 like
                          Last Post Geovanny Suaza  
                          Started by Mindset, 02-09-2026, 11:44 AM
                          0 responses
                          101 views
                          0 likes
                          Last Post Mindset
                          by Mindset
                           
                          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                          0 responses
                          547 views
                          1 like
                          Last Post Geovanny Suaza  
                          Started by RFrosty, 01-28-2026, 06:49 PM
                          0 responses
                          548 views
                          1 like
                          Last Post RFrosty
                          by RFrosty
                           
                          Working...
                          X