Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Error when using Closes[1][0] to access secondary dataseries.

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

    Error when using Closes[1][0] to access secondary dataseries.

    I'm currently developing a strategy in NinjaTrader and I've encountered an issue when trying to access the closing price of a secondary data series. According to the NinjaTrader documentation, I should be able to access the closing price of the secondary data series using Closes[1][0]. However, when I try to do this, I get an error.

    Here's a simplified version of my code:


    protected override void OnStateChange()
    {
    if (State == State.Configure)
    {
    AddDataSeries("SPY", BarsPeriodType.Minute, 1);
    }
    }

    protected override void OnBarUpdate()
    {

    if (CurrentBars[0] < 1)
    return;

    double benchmarkClose = Closes[1][0];
    Print(benchmarkClose)
    }​


    When I use Closes[1][0] I get the following error: "Error on calling 'OnBarUpdate' method on bar 1: 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 understand that this error typically occurs when trying to access a bar that doesn't exist yet. However, I'm trying to access the current bar of the secondary data series, which should exist when OnBarUpdate() is called, since CurrentBars[0] > 1.
    I would appreciate any help in resolving this issue. Is there something I'm missing or misunderstanding about how to access the data of a secondary data series?

    It happens even when using examples from the language reference. For context, I am running in sim mode on a trial account. I am using Ninjatrader for data. I have made sure to pick two instruments that have data in trial mode, and that the error happens regardless of the 'BarsPeriodType'.


    Thank you in advance for your help.​

    #2
    Hello gouteg,

    Ensure BarsInProgress 1 has at least 1 bar.

    if (CurrentBars[1] < 1)
    return;

    Below is a link to the help guide.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Yup, have tried that. If I do something like:

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

      I get no output and the strategy doesn't run.

      Comment


        #4
        Hello gouteg,

        Is the strategy still enabled?

        Are you able to load the SPY on a separate chart?

        Try printing CurrentBars[1] to see if a bar has been processed.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Yes the strategy is still enabled. I'm doing this in strategy analyzer.

          It doesnt seem like the bars are processed. If i print CurrentBars[1] i just get -1 in ouput.

          Comment


            #6
            Hello gouteg,

            I was not able to confirm, are you able to open a separate chart to the SPY?
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Yes, I am able to load it on a separate chart.

              CurrentBars[1] is -1 for some reason on bar update, which explains why the strategy doesnt run when we return nothing if CurrentBars[1] < 1.

              Comment


                #8
                Ok, it seems to be the string in:

                AddDataSeries("SPY", BarsPeriodType.Minute, 1);

                Thats causing the issue. I tried adding a dataseries without the string, and Closes[1][0] worked fine. Is there a different way to reference an instrument? I don't see how "SPY" specifies that we want the data of SPY.

                Comment


                  #9
                  Hello gouteg,

                  There would not be another way to specify the instrument other than the string Instrument parameter.

                  Without specifying this parameter, the instrument of the primary series is used.

                  CurrentBars[1] being -1 means that no data was loaded. This would suggest that you are not connected to a data feed that provides data for equities.

                  May I have a screenshot of the chart of the SPY 1 minute showing up-to-date bars on the chart?
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #10
                    Here's the screenshot.

                    The chart doesnt update live though, but every time I reload it the price updates. The error happens even if I pick an instrument that live updates. I dont see why this is relevant though, as I am testing on historical data through Strategy Analyzer.

                    Comment


                      #11
                      Hello gouteg,

                      If the chart is able to load the data, the strategy analyzer would also be able to load the data.

                      You have provided a chart of the $SP500 which is not the same instrument as the SPY.

                      Please provide a screenshot of the chart with the SPY instrument loaded.
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #12
                        Yes I know, my apologies, but it makes no difference. The same error happens if I use "SP500" as the string. No matter what instrument I put as a string, I get the error, that's my point.

                        (If the chart is able to load the data, the strategy analyzer would also be able to load the data.) Yes I know, that's why I am a little confused as to why I cant access the data.

                        Comment


                          #13
                          Hello gouteg,

                          The symbol would be $SP500 if this is what you are intending to load the ^SP500 index and not SP500 (which would load as an equity).

                          Please provide a screenshot of a chart of the SPY 1 minute so that I may confirm the data is available and further assist.
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #14
                            No worries I fixed it, thanks for the assistance!

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by argusthome, 03-08-2026, 10:06 AM
                            0 responses
                            116 views
                            0 likes
                            Last Post argusthome  
                            Started by NabilKhattabi, 03-06-2026, 11:18 AM
                            0 responses
                            61 views
                            0 likes
                            Last Post NabilKhattabi  
                            Started by Deep42, 03-06-2026, 12:28 AM
                            0 responses
                            40 views
                            0 likes
                            Last Post Deep42
                            by Deep42
                             
                            Started by TheRealMorford, 03-05-2026, 06:15 PM
                            0 responses
                            43 views
                            0 likes
                            Last Post TheRealMorford  
                            Started by Mindset, 02-28-2026, 06:16 AM
                            0 responses
                            82 views
                            0 likes
                            Last Post Mindset
                            by Mindset
                             
                            Working...
                            X