Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Bool not set to true

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

    Bool not set to true

    Hello
    I have the code below, which is supposed to find flat Lows:


    Code:
    private void OnBarUpdate()
    {
    ...
    
    if (PrintFlat)
        Print("Flat Low = " + FlatLow  + "    |    " + (Math.Abs(Lows[0][0] - Lows[0][1]) <= FlatPriceRange * TickSize) + "    |    Bar = " + CurrentBar + "\r\n-");
    
    if (!FlatLow && Math.Abs(Lows[0][0] - Lows[0][1]) <= FlatPriceRange * TickSize)
    {                
         FlatLowPrice.Add(Lows[0][1]);
         if (!FlatLowBar)
        {
              flatLowPriceBar = CurrentBar - 1;
              FlatLowBar = true;            
              Draw.ArrowUp(this, "Flat L Start Bar = " + flatLowPriceBar, true, CurrentBar - flatLowPriceBar, FlatLowPrice[w] - TickSize, Brushes.DodgerBlue, true);
        }
        FlatLow = true;
         w++;
     }
     if (PrintFlat)
          Print ("Flat Low Bar = " + FlatLowBar + "    |    " + (MAX(Lows[0], CurrentBar - flatLowPriceBar)[0] - MIN(Lows[0], CurrentBar - flatLowPriceBar)[0] > FlatPriceRange * TickSize) +
                    "    |    Flat Bar =" + flatLowPriceBar + " | F1 - F2 = " + (MAX(Lows[0], CurrentBar - flatLowPriceBar)[0] - MIN(Lows[0], CurrentBar - flatLowPriceBar)[0]).ToString("F4") + "    |    Bar = " + CurrentBar + "\r\n-");
    
    else if (FlatLowBar && MAX(Lows[0], CurrentBar - flatLowPriceBar)[0] - MIN(Lows[0], CurrentBar - flatLowPriceBar)[0] > FlatPriceRange * TickSize)
    {
        flatLowPriceEnd = CurrentBar - 1;
        FlatLowBar = false;
        FlatLow = false;
        Draw.ArrowUp(this, "Flat L End Bar = " + flatLowPriceEnd, true, CurrentBar - flatLowPriceEnd, Lows[0][1] - TickSize, Brushes.Gray, true);
    }
    if (PrintFlat)
        Print ("Flat Bar End = " + flatLowPriceEnd +  "    |    Bar = " + CurrentBar + "\r\n-");
    This code produces the following prints:

    Flat Low = False | True | Bar = 21
    -
    Flat Low Bar = True | False | Flat Bar =20 | F1 - F2 = 0.0000 | Bar = 21
    -
    Flat Bar End = 0 | Bar = 21
    -
    Flat Low = True | True | Bar = 22
    -
    Flat Low Bar = True | False | Flat Bar =20 | F1 - F2 = 0.0100 | Bar = 22
    -
    Flat Bar End = 0 | Bar = 22
    -
    Flat Low = True | False | Bar = 23
    -
    Flat Low Bar = True | True | Flat Bar =20 | F1 - F2 = 0.0800 | Bar = 23
    -
    Flat Bar End = 0 | Bar = 23
    -
    Flat Low = True | False | Bar = 24
    -
    Flat Low Bar = True | True | Flat Bar =20 | F1 - F2 = 0.1900 | Bar = 24
    -
    Flat Bar End = 0 | Bar = 24


    As you can see, on bar 21 it finds a flat Low price, prints the Flat Bar as 20, sets the " FlatLowBar = true; ", it it still Flat on bar 22, but on bar 23 it never sets the end of Flat (flatLowPriceEnd = 23), while FlatLowBar and FlatLow are still True on bar 24, which in turn never resets the logic for the next occurrence.

    How is the " else if {} " statement never executed, when both conditions are true?!?

    Compare this with the flat High code, basically a mirror code of the Flat Low, but which works as it should:

    Code:
    if (!FlatHigh && Math.Abs(Highs[0][0] - Highs[0][1]) <= FlatPriceRange * TickSize)
                {                
                    FlatHighPrice.Add(Highs[0][1]);
                    if (!FlatHighBar)
                    {
                        flatHighPriceBar = CurrentBar - 1;
                        FlatHighbar = true;            
                        Draw.ArrowDown(this, "Flat H Start Bar = " + flatHighPriceBar, true, CurrentBar - flatHighPriceBar, FlatHighPrice[v] + TickSize, Brushes.Cyan, true);
                    }
                    FlatHigh = true;
                    v++;
                }
                else if (FlatHighbar && (MAX(Highs[0], CurrentBar - flatHighPriceBar)[0] - MIN(Highs[0], CurrentBar - flatHighPriceBar)[0]) > FlatPriceRange * TickSize)
                {
                    flatHighPriceEnd = CurrentBar - 1;
                    FlatHighbar = false;
                    FlatHigh = false;
                    Draw.ArrowDown(this, "Flat H End Bar = " + flatHighPriceEnd, true, CurrentBar - flatHighPriceEnd, Highs[0][1] + TickSize, Brushes.Yellow, true);
                }
    I am getting the same result even when I am trying a new indicator with only the Flat Low logic above in OnBarUpdate and I am getting no errors in the Log window.

    Please see attachment with only the Flat High and Flat Low parts of the indicator enabled and observe Flat High arrows, vs only the start arrow of the first Flat Low.

    Is this a major bug, or what?
    Attached Files
    Last edited by itrader46; 02-09-2020, 10:23 AM.

    #2
    OK then... let me answer myself, since I can't delete this post: the else if() has been hijacked by the if (PrintFlat) before it

    Comment


      #3
      Hi itrader46, thanks for your post.

      Im glad you were able to find the issue here. If I can help with anything else please let me know.

      Comment


        #4
        Hi Chris

        Well... learning every day, isn't it?

        Just a a quick question: are the two lines of code below equivalent from a floating point ... point of view?

        To me it seems like the second line would do both statements of the first line, but if I am wrong, can you please explain why?

        PS: would I be able to do Math.Round() >= 0 * TickSize?

        Thank you

        Code:
        Math.Round(HSwDMIprice[b - 1] - HSwDMIprice[b - 2], 2) >= TickSize  || HSwDMIprice[b - 1].ApproxCompare(HSwDMIprice[b - 2]) == 0
        
        Math.Round(HSwDMIprice[b - 1] - HSwDMIprice[b - 2], 2) >= 0
        Last edited by itrader46; 02-10-2020, 01:29 PM.

        Comment


          #5
          Hi itrader46, thanks for your reply.

          I'm not really sure on the two code statements, you would need to print out the output here to tell the difference. Try ]

          Print(Math.Round(HSwDMIprice[b - 1] - HSwDMIprice[b - 2], 2));

          and

          Print(HSwDMIprice[b - 1].ApproxCompare(HSwDMIprice[b - 2]));

          then see what will come up in the output window, this will make it more clear on what to compare these values against.

          Comment

          Latest Posts

          Collapse

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