//+------------------------------------------------------------------+ //| RSI.mq4 | //| Copyright © 2004, MetaQuotes Software Corp. | //| http://www.metaquotes.net/ | //+------------------------------------------------------------------+ #property copyright "Copyright © 2004, MetaQuotes Software Corp." #property link "http://www.metaquotes.net/" #property indicator_separate_window #property indicator_minimum 0 #property indicator_maximum 100 #property indicator_buffers 1 #property indicator_color1 DodgerBlue //#property indicator_level1 30.0 //#property indicator_level2 70.0 //#property indicator_level3 50.0 //---- input parameters extern int RSIPeriod=14; extern int TL_bars=2; // 1 or 2 //---- buffers double RSIBuffer[]; double PosBuffer[]; double NegBuffer[]; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ string short_name; int init() { //---- 2 additional buffers are used for counting. IndicatorBuffers(3); SetIndexBuffer(1,PosBuffer); SetIndexBuffer(2,NegBuffer); //---- indicator line SetIndexStyle(0,DRAW_LINE); SetIndexBuffer(0,RSIBuffer); //---- name for DataWindow and indicator subwindow label short_name="RSI-TL"+DoubleToStr(TL_bars,0)+"("+RSIPeriod+")"; IndicatorShortName(short_name); SetIndexLabel(0,short_name); //---- SetIndexDrawBegin(0,RSIPeriod); //---- return(0); } int deinit() { ObjectDelete("RSI-TL_S"+DoubleToStr(TL_bars,0)); ObjectDelete("RSI-TL_R"+DoubleToStr(TL_bars,0)); } //+------------------------------------------------------------------+ //| Relative Strength Index | //+------------------------------------------------------------------+ int start() { int i,counted_bars=IndicatorCounted(); double rel,negative,positive; //---- if(Bars<=RSIPeriod) return(0); //---- initial zero if(counted_bars<1) for(i=1;i<=RSIPeriod;i++) RSIBuffer[Bars-i]=0.0; //---- i=Bars-RSIPeriod-1; if(counted_bars>=RSIPeriod) i=Bars-counted_bars-1; while(i>=0) { double sumn=0.0,sump=0.0; if(i==Bars-RSIPeriod-1) { int k=Bars-2; //---- initial accumulation while(k>=i) { rel=Close[k]-Close[k+1]; if(rel>0) sump+=rel; else sumn-=rel; k--; } positive=sump/RSIPeriod; negative=sumn/RSIPeriod; } else { //---- smoothed moving average rel=Close[i]-Close[i+1]; if(rel>0) sump=rel; else sumn=-rel; positive=(PosBuffer[i+1]*(RSIPeriod-1)+sump)/RSIPeriod; negative=(NegBuffer[i+1]*(RSIPeriod-1)+sumn)/RSIPeriod; } PosBuffer[i]=positive; NegBuffer[i]=negative; if(negative==0.0) RSIBuffer[i]=0.0; else RSIBuffer[i]=100.0-100.0/(1+positive/negative); i--; } //---- int h1=0, h2=0, l1=0, l2=0; for(i=TL_bars+1;i0) && (h2==0)) { if (TL_bars==1) { if ( (RSIBuffer[i-1]RSIBuffer[h1]) ) { h2=i; } } if (TL_bars==2) { if ( (RSIBuffer[i-1]RSIBuffer[h1]) ) { h2=i; } } } if (l1==0) { if (TL_bars==1) { if ( (RSIBuffer[i-1]>RSIBuffer[i]) && (RSIBuffer[i+1]>RSIBuffer[i]) ) { l1=i; } } if (TL_bars==2) { if ( (RSIBuffer[i-1]>RSIBuffer[i]) && (RSIBuffer[i+1]>RSIBuffer[i]) && (RSIBuffer[i-2]>RSIBuffer[i]) && (RSIBuffer[i+2]>RSIBuffer[i]) ) { l1=i; } } } if ((l1>0) && (l2==0)) { if (TL_bars==1) { if ( (RSIBuffer[i-1]>RSIBuffer[i]) && (RSIBuffer[i+1]>RSIBuffer[i]) && (RSIBuffer[i]RSIBuffer[i]) && (RSIBuffer[i+1]>RSIBuffer[i]) && (RSIBuffer[i-2]>RSIBuffer[i]) && (RSIBuffer[i+2]>RSIBuffer[i]) && (RSIBuffer[i]0) && (h2>0) && (l1>0) && (l2>0)) { break; } } if ((h1>0) && (h2>0)) { DrawIndicatorTrendLine(Time[h2], Time[h1], RSIBuffer[h2], RSIBuffer[h1], Red, short_name, "RSI"+DoubleToStr(RSIPeriod,0)+"-TL_R"+DoubleToStr(TL_bars,0)); } if ((l1>0) && (l2>0)) { DrawIndicatorTrendLine(Time[l2], Time[l1], RSIBuffer[l2], RSIBuffer[l1], Blue, short_name, "RSI"+DoubleToStr(RSIPeriod,0)+"-TL_S"+DoubleToStr(TL_bars,0)); } return(0); } //+------------------------------------------------------------------+ void DrawIndicatorTrendLine(datetime x1, datetime x2, double y1, double y2, color lineColor, string IndicatorName, string label) { int indicatorWindow = WindowFind(IndicatorName); if (indicatorWindow < 0) return; ObjectDelete(label); ObjectCreate(label, OBJ_TREND, indicatorWindow, x1, y1, x2, y2, 0, 0); ObjectSet(label, OBJPROP_RAY, true); ObjectSet(label, OBJPROP_COLOR, lineColor); ObjectSet(label, OBJPROP_STYLE, STYLE_SOLID); ObjectSet(label, OBJPROP_WIDTH, 1); }