# RSI (sma) code, does it look correct?

104

Hi, am programming in C# and wants to use my code to backtest RSI settings that I then wish to program in MQL.
DOES THE CALCULATION LOOK CORRECT BELOW? I'm using SMA-method for the calculation.

In the example below I will have a setting of RSI 5

1) Calculate ChangeUp and ChangeDown for each bar by looking at current bar closeprice vs diff of previous bar closeprice.

2) For current bar RSI I add up all ChangeDown(and same for ChangeUp) for the last 4 bars + current (yes, I won't know RSI until current bar closes) / N bars. This gives me an AverageUp and AverageDown for the current bar.

3) Calculate RS by taking AverageUp/AverageDown.

Then RSI for the CURRENT bar is = (100 - (100 / (1 + RS)));

ANY COMMENTS ON MY CALCULATIONS CORRECTNESS?

15901

Johan Strandberg: 2) For current bar RSI I add up all ChangeDown(and same for ChangeUp) for the last 4 bars + current (yes, I won't know RSI until current bar closes) / N bars.
That isn't the definition. It's not a SMA. The ChangeUp is a (exponential) moving average.
Average Gain = [(previous Average Gain) x 13 + current Gain] / 14.

That isn't the definition. Look at the MT4 code

```diff=close[i]-close[i-1];
ExtPosBuffer[i]=(ExtPosBuffer[i-1]*(InpRSIPeriod-1)+(diff>0.0?diff:0.0))/InpRSIPeriod;
ExtNegBuffer[i]=(ExtNegBuffer[i-1]*(InpRSIPeriod-1)+(diff<0.0?-diff:0.0))/InpRSIPeriod;
if(ExtNegBuffer[i]!=0.0)
ExtRSIBuffer[i]=100.0-100.0/(1+ExtPosBuffer[i]/ExtNegBuffer[i]);
```
2837

Johan Strandberg:

Hi, am programming in C# and wants to use my code to backtest RSI settings that I then wish to program in MQL.
DOES THE CALCULATION LOOK CORRECT BELOW? I'm using SMA-method for the calculation.

In the example below I will have a setting of RSI 5

1) Calculate ChangeUp and ChangeDown for each bar by looking at current bar closeprice vs diff of previous bar closeprice.

2) For current bar RSI I add up all ChangeDown(and same for ChangeUp) for the last 4 bars + current (yes, I won't know RSI until current bar closes) / N bars. This gives me an AverageUp and AverageDown for the current bar.

3) Calculate RS by taking AverageUp/AverageDown.

Then RSI for the CURRENT bar is = (100 - (100 / (1 + RS)));

ANY COMMENTS ON MY CALCULATIONS CORRECTNESS?

Understanding RSI calculation
• 2017.09.01
• www.mql5.com
104

whroeder1:
That isn't the definition. It's not a SMA. The ChangeUp is a (exponential) moving average.

That isn't the definition. Look at the MT4 code

Thanks Whroeder1, the coin dropped now I think. Really appreciate your help.

So I've been using this calculation and choose the SMA-calculation of AverageUp and AverageDown whereas the MT4 indicator uses an EMA-calculation of AverageUp and AverageDown.

Thanks

104

Fernando Carreiro:

61793

Johan Strandberg:

Thanks Whroeder1, the coin dropped now I think. Really appreciate your help.

So I've been using this calculation and choose the SMA-calculation of AverageUp and AverageDown whereas the MT4 indicator uses an EMA-calculation of AverageUp and AverageDown.

Thanks

In that case you are using what is known as Cutler's RSI

PS: original Welles Wilder RSI uses what is sometimes known as Wilders EMA (like bellow) not ema like in the version you are talking about :

```EMA formula = price today * K + EMA yesterday * (1-K) where K = 2 / (N+1)

Wilder EMA formula = price today * K + EMA yesterday (1-K) where K =1/N

Where N = the number of periods.
```
Even though they look different, Wilders EMA and SMMA are producing same results, so you can use SMMA too
2837

In that case you are using what is known as Cutler's RSI

PS: original Welles Wilder RSI uses what is sometimes known as Wilders EMA (like bellow) not ema like in the version you are talking about :

Even though they look different, Wilders EMA and SMMA are producing same results, so you can use SMMA too

Be careful of this OP! I provided similar information to him (see below) on his other thread and I was insulted by him for it, with the following (which he has since removed):

Thank you mr. Nerd ... Wow, you know how to google ... did it ever struck that I also know how to google and asked a specific question which you didn't answer nor provided any clue on how to solve ... Crappy answer my friend, crappy answer indeed.

Understanding RSI calculation

Fernando Carreiro, 2017.09.01 13:35

Have a look at WikiPedia's page on RSI (Relative Strength Indicator) which offers you a complete explanation of how it is calculated.

You can also consult the original source, which is the 1978 book by J. Welles Wilder, New Concepts in Technical Trading Systems, that has an example step-by-step breakdown on how it is calculated.

Understanding RSI calculation

Johan Strandberg, 2017.09.08 14:28

You do know that RSI AveageUp and AverageDown can be calculated using SMA, EMA and Wilders Smooting right?

61793

Fernando Carreiro:

Be careful of this OP! I provided similar information to him (see below) on his other thread and I was insulted by him for it, with the following (which he has since removed):

Don't worry :) I am not known for being too quiet :)
2837

Mladen Rakic: Don't worry :) I am not known for being to quiet :)

OK!

104

Fernando Carreiro:

OK!

Forgive me for being rude but I just wasn't interested in a book to read, I was interested in somebody who had knowledge who could tell me if my code looks correctly based on the information I've already read and now posted.

Feel free to move on and help other people instead of me and also stop spamming my thread.

Kind Regards

104

In that case you are using what is known as Cutler's RSI

PS: original Welles Wilder RSI uses what is sometimes known as Wilders EMA (like bellow) not ema like in the version you are talking about :

Even though they look different, Wilders EMA and SMMA are producing same results, so you can use SMMA too