Wildersmovingaverage
A More Efficient Calculation Of The Relative Strength IndexIntroduction
I explained in my post on indicators settings how computation time might affect your strategy. Therefore efficiency is an important factor that must be taken into account when making an indicator. If i'am known for something on tradingview its certainly not from making huge codes nor fancy plots, but for my short and efficient estimations/indicators, the first estimation i made being the least squares moving average using the rolling z-score method, then came the rolling correlation, and finally the recursive bands that allow to make extremely efficient and smooth extremities.
Today i propose a more efficient version of the relative strength index, one of the most popular technical indicators of all times. This post will also briefly introduce the concept of system linearity/additive superposition.
Breaking Down The Relative Strength Index
The relative strength index (rsi) is a technical indicator that is classified as a momentum oscillator. This technical indicator allow the user to know when an asset is overvalued and thus interesting to sell, or under evaluated, thus interesting to buy. However its many properties, such as constant scale (0,100) and stationarity allowed him to find multiple uses.
The calculation of the relative strength index take into account the direction of the price changes, its pretty straightforward. First we calculate the average price absolute changes based on their sign :
UP = close - previous close if close > previous close else 0
DN = previous close - close if close < previous close else 0
Then the relative strength factor is calculated :
RS = RMA(UP,P)/RMA(DN,P)
Where RMA is the Wilder moving average of period P . The relative strength index is then calculated as follows :
RSI = 100 - 100/(1 + RS)
As a reminder, the Wilder moving average is an exponential filter with the form :
y(t) = a*x+(1-a)*y(t-1) where a = 1/length . The smoothing constant being equal to 1/length allow to get a smoother result than the exponential moving average who use a smoothing constant of 2/(length+1).
Simple Efficient Changes
As we can see the calculation is not that complicated, the use of an exponential filter make the indicator extremely efficient. However there is room for improvement. First we can skip the if else or any conditional operator by using the max function.
change = close - previous close
UP = max(change,0)
DN = max(change*-1,0)
This is easy to understand, when the closing price is greater than the previous one the change will be positive, therefore the maximum value between the change and 0 will be the change since change > 0 , values of change lower than 0 mean that the closing price is lower than the previous one, in this case max(change,0) = 0 .
For Dn we do the same except that we reverse the sign of the change, this allow us to get a positive results when the closing price is lower than the previous one, then we reuse the trick with max , and we therefore get the positive price change during a downward price change.
Then come the calculation of the relative strength index : 100 - 100/(1 + RS) . We can simplify it easily, first lets forget about the scale of (0,100) and focus on a scale of (0,1), a simple scaling solution is done using : a/(a+b) , where (a,b) > 0 , we then are sure to get a value in a scale of (0,1), because a+b >= a . We have two elements, UP and DN , we only need to apply the Wilder Moving Average, and we get :
RMA(UP,P)/(RMA(UP,P) + RMA(DN,P))
In order to scale it in a range of (0,100) we can simply use :
100*RMA(UP,P)/(RMA(UP,P) + RMA(DN,P))
= 100*RMA(max(change,0),P)/(RMA(max(change,0),P) + RMA(max(change*-1,0),P))
And "voila"
Superposition Principle and Linear Filters
A function is said to be linear if : f(a + b) = f(a) + f(b) . If you have studied signal processing a little bit, you must have encountered the term "Linear Filter", its just the same, simply put, a filter is said to be linear if :
filter(a+b) = filter(a) + filter(b)
Simple right ? Lot of filters are linear, the simple moving average is, the wma, lsma, ema...etc. One of most famous non linear filters is the median filter, therefore :
median(a+b) ≠ median(a) + median(b)
When a filter is linear we say that he satisfies the superposition principle. So how can this help us ? Well lets see back our formula :
100*RMA(UP,P)/(RMA(UP,P) + RMA(DN,P))
We use the wilder moving average 3 times, however we can take advantage of the superposition principle by using instead :
100*RMA(UP,P)/RMA(UP + DN,P)
Reducing the number of filters used is always great, even if they recursion.
Final Touch
Thanks to the superposition principle we where able to have RMA(UP + DN,P) , which allow us to only average UP and DN togethers instead of separately, however we can see something odd. We have UP + DN , but UP and DN are only the positive changes of their associated price sign, therefore :
UP + DN = abs(change)
Lets use pinescript, we end up with the following estimation :
a = change(close)
rsi = 100*rma(max(a,0),length)/rma(abs(a),length)
compared to a basic calculation :
up = max(x - x , 0)
dn = max(x - x, 0)
rs = rma(up, length) / rma(dn, length)
rsi = 100 - 100 / (1 + rs)
Here is the difference between our estimate and the rsi function of both length = 14 :
with an average value of 0.000000..., those are certainly due to rounding errors.
In a chart we can see that the difference is not significant.
In our orange our estimate, in blue the classic rsi of both length = 14.
Conclusion
In this post i proposed a simplification of the relative strength index indicator calculation. I introduced the concept of linearity, this is a must have when we have to think efficiently. I also highlighted simple tricks using the max function and some basic calculus related to rescaling. I had a lot of fun while simplifying the relative strength index, its an indicator everyone use. I hope this post was enjoyable to read, with the hope that it was useful to you. If this calculation was already proposed please pm me the reference.
You can check my last paper about this calculation if you want more details, the link to my papers is in the signature. Thanks for reading !