General Topics
Markets
Technical Topics
Brokerage Firms
Community Lounge
Site Support

# How to calulate RSI in C# ?

Discussion in 'Automated Trading' started by jonnysharp, Jun 23, 2008.

1. ### jonnysharp

Hi everyone,

Could someone please provide me with the code of calulating RSI(relative strength index) in C#, i'm having difficulties in trying to achieve this;

The following are the steps to calculate RSI;

1. Calculate the advances and declines of the closing price (differences between Current day closing price and previous day closing price).

If Current Day Closing Price is greater than Previous Day Closing Price then
Advance = Current Day closing price - Previous Day closing price

If Current Day Closing Price is Less than Previous Day Closing Price then
Decline = Previous Day closing price - Current Day closing price

2. Calculate the Average gains and losses (14 periods)

Average gain = Total Gain for the past 14 days / 14
Average Loss = Total Loss for the past 14 days / 14

3. Relative Strength (RS)

Relative Strength = Average Gain / Average Loss

4. RSI RSI

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

Any help would be greatly appreciated.
Thankyou,
Jonny.

2. ### dcraig

can't vouch for whether you have the right procedure for advance/declines or RSI bc I don't use those.

here's the codified version of your procedure as provided in c# however:

BarList bl = new BarList(BarInterval.Day);
int lookback = 14;
decimal sumdecline = 0;

for (int i = bl.NumBars()-1; (bl.NumBars()>=lookback) && (i>bl.NumBars()-1-lookback); i--)

{
Bar current = bl.Get(i);
Bar prevday = bl.Get(i-1);

if (current.Close < prevday.Close)
sumdecline += prevday.Close - current.Close;
else if (current.Close > prevday.Close)