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

what's the best way to multiple if statements

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

    what's the best way to multiple if statements

    Hi I'm new to coding.
    If I need multiple if conditions, is it best to separate them by line?
    eg,

    if
    atr >X);
    (any of three indicators > X);
    (any of two indicators > X);
    (any one indicator > X);

    else if
    (atr < X);
    (any of three indicators < X);
    (any of two indicators < X);
    (any one indicator < X);

    or make one quadruple long condition statement
    eg.,
    (any of three indicators > X) || (any of two indicators > X) || (any one indicator > X);

    else if
    (any of three indicators < X) || (any of two indicators < X) || (any one indicator < X);

    #2
    Cowboy, I'm not sure exactly what you're meaning on the first example, the ; terminates the expression. You're essentially still using the same technique of nested if's here with is legal in C#. When you're using using the && or || operators though you want to be aware that C# short circuit evaluates them, meaning as soon as the overall result of the expression is known, no other parts are evaluated (the && operator only evaluates the first Boolean if false, the || operator only evaluates the first Boolean if true).
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Bertrand thanks for the reply.
      Let me try to say this in english as that language I do speak. lol

      I want a code that says if all 3 events happen do this; if 2 of 3 event happen do another thing; and if only 1 of the three events happen do this third thing. This set of if commands are for a bull direction (>).

      Then the same commands would be there only for bear commands <

      I'm not sure how to code this

      Was I able to express this more clearly?

      Thanks

      Comment


        #4
        Originally posted by Cowboy View Post
        Bertrand thanks for the reply.
        Let me try to say this in english as that language I do speak. lol

        I want a code that says if all 3 events happen do this; if 2 of 3 event happen do another thing; and if only 1 of the three events happen do this third thing. This set of if commands are for a bull direction (>).

        Then the same commands would be there only for bear commands <

        I'm not sure how to code this

        Was I able to express this more clearly?

        Thanks
        Just use a counter?
        Code:
        int eventTotal = 0;
        
        if (condition1Happens) eventTotal++;
        if (condition2Happens) eventTotal++;
        if (condition3Happens) eventTotal++;
        
        if (eventTotal == 3) {...}
        else if (eventTotal == 2) {...}
        else if (eventTotal == 1) {...}

        Comment


          #5
          Thanks, I'll try this - coding all new to me - very interesting what can be achieved

          Comment


            #6
            Hello

            Good asking.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by swestendorf, Today, 11:14 AM
            0 responses
            1 view
            0 likes
            Last Post swestendorf  
            Started by Sparkyboy, Today, 10:57 AM
            0 responses
            3 views
            0 likes
            Last Post Sparkyboy  
            Started by TheMarlin801, 10-13-2020, 01:40 AM
            21 responses
            3,917 views
            0 likes
            Last Post Bidder
            by Bidder
             
            Started by timmbbo, 07-05-2023, 10:21 PM
            3 responses
            155 views
            0 likes
            Last Post grayfrog  
            Started by Lumbeezl, 01-11-2022, 06:50 PM
            30 responses
            812 views
            1 like
            Last Post grayfrog  
            Working...
            X