//+------------------------------------------------------------------+
//|                                               STM_oscillator.mq4 |
//|                                                         Zen_Leow |
//+------------------------------------------------------------------+

//---- indicator settings
#property  indicator_separate_window
#property  indicator_buffers 3
#property  indicator_color1  LightSlateGray
#property  indicator_color2  Red
#property  indicator_color3  Green
//---- 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[];
double     SmoothedMA_Buffer_Down[];
double     SmoothedMA_Buffer_Up[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- drawing settings
   SetIndexStyle(0,DRAW_LINE);
   SetIndexDrawBegin(0,Slow_Period);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexDrawBegin(1,SmoothedMA_Period);
   SetIndexStyle(2,DRAW_LINE);
   SetIndexDrawBegin(2,SmoothedMA_Period);
   
   IndicatorDigits(Digits+1);
//---- indicator buffers mapping
   SetIndexBuffer(0,STM_Buffer);
   SetIndexBuffer(1,SmoothedMA_Buffer_Down);
   SetIndexBuffer(2,SmoothedMA_Buffer_Up);
   
//---- 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_Down[i] = iMAOnArray(STM_Buffer,Bars,SmoothedMA_Period,0,MODE_SMA,i);
      SmoothedMA_Buffer_Up[i] = iMAOnArray(STM_Buffer,Bars,SmoothedMA_Period,0,MODE_SMA,i);
      if (SmoothedMA_Buffer_Up[i]>SmoothedMA_Buffer_Up[i-1])
      {
         SmoothedMA_Buffer_Up[i]=EMPTY_VALUE;
      }
      
   }
//---- done
   return(0);
  }
//+------------------------------------------------------------------+