使用某种编程语言(JS/C#..)计算RSI(相对强弱指数)

Blu*_*Blu 2 javascript c# stocks stock

我正在努力计算RSI (Relative Strength Index).我有这样的数据

**Date|Close|Change|Gain|Loss**
Run Code Online (Sandbox Code Playgroud)

计算它的公式是

RSI = 100 - 100/(1+RS)
where RS = Average Gain / Average Loss
Run Code Online (Sandbox Code Playgroud)

资源

所以我想通过一些编程语言来计算JavaScript或者C#我不知道如何在编程语言中转换它或我需要什么步骤.

如果你想要更多地了解我的问题,我会尝试解释.

Rig*_*iga 6

翻译RSI公式的简单方法:

    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));
    }
Run Code Online (Sandbox Code Playgroud)

有很多项目以不同的方式实施RSI.可以在此处找到增量方式


mm4*_*441 0

我将用伪代码编写它,您可以轻松地用任何语言编写它。最短的编码方式是:

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 )
Run Code Online (Sandbox Code Playgroud)

这可能是在模拟中应用 RSI 的最有效方法。