[第24期] Calculate KD value of the stock by using yfinance and PyQt/使用yfinance與PyQt計算股票的KD值
This video shows how to use yfinance and PyQt to calculate KD value of the stock .
The content includes:
Input Taiwan stock ticker
Calculate K and D valuse with yfinance
本視頻說明如何使用yfinance與PyQt計算股票的KD值。
內容包含:
輸入台灣股票代號
使用yfinance計算KD值
程式碼:
def calculate_kd(self):
stock_code = self.stock_code_lineEdit.text()
if not stock_code:
return
# 從Yahoo Finance獲取股票歷史價格資料
stock = yf.Ticker(stock_code + ".TW")
history = stock.history(period="max")
# 計算KD值
low_min = history["Low"].rolling(window=9).min()
high_max = history["High"].rolling(window=9).max()
rsv = (history["Close"] - low_min) / (high_max - low_min)
k = rsv.ewm(com=2).mean() * 100
d = k.ewm(com=2).mean()
# 顯示結果
kd_result = "最新K值:{:.2f},最新D值:{:.2f}".format(k[-1], d[-1])
self.result_label.setText(kd_result)
留言