I am working to calculate RSI
(Relative Strength Index)
. I have data like this
**Date|Close|Change|Gain|Loss**
The formula for calculating this is
RSI = 100 - 100/(1+RS)
where RS = Average Gain / Average Loss
Source
So I want to calculate via some programming language either in JavaScript
or C#
but i don't know exactly how to convert that in programming language or what steps do I need.
If there is anything you want more to understand my problem i will try to explain.
I am working to calculate RSI
(Relative Strength Index)
. I have data like this
**Date|Close|Change|Gain|Loss**
The formula for calculating this is
RSI = 100 - 100/(1+RS)
where RS = Average Gain / Average Loss
Source
So I want to calculate via some programming language either in JavaScript
or C#
but i don't know exactly how to convert that in programming language or what steps do I need.
If there is anything you want more to understand my problem i will try to explain.
Share Improve this question asked Mar 25, 2014 at 5:38 BluBlu 4,0566 gold badges40 silver badges65 bronze badges2 Answers
Reset to default 15Simple way to translate the RSI formula:
public static double CalculateRsi(IEnumerable<double> closePrices)
{
var prices = closePrices as double[] ?? closePrices.ToArray();
double sumGain = 0;
double sumLoss = 0;
for (int i = 1; i < prices.Length; i++)
{
var difference = prices[i] - prices[i - 1];
if (difference >= 0)
{
sumGain += difference;
}
else
{
sumLoss -= difference;
}
}
if (sumGain == 0) return 0;
if (Math.Abs(sumLoss) < Tolerance) return 100;
var relativeStrength = sumGain / sumLoss;
return 100.0 - (100.0 / (1 + relativeStrength));
}
There are plenty of projects implementing RSI in different ways. An incremental way can be found here
I will write it in a pseudo code that you can easily write it in any languages. shortest way of coding it is:
v0 = 0
v1 = 0
v2 = 0
v3 = 1/N
v4 = 0
if Step == 1: #initialisation
v0 = (Price[t] - Price[t-N] ) / N
v1 = mean( abs( diff(Price[(t-N):t] ) ) # average price change over previous N
else
v2 = Price[t] - Price[t-1]
v0 = vv[t-1] + v3 * ( v2 - v0[t-1] )
v1 = v1[t-1] + v3 * ( abs( v2 ) - v1[t-1] )
if v1 != 0:
v4 = v0 / v1
else
v4 = 0
RSI = 50 * ( v4 + 1 )
This is probably the most efficient way of applying RSI in your simulation.