A variable bool defaulted to false has been triggered to true. Looking for a code library location to plug in a statement such that when a certain number of bars have transpired since the bool fired to true, it gets reset back to false.
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Reset Bool Variable after a Defined Number of 'Bars Since'
Collapse
X
-
Reset Bool Variable after a Defined Number of 'Bars Since'
Scenario:
A variable bool defaulted to false has been triggered to true. Looking for a code library location to plug in a statement such that when a certain number of bars have transpired since the bool fired to true, it gets reset back to false.Tags: None
- Likes 1
-
Hello johnMoss,
Thank you for your inquiry.
There is no default function in NinjaScript to calculate the number of bars since an event occurred, however, what you can do is save CurrentBar to a variable at the same time your bool condition is set to true. You can then add a condition to check if (CurrentBar - TriggerBar) is equal to your desired number of 'bars since'.
For example,
Please let us know if you have any further questions.Code:private bool myBool = false; private int barsSince = 5; private int TriggerBar; //in OnBarUpdate if (High[0] > High[1]) { myBool = true; TriggerBar = CurrentBar; } if(CurrentBar - TriggerBar == barsSince) { myBool = false; }
- Likes 1
-
I am trying to do the same thing but was not successful. I tried it two ways. One with Bars.BarsSinceNewTradingDay and the other with Current Bar. To be specific, I am looking for "myBool" to be true for as many bars as "barsSince" starting when the "if (High[0] > High[1])" condition is true.
I have replaced the recommendations with the below.
private bool myBool = false; with bool posDivFlag = false;
private int barsSince = 5; with int lookBackBars = 5;
private int TriggerBar; with private int posDivTriggerBar;
if (High[0] > High[1]) with if (posDiv)
Below are the relevant snippets of what I have. Where did I go wrong?
namespace NinjaTrader.NinjaScript.Indicators
{
public class Divergence : Indicator
{
private bool posDivFlag;
private bool negDivFlag;
private int LookBackBars;
private int posDivTriggerBar;
private int negDivTriggerBar;
private int posDivbarsSince;
private int negDivbarsSince;
}
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
int lookBackBars = 7;
bool posDivFlag = false;
bool negDivFlag = false;
}
protected override void OnBarUpdate()
if (CurrentBar > 0)
{
//Buy and Sell Conditions
bool posDiv = condition 1;
bool negDiv = condition 2;
if (posDiv)
{
posDivFlag = true;
posDivTriggerBar = CurrentBar;
}
if ((posDivTriggerBar - Bars.BarsSinceNewTradingDay) > LookBackBars)
{
posDivFlag = false;
}
if (negDiv)
{
negDivFlag = true;
negDivTriggerBar = CurrentBar;
}
if ((CurrentBar - negDivTriggerBar) > LookBackBars)
{
negDivFlag = false;
}
bool ABuy = Condition 3 && Condition 4 && posDivFlag == true;
bool ASell = Condition 3 && Condition 5 && negDivFlag == true;
Thank you both!
Comment
-
Hello TheTechnician86,
If condition 1 is true a second time before the LookBack number of bars have elapsed after, do you want to start the counting over?
The bools should not be re-declared locally in OnBarUpdate() or OnStateChange(). Remove the type before the assignments.
bool posDivFlag = false;
should be
posDivFlag = false;
In the scope of the class:
In OnBarUpdate():Code:private bool posDiv; private bool posDivFlag; private int posDivTriggerBar;
Code:if (CurrentBar < 1) return; posDiv = High[0] > High[1]; if (posDiv) { posDivFlag = true; posDivTriggerBar = CurrentBar; } if (CurrentBar - posDivTriggerBar > LookBackBars) { posDivFlag = false; }
Add debugging prints to fully understand how the behavior is working.
Drawing dots from the conditions can also be visually helpful to see where the conditions are evaluating as true.
Attached is a test script.
CountBarsSinceEventTest_NT8.zipChelsea B.NinjaTrader Customer Service
Comment
-
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
605 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
351 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
560 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
561 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|

Comment