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

Bars Ago

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

    Bars Ago

    Hi everyone!

    I've got a code that checks if a condition is verified 10 bars ago AND 15 bars ago.

    The problem is: have I to re-write the code 2 times (the first one for 10 bars ago and the second one for 15 bars ago) or there a better and efficient way to write this?

    My CPU and me thank a lot!

    Mirko

    #2
    Mirkocero,

    I am happy to assist you.

    Essentially you can use the && operation.

    if ( condition1 && condition2 )
    {
    // Do something
    }

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

    Comment


      #3
      Thanks! But it's not so easy!!! I don't tell you all the situation... sorry!

      if ( condition1[x bars ago] or [y bars ago] && condition2[0] && condition3[0])
      {
      // Do something
      }

      Is it possibile to write something like that? When I compile there's an error about the union of boolean operators. Probably I can't do that but only continue to use my strategy in this way:

      if ( condition1[x bars ago] && condition2[0] && condition3[0])
      {
      // Do something
      }
      if ( condition1[y bars ago] && condition2[0] && condition3[0])
      {
      // Do something
      }

      Something wrong? Thanks a lot!

      Comment


        #4
        mirkocero,

        Generally calling an array like Condition[0] isn't a boolean until you compare it to something using an operator like ==, or <=, etc.

        If statements check for a true or a false, and if its true they execute what is inside them. There are logical operators "and', "or" etc you can use to combine multiple logical comparisons.

        If you could post your actual code I could comment further.

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

        Comment


          #5
          Very quickly!!!
          Here's the code:

          if (Close[10] > condition2[0]
          && MACD(12, 26, 9)[0] < var2
          && MACD(12, 26, 9)[0] > var2
          && GetCurrentBid() == condition3[0])
          {
          // Do something
          }

          Can I add something like that? Or I have to rewrite the code changing only the variable?
          if (Close[10] || Close [15]> condition2[0]
          && MACD(12, 26, 9)[0] < var2
          && MACD(12, 26, 9)[0] > var2
          && GetCurrentBid() == condition3[0])
          {
          // Do something
          }

          Thanks!

          Comment


            #6
            mirkocero,

            The first one looks Ok.

            The second has a Close[10] || Close[15] in it. This doesn't work because Close[x] is a double, not a boolean value. Operators such as || or && are only capable of being used for boolean operations, meaning comparing logical statements.

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

            Comment


              #7
              Originally posted by mirkocero View Post
              Very quickly!!!
              Here's the code:

              if (Close[10] > condition2[0]
              && MACD(12, 26, 9)[0] < var2
              && MACD(12, 26, 9)[0] > var2
              && GetCurrentBid() == condition3[0])
              {
              // Do something
              }

              Can I add something like that? Or I have to rewrite the code changing only the variable?
              if (Close[10] || Close [15]> condition2[0]
              && MACD(12, 26, 9)[0] < var2
              && MACD(12, 26, 9)[0] > var2
              && GetCurrentBid() == condition3[0])
              {
              // Do something
              }

              Thanks!
              Is condition2 a DataSeries?

              Comment


                #8
                Originally posted by koganam View Post
                Is condition2 a DataSeries?
                Hi Koganam!

                It's an indicator. Thanks for the reply!

                Comment


                  #9
                  Originally posted by koganam View Post
                  Is condition2 a DataSeries?
                  In that case, your first code should work fine. Does it?

                  Your second code block would have to be written thus:
                  Code:
                  [LEFT]if ((Close[10] > condition2[0] || Close [15] > condition2[0])
                  [/LEFT]
                       && MACD(12, 26, 9)[0] < var2
                      && MACD(12, 26, 9)[0] > var2
                      && GetCurrentBid() == condition3[0])
                  {
                  // Do something
                  }
                  That having been said, you should be careful when comparing unrounded doubles for equality. The condition: GetCurrentBid() == condition3[0] very well may never be true, especially if condition3 returns values to more than the precision of the price data, which is the precision of GetCurrentBid().
                  Last edited by koganam; 12-12-2011, 02:24 AM.

                  Comment


                    #10
                    Thanks!

                    Now it's ok!

                    About the condition3[0]: I know it... but you've already help me in another one thread some days ago!

                    Thanks so much Koganam!

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by GussJ, 03-04-2020, 03:11 PM
                    11 responses
                    3,227 views
                    0 likes
                    Last Post xiinteractive  
                    Started by andrewtrades, Today, 04:57 PM
                    1 response
                    13 views
                    0 likes
                    Last Post NinjaTrader_Manfred  
                    Started by chbruno, Today, 04:10 PM
                    0 responses
                    7 views
                    0 likes
                    Last Post chbruno
                    by chbruno
                     
                    Started by josh18955, 03-25-2023, 11:16 AM
                    6 responses
                    440 views
                    0 likes
                    Last Post Delerium  
                    Started by FAQtrader, Today, 03:35 PM
                    0 responses
                    12 views
                    0 likes
                    Last Post FAQtrader  
                    Working...
                    X