R/fun_SharpeCompare_v3.R

#' fun_SharpeCompare_v3
#'
#' @param listData List of daily data with excess, excessvol adjusted and sigma
#' @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_v3 <- function(listData = listExcess,
                                 withoutBCH = withoutBCH,
                                 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_v2(listData[["days"]], period = "days", lag = lag[1], proAnnuVol = proAnnuVol, annu = 365.25),
    days2 = fun_TSMOM_v2(listData[["days"]], period = "days", lag = lag[2], proAnnuVol = proAnnuVol, annu = 365.25),
    days3 = fun_TSMOM_v2(listData[["days"]], period = "days", lag = lag[3], proAnnuVol = proAnnuVol, annu = 365.25),
    days4 = fun_TSMOM_v2(listData[["days"]], period = "days", lag = lag[4], proAnnuVol = proAnnuVol, annu = 365.25)
  )

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

  Sharpe_gg <- do.call(dplyr::bind_rows, lapply(list(days, months), function(x) {
    lapply(x, function(x) {
      x$Sharpe_tidy
    })}))
  indSharpe <- which(is.na(Sharpe_gg$Sharpe))
  if(length(indSharpe) > 0) {
    Sharpe_gg <- Sharpe_gg[-which(is.na(Sharpe_gg$Sharpe)),]
  }
  Sharpe_gg <- Sharpe_gg[!stringr::str_detect(as.character(Sharpe_gg[["Crypto"]]), "^Cum.*"),]
  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::geom_hline(yintercept=0, color = "black", size=0.8) +
    ggplot2::scale_fill_manual(name="Frequency",
                               labels = c("Days", "Months"),
                               values = c(RColorBrewer::brewer.pal(3,"Set1"))) +
    ggplot2::labs(subtitle="Sharpe ratio for different frequencies and lags",
                  title= "Sharpe Ratios") +
    ggplot2::theme_classic()  +
    ggplot2::theme(axis.text = ggplot2::element_text(size=12),
                   axis.text.x= ggplot2::element_text(angle = 90, vjust = 0.5, hjust=1))

  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,months), function(x) {
    lapply(x, function(x) {
      x$SharpeCum_tidy
    })}))

  indSharpeCum <- which(is.na(Sharpe_gg_Cum$Sharpe))
  if(length(indSharpeCum) > 0) {
    Sharpe_gg_Cum <- Sharpe_gg_Cum[-indSharpeCum,]
  }
  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::geom_hline(yintercept=0, color = "black", size=0.8) +
    ggplot2::scale_fill_manual(name="Frequency",
                               labels = c("Days", "Months"),
                               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_classic()  +
    ggplot2::theme(axis.text = ggplot2::element_text(size=12),
                   axis.text.x= ggplot2::element_text(angle = 90, vjust = 0.5, hjust=1))

  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.