程式說明
# 載入套件
library(quantmod)
library(TTR)
library(dygraphs)
# 設定股票代碼
ticker <- "2330.TW"
# 設定資料下載的起始和結束日期
start_date <- as.Date("2024-01-01")
end_date <- Sys.Date()
# 下載股票資料
getSymbols(ticker, from = start_date, to = end_date, auto.assign = TRUE)
## [1] "2330.TW"
# 取得股票數據
stock_data <- get(ticker)
# ========== 1. 修正變數名稱 ==========
colnames(stock_data) <- c("Open", "High", "Low", "Close", "Volume", "Adjusted")
# 取 OHLC 數據
ohlc_xts <- stock_data[, c("Open", "High", "Low", "Close")]
# ========== 2. 計算技術指標 ==========
# 20 日簡單移動平均線 (SMA)
sma_20 <- SMA(ohlc_xts$Close, n = 20)
sma_20 <- xts(sma_20, order.by = index(stock_data))
colnames(sma_20) <- "SMA_20"
# 20 日指數移動平均線 (EMA)
ema_20 <- EMA(ohlc_xts$Close, n = 20)
ema_20 <- xts(ema_20, order.by = index(stock_data))
colnames(ema_20) <- "EMA_20"
# 計算布林通道 (Bollinger Bands)
bb_xts <- BBands(HLC(stock_data), n = 20, sd = 2)
bb_up <- xts(bb_xts$up, order.by = index(stock_data))
bb_dn <- xts(bb_xts$dn, order.by = index(stock_data))
colnames(bb_up) <- "BB_Upper"
colnames(bb_dn) <- "BB_Lower"
# 計算 MACD
macd_xts <- MACD(ohlc_xts$Close, nFast = 12, nSlow = 26, nSig = 9, maType = "EMA", percent = FALSE)
macd_xts <- xts(macd_xts, order.by = index(stock_data))
colnames(macd_xts) <- c("DIF", "MACD_Signal")
# 計算 RSI (14)
rsi_xts <- RSI(ohlc_xts$Close, n = 14)
rsi_xts <- xts(rsi_xts, order.by = index(stock_data))
colnames(rsi_xts) <- "RSI_14"
# ========== 3. 合併數據 ==========
combined1_xts <- merge(ohlc_xts, sma_20, ema_20, bb_up, bb_dn, all = FALSE)
combined2_xts <- merge(ohlc_xts, macd_xts, all = FALSE)
combined3_xts <- merge(ohlc_xts, rsi_xts, all = FALSE)
# ========== 4. 繪製 Viewer 1: K 線圖 + EMA + SMA + BBands ==========
dygraph(combined1_xts, main = paste(ticker, "K Line + SMA & EMA & BBands")) %>%
dyCandlestick() %>% # 讓 dygraph 變成 K 線圖
dySeries("SMA_20", label = "SMA (20)", color = "green", strokeWidth = 2) %>%
dySeries("EMA_20", label = "EMA (20)", color = "purple", strokeWidth = 2) %>%
dySeries("BB_Upper", label = "BB Upper", color = "red", strokeWidth = 1.5, strokePattern = "dashed") %>%
dySeries("BB_Lower", label = "BB Lower", color = "red", strokeWidth = 1.5, strokePattern = "dashed") %>%
dyAxis("y", label = "Stock Price") %>%
dyOptions(drawGrid = TRUE, gridLineColor = "lightgray") %>%
#dyLegend(show = "always", width = 250, labelsSeparateLines = TRUE) %>%
dyRangeSelector()
# ========== 5. 繪製 Viewer 2: K 線圖 + MACD ==========
dygraph(combined2_xts, main = paste(ticker, "K Line + MACD")) %>%
dyCandlestick() %>% # 讓 dygraph 變成 K 線圖
dySeries("DIF", label = "DIF", color = "blue", axis = "y2") %>%
dySeries("MACD_Signal", label = "MACD Signal", color = "black", axis = "y2") %>%
dyAxis("y", label = "Stock Price") %>%
dyAxis("y2", label = "MACD", independentTicks = TRUE) %>%
dyOptions(drawGrid = TRUE, gridLineColor = "lightgray") %>%
#dyLegend(show = "always", width = 250, labelsSeparateLines = TRUE) %>%
dyRangeSelector()
# ========== 6. 繪製 Viewer 3: K 線圖 + RSI ==========
dygraph(combined3_xts, main = paste(ticker, "K Line + RSI")) %>%
dyCandlestick() %>% # 讓 dygraph 變成 K 線圖
dySeries("RSI_14", label = "RSI (14)", color = "orange", axis = "y2") %>%
dyAxis("y", label = "Stock Price") %>%
dyAxis("y2", label = "RSI", independentTicks = TRUE) %>%
dyOptions(drawGrid = TRUE, gridLineColor = "lightgray") %>%
#dyLegend(show = "always", width = 250, labelsSeparateLines = TRUE) %>%
dyRangeSelector()