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 adeelshahzad, Today, 03:54 AM
            5 responses
            32 views
            0 likes
            Last Post NinjaTrader_BrandonH  
            Started by stafe, 04-15-2024, 08:34 PM
            7 responses
            32 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Started by merzo, 06-25-2023, 02:19 AM
            10 responses
            823 views
            1 like
            Last Post NinjaTrader_ChristopherJ  
            Started by frankthearm, Today, 09:08 AM
            5 responses
            21 views
            0 likes
            Last Post NinjaTrader_Clayton  
            Started by jeronymite, 04-12-2024, 04:26 PM
            3 responses
            43 views
            0 likes
            Last Post jeronymite  
            Working...
            X