library(readr)
library(xts)
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
etf4 <- read_csv('myetf4.csv')
## Parsed with column specification:
## cols(
##   Index = col_date(format = ""),
##   `0050` = col_double(),
##   `0056` = col_double(),
##   `006205` = col_double(),
##   `00646` = col_double()
## )
str(etf4)
## tibble [751 × 5] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
##  $ Index : Date[1:751], format: "2015-12-14" "2015-12-15" ...
##  $ 0050  : num [1:751] 53.3 53.3 54.1 54.8 54.5 ...
##  $ 0056  : num [1:751] 18.2 18.4 18.6 18.8 18.9 ...
##  $ 006205: num [1:751] 31.1 31.6 31.6 32.2 32.2 ...
##  $ 00646 : num [1:751] 19.6 19.6 19.9 20.1 19.9 ...
##  - attr(*, "spec")=
##   .. cols(
##   ..   Index = col_date(format = ""),
##   ..   `0050` = col_double(),
##   ..   `0056` = col_double(),
##   ..   `006205` = col_double(),
##   ..   `00646` = col_double()
##   .. )
head(etf4)
## # A tibble: 6 x 5
##   Index      `0050` `0056` `006205` `00646`
##   <date>      <dbl>  <dbl>    <dbl>   <dbl>
## 1 2015-12-14   53.3   18.2     31.1    19.6
## 2 2015-12-15   53.3   18.4     31.6    19.6
## 3 2015-12-16   54.1   18.6     31.6    19.9
## 4 2015-12-17   54.8   18.8     32.2    20.0
## 5 2015-12-18   54.5   19.0     32.2    19.8
## 6 2015-12-21   54.4   19.0     33      19.6
library(magrittr)
library(PerformanceAnalytics)
## 
## Attaching package: 'PerformanceAnalytics'
## The following object is masked from 'package:graphics':
## 
##     legend
etf4.xts <- xts(etf4[,-1], order.by = etf4$Index)
head(etf4.xts)
##             0050  0056 006205 00646
## 2015-12-14 53.29 18.25  31.06 19.61
## 2015-12-15 53.33 18.38  31.59 19.63
## 2015-12-16 54.14 18.56  31.60 19.89
## 2015-12-17 54.77 18.81  32.23 20.05
## 2015-12-18 54.50 18.95  32.18 19.85
## 2015-12-21 54.41 19.02  33.00 19.64
etf.ret <- etf4.xts %>% Return.calculate() %>% na.omit()
head(etf.ret)
##                     0050         0056        006205        00646
## 2015-12-15  0.0007506099  0.007123288  0.0170637476  0.001019888
## 2015-12-16  0.0151884493  0.009793254  0.0003165559  0.013245033
## 2015-12-17  0.0116364980  0.013469828  0.0199367089  0.008044243
## 2015-12-18 -0.0049297060  0.007442850 -0.0015513497 -0.009975062
## 2015-12-21 -0.0016513761  0.003693931  0.0254816656 -0.010579345
## 2015-12-22  0.0023892667 -0.003680336  0.0030303030  0.004073320
mean(etf.ret)
## [1] 0.0002228601
cov(etf.ret)
##                0050         0056       006205        00646
## 0050   7.837060e-05 4.559164e-05 4.467258e-05 3.663388e-05
## 0056   4.559164e-05 4.526413e-05 2.673674e-05 2.353543e-05
## 006205 4.467258e-05 2.673674e-05 1.304184e-04 2.910367e-05
## 00646  3.663388e-05 2.353543e-05 2.910367e-05 5.902892e-05
etf4.mon.ret <- etf4.xts %>% to.monthly(indexAt = 'lastof', OHLC = FALSE) %>% 
  Return.calculate() %>% na.omit()
head(etf4.mon.ret)
##                   0050         0056       006205        00646
## 2016-01-31 -0.01981651 -0.013785790 -0.173070915 -0.038883350
## 2016-02-29  0.02864096  0.043548387 -0.027578391 -0.003630705
## 2016-03-31  0.05550500 -0.002575992  0.082750583  0.026028110
## 2016-04-30 -0.04724138 -0.037190083 -0.024757804  0.009639777
## 2016-05-31  0.02515382  0.016630901  0.004415011  0.022110553
## 2016-06-30  0.03636364  0.029551451 -0.025641026 -0.026057030