如何解决从RSI计算在Java TA-lib的输出为零?

时间:2019-02-01 19:39:30

标签: java spring-boot ta-lib

我正在Spring-boot应用程序中使用ta-lib和Java计算RSI。 在花了一些时间弄清楚函数调用参数之后,我设法从计算中获得了输出。但是,rsi函数在输出数组中保持返回零值。为什么返回零?

我正在从Binance的API中获取烛台数据,试图计算给定期间的RSI。我已经尝试了stochasticRSI和RSI,但是都在输出数组中返回零。

Array
(
    [0] => Array
        (
            [title] => 1548457954
            [uploader] => hey
        )

    [1] => Array
        (
            [title] => 1548458360
            [uploader] => root
        )

    [2] => Array
        (
            [title] => first
            [uploader] => root
        )

    [3] => Array
        (
            [title] => first
            [uploader] => root
        )

    [4] => Array
        (
            [title] => first
            [uploader] => root
        )

    [5] => Array
        (
            [title] => first
            [uploader] => root
        )

    [6] => Array
        (
            [title] => first
            [uploader] => root
        )

    [7] => Array
        (
            [title] => first
            [uploader] => root
        )

    [8] => Array
        (
            [title] => first
            [uploader] => root
        )

    [9] => Array
        (
            [title] => first
            [uploader] => root
        )

    [10] => Array
        (
            [title] => first
            [uploader] => root
        )

    [11] => Array
        (
            [title] => RAD
            [uploader] => root
        )

    [12] => Array
        (
            [title] => RAD
            [uploader] => root
        )

    [13] => Array
        (
            [title] => GOOD
            [uploader] => root
        )

    [14] => Array
        (
            [title] => HAVE
            [uploader] => root
        )

    [15] => Array
        (
            [title] => rad
            [uploader] => dboss
        )

    [16] => Array
        (
            [title] => rad
            [uploader] => dboss
        )

    [17] => Array
        (
            [title] => new Image
            [uploader] => dboss
        )

    [18] => Array
        (
            [title] => My beautiful file
            [uploader] => root
        )

    [19] => Array
        (
            [title] => 
            [uploader] => root
        )

    [20] => Array
        (
            [title] => Apple
            [uploader] => root
        )

    [keys] => title
)

我希望计算结果是一个包含首选RSI值的数组,但实际输出是以下数组:

public double[] calculateRsi() {
    int OPT_IN_TIME_PERIOD = 14;

    double closePrices[] = {3553.06005859375,3572.5,3555.070068359375,3589.02001953125,3590.219970703125,3576.39990234375,3591.840087890625,3574.590087890625,3584.0,3579.56005859375,3597.6201171875,3604.949951171875,3616.2099609375,3600.8701171875,3604.010009765625,3599.010009765625,3610.679931640625,3587.909912109375,3594.8701171875,3606.1298828125,3604.39990234375,3695.3798828125,3685.739990234375,3686.530029296875,3665.300048828125,3680.719970703125,3678.8701171875,3580.0,3524.6298828125,3518.0,3539.280029296875,3530.949951171875,3526.830078125,3545.050048828125,3528.169921875,3528.820068359375,3526.89990234375,3523.0,3515.699951171875,3535.14990234375,3577.320068359375,3578.4599609375,3570.929931640625,3581.31005859375,3561.72998046875,3577.800048828125,3549.070068359375,3533.39990234375,3552.820068359375,3532.570068359375,3546.330078125,3563.22998046875,3553.409912109375,3574.929931640625,3569.6201171875,3568.06005859375,3568.47998046875,3541.969970703125,3566.919921875,3553.2099609375};

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

    talibCore.rsi(startIdx, endIdx, closePrices, OPT_IN_TIME_PERIOD, outBegIdx, outNbElement, outReal);

    return outReal;
}

1 个答案:

答案 0 :(得分:1)

您应了解函数的参数读一次。您已指定期间= 14,并且获得了N-14的结果。bcs前14天的RSI未定义。 Bcs需要OPT_IN_TIME_PERIOD个闭合值才能计算第一个RSI值。结果总是从0元素开始存储,因此最后有0。同样,在调用函数outBegIdx之后,将包含实际上与输出中的第一个结果相对应的日期的索引。在您的情况下应为13或14,不要记住它是否从0开始。此外,outNbElement将返回outReal中有意义的值的实际数量(N-14)。

实际上,应该为RSI和所有其他指示符提供一个*Lookback函数,该函数允许预先为给定的OPT_IN_TIME_PERIOD和其他OPT_参数找到结果数组的大小。因此,您可以将精确大小传递给double[] outReal = new double[M]以使用最少的内存。我想对于RSI Lookback函数将等于只是closePrices.length - OPT_IN_TIME_PERIOD,但它可能会更复杂其他指标