Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

HighestHigh/LowestLow of every N bars

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

    HighestHigh/LowestLow of every N bars

    Hi,

    (1) The following codes need DateTime function :

    startDateTime01 = new DateTime(Time[0].Year, Time[0].Month, Time[0].Day, 8, 00, 0);
    endtDateTime01 = new DateTime(Time[0].Year, Time[0].Month, Time[0].Day, 9, 00, 0);

    int startBarsAgo01 = GetBar(startDateTime01);
    int endBarsAgo01 = GetBar( endDateTime01);

    hi01.Set (MAX(High, startBarsAgo01 - endBarsAgo01)[endBarsAgo01]);
    lo01.Set (MIN( Low, startBarsAgo01 - endBarsAgo01)[endBarsAgo01]);

    (2) The following codes get the last n bars HighestHigh/LowestLow :

    double HH = MAX(High, 200)[0];
    double LL =MIN(Low,200)[0];

    (3)

    int bar = Bars.BarsSinceSession;

    What if I'd like to obtain the HighestHigh/LowestLow from bar=0 to bar=200 and bar=201 to bar=400 etc in a rolling basis. How should I define the startBarsAgo and endBarsAgo ?
    Your advice is much appreciated.

    #2
    Hello denhu fazu,

    It takes the Highest High of last n bars.
    double HH = MAX(High, 200)[0];

    This moves forward on a rolling basis, but bar to bar (not every 200 bars).
    Bar 199: It looks at highest high from bar 0 to 199
    Bar 200: It looks at highest high from bar 1 to 200
    Bar 201: It looks at highest high from bar 2 to 201

    I see the problem you would like to solve and there are likely many ways of doing this. Following is untested but should work to reset the MAX() calculation based on a CurrentBar index. You need to declare all variables used here in variables region.

    You can use GetBar() function to capture the bars ago at a specific DateTime, and plug this into your calculations.

    Code:
    #region Variables
    private double HH;
    private DateTime myDateTime1;
    private int barsAgo1;
    #endregion
    
    OnBarUpdate:
    
    
    if (CurrentBar < 200)
        HH = MAX(High, 200)[0];
    
    if (CurrentBar == 200)
    {
         myDateTime1 = Time[0];
    }
    
    if (CurrentBar >= 200 && CurrentBar < 400)
    {
         barsAgo1 = GetBar(myDateTime1);
         HH = MAX(High, barsAgo1 + 1)[0];	
    }
    Last edited by NinjaTrader_RyanM1; 04-07-2011, 02:04 PM.
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Code:
      if (CurrentBar >= 200 && CurrentBar < 400)
      Looks as if it will stop updating unconditionally after 400 bars. No?

      Comment


        #4
        Looks as if it will stop updating unconditionally after 400 bars. No?
        Yes, it's not a complete solution - just getting the idea rolling. Will have to create similar code for each 200 bar section. Of course, there may be better ways of implementing this.
        Ryan M.NinjaTrader Customer Service

        Comment


          #5
          The simplest way might be to use a start and offset.


          Code:
          int StartBarsAgo1 = 200;
          int OffsetBars = 200;
          
          int EndBarsAgo1 = StartBarsAgo - OffsetBars + 1;
          int StartBarsAgo2 = StartBarsAgo1 + OffsetBars;
          int EndBarsAgo2 = StartBarsAgo + 1;
          That would make your ranges:

          1 - 200'
          201 - 400;

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          656 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          371 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
          579 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X