Currently, the divide function in src/helper/numArray.ts does not handle division by zero errors.
Some assets have limited trading volume, and therefore may have a day's open, close, high, and low prices all equal. In these cases, applying certain indicators (e.g. the Random Index KDJ) to those stocks will result in their functions returning arrays of NaNs.
Steps to reproduce the behavior:
- Obtain the historical OHLC data from the CELU stock, from 09-Aug-2019 until the current date.
- Apply the KDJ indicator to that stock's data:
const { k, d, j } = kdj(highs, lows, closings, defaultConfig);
- The result will be arrays of NaNs.
Additional context:
The above error will also occur with the following indicators:
- Random Index (KDJ)
- Chaikin Oscillator
- Mass Index
- Accumulation Distribution
Suggestion:
A quick fix would be to refactor the divide function in src/helper/numArray.ts so that it returns 1 when dividing by zero:
export function divide(values1: number[], values2: number[]): number[] {
checkSameLength(values1, values2);
const result = new Array<number>(values1.length);
for (let i = 0; i < result.length; i++) {
if (values2[i] === 0) {
result[i] = 1;
} else {
result[i] = values1[i] / values2[i];
}
}
return result;
}
Alternatively, you could divide by 0.0000001:
result[i] = values1[i] / (values2[i] || 0.0000001);
I hope this helps.
Best regards,
Currently, the
dividefunction insrc/helper/numArray.tsdoes not handle division by zero errors.Some assets have limited trading volume, and therefore may have a day's open, close, high, and low prices all equal. In these cases, applying certain indicators (e.g. the Random Index KDJ) to those stocks will result in their functions returning arrays of NaNs.
Steps to reproduce the behavior:
const { k, d, j } = kdj(highs, lows, closings, defaultConfig);Additional context:
The above error will also occur with the following indicators:
Suggestion:
A quick fix would be to refactor the
dividefunction insrc/helper/numArray.tsso that it returns 1 when dividing by zero:Alternatively, you could divide by 0.0000001:
result[i] = values1[i] / (values2[i] || 0.0000001);I hope this helps.
Best regards,