//+------------------------------------------------------------------+
//|                                                STDEV_EMA_MTF.mq4 |
//|                                                          Kalenzo |
//|                                      bartlomiej.gorski@gmail.com |
//+------------------------------------------------------------------+
#property copyright "Kalenzo"
#property link      "bartlomiej.gorski@gmail.com"
#property indicator_color1 MediumSeaGreen
#property indicator_color2 Blue
#property indicator_buffers 2

extern int STDevPeriod = 20;
extern int STDevMethod = MODE_SMA;
extern int MaType = MODE_EMA;
extern int MaPeriod = 50;
extern int TimeFrame = 60;

double stdev[],
       ma[],
       stdevTF[],
       maTF[];
       
#property indicator_separate_window
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   IndicatorBuffers(4);
   
   SetIndexBuffer(0,stdev);
   SetIndexBuffer(1,ma);
   
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2);
   SetIndexStyle(1,DRAW_LINE,STYLE_DOT,1);
   
   SetIndexBuffer(2,stdevTF);
   SetIndexBuffer(3,maTF);
   
   IndicatorShortName("MTF STDDEV "+tf());
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   
//----
     int bbshift,limit,limitTF;
     limit=Bars;
     limitTF = iBars(Symbol(),TimeFrame);
     
     for(int i=0; i<limitTF; i++) stdevTF[i] = iStdDev(Symbol(),TimeFrame, STDevPeriod, 0, STDevMethod, PRICE_CLOSE, i); 
    
     for(int j=0; j<limitTF; j++) maTF[j] = iMAOnArray(stdevTF,0,MaPeriod,0,MaType,j);
     
     for(int t=0; t<limit; t++)
     {
         bbshift = iBarShift(Symbol(),TimeFrame,Time[t]);
         stdev[t] = stdevTF[bbshift];
         ma[t] = maTF[bbshift];
     }

//----
   return(0);
  }
//+------------------------------------------------------------------+

string tf()
{
   switch(TimeFrame)
   {
      case PERIOD_M1: return("M(1)");
      case PERIOD_M5: return("M(5)");
      case PERIOD_M15: return("M(15)");
      case PERIOD_M30: return("M(30)");
      case PERIOD_H1: return("H(1)");
      case PERIOD_H4: return("H(4)");
      case PERIOD_D1: return("D(1)");
      case PERIOD_W1: return("W(1)");
      case PERIOD_MN1: return("MN(1)");
      default:return("Unknown timeframe");
   }
}