//+------------------------------------------------------------------+ //| Oscillator of Indicator and MA.mq4 | //| Copyright 2014, MetaQuotes Software Corp. | //| http://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2014, MetaQuotes Software Corp." #property link "http://www.mql5.com" #property description "Oscillator of Indicator & MA by pipPod" #property version "1.00" #property strict #property indicator_separate_window #property indicator_buffers 5 #property indicator_color1 clrLimeGreen #property indicator_color2 clrFireBrick #property indicator_color3 clrAqua #property indicator_color4 clrMagenta //--- #property indicator_width1 2 #property indicator_width2 2 #property indicator_width3 2 #property indicator_width4 2 //--- enum ENUM_RANGEMODE { HIGH_LOW, //High/Low CLOSE_CLOSE, //Close/Close HIGH_LOW_CLOSE //High/Low/Close }; //--- enum indicators { INDICATOR_MACD, //Moving Average Convergence/Divergence INDICATOR_PRI, //Percent Range Index INDICATOR_RSI, //Relative Strength Index INDICATOR_CCI, //Commodity Channel Index INDICATOR_RVI, //Relative Vigor Index INDICATOR_DEMARKER, //DeMarker Oscillator }; //--- input indicators Indicator=INDICATOR_MACD; input ENUM_TIMEFRAMES TimeFrame=0; input string _;//--- input bool ShowIndicator=true; input bool ShowOscillator=true; //---MACD input string MACD; input int FastMACD=12; input int SlowMACD=26; input ENUM_APPLIED_PRICE MACDPrice=PRICE_CLOSE; input ENUM_MA_METHOD MACDMethod=MODE_EMA; input int MACDSignal=9; input ENUM_MA_METHOD MACDSignalMethod=MODE_SMA; //---PRI input string PRI; input int PRIPeriod=14; input ENUM_APPLIED_PRICE PRIPrice=PRICE_CLOSE; input int Slowing=3; input ENUM_RANGEMODE RangeMode=HIGH_LOW; input int PRISignal=9; input ENUM_MA_METHOD PRISignalMethod=MODE_SMMA; //---RSI input string RSI; input int RSIPeriod=8; input ENUM_APPLIED_PRICE RSIPrice=PRICE_CLOSE; input int RSISignal=5; input ENUM_MA_METHOD RSISignalMethod=MODE_LWMA; //---CCI input string CCI; input int CCIPeriod=14; input ENUM_APPLIED_PRICE CCIPrice=PRICE_CLOSE; input int CCISignal=9; input ENUM_MA_METHOD CCISignalMethod=MODE_SMMA; //---RVI input string RVI; input int RVIPeriod=10; input int RVISignal=5; input ENUM_MA_METHOD RVISignalMethod=MODE_EMA; //---DeMarker input string DeMarker; input int DeMarkerPeriod=14; input int DeMarkerSignal=9; input ENUM_MA_METHOD DeMarkerSignalMethod=MODE_LWMA; input string __;//--- input int MAShift=0; //--- double indicator, signal, oscillator; //--- double IndicatorBuffer[]; double SignalBuffer[]; double Oscillator[]; double OscBuffer1[]; double OscBuffer2[]; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- indicator buffers mapping IndicatorBuffers(indicator_buffers); SetIndexStyle(0,DRAW_HISTOGRAM); SetIndexStyle(1,DRAW_HISTOGRAM); SetIndexStyle(2,DRAW_LINE); SetIndexStyle(3,DRAW_LINE); SetIndexStyle(4,DRAW_NONE); SetIndexBuffer(0,OscBuffer1); SetIndexBuffer(1,OscBuffer2); SetIndexBuffer(2,IndicatorBuffer); SetIndexBuffer(3,SignalBuffer); SetIndexBuffer(4,Oscillator); SetIndexShift(3,MAShift); //--- string timeFrame = StringSubstr(EnumToString(TimeFrame),7)+" "; if(timeFrame=="CURRENT ") timeFrame=""; //---set indexlabel string label1, label2, label3 = "Signal"+timeFrame, label4 = "Oscillator"; switch(Indicator) { case INDICATOR_MACD: label1 = "iMACD "+timeFrame+"("+ IntegerToString(FastMACD)+","+ IntegerToString(SlowMACD)+","+ IntegerToString(MACDSignal)+") "+ StringSubstr(EnumToString(MACDSignalMethod),5); label2 = "iMACD "+timeFrame; IndicatorSetInteger(INDICATOR_DIGITS,_Digits); break; case INDICATOR_PRI: label1 = "iPRI "+timeFrame+"("+ IntegerToString(PRIPeriod)+","+ IntegerToString(PRISignal)+","+ IntegerToString(Slowing)+") "+ StringSubstr(EnumToString(PRISignalMethod),5); label2 = "iPRI "+timeFrame; IndicatorSetInteger(INDICATOR_DIGITS,1); break; case INDICATOR_RSI: label1 = "iRSI "+timeFrame+"("+ IntegerToString(RSIPeriod)+","+ IntegerToString(RSISignal)+") "+ StringSubstr(EnumToString(RSISignalMethod),5); label2 = "iRSI "+timeFrame; IndicatorSetInteger(INDICATOR_DIGITS,1); break; case INDICATOR_CCI: label1 = "iCCI "+timeFrame+"("+ IntegerToString(CCIPeriod)+","+ IntegerToString(CCISignal)+") "+ StringSubstr(EnumToString(CCISignalMethod),5); label2 = "iCCI "+timeFrame; IndicatorSetInteger(INDICATOR_DIGITS,1); break; case INDICATOR_RVI: label1 = "iRVI "+timeFrame+"("+ IntegerToString(RVIPeriod)+","+ IntegerToString(RVISignal)+") "+ StringSubstr(EnumToString(RVISignalMethod),5); label2 = "iRVI "+timeFrame; IndicatorSetInteger(INDICATOR_DIGITS,1); break; case INDICATOR_DEMARKER: label1 = "iDeMarker "+timeFrame+"("+ IntegerToString(DeMarkerPeriod)+","+ IntegerToString(DeMarkerSignal)+") "+ StringSubstr(EnumToString(DeMarkerSignalMethod),5); label2 = "iDeMarker "+timeFrame; IndicatorSetInteger(INDICATOR_DIGITS,1); } IndicatorSetString(INDICATOR_SHORTNAME,label1); SetIndexLabel(0,label4); SetIndexLabel(1,label4); SetIndexLabel(2,label2); SetIndexLabel(3,label3); //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ 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[]) { //--- if(rates_total<100) return(0); int i,limit,shift; //--- limit = rates_total - prev_calculated; if(prev_calculated==0) limit -= 100; //--- for(i=limit;i>=0;i--) { shift = iBarShift(_Symbol,TimeFrame,time[i]); switch(Indicator) { case INDICATOR_MACD: indicator = iMACD(_Symbol,TimeFrame,FastMACD,SlowMACD,MACDPrice,MACDMethod,MACDSignal,MACDSignalMethod,MODE_MAIN,shift); signal = iMACD(_Symbol,TimeFrame,FastMACD,SlowMACD,MACDPrice,MACDMethod,MACDSignal,MACDSignalMethod,MODE_SIGNAL,shift); break; case INDICATOR_PRI: indicator = iPRI(_Symbol,TimeFrame,PRIPeriod,PRIPrice,Slowing,RangeMode,PRISignal,PRISignalMethod,MODE_MAIN,shift); signal = iPRI(_Symbol,TimeFrame,PRIPeriod,PRIPrice,Slowing,RangeMode,PRISignal,PRISignalMethod,MODE_SIGNAL,shift); break; case INDICATOR_RSI: indicator = iRSI(_Symbol,TimeFrame,RSIPeriod,RSIPrice,RSISignal,RSISignalMethod,MODE_MAIN,shift); signal = iRSI(_Symbol,TimeFrame,RSIPeriod,RSIPrice,RSISignal,RSISignalMethod,MODE_SIGNAL,shift); break; case INDICATOR_CCI: indicator = iCCI(_Symbol,TimeFrame,CCIPeriod,CCIPrice,CCISignal,CCISignalMethod,MODE_MAIN,shift); signal = iCCI(_Symbol,TimeFrame,CCIPeriod,CCIPrice,CCISignal,CCISignalMethod,MODE_SIGNAL,shift); break; case INDICATOR_RVI: indicator = iRVI(_Symbol,TimeFrame,RVIPeriod,RVISignal,RVISignalMethod,MODE_MAIN,shift); signal = iRVI(_Symbol,TimeFrame,RVIPeriod,RVISignal,RVISignalMethod,MODE_SIGNAL,shift); break; case INDICATOR_DEMARKER: indicator = iDeMarker(_Symbol,TimeFrame,DeMarkerPeriod,DeMarkerSignal,DeMarkerSignalMethod,MODE_MAIN,shift); signal = iDeMarker(_Symbol,TimeFrame,DeMarkerPeriod,DeMarkerSignal,DeMarkerSignalMethod,MODE_SIGNAL,shift); } oscillator = indicator - signal; if(ShowIndicator) { IndicatorBuffer[i] = indicator; SignalBuffer[i] = signal; } if(ShowOscillator) { Oscillator[i] = oscillator; if(Oscillator[i]>=Oscillator[i+1]) { OscBuffer1[i] = Oscillator[i]; OscBuffer2[i] = EMPTY_VALUE; } else { OscBuffer2[i] = Oscillator[i]; OscBuffer1[i] = EMPTY_VALUE; } } } //--- return value of prev_calculated for next call return(rates_total); } //+------------------------------------------------------------------+ //| Moving Average Convergence/Divergence | //+------------------------------------------------------------------+ double iMACD(const string symbol, const ENUM_TIMEFRAMES timeframe, int fast_period, int slow_period, const ENUM_APPLIED_PRICE ma_price, const ENUM_MA_METHOD ma_method, int signal_period, const ENUM_MA_METHOD signal_method, int mode, int idx) { double macd; if(mode==MODE_SIGNAL) { static double macdSignal[]; if(ArraySize(macdSignal)!=signal_period) ArrayResize(macdSignal,signal_period); for(int i=0;i100) pri = 100; if(pri<0) pri = 0; //--- return(pri); } //+------------------------------------------------------------------+ //| Relative Strength Index | //+------------------------------------------------------------------+ double iRSI(const string symbol, const ENUM_TIMEFRAMES timeframe, int rsi_period, const ENUM_APPLIED_PRICE rsi_price, int signal_period, const ENUM_MA_METHOD signal_method, int mode, int idx) { double rsi; if(mode==MODE_SIGNAL) { static double rsiSignal[]; if(ArraySize(rsiSignal)!=signal_period) ArrayResize(rsiSignal,signal_period); for(int i=0;i