#' fun_TSMOM_v2
#'
#' @description Calculated time series momentum strategy from p. 236 and return Sharpe ratio
#'
#' @param list List of periodic data
#' @param period character vector Period of rebalance
#' @param lag int Number of lags
#' @param proAnnuVol int: Procent one scale the inverse of the volatility with
#' @param Rf double scalar or vector: Risk free rate
#' @param annu int used to annualize data
#'
#' @return A list
#' \item{name outList}{description List with TSMOM for each crypto}
#' \item{name Sharpe}{description Table of Sharpe ratios}
#' \item{name Sharpe_tidy}{description Sharpe ratios given in tidy form}
#' \item{name SharpeCum_tidy}{description Sharpe ratios cumulated given in tidy form}
#' @export
#'
fun_TSMOM_v2 <- function(list = listExcess[["months"]],
period = c("minutes", "hours", "days", "weeks", "months", "quarters", "years"),
lag = 6,
proAnnuVol = 0.4,
Rf = 0,
annu = 250) {
outList <- lapply(list, function(x) {
out <- fun_Lag_df(x, lag = lag)
TSMOM <- as.numeric(out[-nrow(out), paste0("exReVolSign_lag", lag)]) *
(proAnnuVol / as.numeric(out[-nrow(out), "sigma"])) *
as.numeric(out[-1,"excessReturn"])
out <- cbind(out, TSMOM = c(rep(NA, length(TSMOM[is.infinite(TSMOM) | is.na(TSMOM)]) + 1),
TSMOM[!is.infinite(TSMOM) & !is.na(TSMOM)]))
out <-cbind(out, CumTSMOM = c(rep(NA, length(which(is.na(out[,"TSMOM"])))),
cumsum(na.omit(out[,"TSMOM"]))))
})
SharpeList <- lapply(outList, function(x) {
tseries::sharpe(as.numeric(x[!is.na(x[, "CumTSMOM"]), "CumTSMOM"]),
r = Rf, scale = sqrt(annu))
})
SharpeCumList <- lapply(outList, function(x) {
x[!is.na(x[,"CumTSMOM"]),"CumTSMOM"]
})
SharpeCumInd <- dplyr::intersect(zoo::index(SharpeCumList[[1]]),
zoo::index(SharpeCumList[[2]])) %>%
dplyr::intersect(zoo::index(SharpeCumList[[3]])) %>%
dplyr::intersect(zoo::index(SharpeCumList[[4]])) %>%
lubridate::as_date()
sharpeCum <- do.call(cbind, lapply(SharpeCumList, function(x) {
x[SharpeCumInd]
})) %>%
rowSums()/length(list)
sharpeCum <- tseries::sharpe(sharpeCum, r = Rf, scale = sqrt(annu))
SharpeCum_tidy <- tibble::tibble(Sharpe = sharpeCum, "Freq" = paste(period), Lag = paste0(lag,"-",period))
names(outList) <- names(list)
names(SharpeList) <- names(list)
Sharpe_tidy <- do.call(cbind, SharpeList) %>%
tibble::as_tibble() %>%
tidyr::gather(Crypto, Sharpe) %>%
cbind(Freq = rep(period, length(SharpeList)), Lag = rep(lag, length(SharpeList)))
Sharpe_tidy <- cbind(Sharpe_tidy[,c("Sharpe", "Freq")], Crypto = paste0(Sharpe_tidy[,"Crypto"],"-", Sharpe_tidy[,"Lag"],"-", Sharpe_tidy[,"Freq"]))
out <- list(outList = outList,
Sharpe = SharpeList,
Sharpe_tidy = Sharpe_tidy,
SharpeCum_tidy = SharpeCum_tidy)
return(out)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.