rm(list=ls())
#install.packages("quantmod")
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
## Version 0.4-0 included new data defaults. See ?getSymbols.
#install.packages("tidyquant")
library("tidyquant")
## Loading required package: lubridate
##
## Attaching package: 'lubridate'
## The following object is masked from 'package:base':
##
## date
## Loading required package: PerformanceAnalytics
##
## Attaching package: 'PerformanceAnalytics'
## The following object is masked from 'package:graphics':
##
## legend
## Loading required package: tidyverse
## ── Attaching packages ──── tidyverse 1.2.1 ──
## ✔ ggplot2 3.1.1 ✔ purrr 0.3.2
## ✔ tibble 2.1.1 ✔ dplyr 0.8.1
## ✔ tidyr 0.8.3 ✔ stringr 1.4.0
## ✔ readr 1.3.1 ✔ forcats 0.4.0
## ── Conflicts ─────── tidyverse_conflicts() ──
## ✖ lubridate::as.difftime() masks base::as.difftime()
## ✖ lubridate::date() masks base::date()
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::first() masks xts::first()
## ✖ lubridate::intersect() masks base::intersect()
## ✖ dplyr::lag() masks stats::lag()
## ✖ dplyr::last() masks xts::last()
## ✖ lubridate::setdiff() masks base::setdiff()
## ✖ lubridate::union() masks base::union()
library(lubridate)
library(timetk)
tickers = c("SPY", "QQQ", "EEM" , "IWM", "EFA", "TLT", "IYR", "GLD")
getSymbols(tickers, from ="2010-01-01", auto.assign= TRUE)
## 'getSymbols' currently uses auto.assign=TRUE by default, but will
## use auto.assign=FALSE in 0.5-0. You will still be able to use
## 'loadSymbols' to automatically load data. getOption("getSymbols.env")
## and getOption("getSymbols.auto.assign") will still be checked for
## alternate defaults.
##
## This message is shown once per session and may be disabled by setting
## options("getSymbols.warning4.0"=FALSE). See ?getSymbols for details.
## pausing 1 second between requests for more than 5 symbols
## pausing 1 second between requests for more than 5 symbols
## pausing 1 second between requests for more than 5 symbols
## pausing 1 second between requests for more than 5 symbols
## [1] "SPY" "QQQ" "EEM" "IWM" "EFA" "TLT" "IYR" "GLD"
# There are two primary time series data
# xts and zoo
firm<-merge(Ad(SPY), Ad(QQQ), Ad(EEM), Ad(IWM), Ad(EFA), Ad(TLT), Ad(IYR), Ad(GLD))
head(firm)
## SPY.Adjusted QQQ.Adjusted EEM.Adjusted IWM.Adjusted
## 2010-01-04 94.13087 42.07555 35.67281 56.09659
## 2010-01-05 94.38007 42.07555 35.93174 55.90370
## 2010-01-06 94.44649 41.82175 36.00691 55.85109
## 2010-01-07 94.84521 41.84896 35.79811 56.26318
## 2010-01-08 95.16080 42.19340 36.08208 56.57005
## 2010-01-11 95.29370 42.02117 36.00691 56.34210
## EFA.Adjusted TLT.Adjusted IYR.Adjusted GLD.Adjusted
## 2010-01-04 43.40944 68.21629 32.26275 109.80
## 2010-01-05 43.44770 68.65686 32.34021 109.70
## 2010-01-06 43.63134 67.73775 32.32613 111.51
## 2010-01-07 43.46300 67.85172 32.61488 110.82
## 2010-01-08 43.80734 67.82134 32.39655 111.37
## 2010-01-11 44.16698 67.44915 32.55150 112.85
colnames(firm)<-c("SPY", "QQQ", "EEM", "IWM", "EFA", "TLT", "IYR", "GLD")
head(firm)
## SPY QQQ EEM IWM EFA TLT IYR
## 2010-01-04 94.13087 42.07555 35.67281 56.09659 43.40944 68.21629 32.26275
## 2010-01-05 94.38007 42.07555 35.93174 55.90370 43.44770 68.65686 32.34021
## 2010-01-06 94.44649 41.82175 36.00691 55.85109 43.63134 67.73775 32.32613
## 2010-01-07 94.84521 41.84896 35.79811 56.26318 43.46300 67.85172 32.61488
## 2010-01-08 95.16080 42.19340 36.08208 56.57005 43.80734 67.82134 32.39655
## 2010-01-11 95.29370 42.02117 36.00691 56.34210 44.16698 67.44915 32.55150
## GLD
## 2010-01-04 109.80
## 2010-01-05 109.70
## 2010-01-06 111.51
## 2010-01-07 110.82
## 2010-01-08 111.37
## 2010-01-11 112.85
library(tidyverse)
library(tidyquant)
price_weekly<-to.weekly(firm, indexAt = "last", OHLC=FALSE)
stock_returns_weekly<-na.omit(Return.calculate(price_weekly, method="log"))
head(stock_returns_weekly)
## SPY QQQ EEM IWM EFA
## 2010-01-15 -0.008150151 -0.01515199 -0.02936205 -0.01310461 -0.003499458
## 2010-01-22 -0.039762846 -0.03755592 -0.05739643 -0.03110044 -0.057354334
## 2010-01-29 -0.016805790 -0.03151480 -0.03415430 -0.02659380 -0.026141733
## 2010-02-05 -0.006820688 0.00443036 -0.02861874 -0.01407296 -0.019238669
## 2010-02-12 0.012855091 0.01798540 0.03278968 0.02909818 0.005230991
## 2010-02-19 0.028289105 0.02415713 0.02415957 0.03288520 0.022734752
## TLT IYR GLD
## 2010-01-15 1.984899e-02 -0.006324176 -0.004589865
## 2010-01-22 1.005025e-02 -0.042683035 -0.033851813
## 2010-01-29 3.363481e-03 -0.008483892 -0.011354685
## 2010-02-05 -5.439768e-05 0.003218778 -0.012153577
## 2010-02-12 -1.965291e-02 -0.007602877 0.022294528
## 2010-02-19 -8.238338e-03 0.048966431 0.022447943
prices_monthly <- to.monthly(firm, indexAt = "last", OHLC = FALSE)
stock_returns_xts <- na.omit(Return.calculate(prices_monthly, method = "log"))
head(stock_returns_xts)
## SPY QQQ EEM IWM EFA
## 2010-02-26 0.03071820 0.04501028 0.017607768 0.04377887 0.00266388
## 2010-03-31 0.05909806 0.07428098 0.077987148 0.07909460 0.06189853
## 2010-04-30 0.01535186 0.02217746 -0.001663121 0.05523082 -0.02844674
## 2010-05-28 -0.08278913 -0.07679835 -0.098645456 -0.07835791 -0.11870236
## 2010-06-30 -0.05312741 -0.06161692 -0.014084732 -0.08059607 -0.02083487
## 2010-07-30 0.06606925 0.07006902 0.103751388 0.06514060 0.10984393
## TLT IYR GLD
## 2010-02-26 -0.003430628 0.05313394 0.032223420
## 2010-03-31 -0.020788116 0.09302108 -0.004396042
## 2010-04-30 0.032678280 0.06192350 0.057168648
## 2010-05-28 0.049822170 -0.05851430 0.030056874
## 2010-06-30 0.056360158 -0.04782665 0.023280092
## 2010-07-30 -0.009508924 0.08988416 -0.052210719
as_tibble(stock_returns_xts, rownames = 'date')
## Warning: Calling `as_tibble()` on a vector is discouraged, because the behavior is likely to change in the future. Use `tibble::enframe(name = NULL)` instead.
## This warning is displayed once per session.
## # A tibble: 113 x 9
## date SPY QQQ EEM IWM EFA TLT IYR
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 2010… 0.0307 0.0450 0.0176 0.0438 0.00266 -0.00343 0.0531
## 2 2010… 0.0591 0.0743 0.0780 0.0791 0.0619 -0.0208 0.0930
## 3 2010… 0.0154 0.0222 -0.00166 0.0552 -0.0284 0.0327 0.0619
## 4 2010… -0.0828 -0.0768 -0.0986 -0.0784 -0.119 0.0498 -0.0585
## 5 2010… -0.0531 -0.0616 -0.0141 -0.0806 -0.0208 0.0564 -0.0478
## 6 2010… 0.0661 0.0701 0.104 0.0651 0.110 -0.00951 0.0899
## 7 2010… -0.0460 -0.0527 -0.0329 -0.0774 -0.0387 0.0806 -0.0131
## 8 2010… 0.0858 0.124 0.111 0.117 0.0951 -0.0255 0.0453
## 9 2010… 0.0375 0.0615 0.0297 0.0406 0.0373 -0.0457 0.0386
## 10 2010… 0 -0.00173 -0.0295 0.0343 -0.0494 -0.0170 -0.0160
## # … with 103 more rows, and 1 more variable: GLD <dbl>
head(stock_returns_xts )
## SPY QQQ EEM IWM EFA
## 2010-02-26 0.03071820 0.04501028 0.017607768 0.04377887 0.00266388
## 2010-03-31 0.05909806 0.07428098 0.077987148 0.07909460 0.06189853
## 2010-04-30 0.01535186 0.02217746 -0.001663121 0.05523082 -0.02844674
## 2010-05-28 -0.08278913 -0.07679835 -0.098645456 -0.07835791 -0.11870236
## 2010-06-30 -0.05312741 -0.06161692 -0.014084732 -0.08059607 -0.02083487
## 2010-07-30 0.06606925 0.07006902 0.103751388 0.06514060 0.10984393
## TLT IYR GLD
## 2010-02-26 -0.003430628 0.05313394 0.032223420
## 2010-03-31 -0.020788116 0.09302108 -0.004396042
## 2010-04-30 0.032678280 0.06192350 0.057168648
## 2010-05-28 0.049822170 -0.05851430 0.030056874
## 2010-06-30 0.056360158 -0.04782665 0.023280092
## 2010-07-30 -0.009508924 0.08988416 -0.052210719