i would like to aggregate monthly returns in a dataframe to a yearly return. Unfortunately i don't understand how the aggregate function works based on the information provided by R; or if it's even the right function to begin with. The code itself is based on BatchGetSymbols, because i want to rank all Tickers from an Index (in this case the S&P 500) for Momentum/Contrarian Strategies.
I have already tried the aggregate function, which gives me these error messages:
aggregate(prelimranking, FUN = add_row(prelimranking$ret.adjusted.prices))
"Error: Can't subset with `[` using an object of class NULL.
Call `rlang::last_error()` to see a backtrace
In addition: Warning message:
`.data` must be a data frame in `add_row()` and `add_case()`."
This is the relevant code:
//
library(BatchGetSymbols)
library(tidyverse)
library(plyr)
library(dplyr)
first.date <- Sys.Date() - 365
last.date <- Sys.Date()
GSPCData <- GetSP500Stocks(do.cache = TRUE, cache.folder = "BGS_Cache")
tickers <- GSPCData$Tickers
l.out <- BatchGetSymbols(tickers = tickers,first.date = first.date,last.date = last.date, do.cache=FALSE, freq.data = "monthly")
prelimranking <- na.omit(l.out$df.tickers)
//
So what i get as "prelimranking" is basically a table that has about 6000 rows, with every ticker having 12 rows for the return of every single month. What i would like to have is a table with the monthy returns summed up, so i get around 500 rows, one for each ticker; so that i can rank them based on their returns. Thank you very much for your time.