Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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.
    Chris L.NinjaTrader Customer Service

    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 algospoke, 04-17-2024, 06:40 PM
      5 responses
      46 views
      0 likes
      Last Post NinjaTrader_Jesse  
      Started by arvidvanstaey, Today, 02:19 PM
      1 response
      4 views
      0 likes
      Last Post NinjaTrader_Zachary  
      Started by mmckinnm, Today, 01:34 PM
      3 responses
      5 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by f.saeidi, Today, 01:32 PM
      2 responses
      8 views
      0 likes
      Last Post f.saeidi  
      Started by alifarahani, 04-19-2024, 09:40 AM
      9 responses
      55 views
      0 likes
      Last Post NinjaTrader_Jesse  
      Working...
      X