//+------------------------------------------------------------------+
//|                                                    StdDev-MA.mq4 |
//|                                       Copyright © 2010, Peter Wu |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, Peter Wu"
#property link      ""

#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 Tan
#property indicator_color2 Yellow
#property indicator_color3 Red

extern bool AutoPeriod = false;
extern int StdDevPeriod = 7;
extern int fMAperiod = 7;   // the default settings were designed for M15 time frame
extern int sMAperiod = 49;  // setting will be adjusted in init() if AutoPeriod == true
extern int MAmode = 2;      // 0=sma; 1=ema; 2=smoothed; 3=lwma
//---- buffers
double sBuffer[];
double fMABuffer[];
double sMABuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//----
   if(AutoPeriod) {
      if(Period() >= 60) {fMAperiod = 3; sMAperiod = 13;}
      if(Period() == 30) {fMAperiod = 5; sMAperiod = 21;}
      if(Period() == 15) {fMAperiod = 7; sMAperiod = 49;}
      if(Period() == 5) {fMAperiod = 21; sMAperiod = 147;}
      if(Period() == 1) {fMAperiod = 89; sMAperiod = 297;}
   }
//---- name for DataWindow and indicator subwindow label
   string short_name = "StdDev-MA(" + fMAperiod + "," + sMAperiod + ")";
//---- buffers settings
// Do not need to set indicator buffers here unless they on non-display buffers
//   IndicatorBuffers(3);
// This might cause a problem because you limit the indicator to integer values
//   IndicatorDigits(0);
//---- indicator line
   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(0,sBuffer);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,fMABuffer);
   SetIndexStyle(2,DRAW_LINE);
   SetIndexBuffer(2,sMABuffer);
   IndicatorShortName(short_name);
   SetIndexLabel(0,short_name);
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
   int i,counted_bars=IndicatorCounted();
//----
   if(Bars<=Period()) return(0);
//----
   int limit=Bars-counted_bars;
   if(counted_bars>0) limit++;
   for(i=0; i<limit; i++)
      sBuffer[i]= iStdDev(NULL, 0, 7, 0, MODE_SMA, PRICE_CLOSE, i);
// params were reversed
//    sBuffer[i]= iStdDev(NULL, 0, 7, MODE_SMA, 0, PRICE_CLOSE, i);

//----
// I find looping likew in MACD custom indicator works OK
//   i=Bars-counted_bars-1;
//   while(i>=0)
   for(i=0; i<limit; i++)
     {
      fMABuffer[i]=iMAOnArray(sBuffer,Bars,fMAperiod,0,MAmode,i);
      sMABuffer[i]=iMAOnArray(sBuffer,Bars,sMAperiod,0,MAmode,i);
//      i--;
     }
//----
   return(0);
  }
//+------------------------------------------------------------------+