//+------------------------------------------------------------------+
//|                                                     Trending.mq4 |
//|                                                       Matheszabi |
//|                                         https://www.mathesoft.ro |
//+------------------------------------------------------------------+
#property copyright "Matheszabi"
#property link      "https://www.mathesoft.ro"
#property version   "1.00"
#property strict
//-- indicator settings:
#property indicator_chart_window
#property  indicator_buffers 2
#property  indicator_color1  DeepPink
#property  indicator_color2  Yellow
//-- buffers:

double  ExtBuffer1[];
double  ExtBuffer2[];
//-- inoput:
extern int PERIOD = 40;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping   
   SetIndexStyle(0,DRAW_LINE);   
   SetIndexStyle(1,DRAW_LINE); 
   SetIndexDrawBegin(0,PERIOD);
   SetIndexDrawBegin(1,PERIOD);   
   SetIndexBuffer(0,ExtBuffer1);
   SetIndexBuffer(1,ExtBuffer2);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration 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 i;
   int limit = rates_total - prev_calculated;
   //--- check for rates total:   
   if(rates_total <= PERIOD)
      return(0);
   //--- last counted bar will be recounted
   if(prev_calculated>0)
   {
      limit++;
   }
    
   for(i=limit-1; i>=0; i--){
       ExtBuffer1[i] = High[iHighest( Symbol(), Period(), MODE_HIGH, PERIOD, i )];
       ExtBuffer2[i] = Low[ iLowest( Symbol(), Period(), MODE_LOW, PERIOD, i )];
   }     
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
