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

Using And and Or's

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

    Using And and Or's

    Should something like this work:

    ((A > B && C < D) || (E > F && G < H)) && X > Y && Z > Q

    I had a bit of simple logic performing as desired, based on a string of &&'d statements. But then I added in the bit with the parentheses, and I get compile errors, the editor is looking for a semi colon.

    #2
    coolmoss,

    From what you posted, I can only see one issue :

    ((A > B && C < D) || (E > F && G < H) && X > Y && Z > Q )

    You have an extra parenthesis next to the H and not one at the end next to the Q.
    Adam P.NinjaTrader Customer Service

    Comment


      #3
      Operator Precedence in Formulas

      I don't see a problem with coolmoss parentheses, as parentheses take presendence in computation over comparison operators.

      Is there any limits for parenthese nesting in NT script?

      This type of conditional statement should also be supported....

      Code:
      (A < B < C)
      Any comments?

      Comment


        #4
        Originally posted by borland View Post
        I don't see a problem with coolmoss parentheses, as parentheses take presendence in computation over comparison operators.

        Is there any limits for parenthese nesting in NT script?

        This type of conditional statement should also be supported....

        Code:
        (A < B < C)
        Any comments?
        code it and find out? would take about 1 minute

        Comment


          #5
          Borland,

          NinjaScript is essentially C# so all code would need to adhere to C# standards.

          Please let me know if I may assist further.
          Adam P.NinjaTrader Customer Service

          Comment


            #6
            Originally posted by NinjaTrader_AdamP View Post
            Borland,

            NinjaScript is essentially C# so all code would need to adhere to C# standards.

            Please let me know if I may assist further.

            To NinjaTrader Service Reps,

            "else if ((Stochastics(7, 14, 3).K[0] < (Stochastics(21, 34, 21).D[0]) && ((CrossBelow(MACD(2, 21, 2), MACD(8, 21, 13).Avg, 1) && ((MACD(2, 21, 2)[1] > MACD(2, 21, 2)[2] && (MACD(2, 21, 2)[1] > MACD(2, 21, 2)[0]) && (MACD(2, 21, 2)[1] > MACD(8, 21, 5)[1] && (MACD(2, 21, 2)[0] <= MACD(8, 21, 5).Avg[0] && ((MACD(2, 21, 2)[3] > MACD(2, 21, 2)[4] && (MACD(2, 21, 2)[3] > MACD(2, 21, 2)[2]) || ((MACD(2, 21, 2)[4] > MACD(2, 21, 2)[5] && (MACD(2, 21, 2)[4] > MACD(2, 21, 2)[3]) && ((MACD(2, 21, 2)[1] < MACD(2, 21, 2)[3]) || (MACD(2, 21, 2)[1] < MACD(2, 21, 2)[4]) && ((ChannelChopCount < 20)))))))))))))))"

            I can't find a general explanation on how to use parentheses in NinjaTrader in any of the help guides I've looked in. If I have just a small logic statement, I can get it to compile and work properly, but if I have a large logic statement like the one above, I can get it to compile without any errors, but I realize that one or more statements are NOT seen by the program. In other words if there are 10 different conditions being tested for, maybe only eight or nine of them will be seen. I'm not sure how to correct this.
            I added " && ChannelChopCount < 20" to the end of logic statement. The above logic statement compiles clean, but the program does NOT see the added statement.
            I thought I understood the syntax, but realize that I really don't. Can someone give me an example showing the proper syntax for the use of the parentheses.

            Thanks in advance for your assistance.
            Last edited by RookieTrader; 07-09-2013, 01:15 PM.

            Comment


              #7
              Hello RookieTrader,

              Thank you for your post.

              Here it is very important to understand the use of the parenthesis and the use of the operators '&&' and '||'.

              If I say I want to check conditionA and conditionB is true or if conditionC is true I use the following:
              Code:
              if((conditionA && conditionB) || conditionC)
              Notice the use of the parenthesis around conditionA and conditionB to ensure these are checked together. Now if I used the following the condition would be different:
              Code:
              if(conditionA && (conditionB || conditionC))
              Now we are checking first if conditionA is true and then if either conditionB or conditionC is true.

              I suggest experimenting with these and building your self up to such a complicated condition as you have listed. It is best to understand how these work and what exactly you are asking in your condition.

              Comment


                #8
                I've reduced some extra ()'s, and tried to line things up to make some sense of your statement.

                ChannelChopCount is nested all the way at the last statement.

                I haven't tested it to verify I didn't screw anything up by removing brackets.
                So it's probably best you take the idea from here, and move things around yourself.


                Code:
                  if (     Stochastics(7, 14, 3).K[0] < Stochastics(21, 34, 21).D[0] 
                     &&  (  CrossBelow(MACD(2, 21, 2), MACD(8, 21, 13).Avg, 1) 
                   && (  MACD(2, 21, 2)[1] > MACD(2, 21, 2)[2] 
                     && (MACD(2, 21, 2)[1] > MACD(2, 21, 2)[0])
                      && (    MACD(2, 21, 2)[1] > MACD(8, 21, 5)[1] 
                       && (   MACD(2, 21, 2)[0] <= MACD(8, 21, 5).Avg[0] 
                       && (    MACD(2, 21, 2)[3] > MACD(2, 21, 2)[4] 
                          && (MACD(2, 21, 2)[3] > MACD(2, 21, 2)[2]) 
                         || (   MACD(2, 21, 2)[4] > MACD(2, 21, 2)[5] 
                          &&  MACD(2, 21, 2)[4] > MACD(2, 21, 2)[3] 
                          && (   MACD(2, 21, 2)[1] < MACD(2, 21, 2)[3]
                             ||  MACD(2, 21, 2)[1] < MACD(2, 21, 2)[4]
                           && ChannelChopCount < 20 
                          )
                          )
                       )
                      )
                     )
                    )
                   ) 
                  )

                I think you wanted it outside, like this:

                Code:
                 if (     Stochastics(7, 14, 3).K[0] < Stochastics(21, 34, 21).D[0] 
                     &&  (  CrossBelow(MACD(2, 21, 2), MACD(8, 21, 13).Avg, 1) 
                   && (  MACD(2, 21, 2)[1] > MACD(2, 21, 2)[2] 
                     && (MACD(2, 21, 2)[1] > MACD(2, 21, 2)[0])
                      && (    MACD(2, 21, 2)[1] > MACD(8, 21, 5)[1] 
                       && (   MACD(2, 21, 2)[0] <= MACD(8, 21, 5).Avg[0] 
                       && (    MACD(2, 21, 2)[3] > MACD(2, 21, 2)[4] 
                          && (MACD(2, 21, 2)[3] > MACD(2, 21, 2)[2]) 
                         || (   MACD(2, 21, 2)[4] > MACD(2, 21, 2)[5] 
                          &&  MACD(2, 21, 2)[4] > MACD(2, 21, 2)[3] 
                          && (   MACD(2, 21, 2)[1] < MACD(2, 21, 2)[3]
                             ||  MACD(2, 21, 2)[1] < MACD(2, 21, 2)[4]
                          )
                          )
                       )
                      )
                     )
                    )
                   ) 
                  && ChannelChopCount < 20   
                  )

                Comment


                  #9
                  Patrick, Sledge,

                  I've been sitting in front of the computer so long my mind starts to play tricks on me. Thanks for the refresher guys. It helped me see where the problem was. After walking through the syntax setup I realize the problem wasn't with the placement of the parentheses. It was a logic problem.

                  Thanks for helping resolve. Have a good night... or a good day depending on when you get this message.

                  Comment


                    #10
                    Patrick, Sledge,

                    I know I'm beating a dead horse, but I realize now that I had way too many parentheses in my logic statements. Corrections would sometimes take hours. I was able too minimize the number of parentheses in my logic statements to a bare minimum. Issues with logic statements now take only seconds to correct. I thought I understood the syntax before, but I realize now that I didn't. I've seen the syntax explained before but it never sunk in. This time it clicked. This has truly made a huge difference.

                    Great appreciation to both of you for your help in clarifying my understanding of this.

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by funk10101, Today, 12:02 AM
                    0 responses
                    3 views
                    0 likes
                    Last Post funk10101  
                    Started by gravdigaz6, Yesterday, 11:40 PM
                    1 response
                    7 views
                    0 likes
                    Last Post NinjaTrader_Manfred  
                    Started by MarianApalaghiei, Yesterday, 10:49 PM
                    3 responses
                    10 views
                    0 likes
                    Last Post NinjaTrader_Manfred  
                    Started by XXtrader, Yesterday, 11:30 PM
                    0 responses
                    4 views
                    0 likes
                    Last Post XXtrader  
                    Started by love2code2trade, 04-17-2024, 01:45 PM
                    4 responses
                    28 views
                    0 likes
                    Last Post love2code2trade  
                    Working...
                    X