2

I'm calculating RSI using ta-lib with Java in a spring-boot application. After spending some time figuring out the function call arguments, I have managed to get an output from the calculation. However, the rsi-function keeps returnings zero values in the output array. Why does it return zeros?

I'm fetching candlestick data from Binance's API, trying to calculate RSI for a given period. I have tried both the stochasticRSI and RSI, but both are returning zeros in the output array.

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

I expect the output of the calculation to be an array containing the preferred RSI values, but the actual output is the following array:

[
55.50236554101884,
48.4420340522227,
48.936642078369324,
39.61167139428769,
39.308159841569264,
42.58440044993444,
43.68867735156312,
50.14296711032441,
50.83654805452586,
50.79841825933019,
50.311807765799145,
45.53568823545641,
43.2867452256146,
43.38390663365912,
41.2894895311203,
41.133921783119135,
41.957983467430424,
34.6763182163226,
29.239623192248875,
43.73473552102837,
39.70980937294976,
38.722071869417974,
33.91981286332463,
40.415256033334316,
39.967050108741674,
30.237053069193013,
47.24515019760144,
48.09365697810791,
47.397062335425126,
47.919330703443016,
48.50051589076493,
45.588023453959245,
41.35162036615853,
39.976389434938056,
41.79568536987075,
36.94666938984934,
40.95986916491035,
38.21570616480444,
38.538139356924304,
39.93113081247877,
39.69620754900941,
38.9256798286741,
34.87273047865816,
43.210471567071465,
37.873988767931046,
35.92186113528669,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0
]

1 Answers1

1

You shall read about function arguments one more time. You have specified period = 14 and have got N-14 results back bcs RSI is undefined for first 14 days. Bcs it requires OPT_IN_TIME_PERIOD close values to calculate first RSI value. Results are always stored from 0 element, so you've got 0s at end. Also after function is called the outBegIdx will contain index of the day which is actually corresponds to the 1st result in output. In your case should be 13 or 14, don't remember if it's 0-based. Also outNbElement will return actual number of meaningful values in outReal (N-14).

In fact there should be a *Lookback function for RSI and every other indicator that allows to find the size of result array for given OPT_IN_TIME_PERIOD and other OPT_ params in beforehand. So you can pass exact size to double[] outReal = new double[M] to use minimum memory. I guess for RSI Lookback function will be equal to just closePrices.length - OPT_IN_TIME_PERIOD but it could be more complicated for other indicators

truf
  • 2,843
  • 26
  • 39