Calculation of RSI
A technical momentum indicator that compares the magnitude of recent gains to recent losses in an attempt to determine overbought and oversold conditions of an asset. It is calculated using the following formula:
RSI = 100 – | 100 |
______ | |
1 + RS |
RS = Average of x days’ up closes / Average of x days’ down closes |
As you can see from the chart below, the RSI ranges from 0 to 100. An asset is deemed to be overbought once the RSI approaches the 70 level, meaning that it may be getting overvalued and is a good candidate for a pullback. Likewise, if the RSI approaches 30, it is an indication that the asset may be getting oversold and therefore likely to become undervalued.
For each trading period an upward change (U) or downward change (D) is calculated. Up periods are characterized by the close being higher than the previous close,
- U = closenow − closeprevious
- D = 0
Conversely, a down period is characterized by the close being lower than the previous period’s (note that D is nonetheless a positive number),
- U = 0
- D = closeprevious − closenow
If the last close is the same as the previous, both U and D are zero. An average for U is calculated with an exponential moving average using a given N-period smoothing factor, and likewise for D. The ratio of those averages is the Relative Strength,
This is converted to a Relative Strength Index between 0 and 100,
This can be rewritten as follows to emphasize the way RSI expresses the up as a proportion of the total up and down (averages in each case),
The EMA, in theory, uses an infinite amount of past data. It is necessary either to go back far enough, or alternately at the start of data begin with a simple average of N periods instead,
and then continue from there with the usual EMA formula,
The calculation of D is similar.