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

ATR(5)[1] throwing out of range error

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

    ATR(5)[1] throwing out of range error

    Hello,

    I must be doing something stupid, but have not been able to find what it is...

    I have a fully loaded chart with 252 bars (daily interval).

    When I am trying to get previous day's ATR(5), the following line is throwing an out of range error:

    double ATR_5 = ATR(5)[1];

    The error in the output window is:
    Error on calling 'OnRender' method on bar 251: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.

    The weird thing is that getting today's value works (i.e. double ATR_5 = ATR(5)[0]; )

    What am I missing?

    Thanks a lot!


    Libor

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

    Relative bar indexes should not be used during OnRender, and will occasionally cause messages such as the message you saw. I would like to recommend performing this calculation during OnBarUpdate or OnMarketData/OnMarketDepth, and storing the result for use during OnRender. This help guide section goes more into depth on this (emphasis mine) :

    Originally posted by http://ninjatrader.com/support/helpGuides/nt8/en-us/onrender.htm
    Notes:
    1. Thie method uses the 3rd party SharpDX library to render custom Direct2D Text and Shapes. For a walk through for using the SharpDX, please see the educational resource Using SharpDX for Custom Chart Rendering
    2. The OnRender() method frequently runs once the State has reached State.Realtime in response to market data updates or a user interacting with the chart (e.g., clicking, resizing, rescaling, etc.)
    3. For performance optimizations, the timing of the calls to OnRender() are buffered to at least 250ms, and re-renders once internal logic determines that values may be out-of-date. See also ForceRefresh() for more details
    4. When using the Strategy Analyzer, OnRender() does NOT call until you switch to the "Chart" display and renders from State.Terminated. As a result, this method should NOT be relied on for historical Strategy backtesting logic and should ONLY be used for rendering purposes

    5. Unlike market data events and strategy order related events, there is NO guarantee that the barsAgo indexer used for Series<T> objects are in sync with the current bars in progress. As a result, you should favor using an absolute index method to look up values (e.g., <series>.GetValueAt(), Bars.GetOpen(), etc)


    6. While OnRender() is an excellent means for customizing and enhancing indicators and strategies, its application can easily be abused, resulting in unforeseen performance issues which you may not catch until the right conditions (e.g., in the hands of your users during an FOMC event)
    7. Please limit any calculations or algorithms you may be tempted run in OnRender() simply to rendering. You should always favor precomputed values and store them for rendering later as the preferred approach to working with the OnRender() method (e.g., reusing brushes, passing values from OnBarUpdate, etc.). See also OnRenderTargetChange() method for more information on reusing Brushes
    8. If you are using this method as an opportunity to "hook" onto a user related event, such as when a user selects a 3rd party control, you should alternatively consider using the events of that control independent of official NinjaScript events. See also TriggerCustomEvent()
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      Hi,

      thank you for the quick answer!

      I think the error message mentioned OnRender as it must be a frequent reason for this error, but I am actually getting the same error when I try to access the ATR(5)[1] value from OnBarUpdate event.

      I am not sure what to do to fix this...

      Thanks,

      Libor

      EDIT:

      Sorry, when accessing ATR(5)[1] from OnBarUpdate(), the error message is:
      Indicator 'LATA_Planner': Error on calling 'OnBarUpdate' method on bar 0: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.
      61
      Last edited by liborc; 04-25-2017, 08:15 AM.

      Comment


        #4
        Thank you for this additional information. Another common reason for this is that you are using a multi-timeframe indicator, and there are not enough bars in the current timeframe. You can check against this with

        Code:
        [FONT=Courier New]
        protected override void OnBarUpdate()
        {
            if (CurrentBar < 5)
            {
                return;
            }
            // the rest of your OnBarUpdate code here
        }[/FONT]
        Since CurrentBar will always refer to the current bars set. If this does not resolve your query, would it be possible to post a stripped down code sample?
        Jessica P.NinjaTrader Customer Service

        Comment


          #5
          Hello,

          thanks again for the swift answer.

          I am not sure how to strip my code down as I am using an underlying MySQL database and also it is quite long now.

          The error I get can be seen in the watch panel in VS (see screenshot). My bars are loaded (there are 253 of then now),I am not in OnRender, CurrentBar = 1 and yet, I only can access ATR(5)[0] and not ATR(5)[1].

          It is puzzling...

          Thanks,

          Libor
          Attached Files

          Comment


            #6
            OK - gotcha. Please disregard my previous post.
            Thank you for your help!

            Libor

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by PhillT, Today, 02:16 PM
            2 responses
            6 views
            0 likes
            Last Post PhillT
            by PhillT
             
            Started by Kaledus, Today, 01:29 PM
            3 responses
            10 views
            0 likes
            Last Post NinjaTrader_Jesse  
            Started by frankthearm, Yesterday, 09:08 AM
            14 responses
            47 views
            0 likes
            Last Post NinjaTrader_Clayton  
            Started by gentlebenthebear, Today, 01:30 AM
            2 responses
            14 views
            0 likes
            Last Post gentlebenthebear  
            Started by PaulMohn, Today, 12:36 PM
            2 responses
            17 views
            0 likes
            Last Post PaulMohn  
            Working...
            X