Why is the RSI value never equal to 0 nor 100 ?

 

Evening,

I am trying to apply the RSI formula to another indicator instead of the price, and i have an issue understanding the value it takes.

Here is the official formula :

 

 //--- calculate first visible value
      ExtPosBuffer[ExtPeriodRSI]=sum_pos/ExtPeriodRSI;
      ExtNegBuffer[ExtPeriodRSI]=sum_neg/ExtPeriodRSI;
      if(ExtNegBuffer[ExtPeriodRSI]!=0.0)
         ExtRSIBuffer[ExtPeriodRSI]=100.0-(100.0/(1.0+ExtPosBuffer[ExtPeriodRSI]/ExtNegBuffer[ExtPeriodRSI]));
      else
        {
         if(ExtPosBuffer[ExtPeriodRSI]!=0.0)
            ExtRSIBuffer[ExtPeriodRSI]=100.0;
         else
            ExtRSIBuffer[ExtPeriodRSI]=50.0;
        }
      //--- prepare the position value for main calculation
      pos=ExtPeriodRSI+1;
     }
//--- the main loop of calculations
   for(int i=pos; i<rates_total && !IsStopped(); i++)
     {
      double diff=price[i]-price[i-1];
      ExtPosBuffer[i]=(ExtPosBuffer[i-1]*(ExtPeriodRSI-1)+(diff>0.0?diff:0.0))/ExtPeriodRSI;
      ExtNegBuffer[i]=(ExtNegBuffer[i-1]*(ExtPeriodRSI-1)+(diff<0.0?-diff:0.0))/ExtPeriodRSI;
      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;
        }
     }

Lets suppose -> a) the RSi has a 2 periodicity    b) the last 4 candles are bears     c) x is the average decrease of the price;      referring to the code the calculation should be :

 ExtPosBuffer[i]=(ExtPosBuffer[i-1]*(ExtPeriodRSI-1)+(diff>0.0?diff:0.0))/ExtPeriodRSI

                        = 0/2*1+0/2 = 0

ExtNegBuffer[i]=(ExtNegBuffer[i-1]*(ExtPeriodRSI-1)+(diff<0.0?-diff:0.0))/ExtPeriodRSI

                       = x/2*1+x/2 = x (any positive number)

ExtRSIBuffer[i]=100.0-100.0/(1+ExtPosBuffer[i]/ExtNegBuffer[i])

                       = 100-100/(1+0/x) = 100-100/1 = 0


In this case the RSI value should be 0. Which makes sense because there was no price increasing for that short period. However the indicator isn't showing 0 value.

What am I missing ?

Does it comes from the first part of the formula whom I don't see the role it plays in the main part ?

 
Print your variables and you will know why. It would take 1.5×L values to get close to zero - it will never reach exactly zero.
 
William Roeder:
Print your variables and you will know why. It would take 1.5×L values to get close to zero - it will never reach exactly zero.

Alright I got what i misunderstood.

This is what i thought : ExtPosBuffer[i-1] = The simple average of the sum of the last RSI-1 bars

This is what it actually is : ExtPosBuffer[i-1]  = The simple average of return[i-1] + ExtPosBuffer[i-2]*RSI-1

                                                                                                                            ExtPosBuffer[i-2] = The SMA of return[i-2] + ExtPosBuffer[i-3]* RSI-1

                                                                                                                                                                                              ExtPosBuffer[i-3] = The SMA of return[i-3] + ExtPosBuffer[i-4]* RSI-1

                                                                                                                                                                                                                                                                and so on..

The main loop part needs the value of the previous period, which needed the value of the previous period.. that's where it is "exponential" and that's why this first main loop part can't be calculated before the 14th bar data.

Reason: