跳轉至

建立 2025-02-24 更新 2026-09-16

股票數據分析

拿到股價資料之後,第一步不是急著寫策略,而是先用 pandas 看懂資料本身:價格怎麼變動、變動得穩不穩定、成交量有沒有同步反應。這一頁介紹幾個最基礎、後面章節會反覆用到的分析方法。

基本統計檢視

import yfinance as yf

data = yf.download("AAPL", start="2023-01-01", end="2024-01-01")
print(data.describe())   # 快速看平均、最大最小值、標準差等統計量

移動平均線(Moving Average)

移動平均線把價格「平滑化」,讓趨勢比原始價格線更容易判讀:價格站上均線通常視為偏多,跌破均線則偏空。

data["SMA_20"] = data["Close"].rolling(window=20).mean()

import matplotlib.pyplot as plt

plt.figure(figsize=(10, 5))
plt.plot(data["Close"], label="Close")
plt.plot(data["SMA_20"], label="20-Day SMA", linestyle="dashed")
plt.legend()
plt.show()

報酬率與波動率

報酬率衡量價格漲跌的幅度,波動率衡量價格穩不穩定——同樣是上漲 10%,用一個月漲完和用一年慢慢漲完,風險完全不同。

data["Return"] = data["Close"].pct_change()               # 每日報酬率
data["Volatility"] = data["Return"].rolling(window=20).std()  # 20 日滾動波動率

成交量分析

成交量代表市場對這次價格變動的認同程度:價格上漲但成交量沒有放大,這段漲勢的支撐力道通常較弱。

fig, ax1 = plt.subplots(figsize=(10, 5))
ax1.plot(data["Close"], color="blue")
ax1.set_ylabel("Price")

ax2 = ax1.twinx()
ax2.bar(data.index, data["Volume"], color="gray", alpha=0.3)
ax2.set_ylabel("Volume")
plt.title("Price & Volume")
plt.show()

推薦影音

pandas 基礎教學(適合完全新手)

簡述:彭彭老師講解 pandas 的核心觀念——DataFrame、資料篩選與基本操作,是繁體中文、內容紮實的入門教學。如果對 rolling()pct_change() 這類 pandas 語法還不熟悉,建議先看這支。

用 pandas 做股票數據分析(實戰)

簡述:直接示範用 pandas 處理股價資料、計算指標並畫圖,內容與本頁「移動平均線」「成交量分析」對應,適合已經懂 pandas 基礎、想看它如何套用在股票資料上的人。