## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.1 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.1
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.4'
## (as 'lib' is unspecified)
## Warning: package 'scale' is not available for this version of R
##
## A version of this package for your version of R might be available elsewhere,
## see the ideas at
## https://cran.r-project.org/doc/manuals/r-patched/R-admin.html#Installing-packages
## 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 'scale'
## Warning in p_load(tidyverse, lubridate, readxl, highcharter, tidyquant, : Failed to install/load:
## scale
##
## Attaching package: 'magrittr'
##
## The following object is masked from 'package:purrr':
##
## set_names
##
## The following object is masked from 'package:tidyr':
##
## extract
##
##
## Attaching package: 'scales'
##
## The following object is masked from 'package:purrr':
##
## discard
##
## The following object is masked from 'package:readr':
##
## col_factor
## # A tibble: 12 × 2
## Month Sales
## <ord> <chr>
## 1 Jan $4,089,460
## 2 Feb $5,343,295
## 3 Mar $7,282,280
## 4 Apr $8,386,170
## 5 May $7,935,055
## 6 Jun $7,813,105
## 7 Jul $7,602,005
## 8 Aug $5,346,125
## 9 Sep $5,556,055
## 10 Oct $4,394,300
## 11 Nov $4,169,755
## 12 Dec $3,114,725
2.1 Following outputs are based on data from 2008-2020. You need to update the outputs based on data 2008-2023.
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.4'
## (as 'lib' is unspecified)
## Warning: package '+timetk' is not available for this version of R
##
## A version of this package for your version of R might be available elsewhere,
## see the ideas at
## https://cran.r-project.org/doc/manuals/r-patched/R-admin.html#Installing-packages
## 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:
## +timetk
## 'data.frame': 9123 obs. of 4 variables:
## $ 證券代碼 : int 50 52 56 50 52 56 50 52 56 50 ...
## $ 簡稱 : chr "元大台灣50" "富邦科技" "元大高股息" "元大台灣50" ...
## $ 年月日 : int 20080102 20080102 20080102 20080103 20080103 20080103 20080104 20080104 20080104 20080107 ...
## $ 收盤價.元.: num 39.6 27.1 14.6 39 26.1 ...
## 'data.frame': 3041 obs. of 4 variables:
## $ date: int 20080102 20080103 20080104 20080107 20080108 20080109 20080110 20080111 20080114 20080115 ...
## $ 50 : num 39.6 39 39 37.2 37.6 ...
## $ 52 : num 27.1 26.1 25.9 24.1 24.1 ...
## $ 56 : num 14.6 14.4 14.4 14.2 14.4 ...
## date 50 52 56
## 1 20080102 39.6472 27.0983 14.5739
## 2 20080103 38.9876 26.0676 14.3758
## 3 20080104 38.9876 25.9346 14.4041
## 4 20080107 37.2064 24.1391 14.1777
## 5 20080108 37.5692 24.1391 14.3531
## 6 20080109 38.2619 24.2721 14.4663
## date 50 52 56
## 3036 20200423 80.90 62.65 26.34
## 3037 20200424 80.90 62.55 26.33
## 3038 20200427 82.55 63.75 26.79
## 3039 20200428 82.55 63.10 26.91
## 3040 20200429 83.70 63.80 27.07
## 3041 20200430 85.50 64.75 27.46
## date 50 52 56
## 0 0 0 0
## 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
## 50 52 56
## 2020-04-23 80.90 62.65 26.34
## 2020-04-24 80.90 62.55 26.33
## 2020-04-27 82.55 63.75 26.79
## 2020-04-28 82.55 63.10 26.91
## 2020-04-29 83.70 63.80 27.07
## 2020-04-30 85.50 64.75 27.46
2.2 Compute discrete daily returns.
etf3.ret.log <- Return.calculate(etf3.xts, method = 'log') %>% na.omit()
head(etf3.ret.log)
## 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
tail(etf3.ret.log)
## 50 52 56
## 2020-04-23 0.003715175 0.003998406 0.0041849024
## 2020-04-24 0.000000000 -0.001597444 -0.0003797228
## 2020-04-27 0.020190346 0.019002947 0.0173197100
## 2020-04-28 0.000000000 -0.010248414 0.0044692812
## 2020-04-29 0.013834807 0.011032421 0.0059281389
## 2020-04-30 0.021277398 0.014780510 0.0143042967
2.3 Convert daily closing prices into weekly prices.
# Convert daily closing prices into weekly prices
## etf3.w <- to.weekly(etf3.xts, indexAt = "lastof", OHLC = FALSE)
# this is not working
2.4 Compute weekly returns.
etf3.w1 <- etf3.xts %>%
tk_tbl(preserve_index = TRUE, rename_index ="date") %>%
# this is the the tibbletime function
as_tbl_time(index = date) %>%
as_period(period = "week", side ="end")
head(etf3.w1)
## # A time tibble: 6 × 4
## # Index: date
## date `50` `52` `56`
## <date> <dbl> <dbl> <dbl>
## 1 2008-01-04 39.0 25.9 14.4
## 2 2008-01-11 38.0 24.3 14.6
## 3 2008-01-18 38.6 23.9 14.2
## 4 2008-01-25 37.2 23.6 13.5
## 5 2008-02-01 37.2 23.4 13.0
## 6 2008-02-15 37.8 23.9 13.3
tail(etf3.w1)
## # A time tibble: 6 × 4
## # Index: date
## date `50` `52` `56`
## <date> <dbl> <dbl> <dbl>
## 1 2020-03-27 76.6 57.8 24.4
## 2 2020-04-01 75.9 57.0 24.5
## 3 2020-04-10 79.3 59.4 25.7
## 4 2020-04-17 83.2 63.8 26.5
## 5 2020-04-24 80.9 62.6 26.3
## 6 2020-04-30 85.5 64.8 27.5
etf3.w1.ret <- etf3.w1 %>% Return.calculate(method = "discrete") %>%
na.omit()
head(etf3.w1.ret)
## X50 X52 X56
## 2008-01-11 -0.02453601 -0.061797753 0.01375303
## 2008-01-18 0.01474324 -0.019402513 -0.02907096
## 2008-01-25 -0.03589632 -0.010591036 -0.04990231
## 2008-02-01 0.00000000 -0.006760678 -0.03151401
## 2008-02-15 0.01595693 0.020987312 0.01952367
## 2008-02-22 0.02617982 0.019444769 0.01276653
tail(etf3.w1.ret)
## X50 X52 X56
## 2020-03-27 0.035135135 0.05673695 0.045962199
## 2020-04-01 -0.009138381 -0.01426173 0.006981520
## 2020-04-10 0.044795784 0.04170270 0.049347471
## 2020-04-17 0.049810845 0.07516288 0.029926156
## 2020-04-24 -0.028228228 -0.02034948 -0.006415094
## 2020-04-30 0.056860321 0.03517186 0.042916825
2.5 Convert daily closing prices into monthly prices.
# Convert daily closing prices into monthly prices
## etf3.w <- to.weekly(etf3.xts, indexAt = "lastof", OHLC = FALSE)
# this is not working
2.6 Compute monthly returns.
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
tail(etf3.m)
## 50 52 56
## 2019-11-30 88.6205 64.7226 27.75
## 2019-12-31 93.8990 70.0596 28.97
## 2020-01-31 89.9500 67.6337 27.92
## 2020-02-29 88.6500 65.8870 27.90
## 2020-03-31 76.1500 56.6687 24.49
## 2020-04-30 85.5000 64.7500 27.46
etf3.ret.m1 <- Return.calculate(etf3.m, method = 'log') %>%
na.omit()
head(etf3.ret.m1)
## 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
tail(etf3.ret.m1)
## 50 52 56
## 2019-11-30 0.01541833 0.01891779 0.0061449678
## 2019-12-31 0.05785653 0.07923586 0.0430249716
## 2020-01-31 -0.04296578 -0.03523993 -0.0369175339
## 2020-02-29 -0.01455793 -0.02616523 -0.0007165891
## 2020-03-31 -0.15199095 -0.15071912 -0.1303618179
## 2020-04-30 0.11581130 0.13331167 0.1144655294