//+------------------------------------------------------------------+
//|                                               #MTF_Envelopes.mq4 |
//|                                   Copyright © 2009, Serega Lykov |
//|                                       http://mtexperts.narod.ru/ |
//+------------------------------------------------------------------+

#property copyright "Copyright © 2009, Serega Lykov"
#property link      "http://mtexperts.narod.ru/"

//---- property of indicator ----------------------------------------+
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Blue
#property indicator_color2 Red

//---- external parameters ------------------------------------------+
extern int    TimeFrame           = 240;    // 0 = CurrentChart, 1 = M1, 5 = M5, 15 = M15, 30 = M30,
                                          // 60 = H1, 240 = H4, 1440 = D1, 10080 = W1, 43200 = MN1
extern int    Envelopes_Period    = 6;   // the period
extern int    Envelopes_Method    = 2;    // the method: 0 = Simple, 1 = Exponential, 2 = Smoothed, 3 = Weighted
extern int    Envelopes_Price     = 1;    // the price: 0 = Close, 1 = Open, 2 = High, 3 = Low, 4 = Median, 5 = Typical, 6 = Weighted
extern int    Envelopes_Shift     = 0;    // the shift
extern double Envelopes_Deviation = 0.05;  // the deviation (in percents)

//---- buffers ------------------------------------------------------+
static double EnvUpper[];
static double EnvLower[];

//-------------------------------------------------------------------+
//---- initialization of indicator ----------------------------------+
//-------------------------------------------------------------------+
int init()
  {
   //---- set a "short" name of the indicator -----------------------+
   IndicatorShortName(StringConcatenate("Envelopes(",TimeFrameToStr(TimeFrame),")"));
   //---- set a style for line --------------------------------------+
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,5);
   SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,5);
   //---- set a arrays for line -------------------------------------+
   SetIndexBuffer(0,EnvUpper);
   SetIndexBuffer(1,EnvLower);
   //---- set a names for lines -------------------------------------+
   SetIndexLabel(0,"EnvUpper");
   SetIndexLabel(1,"EnvLower");
   //---- set a first bar for drawing the line ----------------------+
   SetIndexDrawBegin(0,Envelopes_Period);
   SetIndexDrawBegin(1,Envelopes_Period);
   //---- finish of initialization ----------------------------------+
   return(0);
  }
 
//-------------------------------------------------------------------+
//---- deinitialization of indicator --------------------------------+
//-------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }

//-------------------------------------------------------------------+
//---- #MTF_Envelopes -----------------------------------------------+
//-------------------------------------------------------------------+
int start()
  {
   //---- amount not changed bars after last call of the indicator --+
   int counted_bars = IndicatorCounted();
   if(counted_bars < 0) return(-1);
   //---- last counted bar will be counted --------------------------+
   if(counted_bars > 0) counted_bars--;
   int limit = Bars - counted_bars;
   for(int i=limit; i>=0; i--)
     {
      int bar_shift = iBarShift(NULL,TimeFrame,Time[i]);
      EnvUpper[i] = iEnvelopes(NULL,TimeFrame,Envelopes_Period,Envelopes_Method,Envelopes_Shift,Envelopes_Price,Envelopes_Deviation,MODE_UPPER,bar_shift);
      EnvLower[i] = iEnvelopes(NULL,TimeFrame,Envelopes_Period,Envelopes_Method,Envelopes_Shift,Envelopes_Price,Envelopes_Deviation,MODE_LOWER,bar_shift);
     }
   return(0);
  }
  
//-------------------------------------------------------------------+
//---- TimeFrameToStr -----------------------------------------------+
//-------------------------------------------------------------------+
string TimeFrameToStr(int period)
  {
   switch(period)
     {
      case     1: string TimeFrameStr = "M1";          break;
      case     5:        TimeFrameStr = "M5";          break;
      case    15:        TimeFrameStr = "M15";         break;
      case    30:        TimeFrameStr = "M30";         break;
      case    60:        TimeFrameStr = "H1";          break;
      case   240:        TimeFrameStr = "H4";          break;
      case  1440:        TimeFrameStr = "D1";          break;
      case 10080:        TimeFrameStr = "W1";          break;
      case 43200:        TimeFrameStr = "MN1";         break;
      default:           TimeFrameStr = TimeFrameToStr(Period());
     }
   return(TimeFrameStr);
  }

//+------------------------------------------------------------------+