ATR Range calculated

 

Hello,

First of all, I would like to mention that I am not an expert in mql4 and I would appreciate very much any help about this issue that I am facing.  I have spent several hours trying to solve the problem and I couldn't.

The problem:

I am trying to build an indicator that calculates the maximum and minimum of the ATR indicator of the last 100 bars.  That means from bar number 1 (index 0) to bar 100 (index 99), I would like to have the maximum and minimum values of that 100 bars range. Then the maximum and minimum of bars 2 to 101, Then from bar 3 to bar 102, and so on. Therefore every new bar finished, a new calculation will be done.

As you can see on the picture below, the ATR indicator is draw fine on the chart from the ATRBuffer[] which contains the values from the iATR.  My problem is related to the second loop.  How can I get the max and min of the ATR range of 100 bars.

Thank you very much in advance.


#property copyright "Copyright 2015"
#property link      "http://www.learnmql4.com"
#property version   "1.00"
#property strict
#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 clrBlue
#property indicator_color2 clrRed
#property indicator_color3 clrYellow
//#property indicator_width1 2
#property indicator_width2 2
#property indicator_width3 2
#property indicator_levelstyle STYLE_DASH
#property indicator_levelcolor clrRed
//#property indicator_minimum 0
//#property indicator_maximum 100

extern int ATRPeriod =8;//ATR Period
extern int LookBack =100;//LookBack Period

double ATRBuffer[];
double UpLimit[];
double DownLimit[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+

int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   int limit,i;
   int indexmax = 0;
   int indexmin = 0;
   limit=rates_total-prev_calculated;
   if(!prev_calculated)
      limit--;

   for(i=0; i<=limit; i++)
     {  
      ATRBuffer[i]=iATR(NULL,0,ATRPeriod,i);
     }
   for(i=0; i<=limit; i++)
      if(i>LookBack)
        {
         Print(ATRBuffer[i],", ",i);

         indexmax=ArrayMaximum(ATRBuffer,LookBack,i-LookBack);
         indexmin=ArrayMaximum(ATRBuffer,LookBack,i-LookBack);
         UpLimit[i]=ATRBuffer[indexmax];
         DownLimit[i]=ATRBuffer[indexmin];
         Print("UP  ",UpLimit[i]);
         Print("DOWN ",DownLimit[i]);

        }

   return(rates_total);
  }







Files:
Capture100.PNG  46 kb
 
mcetraro:

Hello,

First of all, I would like to mention that I am not an expert in mql4 and I would appreciate very much any help about this issue that I am facing.  I have spent several hours trying to solve the problem and I couldn't.

The problem:

I am trying to build an indicator that calculates the maximum and minimum of the ATR indicator of the last 100 bars.  That means from bar number 1 (index 0) to bar 100 (index 99), I would like to have the maximum and minimum values of that 100 bars range. Then the maximum and minimum of bars 2 to 101, Then from bar 3 to bar 102, and so on. Therefore every new bar finished, a new calculation will be done.

As you can see on the picture below, the ATR indicator is draw fine on the chart from the ATRBuffer[] which contains the values from the iATR.  My problem is related to the second loop.  How can I get the max and min of the ATR range of 100 bars.

Thank you very much in advance.

Change this:

   limit=rates_total-prev_calculated;
   if(!prev_calculated)
      limit--;

To:

   limit = MathMin(rates_total-1, rates_total-prev_calculated);

and you'll start to see lines being drawn...

 
Thank you very much Seng.  It works good now.