Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Retrieve values from previous bars of a Series<double>

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

    Retrieve values from previous bars of a Series<double>

    Hello,

    Can someone please advise how to retrieve values from previous bars of a Series<double> without getting the below error.

    Indicator 'zzWIP': 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.

    Example code might be:

    Code:
    protected override void OnBarUpdate()
            {
                if (IsFirstTickOfBar) && CurrentBar > (61))
                    {
    
                    //DECLARE SMA VARIABLES
                    SMAs1t0 = SMA(BarsArray[1],SMA5mPeriod)[0];
                    SMAs1t1 = SMA(BarsArray[1],SMA5mPeriod)[1];
                    
                    }
            }​
    Where SMAs1t0 and SMAs1t1 are double variables.

    #2
    Hello Apm123,

    Thank you for your post.

    It looks like you are trying to access a negative index (-1) which is not supported. If you want to access the value 1 bar ago, use 1 as the index. 0 is the current bar, 1 is 1 bar ago, etc.



    Please let us know if you have any further questions.
    Gaby V.NinjaTrader Customer Service

    Comment


      #3
      What is preventing access to the data from 1 bar ago in these code:

      SMAs1t1 = SMA(BarsArray[1],SMA5mPeriod)[1];
      SMAs1t1 = SMA(BarsArray[1],SMA5mPeriod)[CurrentBar -1];

      Neither of these work.

      Comment


        #4
        Indicatively the code is as per the attached.
        Attached Files

        Comment


          #5
          Can you please explain the referencing system for bars.

          If we are calculating "OnBarClose" and the if statement occurs "OnFirstTickOfBar", my naive assumption was that if the primary instrument is a 5 min the OnBarUpdate method would run once every five minutes. Is this correct?

          If we have two instruments loaded into the script, My assumption is that CurrentBar only exists for the primary instrument and not the other two instruments that have been loaded. This means that if the primary instrument is at 60 min intervals and we have another instrument with 5 min intervals, the Closes[x][1] of the 5min bar can only be 5 minutes before the closing of the hourly bar (i.e. 12 5-minute bars would have been skipped as we are only updating every hour on close of the primary instrument. Is this a correct understanding?


          UPDATE: Looking at the attached image (text extract below for legibility), it appears we have three "CurrentBars". One for the primary, one for 60m, one for the 5m.
          - Is this correct?
          - How do we get CurrentBar for each of the series loaded and the primary instrument?

          As we can see we have 58th hourly bar at about 708 5m bars....which seems about right (708/12 = circa 58)....then across the three instruments we have
          - 930 x 5 min primary bars
          - 955 x 5 min bars
          - 65 x 60m bars

          .....therefore....by delaying 15 bars on primary instrument, all of 5m bars are out of sync....? Is this correct? (i.e. bar 145 of non-primary will not be bar 145 on the primary 5-min bar.....it will be offset by 15 bars....)



          Code to get print:

          if (CurrentBar < 13) return;
          if (CurrentBar > 15)
          {
          //Print("\n" + "if loop | time: " + Time[0]);
          Print("time: " + Time[0] + " | " + CurrentBar + " | Close primary 5m: " + Close[0] + " | Close 5m: " + Closes[1][0] + " | Close 60m: " + Closes[2][0]);
          }​



          Text Extract:

          time: 11/09/2024 9:00:00 AM | 58 | Close primary 5m: 1.30824 | Close 5m: 1.30824 | Close 60m: 1.30824
          time: 11/09/2024 9:05:00 AM | 708 | Close primary 5m: 0.66615 | Close 5m: 1.30834 | Close 60m: 1.30824
          time: 11/09/2024 9:05:00 AM | 702 | Close primary 5m: 1.30834 | Close 5m: 1.30834 | Close 60m: 1.30824
          time: 11/09/2024 9:10:00 AM | 709 | Close primary 5m: 0.66657 | Close 5m: 1.30846 | Close 60m: 1.30824
          time: 11/09/2024 9:10:00 AM | 703 | Close primary 5m: 1.30846 | Close 5m: 1.30846 | Close 60m: 1.30824
          time: 11/09/2024 9:15:00 AM | 710 | Close primary 5m: 0.66668 | Close 5m: 1.30859 | Close 60m: 1.30824
          time: 11/09/2024 9:15:00 AM | 704 | Close primary 5m: 1.30859 | Close 5m: 1.30859 | Close 60m: 1.30824
          time: 11/09/2024 9:20:00 AM | 711 | Close primary 5m: 0.66628 | Close 5m: 1.30818 | Close 60m: 1.30824
          time: 11/09/2024 9:20:00 AM | 705 | Close primary 5m: 1.30818 | Close 5m: 1.30818 | Close 60m: 1.30824
          time: 11/09/2024 9:25:00 AM | 712 | Close primary 5m: 0.66613 | Close 5m: 1.30815 | Close 60m: 1.30824
          time: 11/09/2024 9:25:00 AM | 706 | Close primary 5m: 1.30815 | Close 5m: 1.30815 | Close 60m: 1.30824
          time: 11/09/2024 9:30:00 AM | 713 | Close primary 5m: 0.66613 | Close 5m: 1.30817 | Close 60m: 1.30824
          time: 11/09/2024 9:30:00 AM | 707 | Close primary 5m: 1.30817 | Close 5m: 1.30817 | Close 60m: 1.30824
          time: 11/09/2024 9:35:00 AM | 714 | Close primary 5m: 0.66613 | Close 5m: 1.30835 | Close 60m: 1.30824
          time: 11/09/2024 9:35:00 AM | 708 | Close primary 5m: 1.30835 | Close 5m: 1.30835 | Close 60m: 1.30824
          time: 11/09/2024 9:40:00 AM | 715 | Close primary 5m: 0.66603 | Close 5m: 1.30837 | Close 60m: 1.30824
          time: 11/09/2024 9:40:00 AM | 709 | Close primary 5m: 1.30837 | Close 5m: 1.30837 | Close 60m: 1.30824
          time: 11/09/2024 9:45:00 AM | 716 | Close primary 5m: 0.66622 | Close 5m: 1.30856 | Close 60m: 1.30824
          time: 11/09/2024 9:45:00 AM | 710 | Close primary 5m: 1.30856 | Close 5m: 1.30856 | Close 60m: 1.30824
          time: 11/09/2024 9:50:00 AM | 717 | Close primary 5m: 0.66614 | Close 5m: 1.30848 | Close 60m: 1.30824
          time: 11/09/2024 9:50:00 AM | 711 | Close primary 5m: 1.30848 | Close 5m: 1.30848 | Close 60m: 1.30824
          time: 11/09/2024 9:55:00 AM | 718 | Close primary 5m: 0.66609 | Close 5m: 1.30848 | Close 60m: 1.30824
          time: 11/09/2024 9:55:00 AM | 712 | Close primary 5m: 1.30848 | Close 5m: 1.30848 | Close 60m: 1.30824
          time: 11/09/2024 10:00:00 AM | 719 | Close primary 5m: 0.66595 | Close 5m: 1.30829 | Close 60m: 1.30829
          time: 11/09/2024 10:00:00 AM | 713 | Close primary 5m: 1.30829 | Close 5m: 1.30829 | Close 60m: 1.30829
          time: 11/09/2024 10:00:00 AM | 59 | Close primary 5m: 1.30829 | Close 5m: 1.30829 | Close 60m: 1.30829
          time: 11/09/2024 10:05:00 AM | 720 | Close primary 5m: 0.66604 | Close 5m: 1.30867 | Close 60m: 1.30829
          time: 11/09/2024 10:05:00 AM | 714 | Close primary 5m: 1.30867 | Close 5m: 1.30867 | Close 60m: 1.30829​
          Attached Files
          Last edited by Apm123; 09-12-2024, 04:13 AM.

          Comment


            #6
            Referring to the attached file, it appears the that for the condition: if (CurrentBar > 15) each data series is independently considered. Therefore the 60m series isnt evaluated until 16 hours have passed......while the 5m series aren't evaluated until 5*16=80 mins have passed.....

            i.e. the first bar for all series is bar number 16, however:
            - primary instrument (5m) starts immediately after 15 bay delay
            - 5m series starts 5 min later (do not know why....????)
            - 60m series start 16 hours later....

            Surely I am not the only one with questions about referencing/indexing across multiple timeframes and instruments....I have not seen any information on NT site about indexing nor on the individual syntax pages....

            Attached Files

            Comment


              #7
              Hello,

              If we are calculating "OnBarClose" and the if statement occurs "OnFirstTickOfBar", my naive assumption was that if the primary instrument is a 5 min the OnBarUpdate method would run once every five minutes. Is this correct?
              IsFirstTickOfBar can only be used on Calculate.OnEachTick or Calculate.OnPriceChange scripts. This is noted in the Help Guide:

              " This property is only of value in scripts that run tick by tick which is when the Calculate property is set to Calculate.OnEachTick or Calculate.OnPriceChange.​"



              "If we have two instruments loaded into the script, My assumption is that CurrentBar only exists for the primary instrument and not the other two instruments that have been loaded. This means that if the primary instrument is at 60 min intervals and we have another instrument with 5 min intervals, the Closes[x][1] of the 5min bar can only be 5 minutes before the closing of the hourly bar (i.e. 12 5-minute bars would have been skipped as we are only updating every hour on close of the primary instrument. Is this a correct understanding?"

              This is not true. There will be a CurrentBars object for every added data series. OnBarUpdate is going to be called for every added Bars object in your script.



              I highly recommend you read the Help Guide page below on Multi Time Frame & Instruments.





              You can also add a print to your script (at the top of OnBarUpdate) to help you understand which bars object is currently updating. For example,

              Print(Times[0][0] + " BarsInProgress: " + BarsInProgress);
              Gaby V.NinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by MiCe1999, Today, 06:49 AM
              0 responses
              7 views
              0 likes
              Last Post MiCe1999  
              Started by gaz0001, 12-12-2023, 09:36 AM
              13 responses
              282 views
              0 likes
              Last Post kevinenergy  
              Started by timwey, Today, 06:01 AM
              0 responses
              6 views
              0 likes
              Last Post timwey
              by timwey
               
              Started by patrickgeys7, Today, 06:01 AM
              0 responses
              2 views
              0 likes
              Last Post patrickgeys7  
              Started by ayham12, Today, 05:42 AM
              0 responses
              11 views
              0 likes
              Last Post ayham12
              by ayham12
               
              Working...
              X