anandanand84 / technicalindicators

A javascript technical indicators written in typescript with pattern recognition right in the browser
MIT License
2.13k stars 550 forks source link

Incorrect Stochastic output #236

Open elyas74 opened 3 years ago

elyas74 commented 3 years ago

I can't match Stoch modules output with tradingview output,

any idea or sample code for it?

thanks.

justertee commented 3 years ago

I think so. Any one is not match?

marcusmota commented 2 years ago

Mine is correct, just add more candle data 👍

bennycode commented 2 years ago

@anandanand84 your Stochastic Oscillator implementation is very different from the results of the famous Tulip Indicators.

I used the values from the Example Calculation of Tulip Indicators and created a test script to verify your implementation:

const Stochastic = require('technicalindicators').Stochastic;

const candles: [number, number, number][] = [
  [82.15, 81.29, 81.59],
  [81.89, 80.64, 81.06],
  [83.03, 81.31, 82.87],
  [83.3, 82.65, 83.0],
  [83.85, 83.07, 83.61],
  [83.9, 83.11, 83.15],
  [83.33, 82.49, 82.84],
  [84.3, 82.3, 83.99],
  [84.84, 84.15, 84.55],
  [85.0, 84.11, 84.36],
  [85.9, 84.03, 85.53],
  [86.58, 85.39, 86.54],
  [86.98, 85.76, 86.89],
  [88.0, 87.17, 87.77],
  [87.87, 87.01, 87.29],
];

const highs = [];
const lows = [];
const closes = [];

for (let i = 0; i < candles.length; i++) {
  const [high, low, close] = candles[i];
  highs.push(high);
  lows.push(low);
  closes.push(close);
}

let input = {
  high: highs,
  low: lows,
  close: closes,
  period: 5,
  signalPeriod: 3,
};

console.log(Stochastic.calculate(input));

Here are your results:

[
  { k: 92.52336448598145, d: undefined },
  { k: 76.99386503067488, d: undefined },
  { k: 59.07335907335904, d: 76.19686286333847 },
  { k: 84.49999999999989, d: 73.52240803467794 },
  { k: 88.58267716535411, d: 77.38534541290434 },
  { k: 76.29629629629629, d: 83.12632448721676 },
  { k: 89.72222222222213, d: 84.8670652279575 },
  { k: 99.06542056074785, d: 88.36131302642208 },
  { k: 96.94915254237277, d: 95.2455984417809 },
  { k: 94.20654911838781, d: 96.74037407383612 },
  { k: 82.11586901763239, d: 91.09052355946432 }
]

With Tulip Indicators the last values should be: 91.09 (k) & 94.36 (d)

marcusmota commented 2 years ago

@bennycode if you add more candles you get the same value as tradingview.

👍

saru2020 commented 1 year ago

doesn't work with values matching the period, but it works when we add more values, so basically try to pass all the candles data and it should work!