Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

If statement with 'or'

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

    If statement with 'or'

    Hi Folks

    I can't seem to get this right yet I've experimented quite a bit and it hasn't worked: any pointers will be much appreciated.

    Just say I have these conditions:

    Close[0] > Open[0]: a definite condition

    Then either:

    Close[1] > Open[1]

    or:

    Close[2] > Open[2]

    or both are true.


    I've tried this but it doesn't seem to do the trick:

    if( Close[0] > Open[0]

    && (
    Close[1] > Open[1]
    || Close[2] > Open[2]
    )


    This doesn't seem to work either:

    if( Close[0] > Open[0]

    || Close[1] > Open[1]
    || Close[2] > Open[2]


    I'm guessing there's a way I haven't seen.

    Thanks in advance.

    #2
    Hello,

    You can accomplish this by nesting your second condition within the first condition. The third condition ("or both are true") will not be needed, because it will always be covered by the second condition ("either are true"). I recommend trying the following:

    Code:
    if (Close[0] > Close[0])
    {
         if (Close[1] > Close[1] || Close[2] > Close[2])
         {
             actions here....
         }
    }
    Please let me know if I can assist further.
    Dave I.NinjaTrader Product Management

    Comment


      #3
      Thanks very much, Dave.

      That seems to be exactly what I've been looking for.

      Much obliged.

      Comment


        #4
        Originally posted by arbuthnot View Post
        Hi Folks

        I can't seem to get this right yet I've experimented quite a bit and it hasn't worked: any pointers will be much appreciated.

        Just say I have these conditions:

        Close[0] > Open[0]: a definite condition

        Then either:

        Close[1] > Open[1]

        or:

        Close[2] > Open[2]

        or both are true.


        I've tried this but it doesn't seem to do the trick:

        if( Close[0] > Open[0]

        && (
        Close[1] > Open[1]
        || Close[2] > Open[2]
        )


        This doesn't seem to work either:

        if( Close[0] > Open[0]

        || Close[1] > Open[1]
        || Close[2] > Open[2]


        I'm guessing there's a way I haven't seen.

        Thanks in advance.
        Your first formulation is correct. How do you mean: "... it doesn't seem to do the trick"? What result are you expecting, that you are not getting?

        Comment


          #5
          Originally posted by koganam View Post
          Your first formulation is correct. How do you mean: "... it doesn't seem to do the trick"? What result are you expecting, that you are not getting?
          Thanks very much, Koganam.

          I'd checked my logic and results by outputting to Excel and the code I had didn't seem to work.

          In light of what you've very kindly just said, I'll check again.

          Cheers.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          566 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          330 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          101 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          547 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          548 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X