rm(list=ls())
library(pacman)
p_load(tidyverse, lubridate, readxl, highcharter, tidyquant,
timetk, tibbletime, quantmod, PerformanceAnalytics, scales)
p_load(reshape2)
#1
library(readr)
X0528 <- read_csv("~/Test/0528.csv")
## Parsed with column specification:
## cols(
## `50` = col_double(),
## 元大台灣50 = col_character(),
## `20080102` = col_double(),
## `39.6472` = col_double()
## )
## Warning: 1214 parsing failures.
## row col expected actual file
## 5828 50 no trailing characters R '~/Test/0528.csv'
## 5833 50 no trailing characters R '~/Test/0528.csv'
## 5838 50 no trailing characters R '~/Test/0528.csv'
## 5843 50 no trailing characters R '~/Test/0528.csv'
## 5848 50 no trailing characters R '~/Test/0528.csv'
## .... ... ...................... ...... .................
## See problems(...) for more details.
etf3<-rename(X0528)
etf3 <- etf3[, -2]
colnames(etf3) <- c('id', 'date', 'close')
etf3.1 <- dcast(etf3, date~id) %>%
na.omit()
## Using close as value column: use value.var to override.
head(etf3)
## # A tibble: 6 x 3
## id date close
## <dbl> <dbl> <dbl>
## 1 52 20080102 27.1
## 2 50 20080103 39.0
## 3 52 20080103 26.1
## 4 50 20080104 39.0
## 5 52 20080104 25.9
## 6 50 20080107 37.2
#2
etf3.xts <- xts(etf3.1 [, -1], order.by = as.Date(as.character(etf3.1$date), format = '%Y%m%d'))
head(etf3.xts)
## 50 52 61 646 6206 NA
## 2015-12-14 51.1736 30.6881 17.74 19.61 28.68 20.05
## 2015-12-15 51.2167 30.9138 17.94 19.63 29.28 19.54
## 2015-12-16 51.9921 31.3820 18.06 19.89 29.19 19.53
## 2015-12-17 52.5951 31.5993 18.36 20.05 29.51 19.13
## 2015-12-18 52.3367 31.8000 18.34 19.85 29.68 18.99
## 2015-12-21 52.2505 31.5993 18.90 19.64 30.40 18.51
#3
etf3.ret <- Return.calculate(etf3.xts, method = 'log') %>% na.omit()
head(etf3.ret)
## 50 52 61 646 6206
## 2015-12-15 0.0008418767 0.007327729 0.011210880 0.001019368 0.020704673
## 2015-12-16 0.0150261332 0.015031794 0.006666691 0.013158085 -0.003078504
## 2015-12-17 0.0115311755 0.006900488 0.016474837 0.008012061 0.010903004
## 2015-12-18 -0.0049251137 0.006331321 -0.001089918 -0.010025147 0.005744229
## 2015-12-21 -0.0016483856 -0.006331321 0.030077455 -0.010635704 0.023969190
## 2015-12-22 0.0024696515 0.000000000 -0.005305052 0.004065046 -0.003955180
## NA
## 2015-12-15 -0.0257655071
## 2015-12-16 -0.0005119017
## 2015-12-17 -0.0206939614
## 2015-12-18 -0.0073452586
## 2015-12-21 -0.0256013982
## 2015-12-22 -0.0005403945
etf3.w <- to.weekly(etf3.xts, indexAt = "lastof", OHLC = FALSE)
head(etf3.w)
## 50 52 61 646 6206 NA
## 2015-12-18 52.3367 31.8000 18.34 19.85 29.68 18.99
## 2015-12-25 53.0690 32.1176 18.84 19.99 30.33 18.40
## 2015-12-31 52.3367 31.7665 18.27 20.06 29.48 19.01
## 2016-01-08 49.4937 29.7017 16.92 19.32 27.53 20.19
## 2016-01-15 48.8476 29.9692 15.80 18.97 25.53 21.88
## 2016-01-22 48.7183 30.0946 15.82 18.77 25.73 21.81
etf3.m <- to.monthly(etf3.xts, indexAt = "lastof", OHLC = FALSE)
head(etf3.m)
## 50 52 61 646 6206 NA
## 2015-12-31 52.3367 31.7665 18.27 20.06 29.48 19.01
## 2016-01-31 51.3029 31.2315 15.34 19.28 25.15 21.70
## 2016-02-29 52.7674 32.8198 15.00 19.21 24.23 21.85
## 2016-03-31 55.6966 34.3914 16.65 19.71 26.40 18.98
## 2016-04-30 53.0690 32.3433 16.05 19.90 25.98 19.30
## 2016-05-31 54.4043 33.7157 16.09 20.34 25.99 19.27
#4
etf3.ret.m <- Return.calculate(etf3.m, method = 'log') %>% na.omit()
head(etf3.ret.m)
## 50 52 61 646 6206
## 2016-01-31 -0.01995057 -0.01698507 -0.174796574 -0.039659493 -0.1588541708
## 2016-02-29 0.02814629 0.04960479 -0.022413595 -0.003637312 -0.0372663617
## 2016-03-31 0.05402553 0.04677454 0.104360015 0.025695144 0.0857724753
## 2016-04-30 -0.04832615 -0.06139964 -0.036701367 0.009593611 -0.0160369989
## 2016-05-31 0.02485024 0.04155671 0.002489111 0.021869659 0.0003848374
## 2016-06-30 0.03577416 0.05013990 -0.004984434 -0.026402530 -0.0194256303
## NA
## 2016-01-31 0.132347104
## 2016-02-29 0.006888661
## 2016-03-31 -0.140815128
## 2016-04-30 0.016719303
## 2016-05-31 -0.001555613
## 2016-06-30 -0.002598079
#5