如何解决将 RSI 从 pinescript 转换为 C#?
从Trading View,我们可以看到 rsi 可以用 pinescript 写成如下:
pine_rsi(x,y) =>
u = max(x - x[1],0) // upward change
d = max(x[1] - x,0) // downward change
rs = rma(u,y) / rma(d,y)
rsi = 100 - 100 / (1 + rs)
我已经用 C# 重写了这个:
public static double RelativeStrengthIndex(List<double> input,int samples)
{
List<double> gains = new List<double>();
List<double> losses = new List<double>();
for (int i = input.Count - samples; i < input.Count; i++)
{
double change = input[i] - input[i - 1];
gains.Add(change >= 0 ? change : 0);
losses.Add(change < 0 ? -1 * change : 0);
}
double rs = RollingMovingAverage(gains,samples) / RollingMovingAverage(losses,samples);
return 100 - 100 / (1 + rs);
}
但是,我的结果并不相同。它们的差异足以排除数据差异(来自交易视图和其他提供商的数据可能略有不同)。我一直在努力解决这个问题,但已经完全放弃了。
有谁知道为什么我的代码会产生不同的结果?
解决方法
我遇到了完全相同的问题,并发现 RSI 是基于 Rolling Moving Average (RMA) 的,它是一个累积函数。对于 14 周期 RSI,您需要大约 100 根柱线才能使其稳定。我不得不将值放入一个 excel 电子表格中来解决这个问题,并得到一个与 Pinescript 匹配的公式。
我最终基于此编写了自己的 C# RSI 函数并进行了测试,结果与 pinescript 相同。基类 AnalyzableBase 来自 Trady - 一个开源指标框架。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using HodlBot.Common.Extensions;
using Trady.Analysis;
using Trady.Analysis.Infrastructure;
using Trady.Core.Infrastructure;
namespace HodlBot.Lite.Strategy
{
public class FastRsi<TOutput> : AnalyzableBase<IOhlcv,IOhlcv,decimal?,TOutput>
{
private readonly List<IOhlcv> _inputs;
public int Period { get; }
private List<decimal?> _rsi = new List<decimal?>();
private int _periodMinus1;
private List<DateTimeOffset> _dateTimes;
private decimal _lastGain = 0;
private decimal _lastLoss = 0;
public FastRsi(IEnumerable<IOhlcv> inputs,int period) : base(inputs,i => i)
{
_inputs = inputs.ToList();
_dateTimes = _inputs.Select(x => x.DateTime).ToList();
Period = period;
_rsi.Add(null);
_periodMinus1 = period - 1;
// RMA_Gain=((Gain*(Period-1)) + RMA_Gain[i-1])/Period
for (int i = 1; i < _inputs.Count; i++)
{
decimal change = _inputs[i].Close - _inputs[i-1].Close;
decimal gain = change > 0 ? change : 0;
decimal loss = change < 0 ? -change : 0;
decimal rmaGain = ((_lastGain * _periodMinus1) + gain) / period;
decimal rmaLoss = ((_lastLoss * _periodMinus1) + loss) / period;
decimal rs = rmaLoss == 0 ? 100 : rmaGain / rmaLoss;
decimal rsi = 100 - (100 / (1 + rs));
_rsi.Add(i < period ? null : (decimal?)rsi);
_lastGain = rmaGain;
_lastLoss = rmaLoss;
}
}
public FastRsi<TOutput> AddOhlcv(IOhlcv ohlc)
{
_inputs.Add(ohlc);
_dateTimes.Add(ohlc.DateTime);
IReadOnlyList<IOhlcv> mappedInputs = _inputs;
IReadOnlyList<DateTimeOffset> mappedDateTimes = _dateTimes;
// Trady base class needs these to be able to compute a single index.
// TODO: Set these
typeof(FastRsi)
.GetField("_mappedInputs",BindingFlags.Instance | BindingFlags.NonPublic)
.SetValue(this,mappedInputs);
typeof(AnalyzableBase<IOhlcv,TOutput>)
.GetField("_mappedDateTimes",mappedDateTimes);
int i = _mappedInputs.Count - 1;
decimal change = i > 0 ? _mappedInputs[i].Close - _mappedInputs[i - 1].Close : 0;
decimal gain = change > 0 ? change : 0;
decimal loss = change < 0 ? -change : 0;
decimal rmaGain = ((_lastGain * _periodMinus1) + gain) / Period;
decimal rmaLoss = ((_lastLoss * _periodMinus1) + loss) / Period;
decimal rs = rmaLoss == 0 ? 100 : rmaGain / rmaLoss;
decimal rsi = 100 - (100 / (1 + rs));
_rsi.Add(i < Period ? null : (decimal?)rsi);
_lastGain = rmaGain;
_lastLoss = rmaLoss;
return this;
}
protected override decimal? ComputeByIndexImpl(IReadOnlyList<IOhlcv> mappedInputs,int index)
{
return _rsi[index];
}
}
public class FastRsi : FastRsi<AnalyzableTick<decimal?>>
{
public FastRsi(IEnumerable<IOhlcv> inputs,period)
{
}
}
}
my implementation and reasoning here 上还有一些内容。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。