iyeung144.github.io

My Github Pages

View My GitHub Profile

About

CV

Blog

10 March 2022

Hong Kong Stock 3

by Yeung Ka Ming, CFA

Summary

Compare return to market portfolio. The market portfolio for Hong Kong stock market is HSI Tracker ETF, 2800.HK

R Libraries

library(quantmod)
library(tidyverse)
library(ggplot2)

Preparation of data

Direct download by getSymbols

tickers <- c("0005.HK","9988.HK", "2800.HK")
hkport <- new.env()
getSymbols(tickers, 
           env=hkport, 
           src = "yahoo",
           index.class = "POSIXct",
           from = "2022-01-01")
## [1] "0005.HK" "9988.HK" "2800.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.

Rename column names

HSBC<-hkport$`0005.HK`
BABA<-hkport$`9988.HK`
HKTR<-hkport$`2800.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.67877
## 2022-01-04        47.10        47.80       47.00         47.80       25500155         47.62530
## 2022-01-05        49.35        49.75       48.75         49.15       59292303         48.97037
## 2022-01-06        49.05        49.05       48.35         49.00       24559582         48.82091
## 2022-01-07        50.10        50.15       49.75         50.10       67407695         49.91689
## 2022-01-10        50.80        51.75       50.60         51.70       59640904         51.51105
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.67877
## 2022-01-04     47.10     47.80    47.00      47.80    25500155      47.62530
## 2022-01-05     49.35     49.75    48.75      49.15    59292303      48.97037
## 2022-01-06     49.05     49.05    48.35      49.00    24559582      48.82091
## 2022-01-07     50.10     50.15    49.75      50.10    67407695      49.91689
## 2022-01-10     50.80     51.75    50.60      51.70    59640904      51.51105
colnames(HKTR) <- sub("2800.HK","HKTR",colnames(hkport$`2800.HK`))
head(hkport$`2800.HK`)
##            2800.HK.Open 2800.HK.High 2800.HK.Low 2800.HK.Close 2800.HK.Volume 2800.HK.Adjusted
## 2022-01-03        23.66        23.76       23.34         23.40      100227198            23.40
## 2022-01-04        23.52        23.58       23.28         23.44       74708335            23.44
## 2022-01-05        23.44        23.44       23.00         23.04      149530742            23.04
## 2022-01-06        23.00        23.22       22.86         23.22      116238361            23.22
## 2022-01-07        23.40        23.66       23.28         23.64      169287010            23.64
## 2022-01-10        23.62        23.92       23.54         23.84      187033452            23.84
head(HKTR)
##            HKTR.Open HKTR.High HKTR.Low HKTR.Close HKTR.Volume HKTR.Adjusted
## 2022-01-03     23.66     23.76    23.34      23.40   100227198         23.40
## 2022-01-04     23.52     23.58    23.28      23.44    74708335         23.44
## 2022-01-05     23.44     23.44    23.00      23.04   149530742         23.04
## 2022-01-06     23.00     23.22    22.86      23.22   116238361         23.22
## 2022-01-07     23.40     23.66    23.28      23.64   169287010         23.64
## 2022-01-10     23.62     23.92    23.54      23.84   187033452         23.84

Prepare a dataframe with portfolio value and plot graph

Prepare dataframe

# Suppose $100,000 for investment on the first trading 
# day of 2022.
# Use around half of the amount for each stock (50/50)

moneyseed <- 100000
portion <- 0.5
shares <- c(portion*moneyseed/HSBC$HSBC.Adjusted[[1]],
            portion*moneyseed/BABA$BABA.Adjusted[[1]],
            moneyseed/HKTR$HKTR.Adjusted[[1]])

# need stock adjusted prices and number of shares to make a portfolio
prices.tbl <- as_tibble(fortify(merge(HSBC, BABA, HKTR)))

port.tbl <- prices.tbl %>% 
  select(Index,HSBC.Adjusted,BABA.Adjusted,HKTR.Adjusted)
port.tbl
## # A tibble: 45 x 4
##    Index               HSBC.Adjusted BABA.Adjusted HKTR.Adjusted
##    <dttm>                      <dbl>         <dbl>         <dbl>
##  1 2022-01-03 00:00:00          46.7          115           23.4
##  2 2022-01-04 00:00:00          47.6          117.          23.4
##  3 2022-01-05 00:00:00          49.0          114.          23.0
##  4 2022-01-06 00:00:00          48.8          121           23.2
##  5 2022-01-07 00:00:00          49.9          129.          23.6
##  6 2022-01-10 00:00:00          51.5          128.          23.8
##  7 2022-01-11 00:00:00          51.8          126.          23.9
##  8 2022-01-12 00:00:00          52.6          133           24.5
##  9 2022-01-13 00:00:00          53.5          132.          24.6
## 10 2022-01-14 00:00:00          54.4          129.          24.5
## # ... with 35 more rows
port.tbl <- port.tbl %>% 
  mutate(port.value = shares[1]*HSBC.Adjusted + 
           shares[2]*BABA.Adjusted) %>% 
  mutate(mktport.value = shares[3]*HKTR.Adjusted)
port.tbl
## # A tibble: 45 x 6
##    Index               HSBC.Adjusted BABA.Adjusted HKTR.Adjusted port.value mktport.value
##    <dttm>                      <dbl>         <dbl>         <dbl>      <dbl>         <dbl>
##  1 2022-01-03 00:00:00          46.7          115           23.4    100000        100000 
##  2 2022-01-04 00:00:00          47.6          117.          23.4    101840.       100171.
##  3 2022-01-05 00:00:00          49.0          114.          23.0    102237.        98462.
##  4 2022-01-06 00:00:00          48.8          121           23.2    104903.        99231.
##  5 2022-01-07 00:00:00          49.9          129.          23.6    109469.       101026.
##  6 2022-01-10 00:00:00          51.5          128.          23.8    110654.       101880.
##  7 2022-01-11 00:00:00          51.8          126.          23.9    110052.       101966.
##  8 2022-01-12 00:00:00          52.6          133           24.5    114123.       104701.
##  9 2022-01-13 00:00:00          53.5          132.          24.6    114735.       104957.
## 10 2022-01-14 00:00:00          54.4          129.          24.5    114435.       104615.
## # ... with 35 more rows

Plot graph

plot(x = port.tbl$Index, 
     y = port.tbl$mktport.value, 
     type="l", col="red", ylim = c(90000,120000))
lines(x = port.tbl$Index, y = port.tbl$port.value, 
      type="l", col="blue")
legend(x = "topright", 
       legend=c("Market Portfolio", "Portfolio"), 
       lty=c(1,1),col=c("red", "blue"))

port.long.tbl <- port.tbl %>%
  pivot_longer(c(port.value,mktport.value), 
               names_to = "Portfolio", 
               values_to = "Value") %>% 
  select(c(Index,Portfolio,Value))
port.long.tbl
## # A tibble: 90 x 3
##    Index               Portfolio       Value
##    <dttm>              <chr>           <dbl>
##  1 2022-01-03 00:00:00 port.value    100000 
##  2 2022-01-03 00:00:00 mktport.value 100000 
##  3 2022-01-04 00:00:00 port.value    101840.
##  4 2022-01-04 00:00:00 mktport.value 100171.
##  5 2022-01-05 00:00:00 port.value    102237.
##  6 2022-01-05 00:00:00 mktport.value  98462.
##  7 2022-01-06 00:00:00 port.value    104903.
##  8 2022-01-06 00:00:00 mktport.value  99231.
##  9 2022-01-07 00:00:00 port.value    109469.
## 10 2022-01-07 00:00:00 mktport.value 101026.
## # ... with 80 more rows
p2 <- ggplot(port.long.tbl, 
             aes(x=Index, y=Value, color=Portfolio))
p2 + xlab("Date") + ylab("Portfolio Value") + geom_line()

tags: Porfolio