Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Bool set to True on False conditions

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

    Bool set to True on False conditions

    Hello

    My indicator started to behave strangely today: one particular bool is being set to True, despite the one of two conditions for changing its state being False.

    The prints look as follows:

    CB = 2216 | C 2 = 4 | FLSbar[w-1] = 2212 | Flat Found = -1 | Flat End = -1 | FlatLowBar = False | FlatLow = False | Lows[0] = 49.99 | Lows[1] = 50 | Lows[FLSbar[w-1]] = 50.02

    CB = 2217 | C 2 = 1 | FLSbar[w-1] = 2216 | Flat Found = 0 | Flat End = 0 | FlatLowBar = True | FlatLow = True | Lows[0] = 49.99 | Lows[1] = 49.99 | Lows[FLSbar[w-1]] = 49.99

    CB = 2218 | C 2 = 2 | FLSbar[w-1] = 2216 | Flat Found = 1 | Flat End = 1 | FlatLowBar = False | FlatLow = False | Lows[0] = 50 | Lows[1] = 49.99 | Lows[FLSbar[w-1]] = 49.99

    CB = 2219 | C 2 = 3 | FLSbar[w-1] = 2216 | Flat Found = 1 | Flat End = 1 | FlatLowBar = False | FlatLow = False | Lows[0] = 50.04 | Lows[1] = 50 | Lows[FLSbar[w-1]] = 49.99

    CB = 2220 | C 2 = 4 | FLSbar[w-1] = 2216 | Flat Found = 1 | Flat End = 1 | FlatLowBar = False | FlatLow = True | Lows[0] = 50.05 | Lows[1] = 50.04 | Lows[FLSbar[w-1]] = 49.99

    As you can see, the last Flat Low is correctly found on bar 2217, but for some reason, on bar 2220, FlatLow bool is set to True, despite the ApproxCompare condition being 1, which disables the script for the rest of the session and I am struggling to understand why, so an answer different to 'We can't assist with debugging' would be appreciated.

    Code:
    private void PriceLowFlat()
            {                        
                if (!FlatLow && Lows[0][0].ApproxCompare(Lows[0][1]) == 0)
                {                
                    if (!FlatLowBar)
                    {
                        FLSbar.Add(CurrentBar-1); // w
                        FlatLowPrice.Add(Lows[0][1]);    
                        FlatLowBar = true;                          
                        w++;
                    }
                    FlatLow = true;
                }            
                else if (FlatLowBar && Lows[0][0].ApproxCompare(Lows[0].GetValueAt(FLSbar[w-1])) != 0)
                {
                    FLEbar.Add(CurrentBar-1);
                    FlatLowBar = false;
                    FlatLow = false;                
                    x++;
                }
            }
    
    if (x > 0 && w > 0)
                Print ("CB = " + CurrentBar + "    |    C 2 = " + (CurrentBar - FLSbar[w-1]) + "    |    FLSbar[w-1] = " + FLSbar[w-1] + "    |    Flat Found = " + Lows[0][0].ApproxCompare(Lows[0][1])
                          + "        |    Flat End = " + Lows[0][0].ApproxCompare(Lows[0].GetValueAt(FLSbar[w-1]))  + "    |    FlatLowBar = " + FlatLowBar + "    |    FlatLow = " + FlatLow
                          +  "    |    Lows[0] = " + Lows[0][0] + "    |    Lows[1] = " + Lows[0][1] + "    |    Lows[FLSbar[w-1]] = " + Lows[0].GetValueAt(FLSbar[w-1]));

    #2
    Hi itrader46, thanks for your post.

    Print out the values you are using here. Its impossible for a block of code to run in an IF block unless all of the conditions are actually true. In this case, you will need to Print out the values from your conditions contiguously and see why they are becoming true.

    e.g.


    Code:
    private void PriceLowFlat()
            {      
                Print("First Condition Check");
                Print(!FlatLow);
                Print(Lows[0][0].ApproxCompare(Lows[0][1]) == 0);                  
                if (!FlatLow && Lows[0][0].ApproxCompare(Lows[0][1]) == 0)
                {           
                    Print("Second Condition Check"); 
                    Print(!FlatLowBar);    
                    if (!FlatLowBar)
                    {
                        FLSbar.Add(CurrentBar-1); // w
                        FlatLowPrice.Add(Lows[0][1]);    
                        FlatLowBar = true;                          
                        w++;
                    }
                    FlatLow = true;
                }            
    
    //And so on with printing out conditions.

    Comment


      #3
      Hi Chris

      Thanks for the answer.

      I found in the meantime that I was also mistakenly setting FlatLow = True in another part of the code

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by charlesugo_1, 05-26-2026, 05:03 PM
      0 responses
      68 views
      0 likes
      Last Post charlesugo_1  
      Started by DannyP96, 05-18-2026, 02:38 PM
      1 response
      150 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by CarlTrading, 05-11-2026, 05:56 AM
      0 responses
      162 views
      0 likes
      Last Post CarlTrading  
      Started by CarlTrading, 05-10-2026, 08:12 PM
      0 responses
      100 views
      0 likes
      Last Post CarlTrading  
      Started by Hwop38, 05-04-2026, 07:02 PM
      0 responses
      288 views
      0 likes
      Last Post Hwop38
      by Hwop38
       
      Working...
      X