The latest try to solve this problem is the following code:
I've tried to fit the LWMA code formula to this but I allways get an RSI=50.
Any suggestion?
//+------------------------------------------------------------------+ //| 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 //---- input parameters extern int RSIPeriod=14; //---- buffers double RSIBuffer[]; double PosBuffer[]; double NegBuffer[]; //---- int ExtCountedBars=0; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int init() { string short_name; //---- 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("+RSIPeriod+")"; IndicatorShortName(short_name); SetIndexLabel(0,short_name); //---- SetIndexDrawBegin(0,RSIPeriod); //---- return(0); } //+------------------------------------------------------------------+ //| Relative Strength Index | //+------------------------------------------------------------------+ int start() { { if(Bars<=RSIPeriod) return(0); ExtCountedBars=IndicatorCounted(); //---- check for possible errors if (ExtCountedBars<0) return(-1); //---- last counted bar will be recounted if (ExtCountedBars>0) ExtCountedBars--; //---- double sump=0.0,sumn=0.0,lsump=0.0,lsumn=0.0; double price; int i,weight=0,pos=Bars-ExtCountedBars-1; //---- initial accumulation if(pos<RSIPeriod) pos=RSIPeriod; for(i=1;i<=RSIPeriod;i++,pos--) { price=(High[pos]+Low[pos]+Close[pos])/3-(High[pos+1]+Low[pos+1]+Close[pos+1])/3; if(price>0){ sump+=price*i; lsump+=price; weight+=i; } else { sumn-=price*i; lsumn-=price; weight+=i; } } //---- main calculation loop pos++; i=pos+RSIPeriod; while(pos>=0) { PosBuffer[pos]=sump/weight; NegBuffer[pos]=sumn/weight; if(pos==0) break; pos--; i--; price=(High[pos]+Low[pos]+Close[pos])/3-(High[pos+1]+Low[pos+1]+Close[pos+1])/3; if(price>0){ sump=sump-lsump+price*RSIPeriod; lsump-=(High[i]+Low[i]+Close[i])/3-(High[i+1]+Low[i+1]+Close[i+1])/3; lsump+=price; } else { sumn=sumn-lsumn+price*RSIPeriod; lsumn-=(High[i]+Low[i]+Close[i])/3-(High[i+1]+Low[i+1]+Close[i+1])/3; lsumn+=price; } if(NegBuffer[pos]==0.0) RSIBuffer[pos]=0.0; else RSIBuffer[pos]=100.0-100.0/(1+PosBuffer[pos]/NegBuffer[pos]); } //---- zero initial bars if(ExtCountedBars<1) for(i=1;i<RSIPeriod;i++) RSIBuffer[Bars-i]=0; } //---- return(0); } //+------------------------------------------------------------------+

You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Hi,
I have little knowledge about programing. Everything I do is mostly copy/paste add/remove from one indicator to another.
In this particular case, i've searched (almost) everywhere for a Weighted RSI applied to PRICE_TYPICAL with no success.
For the PRICE_TYPICAL question, I've managed that in the original code as shown here:
To calculate the weighted moving average instead of smoothed I've tried countless times but with no success.
I know that (positive=sump/SUM(weight)) with (sump=SUM(rel*i)) and (weight=i) and i>1; i<=RSIPeriod but I can't code this.
Anyone can help me on this?
Thanks,
PG