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 CarlTrading, 03-31-2026, 09:41 PM
      1 response
      80 views
      1 like
      Last Post NinjaTrader_ChelseaB  
      Started by CarlTrading, 04-01-2026, 02:41 AM
      0 responses
      40 views
      0 likes
      Last Post CarlTrading  
      Started by CaptainJack, 03-31-2026, 11:44 PM
      0 responses
      63 views
      2 likes
      Last Post CaptainJack  
      Started by CarlTrading, 03-30-2026, 11:51 AM
      0 responses
      63 views
      0 likes
      Last Post CarlTrading  
      Started by CarlTrading, 03-30-2026, 11:48 AM
      0 responses
      54 views
      0 likes
      Last Post CarlTrading  
      Working...
      X