library(tidyverse)
library(lubridate)
library(tidyquant)
#install.packages("fable")
library(fpp3)
Some equities data is available from tidyquant.
Nike <- tq_get("NKE", from="2019-01-01") # Goes and gets stock prices
head(Nike)
## # A tibble: 6 x 7
## date open high low close volume adjusted
## <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 2019-01-02 72.8 74.6 72.2 74.1 6762700 72.6
## 2 2019-01-03 73.2 73.3 71.2 72.8 8007400 71.3
## 3 2019-01-04 73.4 75.1 73.1 74.7 7844200 73.2
## 4 2019-01-07 74.7 76.4 74.3 75.7 8184800 74.2
## 5 2019-01-08 76.8 77.4 76.2 76.7 8809000 75.2
## 6 2019-01-09 77.0 77.2 76.1 76.6 8591000 75.1
NikeT <- Nike %>% as_tsibble(index=date)
head(NikeT)
## # A tsibble: 6 x 7 [1D]
## date open high low close volume adjusted
## <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 2019-01-02 72.8 74.6 72.2 74.1 6762700 72.6
## 2 2019-01-03 73.2 73.3 71.2 72.8 8007400 71.3
## 3 2019-01-04 73.4 75.1 73.1 74.7 7844200 73.2
## 4 2019-01-07 74.7 76.4 74.3 75.7 8184800 74.2
## 5 2019-01-08 76.8 77.4 76.2 76.7 8809000 75.2
## 6 2019-01-09 77.0 77.2 76.1 76.6 8591000 75.1
# Looking at adjusted close price
(NPT <- NikeT %>% autoplot(adjusted)) # we can only autoplot when it has a specified tsibble input
# Looking at Volume
NikeT %>% autoplot(volume)
Variables: date, open, high, low, close, volume (volume of shares that are transacted), adjusted (adj close price)
sportsAp <- c("NKE", "ADDYY") %>% tq_get(., from = "2019-01-01") %>%
as_tsibble(., index = date, key = symbol)
## Taking Nike and Adidas from Jan 2019, applying the index and adding a key
# Looking at adjusted close price
sportsAp %>% autoplot(adjusted)
# Looking at Volume
sportsAp %>% autoplot(volume)
I want to show this as both the time series and as the returns – the far more common method for analysing them.
# Calculating the daily return
NC <- Nike %>%
tq_transmute(adjusted, # Automated way of doing calculating daily return
mutate_fun = periodReturn, # we mutate for a period return
period = "daily") # Here we specify the period
# The key to transmute is that it is going to drop the day at the beginning (to solve the issue below)
## And now the dataset (calendar) is a little bit shorter
## What is a daily return?
### (Todays $ - Yesterdays $) / Yesterdays $
### This issue with this ^? At some point, we don't have yesterdays data (the beginning)
#### We now have a missing bit of data once we do this
NC
## # A tibble: 528 x 2
## date daily.returns
## <date> <dbl>
## 1 2019-01-02 0
## 2 2019-01-03 -0.0177
## 3 2019-01-04 0.0261
## 4 2019-01-07 0.0143
## 5 2019-01-08 0.0133
## 6 2019-01-09 -0.00182
## 7 2019-01-10 -0.00222
## 8 2019-01-11 -0.00497
## 9 2019-01-14 0.000657
## 10 2019-01-15 0.0235
## # … with 518 more rows
NCT <- NC %>%
as_tsibble(.,
index=date) %>%
autoplot(daily.returns)
NCT
NCW <- Nike %>%
tq_transmute(adjusted, # Automated way of doing calculating daily return
mutate_fun = periodReturn, # we mutate for a period return
period = "weekly")
NCW
## # A tibble: 110 x 2
## date weekly.returns
## <date> <dbl>
## 1 2019-01-04 0.00797
## 2 2019-01-11 0.0186
## 3 2019-01-18 0.0580
## 4 2019-01-25 0.00199
## 5 2019-02-01 0.0112
## 6 2019-02-08 0.0104
## 7 2019-02-15 0.0367
## 8 2019-02-22 -0.00726
## 9 2019-03-01 0.0310
## 10 2019-03-08 -0.0271
## # … with 100 more rows
NCW %>%
as_tsibble(.,
index=date) %>%
autoplot(weekly.returns)
NCW %>%
as_tsibble(., index = date) # Applying this in the console will show us it is a daily structure
## # A tsibble: 110 x 2 [1D]
## date weekly.returns
## <date> <dbl>
## 1 2019-01-04 0.00797
## 2 2019-01-11 0.0186
## 3 2019-01-18 0.0580
## 4 2019-01-25 0.00199
## 5 2019-02-01 0.0112
## 6 2019-02-08 0.0104
## 7 2019-02-15 0.0367
## 8 2019-02-22 -0.00726
## 9 2019-03-01 0.0310
## 10 2019-03-08 -0.0271
## # … with 100 more rows
# We need to fix this and change it to monthly data
NCW %>% mutate(YW = yearweek(date)) %>%
as_tsibble(., index = YW)
## # A tsibble: 110 x 3 [1W]
## date weekly.returns YW
## <date> <dbl> <week>
## 1 2019-01-04 0.00797 2019 W01
## 2 2019-01-11 0.0186 2019 W02
## 3 2019-01-18 0.0580 2019 W03
## 4 2019-01-25 0.00199 2019 W04
## 5 2019-02-01 0.0112 2019 W05
## 6 2019-02-08 0.0104 2019 W06
## 7 2019-02-15 0.0367 2019 W07
## 8 2019-02-22 -0.00726 2019 W08
## 9 2019-03-01 0.0310 2019 W09
## 10 2019-03-08 -0.0271 2019 W10
## # … with 100 more rows
NCM <- Nike %>%
tq_transmute(adjusted, # Automated way of doing calculating daily return
mutate_fun = periodReturn, # we mutate for a period return
period = "monthly")
NCM
## # A tibble: 26 x 2
## date monthly.returns
## <date> <dbl>
## 1 2019-01-31 0.106
## 2 2019-02-28 0.0470
## 3 2019-03-29 -0.0152
## 4 2019-04-30 0.0430
## 5 2019-05-31 -0.119
## 6 2019-06-28 0.0883
## 7 2019-07-31 0.0248
## 8 2019-08-30 -0.0152
## 9 2019-09-30 0.111
## 10 2019-10-31 -0.0465
## # … with 16 more rows
NCM %>%
as_tsibble(.,
index=date) %>%
autoplot(monthly.returns)
NCM %>%
as_tsibble(., index = date) # Applying this in the console will show us it is a daily structure
## # A tsibble: 26 x 2 [1D]
## date monthly.returns
## <date> <dbl>
## 1 2019-01-31 0.106
## 2 2019-02-28 0.0470
## 3 2019-03-29 -0.0152
## 4 2019-04-30 0.0430
## 5 2019-05-31 -0.119
## 6 2019-06-28 0.0883
## 7 2019-07-31 0.0248
## 8 2019-08-30 -0.0152
## 9 2019-09-30 0.111
## 10 2019-10-31 -0.0465
## # … with 16 more rows
# We need to fix this and change it to monthly data
NCM %>% mutate(YM = yearmonth(date)) %>%
as_tsibble(., index = YM)
## # A tsibble: 26 x 3 [1M]
## date monthly.returns YM
## <date> <dbl> <mth>
## 1 2019-01-31 0.106 2019 Jan
## 2 2019-02-28 0.0470 2019 Feb
## 3 2019-03-29 -0.0152 2019 Mar
## 4 2019-04-30 0.0430 2019 Apr
## 5 2019-05-31 -0.119 2019 May
## 6 2019-06-28 0.0883 2019 Jun
## 7 2019-07-31 0.0248 2019 Jul
## 8 2019-08-30 -0.0152 2019 Aug
## 9 2019-09-30 0.111 2019 Sep
## 10 2019-10-31 -0.0465 2019 Oct
## # … with 16 more rows
NikeT
## # A tsibble: 528 x 7 [1D]
## date open high low close volume adjusted
## <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 2019-01-02 72.8 74.6 72.2 74.1 6762700 72.6
## 2 2019-01-03 73.2 73.3 71.2 72.8 8007400 71.3
## 3 2019-01-04 73.4 75.1 73.1 74.7 7844200 73.2
## 4 2019-01-07 74.7 76.4 74.3 75.7 8184800 74.2
## 5 2019-01-08 76.8 77.4 76.2 76.7 8809000 75.2
## 6 2019-01-09 77.0 77.2 76.1 76.6 8591000 75.1
## 7 2019-01-10 75.6 77.3 75.5 76.4 11148600 74.9
## 8 2019-01-11 76.3 76.9 75.8 76.0 10689900 74.5
## 9 2019-01-14 75.5 76.8 75.5 76.1 5677400 74.6
## 10 2019-01-15 76.2 78.0 76.1 77.9 6212400 76.3
## # … with 518 more rows
What do they look like together?
#library(patchwork)
#FPT + FCT