R/fun_SharpeCompare.R

#' fun_SharpeCompare
#'
#' @param listData List of daily data with mean and sd in second and third row
#' @param lag Number of lags used for lookback
#' @param proAnnuVol int: Procent one scale the inverse of the volatility with
#' @param name name for outpput blot
#' @param savePlot If one want to save plot
#' @param outputFig Output path if one have said TRUE to saveplot
#' @param width Width of plot saved
#' @param height heigh of plot saved
#'
#' @return A list
#'  \item{name Sharpe_table}{description List with TSMOM for each crypto}
#'  \item{name Sharpe_plot}{description Table of Sharpe ratios}
#'  \item{name Sharpe_Cum_plot}{description Sharpe ratios given in tidy form}
#'  \item{name SharpeCum_tidy}{description Sharpe ratios cumulated given in tidy form}
#' @export
#'
fun_SharpeCompare <- function(listData = list(BTC= BTC.daily, ETH = ETH.daily, XRP = XRP.daily, BCH = BCH.daily),
                              lag = c(1, 3, 6, 12),
                              proAnnuVol = 0.4,
                              savePlot = FALSE, name = "Crypto",
                              outputFig = c("C:/Users/Soren Schwartz/Dropbox/Egne dokumenter/Skole/master/opgave/Figures/"),
                              width = 8, height = 6) {
  # Days
  days <- list(
    days1 <- fun_TSMOM(listData, period = "days", lag = lag[1], proAnnuVol = proAnnuVol, annu = 250),
    days2 <- fun_TSMOM(listData, period = "days", lag = lag[2], proAnnuVol = proAnnuVol, annu = 250),
    days3 <- fun_TSMOM(listData, period = "days", lag = lag[3], proAnnuVol = proAnnuVol, annu = 250),
    days4 <- fun_TSMOM(listData, period = "days", lag = lag[4], proAnnuVol = proAnnuVol, annu = 250)
  )

  # weeks
  weeks <- list(
    weeks1 <- fun_TSMOM(listData, period = "weeks", lag = lag[1], proAnnuVol = proAnnuVol, annu = 52.25),
    weeks2 <- fun_TSMOM(listData, period = "weeks", lag = lag[2], proAnnuVol = proAnnuVol, annu = 52.25),
    weeks3 <- fun_TSMOM(listData, period = "weeks", lag = lag[3], proAnnuVol = proAnnuVol, annu = 52.25),
    weeks4 <- fun_TSMOM(listData, period = "weeks", lag = lag[4], proAnnuVol = proAnnuVol, annu = 52.25)
  )

  # months
  months <- list(
    months1 <- fun_TSMOM(listData, period = "months", lag = lag[1], proAnnuVol = proAnnuVol, annu = 12),
    months2 <- fun_TSMOM(listData, period = "months", lag = lag[2], proAnnuVol = proAnnuVol, annu = 12),
    months3 <- fun_TSMOM(listData, period = "months", lag = lag[3], proAnnuVol = proAnnuVol, annu = 12),
    months4 <- fun_TSMOM(listData, period = "months", lag = lag[4], proAnnuVol = proAnnuVol, annu = 12)
  )

  Sharpe_gg <- do.call(dplyr::bind_rows, lapply(list(days, weeks, months), function(x) {
    lapply(x, function(x) {
      x$Sharpe_tidy
    })}))
  Sharpe_gg <- Sharpe_gg[-which(is.na(Sharpe_gg$Sharpe)),]
  Sharpe_gg$Sharpe <- round(Sharpe_gg$Sharpe, digits = 4)
  Sharpe_gg <- Sharpe_gg[order(Sharpe_gg$Sharpe, decreasing = TRUE), ]
  Sharpe_gg$Crypto <- factor(Sharpe_gg$Crypto, levels = Sharpe_gg$Crypto)

  sharpe_ggplot <- ggplot2::ggplot(Sharpe_gg,
                                   ggplot2::aes(x=Crypto, y=Sharpe, label=Sharpe)) +
    ggplot2::geom_bar(stat='identity',
                      ggplot2::aes(fill=Freq), width=.5)  +
    ggplot2::scale_fill_manual(name="Frequency",
                               labels = c("Days", "Months", "Weeks"),
                               values = c(RColorBrewer::brewer.pal(3,"Set1"))) +
    ggplot2::labs(subtitle="Sharpe ratio for different frequencies and lags",
                  title= "Sharpe Ratios") +
    ggplot2::theme(axis.text = ggplot2::element_text(size=5)) +
    ggplot2::coord_flip()

  if(savePlot == TRUE) {
    pdf(file = paste0(outputFig, "Sharpe_Ratio", name, ".pdf"),
        width = width, height = height)
    print(sharpe_ggplot)
    dev.off()
  }

  Sharpe_gg_Cum <- do.call(dplyr::bind_rows, lapply(list(days, weeks, months), function(x) {
    lapply(x, function(x) {
      x$SharpeCum_tidy
    })}))
  Sharpe_gg_Cum <- Sharpe_gg_Cum[-which(is.na(Sharpe_gg_Cum$Sharpe)),]
  Sharpe_gg_Cum$Sharpe <- round(Sharpe_gg_Cum$Sharpe, digits = 4)
  Sharpe_gg_Cum <- Sharpe_gg_Cum[order(Sharpe_gg_Cum$Sharpe, decreasing = TRUE), ]
  Sharpe_gg_Cum$Lag <- factor(Sharpe_gg_Cum$Lag, levels = Sharpe_gg_Cum$Lag)

  Sharpe_gg_Cumplot <- ggplot2::ggplot(Sharpe_gg_Cum,
                                       ggplot2::aes(x=Lag, y=Sharpe, label=Sharpe)) +
    ggplot2::geom_bar(stat='identity',
                      ggplot2::aes(fill=Freq), width=.5)  +
    ggplot2::scale_fill_manual(name="Frequency",
                               labels = c("Days", "Months", "Weeks"),
                               values = c(RColorBrewer::brewer.pal(3,"Set1"))) +
    ggplot2::labs(subtitle="Sharpe ratio for different frequencies and lags",
                  title= "Sharpe Ratios for cumulated strategy") +
    ggplot2::theme(axis.text = ggplot2::element_text(size=5)) +
    ggplot2::coord_flip()

  if(savePlot == TRUE) {
    pdf(file = paste0(outputFig, "Sharpe_Ratio_Cum", name, ".pdf"),
        width = width, height = height)
    print(Sharpe_gg_Cumplot)
    dev.off()
  }

  out <- list(Sharpe_table = Sharpe_gg,
              Sharpe_plot = sharpe_ggplot,
              Sharpe_Cum_plot = Sharpe_gg_Cumplot)
  return(out)
}
3schwartz/SpecialeScrAndFun documentation built on May 4, 2019, 6:29 a.m.