R/calcPerformance.R

Defines functions calcPerformance

Documented in calcPerformance

#' Calculate performance metrics from equity curve.
#'
#' @param equity_curve Equity curve by calcEquityCurveFromTrans.
#'
#' @return Performance metrics by data.frame format.
#' @export
calcPerformance <- function(equity_curve) {

  # Omit first row
  ec <- equity_curve[-1]

  # Win/Lose
  win_trades  <- ec[pnl > 0]$pnl
  lose_trades <- ec[pnl <= 0]$pnl

  # Total performance
  ttl_win   <- round(sum(win_trades), digits = 2)
  ttl_lose  <- round(sum(lose_trades), digits = 2)
  drawdown  <- round(min(ec$drawdown), digits = 2)
  win_days  <- length(win_trades)
  lose_days <- length(lose_trades)

  # Create list
  perf <- data.frame(
    ttl_win   = ttl_win,
    ttl_lose  = ttl_lose,
    drawdown  = drawdown,
    win_days  = win_days,
    lose_days = lose_days
  )

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