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
No announcement yet.
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
-
You need to check all your BarSeries for validity.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;
When written this way, you do not have to even think about how many Bars objects you add to the indicator: the escapes will be automatically handled, dynamically, for the correct number of Bars objects.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
-
-
Sort of. The code as written merely validates that the barSeries exist. If you need a further filter, you can always use another filter to your code proper: the part that goes "// the rest goes here ... ".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
-
Are you still getting the "index out of bounds" error?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 Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
574 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
332 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
101 views
0 likes
|
Last Post
by Mindset
02-09-2026, 11:44 AM
|
||
|
Started by Geovanny Suaza, 02-02-2026, 12:30 PM
|
0 responses
553 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
551 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|

Comment