protected override void OnBarUpdate()
{
if (CurrentBar < 45)
return;
bool bNBarsUpEvent = false;
bool bNBarsDownEvent = false;
for (int i = 0; i <= 7; i++)
{
double valueUp = NBarsUp(nBarsPeriod, true, true, true)[i];
double valueDown = NBarsDown(nBarsPeriod, true, true, true)[i];
if (valueUp == 1)
{
bNBarsUpEvent = true;
BackColor = Color.Green;
}
if (valueDown == 1)
{
bNBarsDownEvent = true;
BackColor = Color.Red;
}
}
}
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Why error in log with NBarsUpEvent in for loop
Collapse
X
-
Why error in log with NBarsUpEvent in for loop
Hi, I am trying to check if there was an NBarsUp or -Down event during the last 7 bars. I tried this, but it doesn't work and I get an error message in the log: "Error on calling 'OnBarUpdate' method [...] You are accessing an index with a value that is invalid since its out of range.":
I don't understand why it's not working.Code:Tags: None
-
Hello Lonneman,
I would first try using our built in method CountIf() for this. Example usage below- checks if there has been any occurrence of NBarsup 3 within the last 7 bars and marks with a diamond.
if (CountIf(delegate {return NBarsUp(3, true, true, true)[0] == 1;}, 7) > 0)
{
DrawDiamond("" + CurrentBar, true, 0, High[0], Color.Red);
}Ryan M.NinjaTrader Customer Service
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by cmoran13, 04-16-2026, 01:02 PM
|
0 responses
43 views
0 likes
|
Last Post
by cmoran13
04-16-2026, 01:02 PM
|
||
|
Started by PaulMohn, 04-10-2026, 11:11 AM
|
0 responses
25 views
0 likes
|
Last Post
by PaulMohn
04-10-2026, 11:11 AM
|
||
|
Started by CarlTrading, 03-31-2026, 09:41 PM
|
1 response
162 views
1 like
|
Last Post
|
||
|
Started by CarlTrading, 04-01-2026, 02:41 AM
|
0 responses
98 views
1 like
|
Last Post
by CarlTrading
04-01-2026, 02:41 AM
|
||
|
Started by CaptainJack, 03-31-2026, 11:44 PM
|
0 responses
158 views
2 likes
|
Last Post
by CaptainJack
03-31-2026, 11:44 PM
|

Comment