library(tidyquant)
## Loading required package: lubridate
##
## Attaching package: 'lubridate'
## The following object is masked from 'package:base':
##
## date
## Loading required package: PerformanceAnalytics
## Loading required package: xts
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
## Registered S3 method overwritten by 'xts':
## method from
## as.zoo.xts zoo
##
## Attaching package: 'PerformanceAnalytics'
## The following object is masked from 'package:graphics':
##
## legend
## Loading required package: quantmod
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
## Version 0.4-0 included new data defaults. See ?getSymbols.
## Loading required package: tidyverse
## Registered S3 methods overwritten by 'ggplot2':
## method from
## [.quosures rlang
## c.quosures rlang
## print.quosures rlang
## Registered S3 method overwritten by 'rvest':
## method from
## read_xml.response xml2
## -- Attaching packages --------------------------------------------------------- tidyverse 1.2.1 --
## √ ggplot2 3.1.1 √ purrr 0.3.2
## √ tibble 2.1.1 √ dplyr 0.8.0.1
## √ tidyr 0.8.3 √ stringr 1.4.0
## √ readr 1.3.1 √ forcats 0.4.0
## -- Conflicts ------------------------------------------------------------ tidyverse_conflicts() --
## x lubridate::as.difftime() masks base::as.difftime()
## x lubridate::date() masks base::date()
## x dplyr::filter() masks stats::filter()
## x dplyr::first() masks xts::first()
## x lubridate::intersect() masks base::intersect()
## x dplyr::lag() masks stats::lag()
## x dplyr::last() masks xts::last()
## x lubridate::setdiff() masks base::setdiff()
## x lubridate::union() masks base::union()
FANG_daily_returns <- FANG %>%
dplyr::group_by(symbol) %>%
nest() %>%
dplyr::rename(data_train = data) %>%
dplyr::mutate(returns = map(data_train, ~ (.$adjusted / lag(.$adjusted) - 1))) %>%
dplyr::mutate(ranking = map(returns, ~ dense_rank(.))) %>%
unnest() %>%
select(-open, -high, -low, -close, -volume, -adjusted) %>%
dplyr::arrange(symbol, ranking) %>%
dplyr::filter(ranking <= 8)
FANG_daily_returns
## # A tibble: 32 x 4
## symbol returns ranking date
## <chr> <dbl> <int> <date>
## 1 AMZN -0.110 1 2014-01-31
## 2 AMZN -0.0988 2 2014-04-25
## 3 AMZN -0.0965 3 2014-07-25
## 4 AMZN -0.0834 4 2014-10-24
## 5 AMZN -0.0761 5 2016-01-29
## 6 AMZN -0.0724 6 2013-04-26
## 7 AMZN -0.0636 7 2016-02-05
## 8 AMZN -0.0629 8 2015-08-24
## 9 FB -0.0693 1 2014-03-26
## 10 FB -0.0669 2 2013-10-08
## # ... with 22 more rows