Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Add Data Series to Fibonacci Zones indicator

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

    Add Data Series to Fibonacci Zones indicator

    I am trying to add the UniRenkoBen Data Series to the attached Fibonacci Zones indicator and I am getting this error message: 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.
    The indicator works without error if I add it to the UniRenkoBen chart but if I add it to a 1000 tick chart I get the error.
    Can someone take a look at the code and help me with the code error?
    thanks,
    David
    Attached Files
    Last edited by fiddlinground; 03-10-2022, 01:42 PM.

    #2
    Hello fiddlinground,

    Thank you for your note.

    In multi series processing, the CurrentBars starting value will be -1 until all series have processed the first bar. So what that means is you're using CurrentBar before that happens here (line 118-119):

    fA.OnBarUpdate( CurrentBar );
    fB.OnBarUpdate( CurrentBar );

    And until both series have completed processing their first bar on the chart, that's going to be a -1 and throw an error. This should allow you to move forward, though you'll need to change a lot of logic if you're wanting to calculate the zones off the UniRenko bars and then display them on the primary series bars if that's what you're intending to do:

    protected override void OnBarUpdate()
    {
    // ensure both data series have processed at least 1 bar prior to starting
    if(CurrentBars[0] < 1 && CurrentBars[1] < 1)
    return;

    fA.OnBarUpdate( CurrentBar );
    fB.OnBarUpdate( CurrentBar );

    I'd definitely recommend reviewing our help guide on multi series processing I linked at the beginning of this post as that may help with adjusting the logic to utilize the secondary series.

    Please let us know if we may be of further assistance to you.

    Comment


      #3
      Hi Kate,
      I adjusted the code like this but I am still getting the same error message?

      protected override void OnBarUpdate()
      {
      if (BarsInProgress == 1)
      {
      Value[0] = Closes[1][0];
      }
      else if (BarsInProgress == 0 && !Value.IsValidDataPoint(0) && CurrentBars[0] > 0)
      {
      // if there is no value set for this bar (meaning the added series didnt close, set to the previous bars value for a continuous plot)
      Value[0] = Value[1];
      }
      // ensure both data series have processed at least 1 bar prior to starting
      if(CurrentBars[0] < 1 && CurrentBars[1] < 1)
      return;

      fA.OnBarUpdate( CurrentBar );
      fB.OnBarUpdate( CurrentBar );

      if ( CurrentBar < 2 )
      return;

      // A condition to create a rectangle
      if ( UpTrendA[ 1 ] == DownTrendA[ 1 ] )
      createZone( CurrentBar - 1 );

      checkStopZone( CurrentBar );
      showZones();
      }

      thanks,
      David

      Comment


        #4
        Hello David,

        Thank you for your reply.

        Looks like you've modified the code further from what you originally posted. You're still getting the error because you are trying to assign the prior value of the plot to the current value before checking if there have been enough bars in the series to look back 1:

        Code:
        protected override void OnBarUpdate()
        {
        if (BarsInProgress == 1)
        {
        Value[0] = Closes[1][0];
        }
        else if (BarsInProgress == 0 && !Value.IsValidDataPoint(0) && CurrentBars[0] > 0)
        {
        // if there is no value set for this bar (meaning the added series didnt close, set to the previous bars value for a continuous plot)
        [B]Value[0] = Value[1];[/B]
        }
        // ensure both data series have processed at least 1 bar prior to starting
        if(CurrentBars[0] < 1 && CurrentBars[1] < 1)
        return;
        You'd want to put the CurrentBars check at the beginning of OnBarUpdate to avoid this.

        The other thing I'd note is that you're assigning values to a plot on BarsInProgress == 1 and this should be avoided. Plots will always be synced to the primary data series and should be assigned to from that series. It does look like you're trying to avoid issues with that, but it is best practice to assign plot values on BIP == 0.

        Please let us know if we may be of further assistance to you.

        Comment


          #5
          Hi Kate,

          I moved the CurrentBars to the top and also BIP == 0 but I am still having this same warning message?
          Here is what I have now:
          protected override void OnBarUpdate()
          {
          // ensure both data series have processed at least 1 bar prior to starting
          if(CurrentBars[0] < 1 && CurrentBars[1] < 1)
          return;

          if (BarsInProgress == 0)
          {
          Value[0] = Closes[1][0];
          }
          else if (BarsInProgress == 0 && !Value.IsValidDataPoint(0) && CurrentBars[0] > 0)
          {
          // if there is no value set for this bar (meaning the added series didnt close, set to the previous bars value for a continuous plot)
          Value[0] = Value[1];
          }
          // ensure both data series have processed at least 1 bar prior to starting
          if(CurrentBars[0] < 1 && CurrentBars[1] < 1)
          return;

          fA.OnBarUpdate( CurrentBar );
          fB.OnBarUpdate( CurrentBar );

          Comment


            #6
            Hello fiddlinground,

            Thank you for your reply.

            After adding that, it hits an error at line 367:

            double h = UseHighLow ? High[0] : Close[0];

            This looks to be a secondary indicator that inherits from the parent, and is pretty unusual, but I believe it's because it's not able to access the added data series that's going through OnBarUpdate. I will need to check with our team lead on how to handle that, but he is currently out of the office, so I will touch base with him on this when he returns on Monday.

            Thanks in advance for your patience.

            Comment


              #7
              Hello fiddlinground,

              Thank you for your patience.

              I've touched base with our team lead and he thinks that the issue is related to trying to access Series data in the FibonacciST class since that doesn't inherit from the Indicator class. I'm currently trying some things to see if we can't just make that part into an indicator that can then simply be called from the Fibonacci Zones indicator since I would not expect to hit that issue in an indicator or strategy that uses OnBarUpdate.

              It's right about the end of my day today, but I will continue working on this tomorrow and will hopefully have further information for you soon.

              Thanks in advance; I look forward to assisting you further.

              Comment


                #8
                Hi Kate,

                I really appreciate your help,

                David

                Comment


                  #9
                  Hello fiddlinground,

                  Thank you for your patience.

                  I've been working on this and running into some issues. Could you provide the original indicator before any of your changes so I can compare against the original working version?

                  Thanks in advance; I look forward to assisting you further.

                  Comment


                    #10
                    Hi Kate,

                    No Problem,

                    thanks,
                    David
                    Attached Files

                    Comment


                      #11
                      Hi Kate,

                      Could the problem be in the UniRenkoBen Bars type? They were made from UniRenkoBars I am attaching. Maybe using these would help?

                      thanks,
                      David
                      Attached Files

                      Comment


                        #12
                        Hello fiddlinground,

                        Kate asked me to have a look here.

                        It looks like you have just added the single tick data series in your first attempt.

                        There are a few more items involved that must also be considered:
                        • Plots are synchronized to primary data series
                        • We want to have the indicator calculate off the secondary series and plot on the primary series.
                        • We know that Series are being used in the indicator (Series objects are synchronized to a data series, and this needs to be minded)
                        • There is a custom class that handles some of the indicator calculations. Some usage of Series is done here. Which data series are these Series synchronized to?
                        To be able to bake in a data series and have the indicator calculate off that data series, we need to:
                        • Calculate the indicator logic in BarsInProgress == 1 for the added data series
                        • Make sure all Series that are referenced in BarsInProgress == 1 are synchronized to the secondary data series
                        • Make sure that all Series being used in the custom class are synchronized to the secondary data series of the indicator
                        Then to be able to have those values plotted:
                        • Series values calculated in BIP 1 need to be assigned to plots in BIP0, (Since plots are synchronized to the primary data series, this tells us "What was the indicator value at the time the primary bar closed?)
                        Series<T> documentation goes further into detail on working with Series objects, and provides an example showing how to synchronize a Series to an added data series. Linked below.

                        Comment


                          #13
                          Hi Jim,

                          I added the Series<T> code off the link you sent me. I am still getting the error message and the FibonacciZones disappeared from the indicator list? I attached the updated indicator.

                          thanks,
                          David
                          Attached Files

                          Comment


                            #14
                            Hello fiddlinground,

                            An indicator will not list in the Indicators dialog if it hits a run time error before it lists.

                            You can check the Log tab of the Control Center to check for any errors, and then you can check your logic and add prints to identify where exactly the error is being thrown. The errors will need to be corrected in order for the indicator to list.

                            Debugging Tips - https://ninjatrader.com/support/help...script_cod.htm

                            The direction from post #12 can be used to guide you to what would be needed to have the indicator calculated on a secondary data series. Keep in mind, there are going to be several changes involved that may be difficult if you are not familiar with C# syntax and NinjaScript.

                            If a certain error is unclear, you can copy the error message here, and copy the line of code that throws the error, and we could give some additional insight on how to understand and address the error.

                            Comment


                              #15
                              Hi Jim,

                              The error is listed in the Output box as soon as I access indicators.

                              thanks,
                              David

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              574 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              332 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              101 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              553 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              551 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X