//+------------------------------------------------------------------+ //| RSI.mq4 | //| Copyright 2005-2014, MetaQuotes Software Corp. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "2005-2014, MetaQuotes Software Corp." #property link "https://www.mql5.com" #property description "Relative Strength Index" #property strict #property indicator_separate_window #property indicator_buffers 3 #property indicator_plots 1 #property indicator_minimum 0 #property indicator_maximum 100 #property indicator_color1 DodgerBlue #property indicator_level1 30.0 #property indicator_level2 70.0 #property indicator_levelcolor clrSilver #property indicator_levelstyle STYLE_DOT //--- input parameters input int InpRSIPeriod=14; // RSI Period //--- buffers double ExtRSIBuffer[]; double ExtPosBuffer[]; double ExtNegBuffer[]; // // // #include RangeBarIndicator rangeBarsIndicator; // // // //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit(void) { string short_name; //--- 2 additional buffers are used for counting SetIndexBuffer(0,ExtRSIBuffer); SetIndexBuffer(1,ExtPosBuffer); SetIndexBuffer(2,ExtNegBuffer); //--- indicator line PlotIndexSetInteger(0,PLOT_DRAW_TYPE,DRAW_LINE); SetIndexBuffer(0,ExtRSIBuffer); //--- name for DataWindow and indicator subwindow label short_name="RSI("+string(InpRSIPeriod)+")"; IndicatorSetString(INDICATOR_SHORTNAME,short_name); PlotIndexSetString(0,PLOT_LABEL,short_name); //--- check for input if(InpRSIPeriod<2) { Print("Incorrect value for input variable InpRSIPeriod = ",InpRSIPeriod); return(INIT_FAILED); } //--- PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,InpRSIPeriod); //--- initialization done return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Relative Strength Index | //+------------------------------------------------------------------+ 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 &TickVolume[], const long &Volume[], const int &Spread[]) { // // Process data through MedianRenko indicator // if(!rangeBarsIndicator.OnCalculate(rates_total,prev_calculated,Time)) return(0); // // Make the following modifications in the code below: // // rangeBarsIndicator.GetPrevCalculated() should be used instead of prev_calculated // // rangeBarsIndicator.Open[] should be used instead of open[] // rangeBarsIndicator.Low[] should be used instead of low[] // rangeBarsIndicator.High[] should be used instead of high[] // rangeBarsIndicator.Close[] should be used instead of close[] // // rangeBarsIndicator.IsNewBar (true/false) informs you if a renko brick completed // // rangeBarsIndicator.Time[] shold be used instead of Time[] for checking the renko bar time. // (!) rangeBarsIndicator.SetGetTimeFlag() must be called in OnInit() for rangeBarsIndicator.Time[] to be used // // rangeBarsIndicator.Tick_volume[] should be used instead of TickVolume[] // rangeBarsIndicator.Real_volume[] should be used instead of Volume[] // (!) rangeBarsIndicator.SetGetVolumesFlag() must be called in OnInit() for Tick_volume[] & Real_volume[] to be used // // rangeBarsIndicator.Price[] should be used instead of Price[] // (!) rangeBarsIndicator.SetUseAppliedPriceFlag(ENUM_APPLIED_PRICE _applied_price) must be called in OnInit() for rangeBarsIndicator.Price[] to be used // int _prev_calculated = rangeBarsIndicator.GetPrevCalculated(); // // // int i,pos; double diff; //--- if(Bars(_Symbol,_Period)<=InpRSIPeriod || InpRSIPeriod<2) return(0); //--- counting from 0 to rates_total ArraySetAsSeries(ExtRSIBuffer,false); ArraySetAsSeries(ExtPosBuffer,false); ArraySetAsSeries(ExtNegBuffer,false); ArraySetAsSeries(rangeBarsIndicator.Close,false); //--- preliminary calculations pos=_prev_calculated-1; if(pos<=InpRSIPeriod) { //--- first RSIPeriod values of the indicator are not calculated ExtRSIBuffer[0]=0.0; ExtPosBuffer[0]=0.0; ExtNegBuffer[0]=0.0; double sump=0.0; double sumn=0.0; for(i=1; i<=InpRSIPeriod; i++) { ExtRSIBuffer[i]=0.0; ExtPosBuffer[i]=0.0; ExtNegBuffer[i]=0.0; diff=rangeBarsIndicator.Close[i]-rangeBarsIndicator.Close[i-1]; if(diff>0) sump+=diff; else sumn-=diff; } //--- calculate first visible value ExtPosBuffer[InpRSIPeriod]=sump/InpRSIPeriod; ExtNegBuffer[InpRSIPeriod]=sumn/InpRSIPeriod; if(ExtNegBuffer[InpRSIPeriod]!=0.0) ExtRSIBuffer[InpRSIPeriod]=100.0-(100.0/(1.0+ExtPosBuffer[InpRSIPeriod]/ExtNegBuffer[InpRSIPeriod])); else { if(ExtPosBuffer[InpRSIPeriod]!=0.0) ExtRSIBuffer[InpRSIPeriod]=100.0; else ExtRSIBuffer[InpRSIPeriod]=50.0; } //--- prepare the position value for main calculation pos=InpRSIPeriod+1; } //--- the main loop of calculations for(i=pos; i0.0?diff:0.0))/InpRSIPeriod; ExtNegBuffer[i]=(ExtNegBuffer[i-1]*(InpRSIPeriod-1)+(diff<0.0?-diff:0.0))/InpRSIPeriod; if(ExtNegBuffer[i]!=0.0) ExtRSIBuffer[i]=100.0-100.0/(1+ExtPosBuffer[i]/ExtNegBuffer[i]); else { if(ExtPosBuffer[i]!=0.0) ExtRSIBuffer[i]=100.0; else ExtRSIBuffer[i]=50.0; } } //--- return(rates_total); } //+------------------------------------------------------------------+