Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Bool Flag Question

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

    Bool Flag Question

    Hello

    I've developed a strategy that automatically draws an up/downarrow below/above a candle when certain long/short entry conditions are met. If the signal does not result in a confirmed entry (executed buy/sell order), the arrow is removed again once the candle has closed to avoid a cluttered chart.

    What I'd like to achieve now is to integrate a bool flag that prevents two successive up/downarrows from being drawn immediately one after the other. There should be at least one red candle after the confirmed green candle that produced the uparrow before another uparrow is drawn again.

    Example: an uparrow is drawn if certain criteria are met for a long entry and if the candle is green (up candle).The bool flag should then check if at least one red (down) candle has appeared on the chart after a confirmed uparrow before drawing another uparrow.

    How should I go about it? This is what I've got so far:

    if (my conditions here && Close[0] > Open[0])
    {
    DrawArrowUp("BlackArrowLong" + CurrentBar, false, 0, Low[0], Color.Black);
    }

    if (High[0] <= High[1]) //Removes the Arrow if no Higher High was made and the Signal thus wasn't executed

    {
    RemoveDrawObject("BlackArrowLong" + (CurrentBar -1));
    }


    Thanks a lot for your help.
    Last edited by laocoon; 03-21-2011, 08:40 AM.

    #2
    Hi laocoon,

    For coding sequences we have this sample which uses User variables but the same principles can be used for bool flags:


    There are also lots of examples available on the forums.
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by laocoon View Post
      Example: an uparrow is drawn if certain criteria are met for a long entry and if the candle is green (up candle).The bool flag should then check if at least one red (down) candle has appeared on the chart after a confirmed uparrow before drawing another uparrow.

      How should I go about it? This is what I've got so far:

      if (my conditions here && Close[0] > Open[0])
      {
      DrawArrowUp("BlackArrowLong" + CurrentBar, false, 0, Low[0], Color.Black);
      }

      if (High[0] <= High[1]) //Removes the Arrow if no Higher High was made and the Signal thus wasn't executed

      {
      RemoveDrawObject("BlackArrowLong" + (CurrentBar -1));
      }


      Thanks a lot for your help.
      I would not use a bool, as it becomes too difficult to track in rapid succession. I would rather record the bar number on which the arrow is drawn and then have an escape for the number of bars that I want to elapse before allowing another entry.

      Code:
      private int LastArrowBar = 0;
      private int DeadBars = 1;
      
      protected override void OnBarUpdate()
      {
      //etc
      if (my conditions here && Close[0] > Open[0] && CurrentBar > LastArrowBar + DeadBars)
      {
      DrawArrowUp("BlackArrowLong" + CurrentBar, false, 0, Low[0], Color.Black);
      LastArrowBar = CurrentBar;
      }
      
      if (High[0] <= High[1]) //Removes the Arrow if no Higher High was made and the Signal thus wasn't executed
      
      {
      RemoveDrawObject("BlackArrowLong" + (CurrentBar -1));
      }
      //etc
      }

      Comment


        #4
        Thanks for your reply RyanM, I'll look into it.

        Thanks a lot for your suggestion Koganam. I understand what you're saying and it's indeed a good way of doing things, but there are two special situations that need to be addressed:

        1. In a very specific case the number of Dead Bars may be 0: if an arrow doesn't result in a signal because the next bar made no higher high, then the same next bar should produce a signal again if it is an upbar (=two upbars in a row where the second is generating a new signal)

        2. The number of Dead Bars is variable, ie in a strong trend there may be 5 upbars in a row before there's a downbar whereas in a weak trend there may be only 2. That's why I chose the trigger to reinitiate the process (and thus a new long signal) to be a downbar followed by an upbar.

        I'm not sure how to integrate these two scenarios in your suggested solution.

        Thanks again.
        Last edited by laocoon; 03-22-2011, 07:13 AM.

        Comment


          #5
          The number of Dead Bars is variable, ie in a strong trend there may be 5 upbars in a row before there's a downbar whereas in a weak trend there may be only 2. That's why I chose the trigger to reinitiate the process (and thus a new long signal) to be a downbar followed by an upbar.
          The way I wrote it, the number of dead bars is the MINIMUM threshold. ie., if DeadBars = 2 for example, then at least 2 bars MUST pass before entry is allowed again. "DeadBars" gates entry only; nothing else. I guess I should have called the variable "MinDeadBars"?

          Comment


            #6
            I understand what you're saying Koganam. I was just trying to highlight the fact that the main variable for avoiding redundant bars is NOT the number of (Dead) bars (which will vary from case to case) but rather their color, ie there must be at least one red bar after a green bar before there can be another green bar with a long signal (and the opposite for a short signal).

            Thanks

            Comment


              #7
              Originally posted by laocoon View Post
              I understand what you're saying Koganam. I was just trying to highlight the fact that the main variable for avoiding redundant bars is NOT the number of (Dead) bars (which will vary from case to case) but rather their color, ie there must be at least one red bar after a green bar before there can be another green bar with a long signal (and the opposite for a short signal).

              Thanks
              Understood, but I just assumed from your initial statement that that was being handled by the bolded part of your code snippet below:

              if (my conditions here && Close[0] > Open[0])

              I see that I misunderstood. You probably do want a boolean flag.

              In that case, one way might be to:
              1. Set the boolean flag when the UpArrow is drawn
              2. On each update, check for a (red candle && UpArrowBooleanFlag set)
              3. Once it happens, cancel the flag
              4. Only allow re-entry if the flag is NOT set.

              Code:
              private bool BlackArrowLongDrawn = false;
              
              if (BlackArrowLongDrawn && Close[0] < Open[0]) BlackArrowLongDrawn = false;
              if (my conditions here && Close[0] > Open[0] && !BlackArrowLongDrawn)
              {
              DrawArrowUp("BlackArrowLong" + CurrentBar, false, 0, Low[0], Color.Black);
              BlackArrowLongDrawn = true; 
              }
              
              if (High[0] <= High[1]) //Removes the Arrow if no Higher High was made and the Signal thus wasn't executed
              
              {
              RemoveDrawObject("BlackArrowLong" + (CurrentBar -1));
              BlackArrowLongDrawn = false;
              }

              Comment


                #8
                Thank you very much for your help Koganam, it is much appreciated and very generous of you. I'll implement your suggestion and I'll let you know how it goes.

                Comment


                  #9
                  I'm reviving this thread because I'm still having some issues with Bool Flags.

                  Koganam, I implemented the example you kindly submitted but for some reason it doesn't work yet as expected. The "my conditions here" part of the code was replaced by a simple condition, ie for longs the candle must close above the 50 SMA (and the opposite for shorts). The attached picture shows that on many occasions, there are still dots above/below two red/green candles in a row although this is exactly what the code should prevent. (What I'm trying to achieve is that there shouldn't be two signals (represented by dots) in a row without a candle of the opposite color in between.)

                  Here's the code:

                  private bool LimeDotLongDrawn = false;
                  private bool RedDotShortDrawn = false;

                  //LONG
                  if (LimeDotLongDrawn && Close[0] < Open[0]) LimeDotLongDrawn = false;
                  if (Close[0] > SMA(50)[0] && Close[0] > Open[0] && !LimeDotLongDrawn)
                  {
                  DrawDot("NoDoubleLong" + CurrentBar, false, 0, Low[0]- 1*(TickSize), Color.Lime);
                  LimeDotLongDrawn = true;
                  }


                  if (High[0] <= High[1])
                  {
                  RemoveDrawObject("NoDoubleLong" + (CurrentBar -1));
                  LimeDotLongDrawn = false;
                  }



                  //SHORT
                  if (RedDotShortDrawn && Close[0] > Open[0]) RedDotShortDrawn = false;
                  if (Close[0] < SMA(50)[0] && Close[0] < Open[0] && !RedDotShortDrawn)
                  {
                  DrawDot("NoDoubleShort" + CurrentBar, false, 0, High[0]+ 1*(TickSize), Color.Red);
                  RedDotShortDrawn = true;
                  }


                  if (Low[0] >= Low[1])
                  {
                  RemoveDrawObject("NoDoubleShort" + (CurrentBar -1));
                  RedDotShortDrawn = false;
                  }
                  Attached Files
                  Last edited by laocoon; 05-21-2012, 03:35 AM.

                  Comment


                    #10
                    Originally posted by laocoon View Post
                    I'm reviving this thread because I'm still having some issues with Bool Flags.

                    Koganam, I implemented the example you kindly submitted but for some reason it doesn't work yet as expected. The "my conditions here" part of the code was replaced by a simple condition, ie for longs the candle must close above the 50 SMA (and the opposite for shorts). The attached picture shows that on many occasions, there are still dots above/below two red/green candles in a row although this is exactly what the code should prevent. (What I'm trying to achieve is that there shouldn't be two signals (represented by dots) in a row without a candle of the opposite color in between.)

                    Here's the code:

                    private bool LimeDotLongDrawn = false;
                    private bool RedDotShortDrawn = false;

                    //LONG
                    if (LimeDotLongDrawn && Close[0] < Open[0]) LimeDotLongDrawn = false;
                    if (Close[0] > SMA(50)[0] && Close[0] > Open[0] && !LimeDotLongDrawn)
                    {
                    DrawDot("NoDoubleLong" + CurrentBar, false, 0, Low[0]- 1*(TickSize), Color.Lime);
                    LimeDotLongDrawn = true;
                    }


                    if (High[0] <= High[1])
                    {
                    RemoveDrawObject("NoDoubleLong" + (CurrentBar -1));
                    LimeDotLongDrawn = false;
                    }



                    //SHORT
                    if (RedDotShortDrawn && Close[0] > Open[0]) RedDotShortDrawn = false;
                    if (Close[0] < SMA(50)[0] && Close[0] < Open[0] && !RedDotShortDrawn)
                    {
                    DrawDot("NoDoubleShort" + CurrentBar, false, 0, High[0]+ 1*(TickSize), Color.Red);
                    RedDotShortDrawn = true;
                    }


                    if (Low[0] >= Low[1])
                    {
                    RemoveDrawObject("NoDoubleShort" + (CurrentBar -1));
                    RedDotShortDrawn = false;
                    }
                    It may be the manner in which your conditions are written. Let me ask a different question. What are the entry conditions in their entirity? I ask, because, given what you now write, the arrow removal may not be necessary, and its condition can be subsumed into the main entry condition, rather than being an after check. IOW, why do we need to remove any objects at all in the first place?

                    Comment


                      #11
                      Thanks a lot for bearing with me on this one, koganam.
                      The entry conditions in their entirety aren't relevant in this case because the reason why a dot is removed or not isn't depending on them. I removed all entry conditions and replaced them by a single condition to simplify things.

                      The logic of my strategy is as follows.
                      If all entry criteria are met, a signal (green=long / red=short dot) is drawn on the chart below/above the corresponding 10 minute candle. The signal is valid for the duration of the current candle (ie 10 minutes). Once the candle is closed and the next one opens, the dot either stays on the chart if the signal was validated, or it is removed if the signal was invalidated.

                      A Long signal is validated if the high of the corresponding candle is breached to the upside (for Shorts if the low of the candle is breached to the downside). The removal of the invalidated dots is important to me because they can create confusion if they stay on the chart once they're no longer valid.

                      I hope that this makes things clearer.

                      Comment


                        #12
                        Originally posted by laocoon View Post
                        Thanks a lot for bearing with me on this one, koganam.
                        The entry conditions in their entirety aren't relevant in this case because the reason why a dot is removed or not isn't depending on them. I removed all entry conditions and replaced them by a single condition to simplify things.

                        The logic of my strategy is as follows.
                        If all entry criteria are met, a signal (green=long / red=short dot) is drawn on the chart below/above the corresponding 10 minute candle. The signal is valid for the duration of the current candle (ie 10 minutes). Once the candle is closed and the next one opens, the dot either stays on the chart if the signal was validated, or it is removed if the signal was invalidated.

                        A Long signal is validated if the high of the corresponding candle is breached to the upside (for Shorts if the low of the candle is breached to the downside). The removal of the invalidated dots is important to me because they can create confusion if they stay on the chart once they're no longer valid.

                        I hope that this makes things clearer.
                        OK. I think I see what you are saying. Basically, the signal is triggered on one bar, then validated on the next bar, but the signal is drawn when triggered and removed if not validated. Is that essentially right?

                        What is the desired COBC setting?

                        Comment


                          #13
                          Koganam,

                          Your understanding is correct.
                          What do you mean by COBC Setting? If you mean CalculateOnBarClose, I have set it to true.

                          Thanks again.

                          Comment


                            #14
                            Originally posted by laocoon View Post
                            Koganam,

                            Your understanding is correct.
                            What do you mean by COBC Setting? If you mean CalculateOnBarClose, I have set it to true.

                            Thanks again.
                            Try this for your long side:
                            Code:
                            			if (this.LimeDotLongDrawn && High[0] <= High[1] && CurrentBar == this.TriggerCandle + 1) 
                            			{
                            				RemoveDrawObject("NoDoubleLong" + (CurrentBar - 1).ToString());
                            				if (Close[0] > SMA(50)[0] && Close[0] > Open[0])
                            					{
                            						DrawDot("NoDoubleLong" + CurrentBar.ToString(), false, 0, Low[0]- 1*(TickSize), Color.Lime);
                            						LimeDotLongDrawn = true;
                            						TriggerCandle = CurrentBar;
                            					}
                            			}
                            
                            			if (Close[0] > SMA(50)[0] && Close[0] > Open[0] && !this.LimeDotLongDrawn) 
                            				{
                            					DrawDot("NoDoubleLong" + CurrentBar.ToString(), false, 0, Low[0]- 1*(TickSize), Color.Lime);
                            					LimeDotLongDrawn = true;
                            					TriggerCandle = CurrentBar;
                            				}
                            
                            			if (this.LimeDotLongDrawn && Close[0] < Open[0]) LimeDotLongDrawn = false;
                            Naturally, you do not need me to show you how to reverse it for the short side.
                            Last edited by koganam; 05-22-2012, 08:00 PM.

                            Comment


                              #15
                              Koganam,

                              I'm currently testing your code and so far it looks great...
                              Thank you very much for taking the time to helping me out here, it is much appreciated.

                              Have a great day.

                              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
                              576 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X