Announcement

Collapse
No announcement yet.

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 Geovanny Suaza, 02-11-2026, 06:32 PM
                    0 responses
                    649 views
                    0 likes
                    Last Post Geovanny Suaza  
                    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                    0 responses
                    370 views
                    1 like
                    Last Post Geovanny Suaza  
                    Started by Mindset, 02-09-2026, 11:44 AM
                    0 responses
                    109 views
                    0 likes
                    Last Post Mindset
                    by Mindset
                     
                    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                    0 responses
                    574 views
                    1 like
                    Last Post Geovanny Suaza  
                    Started by RFrosty, 01-28-2026, 06:49 PM
                    0 responses
                    576 views
                    1 like
                    Last Post RFrosty
                    by RFrosty
                     
                    Working...
                    X