//+------------------------------------------------------------------+
//|                                               STM_oscillator.mq4 |
//|                                                         Zen_Leow |
//+------------------------------------------------------------------+

//---- indicator settings
#property  indicator_separate_window
#property  indicator_buffers 2
#property  indicator_color1  Green
#property  indicator_color2  Red
//---- indicator parameters
extern int Fast_Period=2;
extern int Slow_Period=9;
extern int SmoothedMA_Period=15;
//---- indicator buffers
double     STM_Buffer[];
double     SmoothedMA_Buffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- drawing settings
   SetIndexStyle(0,DRAW_LINE);
   SetIndexDrawBegin(0,Slow_Period);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexDrawBegin(1,SmoothedMA_Period);
   IndicatorDigits(Digits+1);
//---- indicator buffers mapping
   SetIndexBuffer(0,STM_Buffer);
   SetIndexBuffer(1,SmoothedMA_Buffer);
//---- name for DataWindow and indicator subwindow label
   IndicatorShortName("STM_oscillator("+Fast_Period+","+Slow_Period+","+SmoothedMA_Period+")");
   SetIndexLabel(0,"STM");
   SetIndexLabel(1,"Smoothed_MA");
//---- initialization done
   return(0);
  }
//+------------------------------------------------------------------+
//| Moving Averages Convergence/Divergence                           |
//+------------------------------------------------------------------+
int start()
  {
   int limit;
   int counted_bars=IndicatorCounted();
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
//---- macd counted in the 1-st buffer
   for(int i=0; i<limit; i++)
      STM_Buffer[i]=iMA(NULL,0,Fast_Period,0,MODE_SMA,PRICE_CLOSE,i)-iMA(NULL,0,Slow_Period,0,MODE_SMA,PRICE_CLOSE,i);
//---- smoothed MA line counted in the 2-nd buffer
   for(i=0; i<limit; i++)
      SmoothedMA_Buffer[i]=iMAOnArray(STM_Buffer,Bars,SmoothedMA_Period,0,MODE_SMA,i);
//---- done
   return(0);
  }
//+------------------------------------------------------------------+