//+------------------------------------------------------------------+
//|                                                   LeManTrend.mq4 |
//|                                         Copyright © 2009, LeMan. |
//|                                                 b-market@mail.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, LeMan."
#property link      "b-market@mail.ru"
//----
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Green
#property indicator_color2 Red
//---- Âõîäíûå ïàðàìåòðû
extern ENUM_TIMEFRAMES  TimeFrame = PERIOD_CURRENT;
extern int     Min       = 13;
extern int     Midle     = 21;
extern int     Max       = 34;
extern int     PeriodEMA = 3;
//---- Áóôåðû
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double TempBuffer1[];
double TempBuffer2[];

string indicatorFileName;
bool   returnBars;

//+------------------------------------------------------------------+
int init() {
//----
   IndicatorDigits(Digits);
   IndicatorBuffers(4);
   
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexBuffer(1,ExtMapBuffer2);
   SetIndexBuffer(2,TempBuffer1);                              
   SetIndexBuffer(3,TempBuffer2);                              

   SetIndexStyle(0,DRAW_LINE);
   SetIndexStyle(1,DRAW_LINE); 
   
   SetIndexDrawBegin(0,Max);           
   SetIndexDrawBegin(1,Max);
   
   indicatorFileName = WindowExpertName();
   returnBars        = TimeFrame == -99;
   TimeFrame         = MathMax(TimeFrame,_Period);           
   IndicatorShortName(timeFrameToString(TimeFrame)+" LeManTrend");
   return(0);
}
//+------------------------------------------------------------------+
int deinit() {
//----  
   
//----
   return(0);
}
//+------------------------------------------------------------------+
int start() 
{
   int counted_bars=IndicatorCounted();
      if(counted_bars<0) return(-1);
      if(counted_bars>0) counted_bars--;
           int limit=MathMin(Bars-counted_bars,Bars-1);
           if (returnBars) { ExtMapBuffer1[0] = limit+1; return(0); }
   
            if (TimeFrame!=Period())
            {
               limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,TimeFrame,indicatorFileName,-99,0,0)*TimeFrame/Period()));
               for (int i=limit; i>=0; i--)
               {
                  int y = iBarShift(NULL,TimeFrame,Time[i]);              
                     ExtMapBuffer1[i] = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,Min,Midle,Max,PeriodEMA,0,y);
                     ExtMapBuffer2[i] = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,Min,Midle,Max,PeriodEMA,1,y);
               }
               return(0);
            }
//----
   if (Bars <= Max) {
      return(0);
   }
//---- initial zero
   if (counted_bars < 1) {
      for(i=1; i<=Max; i++) {
         ExtMapBuffer1[Bars-i] = 0.0;
         ExtMapBuffer2[Bars-i] = 0.0;
      }
   }
//----
   i = Bars-counted_bars-1;
//----
   while(i >= 0) {
      double High1 = iHigh(NULL, 0, iHighest(NULL, 0, MODE_HIGH, Min,i+1));
      double High2 = iHigh(NULL, 0, iHighest(NULL, 0, MODE_HIGH, Midle,i+1));
      double High3 = iHigh(NULL, 0, iHighest(NULL, 0, MODE_HIGH, Max,i+1));    
      TempBuffer1[i] = ((High[i]-High1)+(High[i]-High2)+(High[i]-High3));
      double Low1 = iLow(NULL, 0, iLowest(NULL, 0, MODE_LOW, Min, i+1));
      double Low2 = iLow(NULL, 0, iLowest(NULL, 0, MODE_LOW, Midle, i+1));
      double Low3 = iLow(NULL, 0, iLowest(NULL, 0, MODE_LOW, Max, i+1));    
      TempBuffer2[i] = ((Low1-Low[i])+(Low2-Low[i])+(Low3-Low[i]));
      i--;
   } 
   if (counted_bars > 0) {
      counted_bars--;
   }  

   for(i=0; i<limit; i++) {
      if (PeriodEMA > 0 ) {
         ExtMapBuffer1[i] = iMAOnArray(TempBuffer1,Bars,PeriodEMA,0,MODE_EMA,i);
         ExtMapBuffer2[i] = iMAOnArray(TempBuffer2,Bars,PeriodEMA,0,MODE_EMA,i);
      } else {
         ExtMapBuffer1[i] = TempBuffer1[i];
         ExtMapBuffer2[i] = TempBuffer2[i];      
      }
   }
//----     
   return(0);
  }

//-------------------------------------------------------------------
//
//-------------------------------------------------------------------
//
//
//
//
//

string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
int    iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};

string timeFrameToString(int tf)
{
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
         if (tf==iTfTable[i]) return(sTfTable[i]);
                              return("");
}