Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to find the previous 20 day high

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

    How to find the previous 20 day high

    I know that I can use MAX(High, 20) [0] to find the current 20 day high.

    How to find the previous 20 day high, which could occur several bars ago?

    I tried to use MAX(High, 20)[1] but it does not seem to work.

    What I'm trying to do is: whenever a bar closes above a 20 day high, I will draw an up arrow, and vice versa.

    I'd appreciate it very much if you could provide some suggestions?

    Please let me know, if you need more information.

    Thank you.

    Richard
    Last edited by Richard168; 02-27-2013, 08:49 PM.

    #2
    What I'm trying to do is: whenever a bar closes above a 20 day high, I will draw an up arrow, and vice versa.
    why do you need to know the previous 20day high ? The current 20day high is all you need.

    The following code should do the trick

    Code:
     
    if ( Close[0] > MAX(High, 20) [0] )
    DrawArrowUp("tag1"+CurrentBar, true, 0, High[0] + 5*TickSize, Color.Green);

    Marco

    Comment


      #3
      You would need to reference the 1 bar back for the MAX call, as otherwise the condition would never trigger.

      Why this did not work on Richard's end is likely the result of a missing check for CurrentBar at his OnBarUpdate() start:

      Code:
      if (CurrentBar < 1) return;
      			
      if (Close[0] > MAX(High, 20)[1])
          DrawArrowUp("tag1"+CurrentBar, true, 0, High[0] + 5*TickSize, Color.Green);

      Comment


        #4
        Hi Bertrand,

        Thank you very much for your suggestion of referencing the 1 bar back for the MAX call.

        Close[0] > MAX(High, 20)[1] worked!

        Richard

        Comment


          #5
          Originally posted by NinjaTrader_Bertrand View Post
          You would need to reference the 1 bar back for the MAX call, as otherwise the condition would never trigger.

          Why this did not work on Richard's end is likely the result of a missing check for CurrentBar at his OnBarUpdate() start:

          Code:
          if (CurrentBar < 1) return;
           
          if (Close[0] > MAX(High, 20)[1])
              DrawArrowUp("tag1"+CurrentBar, true, 0, High[0] + 5*TickSize, Color.Green);
          Bertrand, the NT help file says:
          Code:
           
          // Prints the highest high value over the last 20 periods
          double value = MAX(High, 20)[0];
          Print("The current MAX value is " + value.ToString());
          so the MAX[0] should give the High over the last 20 bars please explain.
          And because each new Arrow has its unique tag ("tag1"+CurrentBar), the "old" arrows will still be visible, hence my comment that you don't need the previous 20day high to do this

          Comment


            #6
            Right, it would get the 20 bar High, but the condition can never trigger if you don't compare against this value one bar ago, as the current bar high would be included in the return.

            Comment


              #7
              thanks Bertrand,

              of course ! Close[0] can never be greater than the current 20 bar High, kind of a circular reference here...

              Comment


                #8
                lets say i want to find three consecutive highs, in this case i access a 2nd dataseries (daily data)

                Code:
                if(Closes[1][0] > MAX(Closes[1], 200)[1]
                	&& Closes[1][1]  > MAX(Closes[1], 200)[2]
                	&& Closes[1][2] > MAX(Closes[1], 200)[3]
                		)
                {}{return;}
                but i always get zero trades.

                Comment


                  #9
                  Hello markus1000,

                  Thank you for your post.

                  Try using a Print() statement after this condition to see if it returns true.
                  Code:
                  if(Closes[1][0] > MAX(Closes[1], 200)[1]
                  	&& Closes[1][1]  > MAX(Closes[1], 200)[2]
                  	&& Closes[1][2] > MAX(Closes[1], 200)[3])
                  {
                  Print("Three consecutive days are greater than the last 200 days.");
                  }
                  This will output to the Output window (Tools > Output Window) if it returns true.

                  Please let me know if you have any questions.

                  Comment


                    #10
                    its printing several times, but the backtest is done way quicker than normal. not sure why it is stopping earlier...

                    No log file messages after starting the backtest

                    if i remove that code everything runs as usual.

                    Comment


                      #11
                      Originally posted by markus1000 View Post
                      its printing several times, but the backtest is done way quicker than normal. not sure why it is stopping earlier...

                      No log file messages after starting the backtest

                      if i remove that code everything runs as usual.
                      What and where is your CurrentBars check statement?

                      Comment


                        #12
                        nope, but i adjusted the minimum bars needed n the backtest window to 204 i, the question is if the minbar and the currentbar statement do only count for the primary dataseries or for all

                        Comment


                          #13
                          Originally posted by markus1000 View Post
                          nope, but i adjusted the minimum bars needed n the backtest window to 204 i, the question is if the minbar and the currentbar statement do only count for the primary dataseries or for all
                          I repeat the question: what is your CurrentBars check statement, and where have you located it? If we do not know that information, we cannot tell you why it may not be working correctly.
                          Last edited by koganam; 05-10-2013, 11:41 AM.

                          Comment


                            #14
                            i dont have a currentbar statement

                            Comment


                              #15
                              Originally posted by markus1000 View Post
                              i dont have a currentbar statement
                              If you are not making a CurrentBars check, that may be your entire problem.

                              ref: http://www.ninjatrader.com/support/h...urrentbars.htm

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              574 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              333 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              101 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              553 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              551 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X