Trying to modify RSI smoothing formula to weighted without success. Anyone can help with this?

 

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:

//----
   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=(High[k]+Low[k]+Close[k])/3-(High[k+1]+Low[k+1]+Close[k+1])/3;
            if(rel>0) sump+=rel;
            else      sumn-=rel;
            k--;
           }
         positive=sump/RSIPeriod;
         negative=sumn/RSIPeriod;
        }
      else
        {
         //---- smoothed moving average
         rel=(High[i]+Low[i]+Close[i])/3-(High[i+1]+Low[i+1]+Close[i+1])/3;
         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--;
     }
//----
   return(0);
  }

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

 

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);
  }
//+------------------------------------------------------------------+
Reason: