參考資料:
https://www.mirrormedia.mg/story/20190817fin001/
#MacOS---
InMac <- TRUE
#自動檢查
InMac <- FALSE
if (Sys.info()["sysname"] == "Darwin") #--Darwin is MacOS
{
InMac <- TRUE
}
#stockID <- "3630.TWO"
stockID <- "2002.TW"
stockName <- "中鋼"
if (InMac)
{
install.packages("showtext")
library(showtext)
showtext.auto(enable = TRUE)
font.add("蘋方-繁", "蘋方-繁.otf")
}
#--------
install.packages("quantmod")
library(quantmod)
s_day <- Sys.Date() - 3600
e_day <- Sys.Date()
區間<- "2018-01::2020-11"
getSymbols(stockID, from = s_day, to = e_day)
TSMC <- (stockID)
x <- get(TSMC)
barChart(x)
my_name <- paste("台股",TSMC,stockName,sep = '-')
chartSeries(x[區間], theme = "white" ,name = my_name)
平均20<- runMean(x[,4],n = 20)
平均60<- runMean(x[,4],n = 60)
addTA(平均20, on=1 ,col="yellow")
addTA(平均60, on=1 ,col="red")
position <- Lag(ifelse(平均20 > 平均60, 0))
return <- ROC(Cl(x)) * position
#macOS---
if (InMac)
{
par(family="黑體-繁 中黑")
}
#--------
plot(return)
# theme(text=element_text(family="黑體-繁 中黑", size=14))
#再畫出股價走勢圖後,再輸入指令 addBBands()
addBBands()
# #畫在下方
addBBands(draw="p")