//+------------------------------------------------------------------+
//|                                                         OsMA.mq4 |
//|                   Copyright 2005-2014, MetaQuotes Software Corp. |
//|                                              http://www.mql4.com |
//+------------------------------------------------------------------+
#property copyright   "2005-2014, MetaQuotes Software Corp."
#property link        "http://www.mql4.com"
#property description "Moving Averages of Oscillator"
#property strict

//--- indicator settings
#property  indicator_separate_window
#property  indicator_buffers 3
#property  indicator_color1  DarkGray
#property  indicator_width1  1
#property  indicator_color2  Red
#property  indicator_width2  2
#property  indicator_color3  Orange
#property  indicator_width3  2
//--- indicator parameters
input int InpFastEMA=12;   // Fast EMA Period
input int InpSlowEMA=26;   // Slow EMA Period
input int InpSignalSMA=9;  // Signal SMA Period
input int InpLWMA01=8;     // LWMA01
input int InpLWMA02=38;    // LWMA02
//--- indicator buffers
double ExtOsmaBuffer[];
double ExtMacdBuffer[];
double ExtSignalBuffer[];
double ExtLWMA01Buffer[];
double ExtLWMA02Buffer[];
//--- right input parameters flag
bool   ExtParameters=false;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit(void)
  {
//--- 2 additional buffers are used for counting.
   IndicatorBuffers(5);
//--- drawing settings
   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexDrawBegin(0,InpSignalSMA);
   IndicatorDigits(Digits+2);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexDrawBegin(1,InpLWMA01);
   SetIndexStyle(2,DRAW_LINE);
   SetIndexDrawBegin(2,InpLWMA02);
//--- 3 indicator buffers mapping
   SetIndexBuffer(0,ExtOsmaBuffer);
   SetIndexBuffer(1,ExtLWMA01Buffer);
   SetIndexBuffer(2,ExtLWMA02Buffer);
   SetIndexBuffer(3,ExtMacdBuffer);
   SetIndexBuffer(4,ExtSignalBuffer);
//--- name for DataWindow and indicator subwindow label
   IndicatorShortName("OsMA with 2 LWMAs("+IntegerToString(InpFastEMA)+","+IntegerToString(InpSlowEMA)+","+IntegerToString(InpSignalSMA)+"|LWMA01 "+IntegerToString(InpLWMA01)+"|LWMA02 "+IntegerToString(InpLWMA02)+")");
   SetIndexLabel(0,"OsMA with 2 LWMAs");
   SetIndexLabel(1,"LWMA01");
   SetIndexLabel(2,"LWMA02");
//--- check for input parameters
   if(InpFastEMA<=1 || InpSlowEMA<=1 || InpSignalSMA<=1 || InpFastEMA>=InpSlowEMA)
     {
      Print("Wrong input parameters");
      ExtParameters=false;
      return(INIT_FAILED);
     }
   else
      ExtParameters=true;
//--- initialization done
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Moving Average of Oscillator                                     |
//+------------------------------------------------------------------+
int OnCalculate (const int rates_total,
                 const int prev_calculated,
                 const datetime& time[],
                 const double& open[],
                 const double& high[],
                 const double& low[],
                 const double& close[],
                 const long& tick_volume[],
                 const long& volume[],
                 const int& spread[])
  {
   int i,limit=fmin(rates_total-prev_calculated+1,rates_total-1);
//---
//--- macd counted in the 4-th buffer
   for(i=limit;i>=0 && !_StopFlag; i--)
      ExtMacdBuffer[i]=iMA(NULL,0,InpFastEMA,0,MODE_EMA,PRICE_CLOSE,i)-
                       iMA(NULL,0,InpSlowEMA,0,MODE_EMA,PRICE_CLOSE,i);
//--- signal line counted in the 5-th buffer
   for(i=limit;i>=0 && !_StopFlag; i--) 
   {
      ExtSignalBuffer[i] = iMAOnArray(ExtMacdBuffer,0,InpSignalSMA,0,MODE_SMA,i);   
      ExtOsmaBuffer[i]   = ExtMacdBuffer[i]-ExtSignalBuffer[i];
   }
   for(i=limit;i>=0 && !_StopFlag; i--) ExtLWMA01Buffer[i]  = iMAOnArray(ExtOsmaBuffer,0,InpLWMA01,0,MODE_LWMA,i);   
//--- LWMA02 line counted in the 3-rd buffer   
   for(i=limit;i>=0 && !_StopFlag; i--) ExtLWMA02Buffer[i]  = iMAOnArray(ExtOsmaBuffer,0,InpLWMA02,0,MODE_LWMA,i);   
//--- done
   return(0);
  }
//+------------------------------------------------------------------+
