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 !
Pinescript
Making A Good Indicator DescriptionIntroduction
When posting an indicator a good concept/code isn't the only thing required in order to have a quality indicator, its description is also extremely important. Each person has its own style,
mine is pretty academic, but as long as your description respect certain criterions you are free to do as you wish.
In this post i want to help you make the best of your published indicator by introducing basic concepts in post writing.
Basic Description
An indicator description should at least explain what kind of information your indicator is giving as well as how to interpret/use it. Lets take the Supertrend indicator as example, we'll be making a brief description of it.
The Supertrend indicator is a technical indicator created by the french trader Olivier Seban that aim to detect the current market trend direction. The indicator detect an up-trend when below the market price and a down-trend when above.
This indicator posses two setting, length and mult, higher values of length/mult allow the indicator to detect trends of longer period. This indicator can also be used as trailing stop loss, it can also provide a support level when below the price or a resistance level when above.
Lets breakdown this simple description.
-"The Supertrend indicator is a technical indicator created by the french trader Olivier Seban"
If the indicator is not yours, always credit/mention the original author, this is no option.
-"that aim to detect the current market trend direction"
Aim of the indicator.
-"The indicator detect an up-trending market when below the market price and a down-trend when above."
How to interpret the indicator.
-"This indicator posses two setting, length and mult, higher values of length/mult allow the indicator to detect trends of longer period."
Always try to introduce the indicator settings and explain how they affect the indicator output.
-"This indicator can also be used as trailing stop loss, it can also provide a support level when below the price or a resistance level when above."
Alternative uses of the indicator, try to provide a graphic showing the indicator acting as mentioned, i our case we should show a graphic showing the Supertrend acting as a support/resistance.
Once you write up your indicator description read it back several times (something i wish i could do more often) and always ask yourself if your indicator description fully describe the aim of the indicator. If you like writing you can of course add more elements, although try not to be redundant, this is really important, skip things such as :
"Yesterday i was dinning with a colleague and he asked me how to get zero-lag filters, since then i tried to..."
This is tiring for the reader and so try not including it.
More Complete Description
A more complete description introduce additional elements regarding your indicator such as your motivation behind its publishing, its calculation...etc.
Such description of the Supertrend indicator could look like this :
Detecting the current market price trend has always been a major challenge in technical analysis, systems relying on the cross between a noisy series and a certain level/indicator can produce whipsaws trades. In order to provide a solution to this problem more advanced indicators have been proposed, one of them being the Supertrend indicator developed by the french trader Olivier Seban.
This indicator aim to detect the current market trend direction, the indicator detect an up-trending market when the price is superior to the Supertrend, and a down trending market when the price is inferior to the Supertrend.
The indicator is an iterative calculation, the core element of its calculation involve creating two bands, denoted as upper/lower, calculated as follows :
upper = hl2 + atr(length)*mult
lower = hl2 - atr(length)*mult
where atr is the average true range of period length and hl2 is the median price defined as : (high + low)/2.
Higher values of length/mult allow the indicator to be less sensitive to shorter price variations, this allow to detect trends of longer period.
Apart from the basic indicator usage described above, the Supertrend indicator can also act as a :
- trailing stop loss, in this case if the closing price cross the value of the indicator, the trade will be closed.
- support/resistance level, in this case an upward movements can be expected after the low price cross the Supertrend, and a downward movement when the high price cross the Supertrend indicator.
In conclusion, the Supertrend indicator is an elegant solution when it comes to detecting the current market price trend, its ability to avoid whipsaws trades makes him a good ally for decision making.
Including Graphics
Graphics of your indicators can add more clarity to your description since visual examples are often easier to understand, the goal here is to see how your indicator interact with the market price. Try including readable graphics, this can mean charts without other indicators (except if your indicator can work in conjunction with other indicators).
However keep your main chart (your current chart layout when publishing the indicator) free of any other indicators/elements, it should only contain the price and your indicator, its the first image people we'll see and if it include other indicators then people will have an hard time telling which one is the one you are publishing.
It can be interesting to show your indicators using different settings rather than the one put by default. You can make use of figures such as arrows or crosses in order to highlight the signals made by your indicator.
Additional Advices
Use a bold text when declaring the title of a section, use italic with text that represent code, formulas, variables names.
You can first write your indicator description on a text editor.
If your code include another work from tradingview don't forget to mention it in the description, for example :
this indicator make use of the "name of the indicator" made by "name of the author" that you can find here "tradingview link of the indicator".
In this case make sure you have the consent of the author of the indicator you are using. This is no option, else you can have your indicator taken down.
Don't include bitcoin wallets/donation links in your indicator description, those go on your signature and nowhere else.
Knowing when not to make a description is important, and by that i mean knowing when not to publish, make sure to read the house rules before you are publishing an indicator.
If you are still struggling then you can try to read other indicators description on the net, if you want a deadly serious description then you can learn from the structure of research papers. For example if your indicator is similar to a moving average try reading descriptions of moving average indicators and inspire yourself from them.
Conclusion
Making a good indicator description is essential, the benefits of doing it are enormous, don't forget that scripts take space in servers. If you are publishing a script and that you know that your description is to short and doesn't explain the aim of your indicator then you are just adding more work for the moderators, its not nice.
Also great descriptions allow you to gain trust and credibility in the community, and more importantly it shows that you care about your indicator, i might be harsh but people that does not care about their indicators does not deserve to publish them (however its not case with you nice reader, right ?).
I hope i can help users with this post, its an important subject, if you have doubts about your indicator description try seeking for help in the pinescript chat, you can even contact me, i will be happy to help.
Thanks for reading, and if you plan to use those advices then thanks for caring, love u :kokoro:






















