R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

library(pacman)
p_load(tidyverse, lubridate, readxl, highcharter, tidyquant, 
       timetk, tibbletime, quantmod, PerformanceAnalytics, scales)
## Installing package into '/home/rstudio/R/x86_64-pc-linux-gnu-library/4.2'
## (as 'lib' is unspecified)
## also installing the dependencies 'RcppArmadillo', 'forecast', 'tsfeatures', 'alphavantager', 'timetk'
## Warning in utils::install.packages(package, ...): installation of package
## 'RcppArmadillo' had non-zero exit status
## Warning in utils::install.packages(package, ...): installation of package
## 'forecast' had non-zero exit status
## Warning in utils::install.packages(package, ...): installation of package
## 'tsfeatures' had non-zero exit status
## Warning in utils::install.packages(package, ...): installation of package
## 'timetk' had non-zero exit status
## Warning in utils::install.packages(package, ...): installation of package
## 'alphavantager' had non-zero exit status
## Warning in utils::install.packages(package, ...): installation of package
## 'tidyquant' had non-zero exit status
## Warning in p_install(package, character.only = TRUE, ...):
## Warning in library(package, lib.loc = lib.loc, character.only = TRUE,
## logical.return = TRUE, : there is no package called 'tidyquant'
## Installing package into '/home/rstudio/R/x86_64-pc-linux-gnu-library/4.2'
## (as 'lib' is unspecified)
## also installing the dependencies 'RcppArmadillo', 'forecast', 'tsfeatures'
## Warning in utils::install.packages(package, ...): installation of package
## 'RcppArmadillo' had non-zero exit status
## Warning in utils::install.packages(package, ...): installation of package
## 'forecast' had non-zero exit status
## Warning in utils::install.packages(package, ...): installation of package
## 'tsfeatures' had non-zero exit status
## Warning in utils::install.packages(package, ...): installation of package
## 'timetk' had non-zero exit status
## Warning in p_install(package, character.only = TRUE, ...):
## Warning in library(package, lib.loc = lib.loc, character.only = TRUE,
## logical.return = TRUE, : there is no package called 'timetk'
## Warning in p_load(tidyverse, lubridate, readxl, highcharter, tidyquant, : Failed to install/load:
## tidyquant, timetk
p_load(reshape2)
getwd()
## [1] "/home/rstudio/aws_demo"
setwd(dir="/home/rstudio/aws_demo")
etf3 <- read.table("tw_ETF_2008_2020_chinese.txt",header = TRUE)

etf3 <- etf3[,-2]
colnames(etf3) <- c('id','date','close')
etf3.1 <- dcast(etf3, date~id)
## Using close as value column: use value.var to override.
etf3.xts <- xts(etf3.1[, -1], order.by = as.Date(as.character(etf3.1$date), 
                                                 format = '%Y%m%d'))
head(etf3.xts)
##                 50      52      56
## 2008-01-02 39.6472 27.0983 14.5739
## 2008-01-03 38.9876 26.0676 14.3758
## 2008-01-04 38.9876 25.9346 14.4041
## 2008-01-07 37.2064 24.1391 14.1777
## 2008-01-08 37.5692 24.1391 14.3531
## 2008-01-09 38.2619 24.2721 14.4663
# compute returns
etf3.ret <- Return.calculate(etf3.xts, method = 'log') %>% 
  na.omit()
head(etf3.ret)
##                      50           52           56
## 2008-01-03 -0.016776681 -0.038777831 -0.013686020
## 2008-01-04  0.000000000 -0.005115179  0.001966651
## 2008-01-07 -0.046762857 -0.071745052 -0.015842581
## 2008-01-08  0.009703776  0.000000000  0.012295639
## 2008-01-09  0.018270057  0.005494610  0.007855860
## 2008-01-10 -0.001726443  0.000000000  0.007410451
# convert into weekly and monthly prices
etf3.w <- to.weekly(etf3.xts, indexAt = "lastof", OHLC = FALSE)
## Warning in !missing(sec) && sec%%1 != 0: 'length(x) = 635 > 1' in coercion to
## 'logical(1)'
etf3.m <- to.monthly(etf3.xts, indexAt = "lastof", OHLC = FALSE)
head(etf3.m)
##                 50      52      56
## 2008-01-31 36.2499 23.2680 12.8646
## 2008-02-29 40.1420 25.5356 14.0079
## 2008-03-31 39.8781 25.1366 14.4324
## 2008-04-30 42.1541 26.7326 14.8851
## 2008-05-31 41.0326 26.1008 14.5625
## 2008-06-30 36.2828 23.1416 12.9608
# compute monthly returns
etf3.ret.m <- Return.calculate(etf3.m, method = 'log') %>% 
  na.omit()
head(etf3.ret.m)
##                      50          52          56
## 2008-02-29  0.101986545  0.09299453  0.08514210
## 2008-03-31 -0.006595867 -0.01574861  0.02985422
## 2008-04-30  0.055504650  0.06155884  0.03088503
## 2008-05-31 -0.026965079 -0.02391783 -0.02191098
## 2008-06-30 -0.123023073 -0.12033410 -0.11652031
## 2008-07-31 -0.048424080 -0.06408000 -0.06447680
etf3.ret.w <- Return.calculate(etf3.w, method = 'log') %>% 
  na.omit()
head(etf3.ret.w)
##                     50           52          56
## 2008-01-11 -0.02484203 -0.063789738  0.01365931
## 2008-01-18  0.01463561 -0.019593212 -0.02950189
## 2008-01-25 -0.03655644 -0.010647520 -0.05119047
## 2008-02-01  0.00000000 -0.006783635 -0.03202126
## 2008-02-15  0.01583096  0.020770112  0.01933553
## 2008-02-22  0.02584300  0.019258135  0.01268573