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 21

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

    Error on calling 'OnBarUpdate' method on bar 21

    Hello,

    I am trying to use the Swing method or the Most Recent Occurrence (MRO) method to give a signal when one of my plots achieves a higher high, but I am getting the following error:

    "Error on calling 'OnBarUpdate' method on bar 21: 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 have read the threads about this, and my indicator is not multiseries, and I have used
    Code:
    if (CurrentBars[0] <= BarsRequiredToPlot)
    			{ return;}
    at the start of the OnBarUpdate section. Below is a sample of the two different methods I have tried. Any advice would be much appreciated.

    Code:
    //Method A
    			int barsAgoSHH1 = Swing(Values[0], 1).SwingHighBar(0, 3, 14);
    			int barsAgoSHH0 = Swing(Values[0], 1).SwingHighBar(0, 4, 14);
    			if(Values[0][barsAgoSHH1] > Values[0][barsAgoSHH0])
    			{
    			Draw.RegionHighlightX(this, "tagbullrise" + CurrentBar, 6, 0, Brushes.Transparent, Brushes.Green, 25); 			
    			}
    			
    			//Method B
    			int barsAgoHH1 = MRO(() => Values[0][0] > Values[0][1], 3, 12);
    			int barsAgoHH0 = MRO(() => Values[0][0] > Values[0][1], 4, 12);
    			if(Values[0][barsAgoHH1] > Values[0][barsAgoHH0])
    			{
    			Draw.RegionHighlightX(this, "tagbullrise" + CurrentBar, 6, 0, Brushes.Transparent, Brushes.Green, 25); 	
    			}
    Thank you.

    #2
    Hello,

    Thank you for the post.

    I wanted to check, is the BarsRequiredToPlot set to a value that would be greater than the BarsAgo you are using? Usually, BarsRequiredToPlot is only good if you have a variable amount of bars to load needed, then you can control it from the UI. Because the error mentions bar 21, I would presume that you have not set a value and it is using the default of 20 bars. Based on the error, you are using more than 20 bars ago.

    I would suggest to instead look through your logic for the maximum BarsAgo you are using, and then hard code that value:

    Code:
    if (CurrentBars[0] <= 55)  return;
    Or, once you know the number of bars needed, you could set that from SetDefaults

    Code:
    BarsRequiredToPlot = 55;

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Originally posted by GeorgeW View Post
      Hello,

      I am trying to use the Swing method or the Most Recent Occurrence (MRO) method to give a signal when one of my plots achieves a higher high, but I am getting the following error:

      "Error on calling 'OnBarUpdate' method on bar 21: 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 have read the threads about this, and my indicator is not multiseries, and I have used
      Code:
      if (CurrentBars[0] <= BarsRequiredToPlot)
                  { return;}
      at the start of the OnBarUpdate section. Below is a sample of the two different methods I have tried. Any advice would be much appreciated.

      Code:
      //Method A
                  int barsAgoSHH1 = Swing(Values[0], 1).SwingHighBar(0, 3, 14);
                  int barsAgoSHH0 = Swing(Values[0], 1).SwingHighBar(0, 4, 14);
                  if(Values[0][barsAgoSHH1] > Values[0][barsAgoSHH0])
                  {
                  Draw.RegionHighlightX(this, "tagbullrise" + CurrentBar, 6, 0, Brushes.Transparent, Brushes.Green, 25);             
                  }
                  
                  //Method B
                  int barsAgoHH1 = MRO(() => Values[0][0] > Values[0][1], 3, 12);
                  int barsAgoHH0 = MRO(() => Values[0][0] > Values[0][1], 4, 12);
                  if(Values[0][barsAgoHH1] > Values[0][barsAgoHH0])
                  {
                  Draw.RegionHighlightX(this, "tagbullrise" + CurrentBar, 6, 0, Brushes.Transparent, Brushes.Green, 25);     
                  }
      Thank you.
      Your code is asking for the 3rd and 4th instances of SwingHighBar. It is very unlikely that that occurs within 12 bars that you have specified. The literature clearly gives an example, which shows that you have to test for the return condition of -1, as a null hypothesis validity check, before using the value of MRO(). Make the check. Then write your condition so that it turns valid at some point, if you really want to use the MRO value.

      Comment


        #4
        Thanks Jesse & Koganam.

        I have done the check for barsAgo -1 as suggested by Koganam, and that has shown that I am getting -1 in some places, hence the reason the indicator is not working. Your hint that MRO may not be what I want to use led me to do some more research and I have realised that I should be using the ZigZag method if I am searching for higher highs where there is a peak. Cheers!

        Comment


          #5
          Hello again,

          Using the Zigzag method is giving me the signals I want where all the moves are from high to low to high etc. But if after a high there is a sideways move of the plot before the next move up, where the sideways move occurred is not recognised as both a high point and a low point by the Zigzag method. Is there a way for me to make some adjustments to the code to make it do so? I have attached an image to illustrate what I mean.

          On a lesser point, the plot I am trying to identify the swings on has values from 0 to 100. The Zigzag method does not recognise 0 as being a swing point and so I have had to include the following line in the code so that swings up from 0 are picked up:
          Code:
          if (Values[0][0] < 1) Values[0][0] = 1;
          Is this the way to do it, especially as other parts of the code may still want to recognise when 0 is touched?

          Thank you.
          Attached Files

          Comment


            #6
            Hello GeorgeW,

            Thanks for your reply.

            For the zig-zag, you will want to try variations of the deviation value to find the best fit for your needs.

            If you wish to modify a copy of the zig-zag, try changing line 304 and set the value to be less than zero, such as -1. In a quick test here that allows the zig-zag to touch the zero line when using the MACD as the input to the zigzag.
            Paul H.NinjaTrader Customer Service

            Comment


              #7
              Just make sure your index of your swing greater than the current bar ie

              nSwingIndex =5;


              If (nSwingIndex > CurrentBar)
              {

              do stuff

              }

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by TraderBCL, Today, 04:38 AM
              2 responses
              12 views
              0 likes
              Last Post TraderBCL  
              Started by martin70, 03-24-2023, 04:58 AM
              14 responses
              105 views
              0 likes
              Last Post martin70  
              Started by Radano, 06-10-2021, 01:40 AM
              19 responses
              607 views
              0 likes
              Last Post Radano
              by Radano
               
              Started by KenneGaray, Today, 03:48 AM
              0 responses
              4 views
              0 likes
              Last Post KenneGaray  
              Started by thanajo, 05-04-2021, 02:11 AM
              4 responses
              471 views
              0 likes
              Last Post tradingnasdaqprueba  
              Working...
              X