R/apply_strategy.R

#' Function to Calculate Strategy Results
#'
#' @param data xts object with open, high, low, close market data
#' @param strategy strategy object
#' @param ... not in use
#' @return xts object containing all strategy signals, weights and returns
#' @export
#' @import xts
#' @import quantmod
#' @examples apply_strategy(data, strategy)
#'

apply_strategy <- function (data, strategy, ...) {

  if (!isTRUE(all(has.OHLC(data)))) {
    stop("Data has no OHLC columns.")
  }

  data_tmp <- assertive.base::coerce_to(data, target_class = "xts")
  data_tmp <- extendXts(data_tmp)
  data_tmp$daily_returns_opop <- lag(
    dailyReturn(Op(data),
                type = "arithmetic",
                leading = FALSE),
    -1)
  data_tmp$daily_returns_clcl <- dailyReturn(Cl(data),
                                             type = "arithmetic",
                                             leading = FALSE)


  #
  # # condition ---------------------------------------------------------------
  #
  data_cond <- trato:::resolve_condition(data, data_tmp, strategy)
  #
  # # buy signal --------------------------------------------------------------
  #
  data_buy <- trato:::resolve_buy_signal(data, data_cond, strategy)
  #
  # # stop loss ---------------------------------------------------------------
  #
  data_complete <- trato:::resolve_stoploss(data, data_buy, strategy)
  #
  # ---------------------------------------------------------------------------
  #
  data_complete

}
rengelke/quantTraiding_trato documentation built on Oct. 13, 2020, 12:01 p.m.