Open machineCYC opened 4 years ago
Hi @machineCYC,
I am not familiarized with the way Yahoo calc the indicators.
StochasticOscillator
implements exactly the calc described in https://school.stockcharts.com/doku.php?id=technical_indicators:stochastic_oscillator_fast_slow_and_full. Also, it has the tests developed using the example spreadsheet in the last link.
You can check the implementation of indicator, the test implementation and the input data.
Best, Dario
Thanks , great information for me.
I check the kd value in yahoo finance, it is a smoothing version kd value. formula is blew:
N = 3 RSVt is the same as kd.stoch() in ta library
Kt = ( 1-1/N ) Kt-ı + ( 1 / N ) RSVt
Dt = ( 1-1/N) Dt-ı + ( 1 / N ) Kt
Maybe you can consider add this version kd in the library, it is useful for user
import requests
import pandas as pd
from ta.momentum import StochasticOscillator
url = "http://api.finmindtrade.com/api/v3/data"
params = {
'dataset':"TaiwanStockPrice",
'stock_id':"2330",
'date':"2019-06-14",
"end_date":"2020-06-14",
"user_id": "",
"password": "",
}
res = requests.get(url, verify=True, params=params)
ret = res.json()
base_data = pd.DataFrame( ret["data"])
kd = StochasticOscillator(
high=base_data["max"],
low=base_data["min"],
close=base_data["close"],
n=self.kdays,
)
rsv_ = kd.stoch().fillna(50)
_k = np.zeros(base_data.shape[0])
_d = np.zeros(base_data.shape[0])
for i, r in enumerate(rsv_):
if i == 0:
_k[i] = 50
_d[i] = 50
else:
_k[i] = _k[i-1] * 2 / 3 + r / 3
_d[i] = _d[i-1] * 2 / 3 + _k[i] / 3
base_data["K"] = _k
base_data["D"] = _d
print(base_data[["date", "K", "D"]])
date K D
0 2019-06-14 50.000000 50.000000
1 2019-06-17 50.000000 50.000000
2 2019-06-18 50.000000 50.000000
3 2019-06-19 50.000000 50.000000
4 2019-06-20 50.000000 50.000000
.. ... ... ...
239 2020-06-08 91.271136 81.063862
240 2020-06-09 94.180757 85.436160
241 2020-06-10 94.649916 88.507412
242 2020-06-11 89.965616 88.993480
243 2020-06-12 81.482454 86.48980
Hi! Just to you know: I'm having the same issue here. It seems stoch() is not processing the stochastic as tradingview or others do. And it is only with stochastic, other indicators are returning correct data, so the problem must be on stoch()
edit: function stock_signal() does the job!
The Stochastic Oscillator method in most finance web call kd value (ex: yahoo finance) with parameter with 9 days.
I choose the same stock_id(2330.TW) in yahoo finance with one year period, the k value is 80.23 d value is 89.89, is really different from the ta library result
The default method in yahoo finance usually smooth the k, d value. below is the code that calculate kd value without smoothing,
Is any method to create the same result with yahoo finance?