将 RSI 从 pinescript 转换为 C#?

问题描述 投票:0回答:3

交易视图,我们看到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);
}

然而,我的结果并不等同。它们的差异足以排除数据差异(交易视图和其他提供商的数据可能略有不同)。我已经尝试解决这个问题一段时间了,但已经完全放弃了。

有谁知道为什么我的代码会产生不同的结果?

c# finance pine-script tradingview-api
3个回答
2
投票

我遇到了完全相同的问题,发现 RSI 是基于 滚动移动平均线 (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, IOhlcv, decimal?, TOutput>)
                .GetField("_mappedDateTimes", BindingFlags.Instance | BindingFlags.NonPublic)
                .SetValue(this, 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, int period) : base(inputs, period)
        {
        }
    }
}

这里还有更多内容我的实施和推理.


0
投票

您没有处理初始 MA 周期,您需要考虑第一个 RSI 值。

我使用给定股票代码的历史数据为 RSI 和 StochRSI 编写的以下方法

public DataTable getRSIDataTableFromDaily(string symbol, string exchange, string seriestype = "CLOSE", string outputsize = "Compact", string time_interval = "1d",
            string fromDate = null, string period = "14", bool stochRSI = false)
        {
            DataTable dailyTable = null;
            //DataTable rsiDataTable = null;
            int iPeriod;
            double change, gain, loss, avgGain = 0.00, avgLoss = 0.00, rs, rsi;
            double sumOfGain = 0.00, sumOfLoss = 0.00;
            //DateTime dateCurrentRow = DateTime.Today;
            List<string> seriesNameList;
            try
            {
            //GetStockPriceData returns data table with historical OHLC values for period specified
                dailyTable = GetStockPriceData(symbol, exchange, seriestype, outputsize, time_interval, fromDate, sqlite_cmd: null);
                if ((dailyTable != null) && (dailyTable.Rows.Count > 0))
                {
                    iPeriod = System.Convert.ToInt32(period);
                    DataColumn newCol;
                    if (stochRSI == false)
                    {
                    //If caller wants only RSI then we will only use the seriestype specified by caller to calculate the RSI for specified period
                        newCol = new DataColumn("RSI_" + seriestype, typeof(decimal));
                        newCol.DefaultValue = 0.00;

                        dailyTable.Columns.Add(newCol);
                        seriesNameList = new List<string> { seriestype };
                    }
                    else
                    {
                    //If caller wants StochRSI then we need to include all of OHLC values
                        newCol = new DataColumn("RSI_OPEN", typeof(decimal));
                        newCol.DefaultValue = 0.00;

                        dailyTable.Columns.Add(newCol);
                        newCol = new DataColumn("RSI_CLOSE", typeof(decimal));
                        newCol.DefaultValue = 0.00;

                        dailyTable.Columns.Add(newCol);
                        newCol = new DataColumn("RSI_HIGH", typeof(decimal));
                        newCol.DefaultValue = 0.00;

                        dailyTable.Columns.Add(newCol);
                        newCol = new DataColumn("RSI_LOW", typeof(decimal));
                        newCol.DefaultValue = 0.00;

                        dailyTable.Columns.Add(newCol);

                        seriesNameList = new List<string> { "CLOSE", "OPEN", "HIGH", "LOW" };
                    }
                    foreach (var item in seriesNameList)
                    {
                        change = gain = loss = avgGain = avgLoss = rs = rsi = 0.00;
                        sumOfGain = sumOfLoss = 0.00;

                        for (int rownum = 1; rownum < dailyTable.Rows.Count; rownum++)
                        {
                            //current - prev
                            //change = System.Convert.ToDouble(dailyTable.Rows[rownum][seriestype]) - System.Convert.ToDouble(dailyTable.Rows[rownum - 1][seriestype]);
                            change = System.Convert.ToDouble(dailyTable.Rows[rownum][item.ToString()]) - System.Convert.ToDouble(dailyTable.Rows[rownum - 1][item.ToString()]);
                            //dateCurrentRow = System.Convert.ToDateTime(dailyTable.Rows[rownum]["TIMESTAMP"]);

                            if (change < 0)
                            {
                                loss = Math.Abs(change);
                                gain = 0.00;
                            }
                            else
                            {
                                gain = change;
                                loss = 0.00;
                            }

                            //for the first iPeriod keep adding loss & gain
                            if (rownum < iPeriod)
                            {
                                sumOfGain += gain;
                                sumOfLoss += loss;
                            }
                            else
                            {
                                if (rownum == iPeriod)
                                {
                                //this means we are at period specified
                                    sumOfGain += gain;
                                    sumOfLoss += loss;
                                    //we also find  other fields and SAVE
                                    avgGain = sumOfGain / iPeriod;
                                    avgLoss = sumOfLoss / iPeriod;
                                    rs = avgGain / avgLoss;
                                    rsi = 100 - (100 / (1 + rs));
                                }
                                else
                                {
                                //this means we are now beyond the period, calculate RSI
                                    avgGain = ((avgGain * (iPeriod - 1)) + gain) / iPeriod;
                                    avgLoss = ((avgLoss * (iPeriod - 1)) + loss) / iPeriod;
                                    rs = avgGain / avgLoss;
                                    rsi = 100 - (100 / (1 + rs));
                                }
                                //dailyTable.Rows[rownum]["RSI"] = Math.Round(rsi, 2);
                                dailyTable.Rows[rownum]["RSI_" + item.ToString()] = Math.Round(rsi, 2);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("getRSIDataTableFromDaily exception: " + ex.Message);

                if (dailyTable != null)
                {
                    dailyTable.Clear();
                    dailyTable.Dispose();
                }
                dailyTable = null;
            }
            return dailyTable;
        }


0
投票

博士Andrew Burnett-Thompson 你是个天才。有用。非常感谢,祝你生活愉快=)

我一直在寻找在您提出的选项中使用 RMA 的 RIS 解决方案,99.99% 给出了相同的结果。

© www.soinside.com 2019 - 2024. All rights reserved.