Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Need to run condition from X bars ago to current bar
Collapse
X
-
Need to run condition from X bars ago to current bar
Hi, I need help in how to code the logic to loop thru and check a condition from 'X' bars ago (this can be variable) to the most recent bar. That is based on the OHLC value of an 'X' bar ago, I need to check the condition on every future bar until the last current bar. (as per image below). Any help would be appreciated. Thanks.
Last edited by Kuvala; 04-03-2023, 11:12 PM.Tags: None
-
Hello Kuvala,
There is a NinjaScript method which can be used for checking a condition over a period of bars, that is called CountIF: https://ninjatrader.com/support/help...ghtsub=countif
You can also use a C# for loop for that purpose.
-
Thank you. But how to identify the bar number from where I start checking my condition and I need to check the condition in each future bar until the current bar..need help in identifying the start bar number and current bar number so that I can write a loop logic. When I print current bar number at each candle I get incremental value from left most bar in chart..so not sure how to come up with the loop logic. Please refer to the diagram I attached of what I am looking for.
Comment
-
Hello Kuvala,
If you want to use a loop I would suggest looking at external C# tutorials on how to use loops in C#. NinjaScript is C# language.
To find the bar number in a loop would require that you subtract the iterator in the loop from the CurrentBar. That would give you a BarsAgo. For example if you started a loop with its iterator as CurrentBar and subtract 1 for each loop iteration you would get a number less than CurrentBar for each loop. When you subtract that number from the current bar it would give you a BarsAgo which you can use with any of the price or time series.
.
Comment
-
Hello Kuvala,
I am not aware of any samples using a for loop in NinjaScript. for loops are a general C# concept which you can learn about in external C# learning resources. I would suggest to search online for "C# for loop example" to get some samples of using loops.
To get a BarsAgo from the CurrentBar you would use math and subtract the iterator for currentbar:
When working with loops there is a good chance you will hit an indexing error if the loop is not properly formed, I would also suggest using Prints with the loop to observe how your code is working as part of the debugging process. https://ninjatrader.com/support/help...lightsub=printCode:int barsAgo = CurrentBar - i;
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
612 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
355 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
105 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
561 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
564 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|

Comment