bukosabino / ta

Technical Analysis Library using Pandas and Numpy
https://technical-analysis-library-in-python.readthedocs.io/en/latest/
MIT License
4.25k stars 867 forks source link

How to set Stochastic Oscillator parameter cause the result will be the same as yahoo fiance #170

Open machineCYC opened 4 years ago

machineCYC commented 4 years ago

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?

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=9, d_n=3
)

base_data["K"] = kd.stoch()
base_data["D"] = kd.stoch_signal()
print(base_data[["date", "K", "D"]])

           date           K          D
0    2019-06-14         NaN        NaN
1    2019-06-17         NaN        NaN
2    2019-06-18         NaN        NaN
3    2019-06-19         NaN        NaN
4    2019-06-20         NaN        NaN
..          ...         ...        ...
239  2020-06-08   96.551724  98.092999
240  2020-06-09  100.000000  98.092999
241  2020-06-10   95.588235  97.379986
242  2020-06-11   80.597015  92.061750
243  2020-06-12   64.516129  80.233793
bukosabino commented 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

machineCYC commented 4 years ago

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
joao-aguilera-c commented 3 years ago

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!