library(quantmod)
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(timetk)
library(tidyquant)
## Loading required package: lubridate
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
## Loading required package: PerformanceAnalytics
## 
## Attaching package: 'PerformanceAnalytics'
## The following object is masked from 'package:graphics':
## 
##     legend
## ══ Need to Learn tidyquant? ════════════════════════════════════════════════════
## Business Science offers a 1-hour course - Learning Lab #9: Performance Analysis & Portfolio Optimization with tidyquant!
## </> Learn more at: https://university.business-science.io/p/learning-labs-pro </>
library(readr)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:xts':
## 
##     first, last
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyr)
rm(list=ls())
ETFdata<-read_tsv("Homework 8.txt")
## Rows: 5920 Columns: 4
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: "\t"
## chr (2): CO_ID, CoName
## dbl (2): Date, Close
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
ETFdata$CO_ID <- as.character(ETFdata$CO_ID)
ETFdata$Date <- as.character(ETFdata$Date)
ETFdata$Date <- as.Date(ETFdata$Date, "%Y%m%d")
ETFdata %>% 
  select(CO_ID, Date, Close) %>%
  spread(key = CO_ID, value = Close)
## # A tibble: 1,480 × 5
##    Date       `0050` `0056` `006205` `00646`
##    <date>      <dbl>  <dbl>    <dbl>   <dbl>
##  1 2015-12-14   49.6   15.3     31.1    19.6
##  2 2015-12-15   49.6   15.4     31.6    19.6
##  3 2015-12-16   50.4   15.6     31.6    19.9
##  4 2015-12-17   51.0   15.8     32.2    20.0
##  5 2015-12-18   50.7   15.9     32.2    19.8
##  6 2015-12-21   50.6   15.9     33      19.6
##  7 2015-12-22   50.8   15.9     33.1    19.7
##  8 2015-12-23   50.8   15.9     33.1    19.8
##  9 2015-12-24   51.1   15.9     32.8    20.0
## 10 2015-12-25   51.4   15.9     33.0    20.0
## # … with 1,470 more rows
#daily_returns
ret_day <- ETFdata %>% 
  tk_xts(select = -Date, date_var = Date) %>% 
  Return.calculate(method = "log") 
## Warning: Non-numeric columns being dropped: CO_ID, CoName
ret_day[is.na(ret_day)] <- 0
ret_day[1:10,]
##                 Close
## 2015-12-14  0.0000000
## 2015-12-14 -1.1763982
## 2015-12-14  0.7084471
## 2015-12-14 -0.4598812
## 2015-12-15  0.9286727
## 2015-12-15 -1.1701695
## 2015-12-15  0.7182978
## 2015-12-15 -0.4757816
## 2015-12-16  0.9426782
## 2015-12-16 -1.1753743
#monthly_returns
ret_mon <- ETFdata %>% 
  tk_xts(select = -Date, date_var = Date) %>%
  to.period(period  = "months",
            indexAt = "lastof",
            OHLC    = FALSE)  %>%
  Return.calculate(method = 'log')
## Warning: Non-numeric columns being dropped: CO_ID, CoName
ret_mon[is.na(ret_mon)] <- 0
ret_mon[1:10,]
##                   Close
## 2015-12-31  0.000000000
## 2016-01-31 -0.039659493
## 2016-02-29 -0.003637312
## 2016-03-31  0.025695144
## 2016-04-30  0.009593611
## 2016-05-31  0.021869659
## 2016-06-30 -0.026402530
## 2016-07-31  0.030327952
## 2016-08-31  0.003910073
## 2016-09-30 -0.023693112