Go Back   NinjaTrader Support Forum > NinjaTrader 8 > Indicator Development

Indicator Development Support for the development of custom indicators using NinjaScript.

NinjaTrader
Reply
 
Thread Tools Display Modes
Old 09-12-2017, 12:04 AM   #1
GeorgeW
Senior Member
 
Join Date: Nov 2014
Posts: 272
Thanks: 80
Thanked 25 times in 22 posts
Default 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.
GeorgeW is offline  
Reply With Quote
Old 09-12-2017, 07:22 AM   #2
NinjaTrader_Jesse
NinjaTrader Customer Service
 
NinjaTrader_Jesse's Avatar
 
Join Date: Mar 2014
Location: Denver, CO
Posts: 4,068
Thanks: 28
Thanked 1,053 times in 985 posts
Default

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.
NinjaTrader_Jesse is offline  
Reply With Quote
Old 09-12-2017, 08:27 AM   #3
koganam
Vendor
 
Join Date: Feb 2008
Posts: 7,142
Thanks: 56
Thanked 2,946 times in 2,389 posts
Default

Quote:
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.
koganam
NinjaTrader Ecosystem Vendor - OmegaSigma Indicators
koganam is offline  
Reply With Quote
Old 09-16-2017, 05:03 AM   #4
GeorgeW
Senior Member
 
Join Date: Nov 2014
Posts: 272
Thanks: 80
Thanked 25 times in 22 posts
Default

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!
GeorgeW is offline  
Reply With Quote
Old 09-19-2017, 10:27 AM   #5
GeorgeW
Senior Member
 
Join Date: Nov 2014
Posts: 272
Thanks: 80
Thanked 25 times in 22 posts
Default

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
File Type: pdf Zigzag and Sideways.pdf (479.9 KB, 3 views)
GeorgeW is offline  
Reply With Quote
Old 09-20-2017, 07:49 AM   #6
NinjaTrader_Paul
NinjaTrader Customer Service
 
NinjaTrader_Paul's Avatar
 
Join Date: May 2014
Posts: 3,214
Thanks: 370
Thanked 757 times in 732 posts
Default

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.
NinjaTrader_Paul is offline  
Reply With Quote
The following user says thank you to NinjaTrader_Paul for this post:
Old 10-01-2017, 01:18 PM   #7
ballboy11
Senior Member
 
Join Date: Jan 2016
Posts: 218
Thanks: 0
Thanked 6 times in 6 posts
Default

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

nSwingIndex =5;


If (nSwingIndex > CurrentBar)
{

do stuff

}
ballboy11 is offline  
Reply With Quote
The following user says thank you to ballboy11 for this post:
Reply

Tags
mro, onbarupdate error, swing

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Error on calling OnBarUpdate method on bar 0 Metal15 Strategy Development 2 05-13-2017 09:11 AM
Error on calling 'OnBarUpdate' method for indicator on bar... nitroblower Indicator Development 1 08-24-2016 07:25 AM
Error on calling 'OnBarUpdate' method on bar 0 Aqua-Life Strategy Development 21 07-29-2016 08:14 AM
Indicator 'MyDrawRegionTest': Error on calling 'OnBarUpdate' method on bar 0: The cal sledge Version 8 Beta 20 05-18-2015 02:35 PM
Error on calling 'OnBarUpdate' method for indicator ' ' on bar 0: kaywai Indicator Development 5 10-31-2011 09:48 AM


All times are GMT -6. The time now is 03:19 AM.

Futures, foreign currency and options trading contains substantial risk and is not for every investor. An investor could potentially lose all or more than the initial investment. Risk capital is money that can be lost without jeopardizing ones financial security or lifestyle. Only risk capital should be used for trading and only those with sufficient risk capital should consider trading. Past performance is not necessarily indicative of future results. View Full Risk Disclosure.

CFTC Rules 4.41 - Hypothetical or Simulated performance results have certain limitations, unlike an actual performance record, simulated results do not represent actual trading. Also, since the trades have not been executed, the results may have under-or-over compensated for the impact, if any, of certain market factors, such as lack of liquidity. Simulated trading programs in general are also subject to the fact that they are designed with the benefit of hindsight. No representation is being made that any account will or is likely to achieve profit or losses similar to those shown.

This website is hosted and operated by NinjaTrader, LLC (“NT”), a software development company which owns and supports all proprietary technology relating to and including the NinjaTrader trading platform. NT is an affiliated company to NinjaTrader Brokerage (“NTB”), which is a NFA registered introducing broker (NFA #0339976) providing brokerage services to traders of futures and foreign exchange products. This website is intended for educational and informational purposes only and should not be viewed as a solicitation or recommendation of any product, service or trading strategy. No offer or solicitation to buy or sell securities, securities derivative or futures products of any kind, or any type of trading or investment advice, recommendation or strategy, is made, given, or in any manner endorsed by any NT affiliate and the information made available on this Web site is not an offer or solicitation of any kind. Specific questions related to a brokerage account should be sent to your broker directly. The content and opinions expressed on this website are those of the authors and do not necessarily reflect the official policy or position of NT or any of its affiliates. 

Vendors along with their websites, products and services, collectively referred to as (“Vendor Content”), are independent persons or companies that are in no manner affiliated with NT or any if its affiliates. NT or any of its affiliates are not responsible for, do not approve, recommend or endorse any Vendor Content referenced on this website and it’s your sole responsibility to evaluate Vendor Content. Please be aware that any performance information provided by a vendor should be considered hypothetical and must contain the disclosures required by NFA Rule 2-29(c). If you are interested in learning more about, or investigating the quality of, any such Vendor Content you must contact the vendor, provider or seller of such Vendor Content. No person employed by, or associated with, NT or any of its affiliates is authorized to provide any information about any such Vendor Content.