gpt4 book ai didi

c# - RSI vs Wilder 的 RSI 计算问题

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:19:07 25 4
gpt4 key购买 nike

我无法获得平滑的 RSI。下图来自 freestockcharts.com。计算使用此代码。

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));
}

https://stackoverflow.com/questions/...th-index-using-some-programming-language-js-c

这似乎是没有平滑的纯 RSI。如何计算平滑的 RSI?我尝试更改它以适应这两个站点的定义,但输出不正确。它几乎没有被平滑。

(我没有足够的代表发布链接)

tc2000 -> Indicators -> RSI_and_Wilder_s_RSI (Wilder's smoothing = Previous MA value + (1/n periods * (Close - Previous MA)))

priceactionlab -> wilders-cutlers-and-harris-relative-strength-index (RS = EMA(Gain(n), n)/EMA(Loss(n), n))

有人真的可以用一些样本数据进行计算吗?

Wilder's RSI vs RSI enter image description here

最佳答案

为了计算 RSI,您需要一个周期来计算它。 As noted on Wikipedia, 14 is used quite often.

所以计算步骤如下:

周期 1 - 13,RSI = 0

第 14 期:

AverageGain = TotalGain / PeriodCount;
AverageLoss = TotalLoss / PeriodCount;
RS = AverageGain / AverageLoss;
RSI = 100 - 100 / (1 + RS);

第 15 期 - 至第 (N) 期:

if (Period(N)Change > 0
AverageGain(N) = ((AverageGain(N - 1) * (PeriodCount - 1)) + Period(N)Change) / PeriodCount;
else
AverageGain(N) = (AverageGain(N - 1) * (PeriodCount - 1)) / PeriodCount;

if (this.Change < 0)
AverageLoss(N) = ((AverageLoss(N - 1) * (PeriodCount - 1)) + Math.Abs(Period(N)Change)) / PeriodCount;
else
AverageLoss(N) = (AverageLoss(N - 1) * (PeriodCount - 1)) / PeriodCount;

RS = AverageGain / AverageLoss;
RSI = 100 - (100 / (1 + RS));

此后,为了平滑这些值,您需要将特定时期的移动平均线应用到您的 RSI 值。为此,将 RSI 值从最后一个指数遍历到第一个指数,并根据前面的 x 个平滑周期计算当前周期的平均值。

完成后,只需反转值列表即可获得预期顺序:

List<double> SmoothedRSI(IEnumerable<double> rsiValues, int smoothingPeriod)
{
if (rsiValues.Count() <= smoothingPeriod)
throw new Exception("Smoothing period too large or too few RSI values passed.");

List<double> results = new List<double>();
List<double> reversedRSIValues = rsiValues.Reverse().ToList();

for (int i = 1; i < reversedRSIValues.Count() - smoothingPeriod - 1; i++)
results.Add(reversedRSIValues.Subset(i, i + smoothingPeriod).Average());

return results.Reverse().ToList();
}

Subset方法只是一个简单的扩展方法如下:

public static List<double> Subset(this List<double> values, int start, int end)
{
List<double> results = new List<double>();

for (int i = start; i <= end; i++)
results.Add(values[i]);

return results;
}

免责声明,我没有测试代码,但它应该让您了解如何应用平滑。

关于c# - RSI vs Wilder 的 RSI 计算问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38481354/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com