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

Best Way to do when a Condition meets anytime within the Last x number of bars

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

    Best Way to do when a Condition meets anytime within the Last x number of bars

    Hello,

    Im looking to get feedback on how to best do the following. Strat buikder or code works. Im comfortable with either. I tried the 'bars ago' option in strategy builder but I believe that means only for x number of bars ago, not anytime within the number of bars.

    First condition is met. Take trade if the Second Condition is met anytime in the last 5 bars.

    Thanks.

    #2
    Hello amigatlin,

    Thank you for your post.

    If you are working with a single series script, you could use Least RecentO Occurrence or Most Recent Occurrence. Both of these methods check for a test condition and whether it evaluated to true within a lookback period (which is where you could specify the last 5 bars). The return value is -1 if the condition was not found to be true in the lookback period. For more information:



    Otherwise, please specify which 5 bars you are checking for the second condition. Once the first condition is met, are you check the last 5 bars leading up to when the first condition was met? Or checking for 5 bars that happen after the condition is met?

    Please let me know if I may be of further assistance.
    Emily C.NinjaTrader Customer Service

    Comment


      #3
      Emily, thanks for the response. I’m looking for the following. Do you have an example of the most recent occurrence used in a strategy?

      Once the first condition is met, are you check the last 5 bars leading up to when the first condition was met?
      thanks for the help.

      Comment


        #4
        Hello amigatlin,

        Thank you for your reply.

        There is a code example on the MRO page:
        Code:
        protected override void OnBarUpdate()
        {
          // Prints the high price of the most recent up bar over the last 10 bars (current bar + look back period's 9 bars before that)
          int barsAgo = MRO(() => Close[0] > Open[0], 1, 9);
          if (barsAgo > -1)
              Print("The bar high was " + High[barsAgo]);  
        }​
        I also created a quick example strategy that uses MRO. The first condition in the statement is if (Close[0] > Close[1]) and it also checks for an MRO condition. I added print statements to help demonstrate that MRO will output -1 if the condition did not occur, otherwise it will print a barsAgo value. The example is attached to this post.

        Please let me know if I may be of further assistance.
        Attached Files
        Emily C.NinjaTrader Customer Service

        Comment


          #5
          I was able to print my statement using your example. See code and print statement below. Momentum1 of 1 bar calculating with data series HMA of 5 bars.

          Code:
          if ((MRO(() => Momentum1[0]<= -11.5, 1, 10) != -1))
                      {
                          Print(String.Format("{0} Momentum1[0] less than -11.5 {3} barsAgo", Time[0], Close[0], Close[1], MRO(() => Momentum1[0]<= -11.5, 1, 10)));
                      }​
          Results

          3/19/2023 9:08:44 PM Momentum1[0] less than -11.5 6 barsAgo

          My question is now, How do use that value in my strategy If i wanted to take a long using the MRO output of "6 bars".

          Code:
          If close[0] > close[1] && MRO Number >= 5
          {
          EnterLong(Convert.ToInt32(DefaultQuantity), @"enterLong1");
          }

          I dont know how to exaclty accomplish this. I want the MRO calculation to happen and update its number every bar and i want to use that number along side another condition. Should i create a int and store the MRO bars ago number and use that int in my strategy?

          Thanks for the help​
          Last edited by amigatlin; 03-19-2023, 07:24 PM.

          Comment


            #6
            amigatlin,

            Thank you for your reply.

            In the strategy that I attached, the MRO is used alongside another condition just like you are asking. I previously stated the following:
            I also created a quick example strategy that uses MRO. The first condition in the statement is if (Close[0] > Close[1]) and it also checks for an MRO condition.
            You could modify this portion of code to do the MRO check on your desired condition:
            Code:
            // this MRO should return a barsAgo value because the Close price was greater than 0 within the last 5 bars. the print should appear
            if (Close[0] > Close[1] && (MRO(() => Close[0] > 0, 1, 5) != -1))
            {
            Print(String.Format("{0} Close[0] {1} > Close[1] {2} and Close[0] greater than 0 {3} barsAgo", Time[0], Close[0], Close[1], MRO(() => Close[0] > 0, 1, 5)));
            // enter desired actions here
            }
            The condition could instead be set up as:
            Code:
            if (Close[0] > Close[1] && (MRO(() => Momentum1[0]<= -11.5, 1, 10) != -1))
            {
            EnterLong(Convert.ToInt32(DefaultQuantity), @"enterLong1");
            }
            If you'd prefer to save the MRO output to a variable, then you could do what is shown in the help guide snippet I previously shared:
            Code:
            protected override void OnBarUpdate()
            {
            // Prints the high price of the most recent up bar over the last 10 bars (current bar + look back period's 9 bars before that)
            int barsAgo = MRO(() => Close[0] > Open[0], 1, 9);
            if (barsAgo > -1)
            Print("The bar high was " + High[barsAgo]);
            }​​
            ​​

            With your Momentum condition, it could look like this:
            Code:
            int barsAgo = (MRO(() => Momentum1[0]<= -11.5, 1, 10) != -1);
            if (barsAgo > -1 && Close[0] > Close[1])
            {
            EnterLong(Convert.ToInt32(DefaultQuantity), @"enterLong1");
            }​
            Feel free to try out these different options and add Print() statements to your script to help understand the values being compared in your conditons as well as how the conditions are being evaluated. For more information about using prints to debug and better understand your script's behavior:


            Please let me know if I may be of further assistance.
            Emily C.NinjaTrader Customer Service

            Comment


              #7
              Sorry for late reply but i was able to code it and have it work. Thanks.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Jonafare, 12-06-2012, 03:48 PM
              5 responses
              3,986 views
              0 likes
              Last Post rene69851  
              Started by Fitspressorest, Today, 01:38 PM
              0 responses
              2 views
              0 likes
              Last Post Fitspressorest  
              Started by Jonker, Today, 01:19 PM
              0 responses
              2 views
              0 likes
              Last Post Jonker
              by Jonker
               
              Started by futtrader, Today, 01:16 PM
              0 responses
              9 views
              0 likes
              Last Post futtrader  
              Started by Segwin, 05-07-2018, 02:15 PM
              14 responses
              1,792 views
              0 likes
              Last Post aligator  
              Working...
              X