R/buildEquityCurvePlot.R

Defines functions buildEquityCurvePlot

Documented in buildEquityCurvePlot

#' Build equity curve and drawdown plot by ggplot2
#'
#' @param equity_curve Equity curve from calcEuityCurveFromTrans.
#' @param perf Performance metrics from calcPerformance.
#'
#' @return Plot by ggplot2
#' @export
buildEquityCurvePlot <- function(equity_curve, perf) {

  # Equity curve
  ec <- equity_curve[, c("date", "cum_pnl", "drawdown")]

  # Equity curve subtitle
  return    <- perf$ttl_win + perf$ttl_lose
  pf        <- perf$ttl_win / abs(perf$ttl_lose)
  win_ratio <- perf$win_days / (perf$win_days + perf$lose_days)

  subtitle <- paste0(
    "Return = ",      formattable::currency(return, digits = 0),
    ", Drawdown = ",  formattable::currency(perf$drawdown, digits = 0),
    ", Ret/DD = ",    round((return / abs(perf$drawdown)), digits = 2),
    ", PF = ",        round(pf, digits = 2),
    ", Win ratio = ", formattable::percent(win_ratio)
  )

  plot <- ec %>%
    ggplot2::ggplot(ggplot2::aes(x = date, y = cum_pnl)) +
    ggplot2::ggtitle(label = "Gap strategy equity curve",
                     subtitle = subtitle) +
    ggplot2::geom_hline(yintercept = 0) +
    ggplot2::geom_line(ggplot2::aes(y = cum_pnl),
                       color = "darkgreen") +
    ggplot2::geom_area(ggplot2::aes(y = cum_pnl),
                       fill = "darkgreen", alpha = 0.3) +
    ggplot2::geom_line(ggplot2::aes(y = drawdown), color = "red") +
    ggplot2::geom_area(ggplot2::aes(y = drawdown), fill = "red", alpha = 0.3) +
    getGGTheme()

  return(plot)
}
tmk-c/myrlib documentation built on May 29, 2019, 1:44 p.m.