protected override void OnBarUpdate() { if(CurrentBar <= SlowMA+12){return;} else { if(BarsInProgress==0) { //Check for uptrend int uptrendCheckCount = 0; int countOfClosesOverSlowEma = 0; double keyPriceViolationLevelPriorDay = EMA(Closes[1],slowMA)[0]*1.0025; double keyPriceViolationLevelToday = EMA(Closes[1],slowMA)[0] + Math.Abs(EMA(Closes[1],slowMA)[0] - EMA(Closes[1],slowMA)[1]); for(int i=0;i<10;i++) { if((EMA(Closes[1],fastMA)[i] >= EMA(Closes[1],medMA)[i]) && (EMA(Closes[1],medMA)[i] >= EMA(Closes[1],slowMA)[i])) { uptrendCheckCount++; } if(Closes[1][i+1] >= EMA(Closes[1],slowMA)[i+1]) { countOfClosesOverSlowEma++; } } if((countOfClosesOverSlowEma==10)&&(uptrendCheckCount==10)) { if(Lows[1][0] <= keyPriceViolationLevelPriorDay) { if((Close[0] > Open[0])&&(Close[0] > keyPriceViolationLevelToday)) { Plot0.Set(1); } } } } } }
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!
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
NinjaTrader
Indicator bars error "index was out of bounds of the array"
Collapse
X
-
Indicator bars error "index was out of bounds of the array"
Hi, I'm trying to run a custom indicator from market analyzer to generate alerts. I have the common error of the index being out of bounds of the array. I thought i had headed this off with my CurrentBar check at the beginning of on bar update. It's a multi- instrument, if that impacts things. There are three variables, a slow, med and fast value for moving averages. I'm passing 50, 20, 10. So my code as I see it should need 60 bars... I'm looking back 70 in the strategy analyzer. Can anyone point out the obvious? Many thanks in advance, the code is below...
Code:Tags: None
-
Hi Joydeep, thanks.. I changed the line to:
if(CurrentBars[0] < (slowMA + 12) || CurrentBars[1] < (slowMA + 12)){return;}
and I'm still getting the errors when I reload the market analyzer.
Comment
-
Originally posted by CSharpTrader View PostHi, I'm trying to run a custom indicator from market analyzer to generate alerts. I have the common error of the index being out of bounds of the array. I thought i had headed this off with my CurrentBar check at the beginning of on bar update. It's a multi- instrument, if that impacts things. There are three variables, a slow, med and fast value for moving averages. I'm passing 50, 20, 10. So my code as I see it should need 60 bars... I'm looking back 70 in the strategy analyzer. Can anyone point out the obvious? Many thanks in advance, the code is below...
Code:protected override void OnBarUpdate() { if(CurrentBar <= SlowMA+12){return;} else { if(BarsInProgress==0) { //Check for uptrend int uptrendCheckCount = 0; int countOfClosesOverSlowEma = 0; double keyPriceViolationLevelPriorDay = EMA(Closes[1],slowMA)[0]*1.0025; double keyPriceViolationLevelToday = EMA(Closes[1],slowMA)[0] + Math.Abs(EMA(Closes[1],slowMA)[0] - EMA(Closes[1],slowMA)[1]); for(int i=0;i<10;i++) { if((EMA(Closes[1],fastMA)[i] >= EMA(Closes[1],medMA)[i]) && (EMA(Closes[1],medMA)[i] >= EMA(Closes[1],slowMA)[i])) { uptrendCheckCount++; } if(Closes[1][i+1] >= EMA(Closes[1],slowMA)[i+1]) { countOfClosesOverSlowEma++; } } if((countOfClosesOverSlowEma==10)&&(uptrendCheckCount==10)) { if(Lows[1][0] <= keyPriceViolationLevelPriorDay) { if((Close[0] > Open[0])&&(Close[0] > keyPriceViolationLevelToday)) { Plot0.Set(1); } } } } } }
Code:private int _intEscapeBars = 1;
Code:protected override void OnBarUpdate() { for (int index = 0; index < BarsArray.Length; index++) { if (CurrentBars[index] < this._intEscapeBars) return; } // the rest goes here ... }
Comment
-
I appreciate the help. I'll try implementing that. If you could help me understand it though... I know the loop checks any data series/ bars array. But it's checking that CurrentBars < 1... don't I need CurrentBars < barsRequiredForAllLogic? (in my case, 60).
Comment
-
-
Originally posted by CSharpTrader View PostI appreciate the help. I'll try implementing that. If you could help me understand it though... I know the loop checks any data series/ bars array. But it's checking that CurrentBars < 1... don't I need CurrentBars < barsRequiredForAllLogic? (in my case, 60).
Alternatively, you can think of the "1" as just a place holder, and replace that with however many bars that you want to check. However, best practice is to check the validity of the barSeries independently of any escapes that you want to impose on the code proper. That avoids any side effects: your code processing should be independent of if the bars are valid, which should be an independent gate on the code..
Comment
-
Hello CSharpTrader,
If I try the below code then I can get it work fine
Code:if(CurrentBars[0] <= slowMA+12 || CurrentBars[1] < slowMA + 12){return;} else { if(BarsInProgress==0) { //Check for uptrend int uptrendCheckCount = 0; int countOfClosesOverSlowEma = 0; double keyPriceViolationLevelPriorDay = EMA(Closes[1],slowMA)[0]*1.0025; double keyPriceViolationLevelToday = EMA(Closes[1],slowMA)[0] + Math.Abs(EMA(Closes[1],slowMA)[0] - EMA(Closes[1],slowMA)[1]); for(int i=0;i<10;i++) { if((EMA(Closes[1],fastMA)[i] >= EMA(Closes[1],medMA)[i]) && (EMA(Closes[1],medMA)[i] >= EMA(Closes[1],slowMA)[i])) { uptrendCheckCount++; } if(Closes[1][i+1] >= EMA(Closes[1],slowMA)[i+1]) { countOfClosesOverSlowEma++; } } if((countOfClosesOverSlowEma==10)&&(uptrendCheckCount==10)) { if(Lows[1][0] <= keyPriceViolationLevelPriorDay) { if((Close[0] > Open[0])&&(Close[0] > keyPriceViolationLevelToday)) { Plot0.Set(1); } } } } }
JoydeepNinjaTrader Customer Service
Comment
-
So it's not checking for required bars, just the existence. Got it. I'm all for best practices, but for now to get it running, I'm using:
for (int index = 0; index < BarsArray.Length; index++)
{
if (CurrentBars[index] < (slowMA+12)) return;
}
And I'm still getting errors.
Comment
-
Joydeep, thanks... how many bars are you using in market analyzer? And you're getting no out of bounds errors at all?
Comment
-
Sure... I never deleted this default code, still the same original method:
protected override void Initialize()
{
Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "Plot0"));
Overlay = false;
}
Comment
-
Originally posted by CSharpTrader View PostSo it's not checking for required bars, just the existence. Got it. I'm all for best practices, but for now to get it running, I'm using:
for (int index = 0; index < BarsArray.Length; index++)
{
if (CurrentBars[index] < (slowMA+12)) return;
}
And I'm still getting errors.
Comment
-
Hi Koganam, I am... it's logically the same as Joydeeps' earlier suggestion, that just checked them both individually with the ||.
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by bortz, 11-06-2023, 08:04 AM
|
47 responses
1,609 views
0 likes
|
Last Post
by aligator
Today, 07:22 PM
|
||
Started by jaybedreamin, Today, 05:56 PM
|
0 responses
9 views
0 likes
|
Last Post
by jaybedreamin
Today, 05:56 PM
|
||
Started by DJ888, 04-16-2024, 06:09 PM
|
6 responses
19 views
0 likes
|
Last Post
by DJ888
Today, 05:12 PM
|
||
Started by Jon17, Today, 04:33 PM
|
0 responses
6 views
0 likes
|
Last Post
by Jon17
Today, 04:33 PM
|
||
Started by Javierw.ok, Today, 04:12 PM
|
0 responses
16 views
0 likes
|
Last Post
by Javierw.ok
Today, 04:12 PM
|
Comment