0

我不明白为什么,但我的 RSI 总是与 Tradingview 的 RSI 不同。

使用同一时期(14 根蜡烛,每根 15 分钟),我使用相同类型的值(收盘价),我尝试添加最后一个未关闭的蜡烛,但我从未得到相同的 RSI。

Tradingview RSI代码:

//@version=4
study(title="Relative Strength Index", shorttitle="RSI", 
format=format.price, precision=2, resolution="")
len = input(14, minval=1, title="Length")
src = input(close, "Source", type = input.source)
up = rma(max(change(src), 0), len)
down = rma(-min(change(src), 0), len)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
plot(rsi, "RSI", color=#7E57C2)
band1 = hline(70, "Upper Band", color=#787B86)
bandm = hline(50, "Middle Band", color=color.new(#787B86, 50))
band0 = hline(30, "Lower Band", color=#787B86)
fill(band1, band0, color=color.rgb(126, 87, 194, 90), title="Background")

我的 TA-Lib 代码

MInteger outBegIdx = new MInteger();
MInteger outNbElement = new MInteger();
double[] outReal = new double[array.length-1];
int startIdx = 0;
int endIdx = array.length - 1;

Core core = new Core();
core.rsi(startIdx, endIdx, array, length-1, outBegIdx, outNbElement, outReal);
System.out.println(Arrays.toString(outReal));

return outReal[0];

我没有插件的自定义代码

 double av_gain_up_periods = 0;
 double av_loss_down_periods = 0;
 int gain_count = 0;
 int loss_count = 0;
 double previous_observation = array[0];

 for (int i = 1; i < array.length; i++) {
            if (previous_observation <= array[i]) { // if gain
                double gain = array[i] - previous_observation;
                gain_count++;
                av_gain_up_periods += gain;
            }
            else { // if loss
                double loss = previous_observation - array[i];
                loss_count++;
                av_loss_down_periods += loss;
            }
            previous_observation = array[i];
        }
        av_gain_up_periods = av_gain_up_periods/gain_count;
        av_loss_down_periods = av_loss_down_periods/loss_count;

        // CALCULATE RSI
        double relative_strength = av_gain_up_periods/av_loss_down_periods;
        double relative_strength_index = 100-(100/(1+relative_strength));

        // PRINT RESULT
        return relative_strength_index;

我可以向您保证,我有 14 个收盘价,它们与 Tradingview 的相同。区别在于计算。

与此问题相关

谢谢

4

0 回答 0