現在使用Ta-lib讓我們的資料可以開心的畫圖。
可參考文章:https://www.finlab.tw/Python-簡單158種技術指標計算/
可參考github檔案:Day26_createLine.ipynb
首先取得開盤價、最高價、最低價、收盤價的資料,我們從資料庫取得:
query = f"SELECT TradeDate, ClosePrice As 'close', HighPrice as high, LowPrice as low FROM [dbo].[DailyPrice] WHERE Symbol ='2330' ORDER BY TradeDate;"
def talib2df(talib_output):
if type(talib_output) == list:
ret = pandas.DataFrame(talib_output).transpose()
else:
ret = pandas.Series(talib_output)
ret.index = tsmc['close'].index
return ret;
for k, d in df.items():
if k == "ClosePrice":
close = d # Dataframe.transpose() 這是倒轉
close.index = pandas.to_datetime(close.index)
elif k == "OpenPrice":
open = d
open.index = pandas.to_datetime(open.index)
elif k == "HighPrice":
high = d
high.index = pandas.to_datetime(high.index)
elif k == "LowPrice":
low = d
low.index = pandas.to_datetime(low.index)
tsmc = {
'close':close.dropna().astype(float),
'open':open.dropna().astype(float),
'high':high.dropna().astype(float),
'low':low.dropna().astype(float)
}
kd_value = abstract.STOCH(tsmc)
talib2df(kd_value).plot()
tsmc['close'].plot(secondary_y=True)
這樣就完成KD值囉~