Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Division by zero error #472

Open
ebadran opened this issue May 22, 2024 · 2 comments
Open

Division by zero error #472

ebadran opened this issue May 22, 2024 · 2 comments

Comments

@ebadran
Copy link

ebadran commented May 22, 2024

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:

  1. Obtain the historical OHLC data from the CELU stock, from 09-Aug-2019 until the current date.
  2. Apply the KDJ indicator to that stock's data: const { k, d, j } = kdj(highs, lows, closings, defaultConfig);
  3. 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,

@cinar
Copy link
Owner

cinar commented May 23, 2024

Thank you very much for reporting it. Yes, it sounds like that condition will impact multiple indicators here. We can certainly do that, but I am wondering what is the common practice for these, as this seems to be a normal case. Let me do some research and get back to you.

@ebadran
Copy link
Author

ebadran commented May 23, 2024

Thanks, Onur.

I noticed that the Random Index (KDJ), Chaikin Oscillator and Accumulation Distribution have this calculation in common:

((Closing Price - Low Price) - (High Price - Closing Price)) / (High Price - Low Price)

In the context of an asset having all OHLC values equal, we’re essentially dividing zero by zero, so it might make sense for the divide formula to return 1.

So a light-handed approach would be to conditionally return 1 if both the numerator and denominator are zero; and setting the denominator value to 1 if only the denominator is zero.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants