使用某种编程语言(JS/C#..)计算 RSI(相对强弱指数)
Posted
技术标签:
【中文标题】使用某种编程语言(JS/C#..)计算 RSI(相对强弱指数)【英文标题】:Calculate RSI(Relative Strength Index) using some programming language (JS/C#..) 【发布时间】:2014-05-02 19:17:02 【问题描述】:我正在计算RSI
(Relative Strength Index)
。我有这样的数据
**Date|Close|Change|Gain|Loss**
计算公式是
RSI = 100 - 100/(1+RS)
where RS = Average Gain / Average Loss
Source
所以我想通过javascript
或C#
中的某种编程语言进行计算,但我不知道如何将其转换为编程语言或我需要哪些步骤。
如果你有什么想进一步了解我的问题,我会尽力解释。
【问题讨论】:
【参考方案1】:转换 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));
有很多项目以不同的方式实施 RSI。 An incremental way can be found here
【讨论】:
@AmirFo 在这段代码中使用double Tolerance = 10e-20
只是不要在下面的划分中得到错误。
对于RelativeStrength,我们需要将它除以总增益的平均值和总损失的平均值。所以我觉得在上面的代码中我们需要更改var relativeStrength = (sumGain/Period) / (sumLoss/Period )..来自投资百科的参考。 RSI =100−[ 100/( 1+ (平均损失/平均增益)) ] investopedia.com/terms/r/rsi.asp【参考方案2】:
这应该与 Riga 的答案没有什么不同,但它似乎永远不会低于 40,所以要小心,也许只坚持使用 TA_LIB?
//Relative Strength Index
function rsi($ar, $period, $opt, $offset=0) //opt: 0=none, 1=exponential, 2=wilder, 3=average all
GLOBAL $smoothsteps;
$pag = 0; //Previous Average Losses
$pal = 0; //Previous Average Gains
//Count average losses and gains
$len = sizeof($ar)-1-$offset;
$end = $len-$period-$offset;
for($i = $len; $i > $end; $i--)
if($ar[$i] > $ar[$i-1]) //Gain
$pag += $ar[$i] - $ar[$i-1];
else //Loss
$pal += $ar[$i-1] - $ar[$i];
$pag /= $period;
$pal /= $period;
//Smooth
$ag = 0; //Average Losses
$al = 0; //Average Gains
for($i = $len; $i > 0; $i--)
if($ar[$i] > $ar[$i-1]) //Gain
$ag += $ar[$i] - $ar[$i-1];
else //Loss
$al += $ar[$i-1] - $ar[$i];
if($opt == 3) //Average All Three
$a = 1 / $smoothsteps;
$tag = $a * $ag + (1 - $a) * $pag;
$tal = $a * $al + (1 - $a) * $pal;
$wag = $pag * 13 + $ag;
$wal = $pal * 13 + $al;
$ag = ($wag+$tag+$pag)/3;
$al = ($wal+$tal+$pal)/3;
else if($opt == 2) //Wilder Exp
$ag = $pag * 13 + $ag;
$al = $pal * 13 + $al;
else if($opt == 1) //Exponential (Lame) [Closest to Trading View]
$sa = 1 / $smoothsteps;
$ag = $sa * $ag + (1 - $sa) * $pag;
$al = $sa * $al + (1 - $sa) * $pal;
else if($opt == 0) //None
$ag = $pag;
$al = $pal;
//Relative Strength
$rs = $ag / $al;
//Relative Strength Index
return 100 - (100 / (1+$rs));
【讨论】:
【参考方案3】:我将用伪代码编写它,您可以轻松地用任何语言编写它。 最短的编码方式是:
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 )
这可能是在模拟中应用 RSI 的最有效方式。
【讨论】:
以上是关于使用某种编程语言(JS/C#..)计算 RSI(相对强弱指数)的主要内容,如果未能解决你的问题,请参考以下文章