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

Indicator generating index out of range error

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

    Indicator generating index out of range error

    Hi... I have an indicator generating the error: "Error on calling... on bar 0: You are accessing an index with a value that is invalid since its out of range." I can't spot the problem in my code. Any help would be greatly appreciated. The method is below, and I attached the relevant screenshots from Market Analyzer that show my settings..

    Code:
    protected override void OnBarUpdate()
            {
                int countOfHighsUnderSMA50 = 0;
                double totalVol = 0;
                for(int i=1;i<11;i++)
                {
                    totalVol = totalVol + Volume[i];
                    if(High[i]<SMA(50)[i])
                        countOfHighsUnderSMA50++;
                }
                
                if((Close[0] > SMA(50)[0]) && (countOfHighsUnderSMA50==10) && (Volume[0]>(totalVol/10)*2))
                    Plot0.Set(1);
                else
                    Plot0.Set(0);
            }
    Attached Files

    #2
    Hello CSharpTrader,
    Please make sure there are enough bars before you reference your code to the historical bars. If you use the below code then can you make the indicator work

    Code:
    protected override void OnBarUpdate()
    {
    	
    	if (CurrentBar < 11) return;
            //rest of the coe
    }
    Please refer to this post which further discusses it
    JoydeepNinjaTrader Customer Service

    Comment


      #3
      Thanks..

      Thanks, Joydeep. If you could shed a little light on this for me.. I read the post you provided. It says that the on bar update method is called for every bar referenced within the on bar update method. So, it's a recursive call? I don't quite understand. Based on what it says literally, every method that references a historical bar will throw an exception on the first bar in the data series.

      Comment


        #4
        Hello CSharpTrader,
        Suppose there is only one bar on the chart. However from your code you are calling previous bars (Like High[1] to High[11]) which are non-existence. So unless there are 12 bars on the chart you cannot call High[11].

        Please let me know if I can assist you any further.
        JoydeepNinjaTrader Customer Service

        Comment


          #5
          That I understand... but in the post you attached, it's stated that OnBarUpdate is called for each bar in the data series. So just to clarify (hope I'm not too persistent, I just want to understand), this isn't the case. That said, bar[11] is the 12th bar, so that error is because I have less than 12 in the series.

          This leads me to the question: Why would I get that error, if I have 256 bars set in the series? (I attached a screenshot of my settings in the original post) If there is something wrong in how I have the column's bars set, perhaps?

          Comment


            #6
            Hello CSharpTrader,

            The 256 setting here is just the maximum number of elements that a DataSeries can have before it throws older entries away, a DataSeries may have less elements than 256. You can think of a DataSeries sort of like a linked list.

            I.e. on the very first bar on the chart :

            MySeries.Set(Close[0]);

            Adds 1 element to the DataSeries, and the DataSeries has 1 element in it.

            If you start iterating through the bars into the present, the series will add additional elements to itself.
            Adam P.NinjaTrader Customer Service

            Comment


              #7
              Thanks..

              Thanks much, Adam. Again, I don't want to be too persistent, but can I get clarification per my last post? I had a couple direct questions in it that would be very helpful in clarifying all this, maybe to future readers as well since this seems to be a source of confusion..

              Comment


                #8
                CSharpTrader,

                Sorry, could you outline the items you want to cover? Is it in your post immediately before mine?
                Adam P.NinjaTrader Customer Service

                Comment


                  #9
                  Sure

                  Absolutely. The responsiveness here is unparalleled btw. OK...

                  Joydeep referred me to the thread:

                  http://ninjatrader.com/support/forum...ead.php?t=3170

                  when I read it literally, I think it says that OnBarUpdate calls itself recursively for every bar in the data series. Is this the case? My current understanding is that you need the furthest back bar referred to from the current bar[0] in your code. But that bar says in every method call, the code is called on each bar in the series... if that's the case, the first bar (oldest) would always throw an error on, say, Close[1].

                  If I have an indicator on a bar like SMA(20)[0], I need 20 bars, right? Just over- checking..

                  In my first post on this subject, I was getting index out of range errors... if I THINK (key word) I have enough bars, what causes this error? There should be plenty of historical data, I'm not using thinly traded symbols, and I'm only looking back 10 days. That's why I attached the Market Analyzer screenshots, to see if I wasn't holding enough data, but I think I am. So what would cause my index out of range errors?

                  Thanks again for the patience..

                  Comment


                    #10
                    Originally posted by CSharpTrader View Post
                    Absolutely. The responsiveness here is unparalleled btw. OK...

                    Joydeep referred me to the thread:

                    http://ninjatrader.com/support/forum...ead.php?t=3170

                    when I read it literally, I think it says that OnBarUpdate calls itself recursively for every bar in the data series. Is this the case? My current understanding is that you need the furthest back bar referred to from the current bar[0] in your code. But that bar says in every method call, the code is called on each bar in the series... if that's the case, the first bar (oldest) would always throw an error on, say, Close[1].
                    Its not really recursive, its just iterative. All indicators / strategies begin on the first bar at the furthest time into the past that exists on your charts, then iterates forward. Each time a new bar feeds in, the OnBarUpdate() method is called and does whatever it does.

                    Just using the data series "Close" as an example :

                    Let's say I attach the strategy to a chart with N bars on it. It begins at bar 1 (the bar furthest into the past).

                    Bar 1 => MyIndicator.OnBarUpdate() is called => Close with index 0 exists
                    Bar 2 => MyIndicator.OnBarUpdate() is called => Close with index 0 through 1 exists
                    Bar 3 => MyIndicator.OnBarUpdate() is called => Close with index 0 through 2 exists
                    .......
                    Bar N => MyIndicator.OnBarUpdate() is called => Close with index 0 through N-1 exists

                    Each bar, the 0 index always references the most recent calculations or price data with the data that is available at that specific time. Index of 1 is the bar previous, etc.

                    At bar N, Close[N-1] will reference the first bar furthest into the past on my chart, and Close[0] will be the current closing price of the most recently closed bar for an instrument.

                    A slight nuance here though is that the DataSeries class is pegged to the actual bars that exist on your chart. So for example if I call :

                    MySeries.Set(0);
                    MySeries.Set(1);

                    MySeries[0] will be 1, 0 is overwritten for that index. In fact MySeries[N] will always be equal one with that snippet because I would always be overwriting it on the current bar. Another bar didn't close, so my series didn't expand the number of elements it's allowed to have.

                    Basically the strategy or indicator operates as if it hasn't seen any bars yet on your chart, starts with the first one in the past on your chart, then iterates forward to the present.

                    Originally posted by CSharpTrader View Post
                    If I have an indicator on a bar like SMA(20)[0], I need 20 bars, right? Just over- checking..
                    Internally the SMA has a check against it causing any issues here. You just wont get an "accurate" SMA calculation until 20 bars have passed.

                    Originally posted by CSharpTrader View Post
                    In my first post on this subject, I was getting index out of range errors... if I THINK (key word) I have enough bars, what causes this error? There should be plenty of historical data, I'm not using thinly traded symbols, and I'm only looking back 10 days. That's why I attached the Market Analyzer screenshots, to see if I wasn't holding enough data, but I think I am. So what would cause my index out of range errors?

                    Thanks again for the patience..
                    It's probably a good idea to do something like this example. If I know I need 20 bars of Close data to calculate something, add the following to the beginning of my OnBarUpdate() method.

                    if ( CurrentBar < 20 ) return;

                    Or you can take a peek at the SMA code and see how it handles it internally. It just uses the equivalent of an SMA(1) on the very first bar, an SMA(2) on the second, an SMA(3) on the 3rd, etc. until it hits the 20th bar and from that point forward its using SMA(20) always.

                    One more thing to mention however, is the 256 setting for MaximumBarsLookBack. This is mainly just for saving memory, as it will throw out the oldest entry after 256 elements have been reached and add the newest one to a data series.
                    Last edited by NinjaTrader_AdamP; 09-18-2012, 03:03 PM.
                    Adam P.NinjaTrader Customer Service

                    Comment


                      #11
                      Getting warmer...

                      Thanks for the detail. I think I spot the source of confusion.. I'm not asking/ looking at it in terms of charts, but instead the Market Analyzer. So when referred to that post that explains how the code is called on each bar, starting with the first, am I right to say that that does not apply when the Market Analyzer is using an Indicator for an alert? In that case, it's looking back only as far as it needs, right?

                      That being said, to understand the big picture, what would keep the needed number of bars from being available? (in my case, 10 bars, on an intraday timeframe?)

                      Comment


                        #12
                        CSharpTrader,

                        In the market analyzer, if you right click it > go to "Properties", you will see a "# bars to look back" field. This is the number of bars its loading, however also note that the Market Analyzer operates tick-by-tick rather than on bar close, so each tick inbetween bar closes would overwrite any series' element at the 0 index.

                        It would still behave the same as my example. I.e. it would not immediately have access to all 50 bars, it would load the first bar, then the second, then the third, etc. starting with the one furthest into the past.

                        The only thing that would keep the number of bars from being available is either lack of historical data, loading too few bars into a chart / market analyzer or the fact that the indicator hasn't finished iterating through past bars to the point where 10 bars would be available.
                        Adam P.NinjaTrader Customer Service

                        Comment


                          #13
                          Thanks

                          OK. Getting it.. so can I say that in the context of Market Analyzer, it iterates over the entire collection of bars, but it only is doing the real work/ calculation on and relative to bar[0]? So the out of index errors I was seeing in Market Analyzer could effectively be ignored, if bar[0] had the necessary amount of data bars.

                          Comment


                            #14
                            CSharp,

                            Correct, it iterates only once over every historical bar. As soon as it reaches the present all the "work" is being done on index 0, until another bar closes in the Market Analyzer.

                            The out of index thing may cause run time errors if you don't handle the exception, hence I usually recommend just using something like the following :

                            if ( CurrentBar < HowManyBarsINeed) return;
                            Adam P.NinjaTrader Customer Service

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by michi08, 10-05-2018, 09:31 AM
                            5 responses
                            741 views
                            0 likes
                            Last Post NinjaTrader_ChelseaB  
                            Started by The_Sec, Today, 02:29 PM
                            0 responses
                            1 view
                            0 likes
                            Last Post The_Sec
                            by The_Sec
                             
                            Started by tsantospinto, 04-12-2024, 07:04 PM
                            4 responses
                            62 views
                            0 likes
                            Last Post aligator  
                            Started by sightcareclickhere, Today, 01:55 PM
                            0 responses
                            1 view
                            0 likes
                            Last Post sightcareclickhere  
                            Started by Mindset, 05-06-2023, 09:03 PM
                            9 responses
                            259 views
                            0 likes
                            Last Post ender_wiggum  
                            Working...
                            X