Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Reset Bool Variable after a Defined Number of 'Bars Since'

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    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.

    #2
    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,

    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;
    }
    Please let us know if you have any further questions.

    Comment


      #3
      Awesome, thank you Gaby

      Comment


        #4
        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


          #5
          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:

          Code:
          private bool posDiv;
          private bool posDivFlag;
          private int posDivTriggerBar;
          In OnBarUpdate():

          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.zip
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            That helped me rearrange everything properly. It works! Thank you!

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            599 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            345 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            103 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            558 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            558 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X