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

SMA erroring on secondary data series

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

    SMA erroring on secondary data series

    Hello,

    I have a strategy developed and has so far been working since the beginning of the month, I am currently in a live test phase of development so no development changes have been made to the script.

    For some reason today I go to enable the strategy on Ninjatrader as I normally would however it is throwing an error in the logs:

    Error on calling 'OnBarUpdate' method on bar 22: 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.

    I have narrowed it down to an SMA indicator that I am using, which is being created with a secondary data series

    OnStateChange() Code:
    Code:
    ...
    else if (State == State.Configure)
    {
            AddDataSeries(BarsPeriodType.Minute, 120);
    }
    else if (State == State.DataLoaded)
    {
    SMA2Hour = SMA(Closes[1], 200);
    ...​
    ​
    OnBarUpdate Code:
    Code:
                var sma2HourValue0 = SMA2Hour.Value[0];
                var sma2HourValue1 = SMA2Hour.Value[1];​
    It will error on the second line of the above when assigning the sma2HourValue1, and also further down the script when actually being used in logic.

    This is very worrying since nothing has changed in the script, and I can see the 2 hour data on the charts. This strategy is due to go live at the end of this month however this is a massive problem!

    I have also deleted the NinjaTrader.sqllite file in the db folder as this has caused issues previously, I have also restarted the application. This was working fine last night.

    Any help would be massively appreciated.

    Thank you in advance!

    #2
    Hello MrHump,

    Calling Value[1] would cause an indexing error if there is no check that CurrentBar is greater than 1, as there would not be a bar index for 1 bar ago.

    Below is a link to a forum post on indexing errors.
    Hello, I want to create an indicator that show data in a chart but calculate in other charttime different to the time of the chart where is showed. For example:
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi NinjaTrader_ChelseaB​,

      Thank you for your response!

      What would cause there not to be a bar 1 bar ago? The times this was tested there should definitely be 2 hours worth of data?

      If i go to look at the 2 hour instrument chart there are multiple bars on there, and it has worked on other days at this time? It is also still erroring now after 4 hours has passed so there definitely should be data there!

      Thanks Again.

      Comment


        #4
        Hello MrHump,

        When CurrentBar is 0, this is the first bar.

        Lets say you were putting apples in basket.

        The first apple is apple 0. The second apple is apple 1.

        You put apple 0, the first apple, into the basket.

        There is only 1 apple in the basket and this has an index of 0.

        How do you retrieve the previous apple put in the basket?

        You haven't put another apple in the basket before you put in the first apple.

        This doesn't make sense and is an impossible task.

        When CurrentBar gets to 1, this means two bars have been processed (or with our analogy, 2 apples have been put in the basket) with the indexes 0 and 1.

        Now that there are two bars, you retrieve the bar previous to the most recent bar (or retrieve the apple put in the basket before the last apple).
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Apologies for the confusion, my question was not around how the ArrayOutOfBounds exception works I am aware of how indexing works as I am a Software Development Engineer, my confusion is around there being no data in this series?

          I would expect there to be data, at the time of this error the value of CurrentBar is 22.

          Please note this has now been fixed, the lack of data was due to the higher time frame not having enough data. I assumed that this would have been populated when adding the data series however I assume this is because of how the back tester works with historical data and loading that data, strange that this error has only just become a problem after almost 2 weeks of testing! Thank you again for your help.

          Comment


            #6
            Hello MrHump,

            Different series will have bar closes at different times.

            CurrentBars[0] might be at bar 22, but CurrentBars[1] may not have a bar yet.

            For whichever series you are calling data, you will need to check that the index is less than the CurrentBars[] of the BarsInProgress of that series.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Yea this makes sense, I did not know CurrentBars could access multiple data series!

              It was intermittence because the data series within the instrument moves along and was trying to run this code to enter a long position which meant the trade entry logic was being ran earlier than it ever had previously!

              Thank you again for your support, this is now much less concerning

              Comment


                #8
                Hello MrHump,

                This is in the forum post I linked in post # 2.
                This is especially important when multiple series are added to a single script as the data for all series may not start at the same time. Each series will need a check that CurrentBars[series index] is greater than the largest barsAgo index used.
                // neither the primary series or added 1 minute series have at least 2 bars.
                if (CurrentBars[0] < 1 || CurrentBars[1] < 1)
                return;​
                Chelsea B.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by llanqui, Today, 10:32 AM
                0 responses
                1 view
                0 likes
                Last Post llanqui
                by llanqui
                 
                Started by StockTrader88, 03-06-2021, 08:58 AM
                45 responses
                3,992 views
                3 likes
                Last Post johntraderuser2  
                Started by TAJTrades, Today, 09:46 AM
                0 responses
                7 views
                0 likes
                Last Post TAJTrades  
                Started by rhyminkevin, Yesterday, 04:58 PM
                5 responses
                62 views
                0 likes
                Last Post dp8282
                by dp8282
                 
                Started by realblubb, Today, 09:28 AM
                0 responses
                8 views
                0 likes
                Last Post realblubb  
                Working...
                X