This report downloads daily stock prices for listed companies in the Taiwan stock market for the last two years.
library(tidyverse)
library(quantmod)
library(rvest)
end_date <- Sys.Date()
start_date <- end_date - 730
start_date
## [1] "2024-10-02"
end_date
## [1] "2026-10-02"
stock_url <- "https://openapi.twse.com.tw/v1/opendata/t187ap03_L"
stock_list <- jsonlite::fromJSON(stock_url)
stock_list <- stock_list %>%
filter(grepl("^[0-9]{4}$", 公司代號))
head(stock_list)
## 出表日期 公司代號 公司名稱 公司簡稱 外國企業註冊地國 產業別
## 1 1151001 1101 臺灣水泥股份有限公司 台泥 - 01
## 2 1151001 1102 亞洲水泥股份有限公司 亞泥 - 01
## 3 1151001 1103 嘉新水泥股份有限公司 嘉泥 - 01
## 4 1151001 1104 環球水泥股份有限公司 環泥 - 01
## 5 1151001 1108 幸福水泥股份有限公司 幸福 - 01
## 6 1151001 1109 信大水泥股份有限公司 信大 - 01
## 住址 營利事業統一編號 董事長 總經理 發言人
## 1 台北市中山北路2段113號 11913502 張安平 程耀輝 葉毓君
## 2 台北市大安區敦化南路2段207號30、31樓 03244509 徐旭東 李坤炎 王照宇
## 3 台北市中山北路二段96號 11892801 張剛綸 王立心 金哲毅
## 4 台北市南京東路二段125號10樓 07568009 侯博義 侯智元 楊宗仁
## 5 台北市松江路237號15樓 40601248 陳韻如 陳明賢 溫秀菊
## 6 台北市寶慶路37號7樓 03279507 楊智雄 楊大寬 張清壽
## 發言人職稱 代理發言人 總機電話 成立日期 上市日期
## 1 永續長 賴家柔 (02)2531-7099 19501229 19620209
## 2 協理 楊淯玲 02-2733-8000 19570321 19620608
## 3 董事會秘書室協理 馮文裕 (02)2551-5211 19541213 19691114
## 4 副總經理 高琮堯 02-25077801 19600321 19710201
## 5 財務部經理 陳一誠 25092188 19740507 19900606
## 6 資深協理 蔡素秋 (02)23816731 19640315 19911205
## 普通股每股面額 實收資本額 私募股數 特別股
## 1 新台幣 10.0000元 77231817420 0 200000000
## 2 新台幣 10.0000元 35465628810 0 0
## 3 新台幣 10.0000元 8328745530 0 0
## 4 新台幣 10.0000元 6866818160 0 0
## 5 新台幣 10.0000元 4047380490 0 0
## 6 新台幣 10.0000元 3411588680 0 0
## 編制財務報表類型 股票過戶機構 過戶電話
## 1 1 中國信託商業銀行代理部 66365566
## 2 1 亞東證券股份有限公司 02-7753-1699
## 3 1 群益金鼎證券股份有限公司股代部 (02)2702-3999
## 4 1 永豐金證券股份有限公司 02-23816288
## 5 1 永豐金證券股務代理 2381-6288
## 6 1 中國信託商業銀行代理部 (02)6636-5566
## 過戶地址 簽證會計師事務所 簽證會計師1 簽證會計師2
## 1 台北市重慶南路一段83號5樓 勤業眾信聯合會計師事務所 翁雅玲 邵志明
## 2 新北市板橋區新站路16號13樓 勤業眾信聯合會計師事務所 劉紋伶 陳培德
## 3 台北市敦化南路2段97號地下2樓 勤業眾信聯合會計師事務所 陳薔旬 梁盛泰
## 4 台北市博愛路17號3樓 資誠聯合會計師事務所 王菘澤 林秀珊
## 5 台北市中正區博愛路17號3樓 勤業眾信聯合會計師事務所 陳招美 陳薔旬
## 6 台北巿重慶南路一段83號5樓 資誠聯合會計師事務所 余正富 支秉鈞
## 英文簡稱
## 1 TCC
## 2 ACC
## 3 CHC
## 4 UCC
## 5 LUCKY CEMENT
## 6 HsingTa
## 英文通訊地址
## 1 No.113, Sec.2, Zhongshan N. Rd.,Taipei City 104,Taiwan (R.O.C.)
## 2 30-31F., No.207, Sec. 2, Dunhua S. Rd., Da' an Dist., Taipei City 106, TaiwanTAIPEI,TAIWAN,R.O.C
## 3 No.96, Sec. 2, Zhongshan N. Rd., Zhongshan Dist.Taipei City 10449, Taiwan (R.O.C.)
## 4 10th F., 125 Nanking E.Rd Sec. 2,Taipei Taiwan
## 5 15TH FL.,NO.237,SUNG KIANG RD.,TAIPEI,TAIWAN,R.O.C.
## 6 No. 37, 7F, Baoqing Rd.,Taipei City 100, Taiwan (R.O.C.)
## 傳真機號碼 電子郵件信箱 網址
## 1 (02)2531-6529 finance@taiwancement.com https://www.tccgroupholdings.com/tw/
## 2 02-2736-6263 service@acc.com.tw www.acc.com.tw
## 3 (02)2511-4002 ir@chcgroup.com.tw www.chcgroup.com.tw
## 4 02-25075870 ucc@ucctw.com www.ucctw.com
## 5 25048672 wendy@luckygrp.com.tw www.luckygrp.com.tw
## 6 23314086 service@hsingta.com.tw www.hsingta.com.tw
## 已發行普通股數或TDR原股發行股數
## 1 7523181742
## 2 3546562881
## 3 832874553
## 4 686681816
## 5 404738049
## 6 341158868
nrow(stock_list)
## [1] 1089
symbols <- paste0(stock_list$公司代號, ".TW")
head(symbols)
## [1] "1101.TW" "1102.TW" "1103.TW" "1104.TW" "1108.TW" "1109.TW"
length(symbols)
## [1] 1089
test_stock <- getSymbols(
"2330.TW",
src = "yahoo",
from = start_date,
to = end_date,
auto.assign = FALSE
)
## Warning: 2330.TW contains missing values. Some functions will not work if
## objects contain missing values in the middle of the series. Consider using
## na.omit(), na.approx(), na.fill(), etc to remove or replace them.
head(test_stock)
## 2330.TW.Open 2330.TW.High 2330.TW.Low 2330.TW.Close 2330.TW.Volume
## 2024-10-04 986 986 967 977 40934818
## 2024-10-07 993 1010 989 1005 39667071
## 2024-10-08 1000 1010 997 1010 30845902
## 2024-10-09 1030 1035 1020 1020 46975717
## 2024-10-11 1025 1050 1020 1045 43058754
## 2024-10-14 1045 1055 1035 1045 36858874
## 2330.TW.Adjusted
## 2024-10-04 949.1689
## 2024-10-07 976.3714
## 2024-10-08 981.2289
## 2024-10-09 990.9440
## 2024-10-11 1015.2319
## 2024-10-14 1015.2319
test_symbols <- symbols[1:5]
for (sym in test_symbols) {
tryCatch({
x <- getSymbols(
sym,
src = "yahoo",
from = start_date,
to = end_date,
auto.assign = FALSE
)
x_df <- data.frame(
Date = index(x),
coredata(x)
)
write.csv(
x_df,
paste0(gsub("\\.TW", "", sym), ".csv"),
row.names = FALSE
)
cat("Downloaded:", sym, "\n")
rm(x, x_df)
gc()
}, error = function(e) {
cat("Failed:", sym, "\n")
})
}
## Warning: 1101.TW contains missing values. Some functions will not work if
## objects contain missing values in the middle of the series. Consider using
## na.omit(), na.approx(), na.fill(), etc to remove or replace them.
## Downloaded: 1101.TW
## Warning: 1102.TW contains missing values. Some functions will not work if
## objects contain missing values in the middle of the series. Consider using
## na.omit(), na.approx(), na.fill(), etc to remove or replace them.
## Downloaded: 1102.TW
## Warning: 1103.TW contains missing values. Some functions will not work if
## objects contain missing values in the middle of the series. Consider using
## na.omit(), na.approx(), na.fill(), etc to remove or replace them.
## Downloaded: 1103.TW
## Warning: 1104.TW contains missing values. Some functions will not work if
## objects contain missing values in the middle of the series. Consider using
## na.omit(), na.approx(), na.fill(), etc to remove or replace them.
## Downloaded: 1104.TW
## Warning: 1108.TW contains missing values. Some functions will not work if
## objects contain missing values in the middle of the series. Consider using
## na.omit(), na.approx(), na.fill(), etc to remove or replace them.
## Downloaded: 1108.TW
dir.create("stock_data", showWarnings = FALSE)
success <- 0
failed <- 0
for (sym in symbols[100:length(symbols)]) {
tryCatch({
x <- getSymbols(
sym,
src = "yahoo",
from = start_date,
to = end_date,
auto.assign = FALSE
)
x_df <- data.frame(
Date = index(x),
coredata(x)
)
write.csv(
x_df,
file.path("stock_data", paste0(gsub("\\.TW", "", sym), ".csv")),
row.names = FALSE
)
success <- success + 1
cat("Downloaded:", sym, "\n")
rm(x, x_df)
gc()
}, error = function(e) {
failed <- failed + 1
cat("Failed:", sym, "\n")
})
}