Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Conditions logic

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

    Conditions logic

    Hello!
    Is there a way to break a serial condition when the first one is met?
    For example, I am in a trend. I have the following condition:
    HTML Code:
    if(Open[0] < Close[0] && Open[1] < Close[1])
    { DrawArrow()}
    The problem here is that when three consecutive bars meet the condition, at least two arrows are drown.
    In this case I need only the firt arrow.

    Is there a way to ignore the subsequent arrows after the first one is drawn in the condition?
    I have attached a picture with arrows (blue and Maganta) illustrating the succession of arrows meeting the condition. However, I want only the first arraw in ecah direction.
    What can I add to the condition to ignore the subsequent arrows?

    Many thanks
    Attached Files

    #2
    Hi, thanks for posting.

    This can be controlled with C# logic and utilizing boolean variables and nested IF statements. One way you can do it is to add a boolean variable to the class and make it act as a flag e.g.

    Code:
    private bool flag = false;
    OnBarUpdate()
    {
        if(<bar condition> && !flag)
        {
            //Draw something;
            flag = true;
        }
    }
    You will need to reset flag at some point like when the condition reverses (the opposite is true) or when the next bar closes. You can also keep track of which bar the condition triggered on by saving the CurrentBar variable to a private int and comparing it at a later time. There are many ways of doing this kind of logic in C#, you will need to test out which one is best for your script.

    Kind regards,
    -ChrisL

    Comment


      #3
      Many thanks ChrisL for your reply and your help.

      I will exoplore these possibilities and revert to you if I face problem.

      Many thanks again.

      Comment


        #4
        Hello Chris L.

        I am back with an issue related to this topic. I have successfully implemented the piece of code inn my strategy, realising that it works, well, but at the point where the conditions start to be met, the strategy fails to work.

        I have the following:
        HTML Code:
        if(EMA(9)[0] > EMA(11)[0])
        {
          if(Open[0] < Close[0] && !flag)
         {
          //Draw something;
          flag = true;
         }
        }
        At this level, I have for example for long signal only one arrow until an arrow for short signal appears, and only after that I can have another arrow for long signal. But what I want is to have one arrow when the conditions are met and an arrow everytime a bearish bar would appear followed directly by a bullish bar in a bullish market. Here is excatly where I am having serious problem.

        When I add to the above code the following,
        HTML Code:
        if(Open[0] > Close[0])
        {
         flag = false;
        }
        having the complete followig code.

        HTML Code:
        if(EMA(9)[0] > EMA(11)[0])
        {
          if(Open[0] < Close[0] && !flag)
          {
            //Draw something;
            flag = true;
          }
          if(Open[0] > Close[0])
          {
           flag = false;
          }
        }
        With this complete code, I am failling sometimes to have arrow where the conditions start to be met, but everywhere after the bearish bar is formed in the main condition, I have an arrow.

        Any help would be appreciate.

        Best regards

        Comment


          #5
          Hi, thanks for your reply.

          When you write this:

          Code:
          if(Open[0] < Close[0] && !flag)
          {
              //Draw something;
              flag = true;
          }
          It will be true on only one bar, so you should only get one drawing object once this becomes true. The best way to debug your code will be to use the Print() method to print out data from the script. This will show you why conditions are becoming true:


          Code:
          if(EMA(9)[0] > EMA(11)[0])
          {
              Print("EMA(9)[0] > EMA(11)[0] TRUE");
              Print(flag);
              if(Open[0] < Close[0] && !flag)
              {
                  Print("Open[0] < Close[0] && !flag TRUE, Drawing, setting flag to true");
                  //Draw something;
                  flag = true;
              }
              if(Open[0] > Close[0])
              {
                   Print("Open[0] > Close[0] TRUE, setting flag to false");
                   flag = false;
              }
          }

          Comment


            #6
            Stanfillirenfro You may also want to consider a little code optimisation too, along these lines, based on the helpful code provided by NinjaTrader_ChrisL :
            Code:
            if(EMA(9)[0] > EMA(11)[0])
            {
                Print("EMA(9)[0] > EMA(11)[0] TRUE, & flag is " + flag);  // Reducing Print statements can be more efficient
            
                if (flag)
                    Print("flag is TRUE --> Not drawing");  // Depending on how you initialise flag, it may be true or false on first pass
                else
                if (Open[0] < Close[0])  // Only tests this when flag is false, reducing unnecessary execution
                {
                    Print("Open[0] < Close[0] is TRUE && flag is false --> Drawing");
                    //Draw something;
                }
            
                flag = Open[0] <= Close[0];  // Sets flag as you originally coded, but perhaps not as you really intend?
            }
            Simplifications such as these are not required, but they might make the code easier to understand and more efficient.

            Importantly though, note that there is one case of the values you were testing in your original code that you have not explicitly covered, namely when Open[0] == Close[0] (i.e. a Doji). The code here accounts for that, but does not take any explicit action.

            Hope that helps. As always, code for what you want and works as intended, then consider refactoring to improve efficiency as needs be.

            Thanks.
            Multi-Dimensional Managed Trading
            jeronymite
            NinjaTrader Ecosystem Vendor - Mizpah Software

            Comment


              #7
              Many thanks Jeromynite, many thanks ChrisL,

              Honestly I am still struggling with this topic.
              I am looking for other ways to understand that. The print() function does not tell me what I am not understanding. I do also not localizing the problem. I am still searching.

              Thanks anyway!

              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