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

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 Pattontje, Yesterday, 02:10 PM
          2 responses
          15 views
          0 likes
          Last Post Pattontje  
          Started by flybuzz, 04-21-2024, 04:07 PM
          17 responses
          229 views
          0 likes
          Last Post TradingLoss  
          Started by agclub, 04-21-2024, 08:57 PM
          3 responses
          17 views
          0 likes
          Last Post TradingLoss  
          Started by TradingLoss, 04-21-2024, 04:32 PM
          4 responses
          45 views
          2 likes
          Last Post TradingLoss  
          Started by cre8able, 04-17-2024, 04:16 PM
          6 responses
          57 views
          0 likes
          Last Post cre8able  
          Working...
          X