My Github Pages
by Yeung Ka Ming, CFA
Download HK Stock data
library(quantmod)
Direct download by getSymbols
tickers <- c("0005.HK","9988.HK")
hkport <- new.env()
getSymbols(tickers,
env=hkport,
src = "yahoo",
index.class = "POSIXct",
from = "2022-01-01")
## [1] "0005.HK" "9988.HK"
# auto.assign is FALSE, so an environment is used here. Unlike US stocks,
# the tickers in HK stock market is by number, which cannot be the variable
# name in R. Further fine tune in next step
HSBC<-hkport$`0005.HK`
BABA<-hkport$`9988.HK`
# substitute numeric ticker name to character name. It is inconvenient to handle
# variable name starts with number.
colnames(BABA) <- sub("9988.HK","BABA",colnames(hkport$`9988.HK`))
head(hkport$`9988.HK`)
## 9988.HK.Open 9988.HK.High 9988.HK.Low 9988.HK.Close 9988.HK.Volume 9988.HK.Adjusted
## 2022-01-03 117.0 117.5 114.0 115.0 22176946 115.0
## 2022-01-04 118.4 118.9 115.7 116.9 23228903 116.9
## 2022-01-05 119.0 119.1 113.9 114.5 30717509 114.5
## 2022-01-06 117.5 121.1 117.3 121.0 47231895 121.0
## 2022-01-07 126.5 128.8 122.8 128.8 58778943 128.8
## 2022-01-10 128.8 129.5 125.4 127.6 36814976 127.6
head(BABA)
## BABA.Open BABA.High BABA.Low BABA.Close BABA.Volume BABA.Adjusted
## 2022-01-03 117.0 117.5 114.0 115.0 22176946 115.0
## 2022-01-04 118.4 118.9 115.7 116.9 23228903 116.9
## 2022-01-05 119.0 119.1 113.9 114.5 30717509 114.5
## 2022-01-06 117.5 121.1 117.3 121.0 47231895 121.0
## 2022-01-07 126.5 128.8 122.8 128.8 58778943 128.8
## 2022-01-10 128.8 129.5 125.4 127.6 36814976 127.6
colnames(HSBC) <- sub("0005.HK","HSBC",colnames(hkport$`0005.HK`))
head(hkport$`0005.HK`)
## 0005.HK.Open 0005.HK.High 0005.HK.Low 0005.HK.Close 0005.HK.Volume 0005.HK.Adjusted
## 2022-01-03 47.00 47.15 46.65 46.85 6711558 46.85
## 2022-01-04 47.10 47.80 47.00 47.80 25500155 47.80
## 2022-01-05 49.35 49.75 48.75 49.15 59292303 49.15
## 2022-01-06 49.05 49.05 48.35 49.00 24559582 49.00
## 2022-01-07 50.10 50.15 49.75 50.10 67407695 50.10
## 2022-01-10 50.80 51.75 50.60 51.70 59640904 51.70
head(HSBC)
## HSBC.Open HSBC.High HSBC.Low HSBC.Close HSBC.Volume HSBC.Adjusted
## 2022-01-03 47.00 47.15 46.65 46.85 6711558 46.85
## 2022-01-04 47.10 47.80 47.00 47.80 25500155 47.80
## 2022-01-05 49.35 49.75 48.75 49.15 59292303 49.15
## 2022-01-06 49.05 49.05 48.35 49.00 24559582 49.00
## 2022-01-07 50.10 50.15 49.75 50.10 67407695 50.10
## 2022-01-10 50.80 51.75 50.60 51.70 59640904 51.70