Deconstructing RSI Indicator Script  [SOLVED]

Questions about MultiCharts and user contributed studies.
TrendFirst
Posts: 66
Joined: 23 Nov 2015
Has thanked: 21 times
Been thanked: 8 times

Deconstructing RSI Indicator Script

Postby TrendFirst » 12 Nov 2018

I am in the process of learning Easylanguage. One of the ways I am doing this is reviewing the scripts for some of the basic indicators. I know how to calculate these by hand or in excel, so the idea is that I should be able to figure out and understand the script. As I do this, I am re-writing the scripts using variable names that make sense to me, rather than the var0, var1, etc, and also adding comments. It is nice that the built in indicators are coded in a consistent fashion, but I find them easier to understand with more meaningful variable names.

So, if you have patience and are willing to help, thanks in advance. This will likely bore more experienced coders, so apologies for that.

The formula for RSI is pretty straightforward from a math standpoint.

RSI = 100 - (100/(1 + RS)

where RS = (Avg Up Closes for X periods) / (Avg Down Closes for X periods)

And then there is the script, repeated below.

Code: Select all

inputs:
PriceValue( numericseries ),
Len( numericsimple ) ;

variables:
var0( 0 ),
var1( 0 ),
var2( 0 ),
var3( 1 / Len ),
var4( 0 ) ;

if CurrentBar = 1 then
begin
var0 = ( PriceValue - PriceValue[Len] ) / Len ;
var1 = Average( AbsValue( PriceValue - PriceValue[1] ), Len ) ;
end
else
begin
var2 = PriceValue - PriceValue[1] ;
var0 = var0[1] + var3 * ( var2 - var0[1] ) ;
var1 = var1[1] + var3 * ( AbsValue( var2 ) - var1[1] ) ;
end ;

if var1 <> 0 then
var4 = var0 / var1
else
var4 = 0 ;

RSI = 50 * ( var4 + 1 ) ;
It contains the usual inputs and variables, plus two If/Then/Else statements. It's the If/Then/Else statements I don't understand. Particularly the use of CurrentBar, which is apparently connected to MaxBarsBack. This makes no sense to me. If I have MaxBarsBack set to 200 because I am using a 200day EMA as a filter, it seems this would totally screw up my RSI calculation. Perhaps the definition of CurrentBar should be (or is) related to the number of periods used in the RSI calculation instead of MaxBarsBack? But that's not what the definition says, hence my confusion.

I have many more questions, but perhaps we should start with that one, as it may clear up some of the other questions. Again, thanks in advance.

User avatar
TJ
Posts: 7151
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 988 times
Been thanked: 2041 times

Re: Deconstructing RSI Indicator Script

Postby TJ » 12 Nov 2018

Have you done an audit of the results?
Did the RSI script make any errors?

TrendFirst
Posts: 66
Joined: 23 Nov 2015
Has thanked: 21 times
Been thanked: 8 times

Re: Deconstructing RSI Indicator Script

Postby TrendFirst » 12 Nov 2018

Hi TJ

Thanks for replying. No, I have not done an audit. I have no reason to believe the results are inaccurate. I'm just trying to understand how the code is working.

User avatar
TJ
Posts: 7151
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 988 times
Been thanked: 2041 times

Re: Deconstructing RSI Indicator Script

Postby TJ » 12 Nov 2018

How does the Currentbar "screw up" your calculation?
Are you looking for RSI in the first 200 bars of the chart?

TrendFirst
Posts: 66
Joined: 23 Nov 2015
Has thanked: 21 times
Been thanked: 8 times

Re: Deconstructing RSI Indicator Script

Postby TrendFirst » 12 Nov 2018

How does the Currentbar "screw up" your calculation?
Are you looking for RSI in the first 200 bars of the chart?
No, my typical RSI periods are in the 2 - 14 range.

In the definition of CurrentBar they reference MaxBarsBack. The only place I had seen that previously was in the Signals/Properties area, where there is a "Maximum number of bars the study will reference". So I assumed that was what they were referring to in the CurrentBar definition. And since in many of my signals I will use a 200 day ema as a filter, it made no sense relative to RSI.

Apparently that is not the case. It is used in regard to the length of the period, or number of bars, for that specific study.

I find the dual use of MaxBarsBack to be quite confusing. Be that as it may....... some progress, between my ears, has been made.

Also, in reference to your question about an audit. I answered I have not done one. I know what an accounting audit is. I know what an inventory audit is. Have to admit I haven't a clue what a coding audit is. But my original answer stands, I would guess. :)

User avatar
TJ
Posts: 7151
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 988 times
Been thanked: 2041 times

Re: Deconstructing RSI Indicator Script

Postby TJ » 12 Nov 2018

MaxBarsBack has stumbled many people.

MaxBarsBack is a misnomer. Most people have a misunderstanding of its purpose.

MaxBarsBack simply means the MINIMUM number of bars required to calculate your indicators.

If you have a 200 EMA, the minimum number of bars required is 200.

ie. you will get the first result on bar #201.

if you also have a 14 period RSI on the chart,
the first result of the RSI will also come on bar #201.
These users thanked the author TJ for the post:
TrendFirst

TrendFirst
Posts: 66
Joined: 23 Nov 2015
Has thanked: 21 times
Been thanked: 8 times

Re: Deconstructing RSI Indicator Script  [SOLVED]

Postby TrendFirst » 12 Nov 2018


If you have a 200 EMA, the minimum number of bars required is 200.

ie. you will get the first result on bar #201.

if you also have a 14 period RSI on the chart,
the first result of the RSI will also come on bar #201.
Interesting.....so my first assumption was correct, even tho it confused me to no end. Thank you for that explanation, I did not know that.


Return to “MultiCharts”