Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

"if" statement running twice with old variables

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

    "if" statement running twice with old variables

    I am writing an indicator that sounds an alert when the 5th bar stamps. That part works, but it also sounds the alert after the 6th bar stamps. I've figured out why, but I can't explain it and I can't fix it.

    The problem is that, for some reason, the '"if" statement that triggers the alert is being run a second time, and is apparently getting triggered by variables that have been set to new values by the same "if" statement when it ran the first time.

    Here is the output:

    Code:
    New Day 12/6/2024 6:31:00 AM startBarsAgo = 1, LoopNum = 1
    ELSE Loop - 12/6/2024 6:32:00 AM startBarsAgo = 2, LoopNum = 1
    ELSE Loop - 12/6/2024 6:33:00 AM startBarsAgo = 3, LoopNum = 1
    ELSE Loop - 12/6/2024 6:34:00 AM startBarsAgo = 4, LoopNum = 1
    ELSE Loop - 12/6/2024 6:35:00 AM startBarsAgo = 5, LoopNum = 1
    No Beep 1 12/6/2024 6:35:00 AM startBarsAgo = 5, LoopNum = 1
    No Beep 2 12/6/2024 6:35:00 AM startBarsAgo = 5, LoopNum = 2
    ELSE Loop - 12/6/2024 6:36:00 AM startBarsAgo = 6, LoopNum = 2
    FIRST BEEP 1 12/6/2024 6:36:00 AM startBarsAgo = 6, LoopNum = 2
    FIRST BEEP 2 12/6/2024 6:36:00 AM startBarsAgo = 7, LoopNum = 3
    ELSE Loop - 12/6/2024 6:36:00 AM startBarsAgo = 6, LoopNum = 2
    FIRST BEEP 1 12/6/2024 6:36:00 AM startBarsAgo = 6, LoopNum = 2
    FIRST BEEP 2 12/6/2024 6:36:00 AM startBarsAgo = 7, LoopNum = 3
    ELSE Loop - 12/6/2024 6:37:00 AM startBarsAgo = 8, LoopNum = 3​
    As you can see, the FIRST BEEP loop gets executed twice, even though it changes the values for startBarsAgo and LoopNum. The ELSE loop also seems to see the old values for the variables.

    The relevant code is:

    Code:
    if (IsFirstTickOfBar)
    {
                if (Time[0].Day != Time[1].Day)
                {
                    startBarsAgo   = 1;
                    LoopNum        = 1;
                    Print("New Day " + Time[0].ToString() + " startBarsAgo = " + startBarsAgo.ToString() + ", LoopNum = " + LoopNum.ToString());
                }
                else
                {
                    startBarsAgo++;
                    Print("ELSE Loop - " + Time[0].ToString() + " startBarsAgo = " + startBarsAgo.ToString() + ", LoopNum = " + LoopNum.ToString());
                }
    }
    
    
    if (startBarsAgo == 5 && LoopNum == 1)
    {
                Print("No Beep 1 " + Time[0].ToString() + " startBarsAgo = " + startBarsAgo.ToString() + ", LoopNum = " + LoopNum.ToString());
                High5           = MAX(High, 5)[0];
                Low5            = MIN(Low, 5)[0];
                LoopNum++;
                Print("No Beep 2 " + Time[0].ToString() + " startBarsAgo = " + startBarsAgo.ToString() + ", LoopNum = " + LoopNum.ToString());
    }
                    
    if (startBarsAgo == 6 && LoopNum == 2)
    {
                Print("FIRST BEEP 1 " + Time[0].ToString() + " startBarsAgo = " + startBarsAgo.ToString() + ", LoopNum = " + LoopNum.ToString());
                Alert("5-min.", Priority.High, "5 Minutes", NinjaTrader.Core.Globals.InstallDir+@"\sounds\Alert2.wav", 10, Brushes.Black, Brushes.Yellow);
                startBarsAgo++;
                LoopNum++;
                Print("FIRST BEEP 2 " + Time[0].ToString() + " startBarsAgo = " + startBarsAgo.ToString() + ", LoopNum = " + LoopNum.ToString());            
    }​
    Since, FIRST BEEP sets startBarsAgo to 7 and LoopNum to 3, there should be no possible way for it to be run again on the same day.

    How is it possible for the value of a single variable to be stored in two different places? Where is the second location, and how do I set it to the same new value that the variable now contains?

    And why do I need the IsFirstTickOfBar if I am set to OnBarClose?

    Likewise, if I am set to OnBarClose, why is the ELSE loop running twice on the same bar?

    #2
    Hello NjTMatthew,

    From the given sample it looks like you are using class level variables which hold a value over multiple bars so if the if condition remains true it will continue to alert you. You may need to reset the variables after your alert to prevent that.

    In your print the startBarsAgo is 6 and the LoopNum is 2 for both prints so that should be true, you would have to review how you are setting those variables.

    Comment


      #3
      Originally posted by NinjaTrader_Jesse View Post
      Hello NjTMatthew,

      From the given sample it looks like you are using class level variables which hold a value over multiple bars so if the if condition remains true it will continue to alert you. You may need to reset the variables after your alert to prevent that.

      In your print the startBarsAgo is 6 and the LoopNum is 2 for both prints so that should be true, you would have to review how you are setting those variables.
      You can see in the code for the FIRST BEEP "if" statement, which is the one triggered by startBarsAgo being 6 and the LoopNum being 2, that the value for both of those variables is changed. You can also see in the print that the values have changed to 7 and 3. There is nothing in the code that would decrement those variables back to 6 and 2. They both get set to 1 when a new day starts, but nothing would change them from 7 back to 6 or 3 back to 2.

      Comment


        #4
        Hello NjTMatthew,

        The second beep print is not relevant, that happens after you set a variable but inside the same condition. To know why the condition was true multiple times you need to add prints where you set those variables to see why they are being reset. The condition was true multiple times meaning your logic allowed for that to happen. You likely need to remove the prints you have and add one outside of the condition so you can see what the value is for every update and not just when the condition is true.

        Comment


          #5
          Originally posted by NinjaTrader_Jesse View Post
          Hello NjTMatthew,

          To know why the condition was true multiple times you need to add prints where you set those variables to see why they are being reset. ..... You likely need to remove the prints you have and add one outside of the condition so you can see what the value is for every update and not just when the condition is true.
          Okay, I did that. I added a print before both of the "if" statements and one after them. Here are the results:

          Code:
          Outside Loop A: , startBarsAgo = 7, LoopNum = 3
          Outside Loop B: , startBarsAgo = 7, LoopNum = 3
          ELSE Loop - 12/6/2024 6:37:00 AM startBarsAgo = 8, LoopNum = 3
          Outside Loop A: , startBarsAgo = 8, LoopNum = 3
          Outside Loop B: , startBarsAgo = 8, LoopNum = 3
          ELSE Loop - 12/6/2024 6:36:00 AM startBarsAgo = 6, LoopNum = 2
          Outside Loop A: , startBarsAgo = 6, LoopNum = 2
          FIRST BEEP 1 12/6/2024 6:36:00 AM startBarsAgo = 6, firstBeep = True, LoopNum = 2
          FIRST BEEP 2 12/6/2024 6:36:00 AM startBarsAgo = 7, firstBeep = False, LoopNum = 3
          Outside Loop B: , startBarsAgo = 7, LoopNum = 3
          Outside Loop A: , startBarsAgo = 8, LoopNum = 3
          Outside Loop B: , startBarsAgo = 8, LoopNum = 3​
          Now, you can clearly see it is reverting back to the previous minute's bar and data. Where is it getting that from? The variables have already been changed.

          Comment


            #6
            Hello NjTMatthew,

            This is basically the same as what you were doing, I would suggest removing the other print after the if statements. You only need to know what the value was at the beginning of the if statement to know why it was true. You also need to add print statements anywhere else you use that variable to see why its being reset. C# has no ability to reset or revert to an older value for variables meaning somewhere in your logic you are resetting it back to the original value. What you provided is an incomplete example so I can only assume your logic somewhere else in the script is resetting it back.

            Comment


              #7
              With all due respect, you are absolutely wrong. There is no code that refers to those variables anywhere outside of the code that I posted in my original post. There is no time when those values are being reset to previously held values, except when a new day starts and they are set to one. They are set to 1 or they are incremented up. They are NEVER decremented.

              You can see in the code for my print that it prints Time[0], but it is returning the time from a bar in the past. There is something wrong and it has nothing to do with my code or the way that the variables are being assigned.

              Comment


                #8
                Are you using any calls to AddDataSeries in your indicator?

                Comment


                  #9
                  Hello NjTMatthew,

                  Again all I can suggest is adding additional debugging, the context of what you are doing matters so providing an incomplete sample is not enough to know why that may be happening. In C# variables don't just reset to previous values so something you have going on in that code is causing that result.

                  Comment


                    #10
                    Okay, here is the entire thing. Literally, all of it.

                    Code:
                    public class 5minBnR : Indicator
                             {
                                   protected override void OnStateChange()
                                   {
                                         if (State == State.SetDefaults)
                                         {
                                                         Description              = @"5-minute Breakout Indicator";
                                                         Name                     = "5minBnR";
                                                         Calculate                = Calculate.OnBarClose;
                                                         IsOverlay                = true;
                                                         DisplayInDataBox         = true;
                                                         DrawOnPricePanel         = true;
                                                         DrawHorizontalGridLines  = true;
                                                         DrawVerticalGridLines    = true;
                                                         PaintPriceMarkers        = true;
                                                         ScaleJustification       = NinjaTrader.Gui.Chart.ScaleJustification.Right;
                                                        //Disable this property if your indicator requires custom values that cumulate with each new market data event.
                                                        //See Help Guide for additional information.
                                                         IsSuspendedWhileInactive = true;
                                                         AddPlot(new Stroke(Brushes.SeaGreen, DashStyleHelper.Dot, 2), PlotStyle.HLine, "Line5High");
                                                         AddPlot(new Stroke(Brushes.Firebrick, DashStyleHelper.Dot, 2), PlotStyle.HLine, "Line5Low");
                                                         AddPlot(new Stroke(Brushes.SeaGreen, DashStyleHelper.Dash, 2), PlotStyle.HLine, "HighLine");
                                                         AddPlot(new Stroke(Brushes.Firebrick, DashStyleHelper.Dash, 2), PlotStyle.HLine, "LowLine");
                                                         startBarsAgo             = 1;
                                                         High5                    = 5000;
                                                         Low5                     = 5000;
                                                         DayHigh                  = 5000;
                                                         DayLow                   = 5000;
                                                         LoopNum                  = 1;
                                         }
                                         else if (State == State.Configure)
                                         {
                                         }
                                  }
                    
                                  protected override void OnBarUpdate()
                                 {
                                 //Add your custom indicator logic here.
                    
                                      if (CurrentBar < 1)
                                     { return; }
                    
                                     if (IsFirstTickOfBar)
                                     {
                                          if (Time[0].Day != Time[1].Day)
                                          {
                                              startBarsAgo = 1;
                                              LoopNum      = 1;
                                              Print("New Day " + Time[0].ToString() + " startBarsAgo = " + startBarsAgo.ToString() + ", LoopNum = " + LoopNum.ToString());
                                          }
                                         else
                                         {
                                              startBarsAgo++;
                                              Print("ELSE Loop - " + Time[0].ToString() + " startBarsAgo = " + startBarsAgo.ToString() + ", LoopNum = " + LoopNum.ToString());
                                         }
                                     }
                    
                                     if (startBarsAgo == 391)
                                     {
                                         DayHigh  = MAX(High, startBarsAgo)[0];
                                         DayLow   = MIN(Low, startBarsAgo)[0];
                                     }
                    
                                     if (startBarsAgo == 392)
                                     {
                                         Print(Time[0].Month + "/" + Time[0].Day + "/" + Time[0].Year + ", " + High5.ToString() + ", " + Low5.ToString()+ ", " + DayHigh.ToString()+ ", " + DayLow.ToString());
                                     }
                    
                                     Print("Outside Loop A: " + ", startBarsAgo = " + startBarsAgo.ToString() + ", LoopNum = " + LoopNum.ToString());
                                     if (startBarsAgo == 5 && LoopNum == 1)
                                     {
                                         Print("Second Beep 1 " + Time[0].ToString() + " startBarsAgo = " + startBarsAgo.ToString() + ", LoopNum = " + LoopNum.ToString());
                                         High5   = MAX(High, 5)[0];
                                         Low5    = MIN(Low, 5)[0];
                                         LoopNum++;
                                         Print("Second Beep 2 " + Time[0].ToString() + " startBarsAgo = " + startBarsAgo.ToString() + ", LoopNum = " + LoopNum.ToString());
                                     }
                    
                                     if (startBarsAgo == 6 && LoopNum == 2)
                                     {
                                         Print("FIRST BEEP 1 " + Time[0].ToString() + " startBarsAgo = " + startBarsAgo.ToString() + ", LoopNum = " + LoopNum.ToString());
                                         Alert("5-min.", Priority.High, "5 Minutes", NinjaTrader.Core.Globals.InstallDir+@"\sounds\Aler t2.wav", 10, Brushes.Black, Brushes.Yellow);
                                         startBarsAgo++;
                                         LoopNum++;
                                         Print("FIRST BEEP 2 " + Time[0].ToString() + " startBarsAgo = " + startBarsAgo.ToString() + ", LoopNum = " + LoopNum.ToString());
                                     }
                                     Print("Outside Loop B: " + ", startBarsAgo = " + startBarsAgo.ToString() + ", LoopNum = " + LoopNum.ToString());
                    
                                     Line5High[0]   = High5;
                                     Line5Low[0]    = Low5;
                                     HighLine[0]    = DayHigh;
                                     LowLine[0]     = DayLow;
                    
                            }
                    
                    #region Properties
                    
                        [Browsable(false)]
                        [XmlIgnore]
                        public Series<double> Line5High
                        {
                        get { return Values[0]; }
                        }
                    
                        [Browsable(false)]
                        [XmlIgnore]
                        public Series<double> Line5Low
                        {
                        get { return Values[1]; }
                        }
                    
                        [Browsable(false)]
                        [XmlIgnore]
                        public Series<double> HighLine
                        {
                        get { return Values[2]; }
                        }
                    
                        [Browsable(false)]
                        [XmlIgnore]
                        public Series<double> LowLine
                        {
                        get { return Values[3]; }
                        }
                    
                        [Browsable(false)]
                        [XmlIgnore]
                        public int startBarsAgo
                        { get; set; }
                    
                        [Browsable(false)]
                        [XmlIgnore]
                        public int LoopNum
                        { get; set; }
                    
                        [Browsable(false)]
                        [XmlIgnore]
                        public double High5
                        { get; set; }
                    
                        [Browsable(false)]
                        [XmlIgnore]
                        public double Low5
                        { get; set; }
                    
                        [Browsable(false)]
                        [XmlIgnore]
                        public double DayHigh
                        { get; set; }
                    
                        [Browsable(false)]
                        [XmlIgnore]
                        public double DayLow
                        { get; set; }
                    
                    #endregion
                    
                    }​
                    Originally posted by bitdavid;
                    Are you using any calls to AddDataSeries in your indicator?
                    I don't see that in anywhere in my code, but I do have two Data Series present on my chart. I use the 1-minute for this indicator and the 1-min. VOL, but otherwise I am looking at the 20-second chart. This indicator is, of course, linked to the 1-minute.

                    Comment


                      #11
                      Hello NjTMatthew,

                      This may be due to trying to modify a user input instead of a variable, anything you are setting a value to dynamically should not be a user input. The public properties you have are user inputs, I would suggest making those private variables and then remove/reapply the script to test. For example

                      [Browsable(false)]
                      [XmlIgnore]
                      public int startBarsAgo
                      { get; set; }​

                      should be

                      private int startBarsAgo;

                      Comment

                      Latest Posts

                      Collapse

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