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

Error on calling 'OnBarUpdate' method on bar 0.

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

    Error on calling 'OnBarUpdate' method on bar 0.

    I need help with custom indicator. No matter what I do, i get this error:

    "Indicator 'Custom201010': Error on calling 'OnBarUpdate' method on bar 0: Index was outside the bounds of the array."


    Here's a code part from OnBarUpdate:
    Code:
    protected override void OnBarUpdate()
    {
    double h0 = High[0];
    double l0 = Low[0];
    double c0 = Close[0];
    double o0 = Open[0];
    
    if(CurrentBar==0)
    {
    
    Value[0] = 0;
    }
    else
    {
    double h1 = High[1];
    double l1 = Low[1];
    
    
    Value[0] = (h0 <= h1 && l0 >= l1 ? 0 : h0 > h1 && l0 >= l1 ? 1 : h0 <= h1 && l0 < l1 ? 2 : h0 > h1 && l0 < l1 ? 3 : 4);
    
    }
    }

    #2
    Hello UltraNIX,
    Try :-
    if(CurrentBar <1)
    return;
    // your code...

    Also try to reconcile your calculation for Value[0], I think its too complex using if statement would be better. If you get the results then perfect.
    Hope it helps!

    Comment


      #3
      Now error moves to Bar 1. I suppose it's my calculation, but I'm not sure what's wrong with it.

      Comment


        #4
        Try to use if statements as I believe the syntax you're using is having limitations. Generally its if ? true : false. I think you're adding other conditions so it might be throwing the errors or just unable to compile. You can try with if statements & if error is resolved, can test with the old syntax if it works.

        Comment


          #5
          I just plotted your code, its working without any change.
          Click image for larger version

Name:	test plot.jpg
Views:	769
Size:	35.0 KB
ID:	1122071

          Comment


            #6
            Also tried using multiple if statements for my Value[0] formula, still same error.

            Comment


              #7
              For me it's compiling well, but it gives error when I try to load the indicator on chart.

              Comment


                #8
                Do you see any errors in control center log tab ?

                Comment


                  #9
                  Yes, same error.

                  Comment


                    #10
                    Custom201010.zip
                    Try this, I am not getting any errors.

                    Comment


                      #11
                      Yes, but my idea is to have it for Overlay and have those values appearing not at the additional panel at the bottom, but below each candle.

                      It seems that you have this:

                      [Browsable(false)]
                      [XmlIgnore]
                      public Series<double> Test
                      {
                      get { return Values[0]; }
                      }

                      But I don't have Test double, so it doesn't work for me I suppose.

                      Comment


                        #12
                        What's your idea to plot it, the Value is calculated as 0, 1, 2, 3 & 4. How do you want to see it below bar ?
                        Lets suppose it can be Tick size so you can have a plot Low[0] - Value[0].
                        If you don't need a double series to be plotted then you can have chart markers as well. But there should be something to be plotted / drawn on chart.

                        Comment


                          #13
                          Something like this:


                          But instead of 5 digit number with separators, like ,4-6-6, it would show 0, 1, 2 or 3 below each candle

                          Comment


                            #14
                            Hello UltraNIX,

                            I am including a link to the CondensedCandleSticks indicator so you may view the code of this.
                            As seen in the Candlesticks, Condensed Traders Analysis of Stocks & Commodities February 2015 article by David Cline Critical *Note that on any files that say "File already exists on your PC" that start with an "@" symbol are the ones that came preloaded inside of NinjaTrader so you would say "No" so that you [&#8230;]


                            Interestingly, you are wanting a plot which would not show numbers as text on the chart like CondensedCandleSticks but would instead show a line or bar representation of that number.
                            Are you wanting text, or are you wanting a line plot or histogram plot?

                            Have you added a plot with AddPlot() to generate the Values[0] plot and series?
                            Is Values[0] being set in OnBarUpdate()?


                            s.kinra is correct, the usage of:
                            double h1 = High[1];
                            double l1 = Low[1];
                            Requires at least one bar. This means a check for CurrentBars to be greater than one should be at the top of OnBarUpdate().

                            if (CurrentBars < 1)
                            return;


                            Are there any other indexes that may be greater than 0?

                            The process to correct the script is to identify the index that is invalid first, check the count of the collection (or CurrentBar if a series) to ensure the index is valid.

                            Use prints to first identify what the invalid index is.

                            Add a print above each line (except for below if conditions) to trace which line is the last line of code executed before the error occurs. With this we can identify the line of code causing the error. From there we can identify which object in that line of code is using an invalid index.

                            Below is a link to a forum post that demonstrates using prints to understand behavior.


                            Please include the output from the prints with your reply.
                            Chelsea B.NinjaTrader Customer Service

                            Comment


                              #15
                              Custom201010.zip
                              Hello Chelsea,
                              Thanks for sharing the code & there is a version available for NT8, I am yet to check the code of Condensed Candle.
                              Hello UltraNIX,
                              You can check this, it plots the value you wanted below each candle, please confirm if this is what you wanted or still something is missing.
                              Last edited by s.kinra; 10-11-2020, 11:29 PM.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by alexstox, 10-16-2018, 03:29 PM
                              12 responses
                              353 views
                              0 likes
                              Last Post Tburg1073  
                              Started by truepenny, Today, 03:59 AM
                              0 responses
                              3 views
                              0 likes
                              Last Post truepenny  
                              Started by truepenny, Today, 03:45 AM
                              0 responses
                              8 views
                              0 likes
                              Last Post truepenny  
                              Started by MGHORBEL, Today, 02:28 AM
                              0 responses
                              8 views
                              0 likes
                              Last Post MGHORBEL  
                              Started by dcriador, Today, 01:43 AM
                              1 response
                              9 views
                              0 likes
                              Last Post dcriador  
                              Working...
                              X