Calculate the Relative Strength Index (RSI)

different result when Calculate RSI from excel sheet or from web link

excel sheet formulas
=IF(B3>B4,ABS(B3-B4),0)
=IF(B3<B4,ABS(B3-B4),0)
=(D32/(F$2+1))+(F4(1-(2/(F$2+1))))
=(E32/(G$2+1))+(G4(1-(2/(G$2+1))))
=100-(100/(1+(F3/G3)))

code in AB as excel sheet formulas

arryexl=Close;
perRSEX= Param( "Periods", 14, 2, 200, 1 );
CU=IIf( Close > Ref(Close, -1), abs(arryexl), 0); //Positive close 
CD=IIf( Close < Ref(Close, -1), abs(arryexl), 0); // Negative close 
UpLength =(CU*(2/(perRSEX+1)))+((Ref(CU,-1)*(1-(2/(perRSEX+1)))));
DownLength=(CD*(2/(perRSEX+1)))+((Ref(CD,-1)*(1-(2/(perRSEX+1)))));
rsiexl=100-(100/(1+(UpLength/DownLength)));


link for RSI formula

https://www.incrediblecharts.com/indicators/rsi_relative_strength_index.php

code in AB as link for RSI formula

arry = Close ; /// ///
perRS= Param( "Periods", 14, 2, 200, 1 );
Parry =IIf( Close > Ref(Close, -1), abs(arry), 0); //Positive close 
Narry = IIf( Close < Ref(Close, -1), abs(arry), 0); // Negative close 
totalup=Sum(Parry, perRS);
testdw=Sum(Narry, perRS);
avgup=EMA(totalup,perRS);
avgdw=EMA(testdw,perRS);
R_S= avgup/avgdw  ;
TESTRSI= 100-(100/(1+R_S));

AB RSI built in

REALRSI=RSI(perRS);

but the result is different

test3

what is the right formula for RSI without using loop

full code

_SECTION_BEGIN("RSi test ");

arryexl=Close;
perRSEX= Param( "Periods", 14, 2, 200, 1 );
CU=IIf( Close > Ref(Close, -1), abs(arryexl), 0); //Positive close 
CD=IIf( Close < Ref(Close, -1), abs(arryexl), 0); // Negative close 
UpLength =(CU*(2/(perRSEX+1)))+((Ref(CU,-1)*(1-(2/(perRSEX+1)))));
DownLength=(CD*(2/(perRSEX+1)))+((Ref(CD,-1)*(1-(2/(perRSEX+1)))));
rsiexl=100-(100/(1+(UpLength/DownLength)));








arry = Close ; /// ///
perRS= Param( "Periods", 14, 2, 200, 1 );
Parry =IIf( Close > Ref(Close, -1), abs(arry), 0); //Positive close 
Narry = IIf( Close < Ref(Close, -1), abs(arry), 0); // Negative close 
totalup=Sum(Parry, perRS);
testdw=Sum(Narry, perRS);
avgup=EMA(totalup,perRS);
avgdw=EMA(testdw,perRS);
R_S= avgup/avgdw  ;
TESTRSI= 100-(100/(1+R_S));

REALRSI=RSI(perRS);


Filter=1;

AddColumn(Close,"Close",1.2);
AddColumn(TESTRSI,"TESTRSI",1.2);
AddColumn(REALRSI,"REALRSI",1.2);
AddColumn(rsiexl,"rsiexl",1.2);
 Plot( TESTRSI, "TESTRSI",colorBlack, styleLine | styleThick );
 Plot( REALRSI, "REALRSI",colorBlack, styleLine | styleThick );
 
 
_SECTION_END();

thank you

@needhelp you need to research your indicators as you are using the wrong formula for Welles Wilder's RSI. He did not use a regular exponential moving average to smooth his calculations. He used what is called a "Wilders smoothing".

Unlike a regular exponential moving average with 2/(Period+1) smoothing factor, Wilder used 1/Period.

For an afl looping version of the RSI perhaps you should look at the user guide?
http://amibroker.com/guide/afl/rsi.html

And if you want to see the difference between an ema and wilders, then you could use
https://www.amibroker.com/guide/afl/wilders.html

I think the problem that i face is

the "previous day's EMA" for your first calculation will be the SMA or using
close price as in excel sheet

calculate the simple moving average for the initial value

how to use initial value in the calculation

thank you

Why are worrying about that, why don’t you just use EMA or RSI or Wilders function for that matter. it does all the calculation for you. Reinventing the wheel is pointless.

2 Likes

you know that some the custom indicator need special array.

like the initial value

also like calculation for SLOW Stochastic K% and D%

PRSt = Param( "PRSSO", 5, 2, 200, 1 );
loper=LLV(L,PRSt);
 hipe=HHV (H,PRSt);
 lo_kslow=(hipe-loper)+(Ref(hipe,-1)-Ref(loper,-1))+(Ref(hipe,-2)-Ref(loper,-2));
 hi_kslow=(Close-loper)+(Ref(Close,-1)-Ref(loper,-1))+(Ref(Close,-2)-Ref(loper,-2));
Slow_K=100*(lo_kslow/hi_kslow);
Slow_D=Sum(Slow_K,3)/3;

like this calculation
hi_kslow=(Close-loper)+(Ref(Close,-1)-Ref(loper,-1))+(Ref(Close,-2)-Ref(loper,-2));

thank you

I try to use for

lo_kslowA= Null; 
for ( j = 0, lo_kslowA = 0; j < 3; j++) {
	lo_kslowA += (hipe - Ref(loper,-j));     
}

to calculate this one

lo_kslow=(hipe-loper)+(Ref(hipe,-1)-Ref(loper,-1))+(Ref(hipe,-2)-Ref(loper,-2));

but not working with me

any help

https://forum.amibroker.com/t/math-101-fast-stochastics/6967

As you can see Fast %D is the same as Slow %K. And Slow %D is actually double smoothed Fast %K.

stochastics

slow %K can not be as the fast.
logic why there is Slow %K if it is the same as Fast %D

if we use for Slow %K

PRSt = Param( "PRSSO", 5, 2, 200, 1 );
loper=LLV(L,PRSt);
hipe=HHV (H,PRSt);
lo_kslow=(hipe-loper)+(Ref(hipe,-1)-Ref(loper,-1))+(Ref(hipe,-2)-Ref(loper,-2));
hi_kslow=(Close-loper)+(Ref(Close,-1)-Ref(loper,-1))+(Ref(Close,-2)-Ref(loper,-2));
Slow_K=100*(lo_kslow / hi_kslow);

and the formula is using for Slow %K

https://forum.amibroker.com/t/math-101-fast-stochastics/6967

As you can see Fast %D is the same as Slow %K. And Slow %D is actually double smoothed Fast %K.

stochastics

slow %K can not be as the fast.
logic why there is Slow %K if it is the same as Fast %D

if we use Slow %K this the formula that i should use for Slow %K

PRSt = Param( "PRSSO", 5, 2, 200, 1 );
loper=LLV(L,PRSt);
 hipe=HHV (H,PRSt);
 lo_kslow=(hipe-loper)+(Ref(hipe,-1)-Ref(loper,-1))+(Ref(hipe,-2)-Ref(loper,-2));
 hi_kslow=(Close-loper)+(Ref(Close,-1)-Ref(loper,-1))+(Ref(Close,-2)-Ref(loper,-2));
Slow_K=100*(lo_kslow / hi_kslow);

but i am not sure from the code

I wrote you already: you codes are wrong, instead of using them, use use AMA / AMA2 buil in AmiBroker functions functions for all those self referencing / recursive indicators. And do read the documentation. As to magic initial value you can just use input[0].

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.