Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

how to write the code of double sma crossovers of same condition

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

    how to write the code of double sma crossovers of same condition

    hi team,

    i want to write a code in which

    sma 12 has to cross above sma 50 and the earlier crossabove of sma 12 over sma 50 should have occured atleast 50 bars earlier.

    how to write this logic using our script

    please help​

    #2
    Hello cheryn,

    Thank you for writing in.

    You can use Most Recent Occurence, MRO(), or Least Recent Occurence, LRO(), to check if a condition has evaluated to true within a certain number of bars back. MRO starts at the current bar and works backward, and LRO starts at the designated bars ago and checks toward the current bar. For more information:

    MRO() - https://ninjatrader.com/support/help...urence_mro.htm
    LRO() - https://ninjatrader.com/support/help...urence_lro.htm

    If you are unsure of how to write this in your script, please be more specific; you mentioned "should have occured atleast 50 bars earlier." Do you mean that it should have occurred at least once within the last 50 bars or that it has not occurred in at least the last 50 bars/the last occurrence was 50 or more bars ago?

    Please let me know if I may be of further assistance.

    Comment


      #3
      Alternately, and easier imo, each time you have a cross save the value of CurrentBar into an int variable you create. The variable will hold the latest cross bar and then you can simply subract CurrentBar minus the variable and the difference will be the number of bars since the last cross.

      Comment


        #4
        Originally posted by NinjaTrader_Emily View Post
        Hello cheryn,

        Thank you for writing in.

        You can use Most Recent Occurence, MRO(), or Least Recent Occurence, LRO(), to check if a condition has evaluated to true within a certain number of bars back. MRO starts at the current bar and works backward, and LRO starts at the designated bars ago and checks toward the current bar. For more information:

        MRO() - https://ninjatrader.com/support/help...urence_mro.htm
        LRO() - https://ninjatrader.com/support/help...urence_lro.htm

        If you are unsure of how to write this in your script, please be more specific; you mentioned "should have occured atleast 50 bars earlier." Do you mean that it should have occurred at least once within the last 50 bars or that it has not occurred in at least the last 50 bars/the last occurrence was 50 or more bars ago?

        Please let me know if I may be of further assistance.
        thanks Emily for reply..
        i am unsure how to write this in the code.. in my code i want to write it has not occured atleast in the last 50 bars... or the last occurance was 50 or more bars ago...

        thanks if u could help that wud be so wonderful
        thanks​

        Comment


          #5
          Originally posted by Tasker-182 View Post
          Alternately, and easier imo, each time you have a cross save the value of CurrentBar into an int variable you create. The variable will hold the latest cross bar and then you can simply subract CurrentBar minus the variable and the difference will be the number of bars since the last cross.
          thanks Tasker,

          actually i tried but failed while running in long it didnt fetch even a single order.
          if (CurrentBar < 150)
          return;

          if (CrossAbove(SMA1,SMA2, 2))
          {
          K = CurrentBar; //save the bar number where the cross occured.
          Print( K);
          }

          if (

          (CrossAbove(SMA1, SMA2, 2) && CurrentBar - K > 50)


          thanks tasker for helping out
          thanks
          i will learn from Emily other method also meanwhile and if u could help i will learn two methods. learning is always wonderful. ​

          Comment


            #6
            Your code as written would be excluding the condition CurrentBar - K because in the previous if statement you are setting K to be the same as the CurrentBar so the 2nd if statement will never be true.

            Remove the first if statement and then add the K = CurrentBar in your 2nd if statement so your code looks like:

            if ((CrossAbove(SMA1, SMA2, 2) && CurrentBar - K > 50)
            {

            your other coding...
            K = CurrentBar; //save the bar number where the cross occured AFTER the cross.
            ​}

            When your code processes on the historical data, on the first cross K would be zero so the CurrentBar - K > 50 will be true. Then K is set to the CurrentBar of the first cross and ​will remain that way until the next cross that is > 50 bars away

            Comment


              #7
              Hello cheryn,

              Thank you for your patience.

              Hopefully Tasker-182 has assisted with the approach that uses the saved bar index in a variable. Otherwise, you could use MRO in the following way. The condition checks if SMA1 has crossed above SMA2 && if the second most recent occurrence (since CurrentBar would be the first most recent occurrence) was not within the last 50 bars. The occurrence is represented by the instance value of 2. MRO returns a value of -1 if the specified test condition did not evaluate to true within the look-back period, so that is why we are checking if MRO == -1:​

              Code:
              if (CrossAbove(SMA1,SMA2, 2) && MRO(() => CrossAbove(SMA1,SMA2, 2), 2, 50) == -1)
              {
              // enter action(s) here
              }​​
              Please let us know if we may be of further assistance.

              Comment


                #8
                Originally posted by Tasker-182 View Post
                Your code as written would be excluding the condition CurrentBar - K because in the previous if statement you are setting K to be the same as the CurrentBar so the 2nd if statement will never be true.

                Remove the first if statement and then add the K = CurrentBar in your 2nd if statement so your code looks like:

                if ((CrossAbove(SMA1, SMA2, 2) && CurrentBar - K > 50)
                {

                your other coding...
                K = CurrentBar; //save the bar number where the cross occured AFTER the cross.
                ​}

                When your code processes on the historical data, on the first cross K would be zero so the CurrentBar - K > 50 will be true. Then K is set to the CurrentBar of the first cross and ​will remain that way until the next cross that is > 50 bars away
                thanks tasker,

                ur correction worked... thanks a ton..

                Comment


                  #9
                  Originally posted by NinjaTrader_Emily View Post
                  Hello cheryn,

                  Thank you for your patience.

                  Hopefully Tasker-182 has assisted with the approach that uses the saved bar index in a variable. Otherwise, you could use MRO in the following way. The condition checks if SMA1 has crossed above SMA2 && if the second most recent occurrence (since CurrentBar would be the first most recent occurrence) was not within the last 50 bars. The occurrence is represented by the instance value of 2. MRO returns a value of -1 if the specified test condition did not evaluate to true within the look-back period, so that is why we are checking if MRO == -1:​

                  Code:
                  if (CrossAbove(SMA1,SMA2, 2) && MRO(() => CrossAbove(SMA1,SMA2, 2), 2, 50) == -1)
                  {
                  // enter action(s) here
                  }​​
                  Please let us know if we may be of further assistance.
                  Thank you emily ur code also working fine. awesome thank you vvery much

                  though there is a small issue of my results vary between tasker code and ur code. but great now i am having two codes to tinker with. thank you very much emily and tasker. thanks a ton.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by NullPointStrategies, Yesterday, 05:17 AM
                  0 responses
                  62 views
                  0 likes
                  Last Post NullPointStrategies  
                  Started by argusthome, 03-08-2026, 10:06 AM
                  0 responses
                  134 views
                  0 likes
                  Last Post argusthome  
                  Started by NabilKhattabi, 03-06-2026, 11:18 AM
                  0 responses
                  75 views
                  0 likes
                  Last Post NabilKhattabi  
                  Started by Deep42, 03-06-2026, 12:28 AM
                  0 responses
                  45 views
                  0 likes
                  Last Post Deep42
                  by Deep42
                   
                  Started by TheRealMorford, 03-05-2026, 06:15 PM
                  0 responses
                  50 views
                  0 likes
                  Last Post TheRealMorford  
                  Working...
                  X