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 lorem, Today, 09:18 AM
          1 response
          4 views
          0 likes
          Last Post lorem
          by lorem
           
          Started by bmartz, Today, 09:30 AM
          0 responses
          3 views
          0 likes
          Last Post bmartz
          by bmartz
           
          Started by GussJ, 03-04-2020, 03:11 PM
          14 responses
          3,245 views
          0 likes
          Last Post GussJ
          by GussJ
           
          Started by ArkansasClint, Today, 09:28 AM
          0 responses
          0 views
          0 likes
          Last Post ArkansasClint  
          Started by hazylizard, Today, 08:38 AM
          4 responses
          12 views
          0 likes
          Last Post hazylizard  
          Working...
          X