//+------------------------------------------------------------------+ //| Oscillator of MA | //+------------------------------------------------------------------+ #property copyright "2014, Postolache Vitalie" #property link "http://www.mql4.com" #property description "Moving Averages Oscillator" #property strict //---- #property indicator_separate_window #property indicator_buffers 1 #property indicator_color1 clrRed #property indicator_width1 2 #property indicator_level1 0.0 #property indicator_levelcolor clrSilver //---- input string pair="NULL"; //Trading symbol input ENUM_TIMEFRAMES tf=0; //Timeframe input int period1=13; //1st лю period input int period2=24; //2-nd MA period input int shift=0; //2nd MA shift (bars) input ENUM_MA_METHOD mode=MODE_SMA; //Averaging method input ENUM_APPLIED_PRICE price=PRICE_CLOSE; //Applied price //---- double ma[]; //---- int period=0; string par,label,name; //+------------------------------------------------------------------+ //| Indicator init function | //+------------------------------------------------------------------+ int OnInit(void) { if(pair=="NULL") par=Symbol(); period = MathMax(period1,period2); label=StringConcatenate(MQLInfoString(MQL_PROGRAM_NAME)," ,",par); name=StringConcatenate(MQLInfoString(MQL_PROGRAM_NAME)," ,",par,", MA",period1," on MA", period2,", shift ",shift,": "); //---- SetIndexStyle(0,DRAW_LINE); SetIndexBuffer(0,ma); SetIndexDrawBegin(0,period+shift); //---- IndicatorShortName(name); SetIndexLabel(0,label); IndicatorDigits(_Digits); //---- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Main indicator block starts here | //+------------------------------------------------------------------+ 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; //----if not enough bars, exitting if(rates_total-1<=(period+shift)) return(0); //----get number of bars to count if(prev_calculated>1) limit=rates_total-1-prev_calculated; //if some bars counted already, excluding them else { limit=rates_total-1-period-shift; //else - counting all bars //zero all indicator buffers for(i=0; i<=rates_total-1; i++) { ma[i]=0.0; } } for(i=0; i<=limit; i++) { double ma0=iMA(par,tf,period1,0,mode,price,i); //1st MA calculation double ma1=iMA(par,tf,period2,0,mode,price,i+shift); //2-nd MA calculation ma[i] = ma0-ma1; //indicator value calculation } return(rates_total); }