R/initiateStrategy.R

# initiateStrategy --------------------------------------------------------



#' Initiate Trading Strategy
#'
#' @param name strategy name
#'
#' @return Empty strategy object
#' @export initiateStrategy
#'
#' @examples initiateStrategy("CCI_pullback")
initiateStrategy <- function (name) {

  return(strategy <- list(name=name))

}



# addRule -----------------------------------------------------------------


#' Add Rule to Trading Strategy
#'
#' @param strategy a trading strategy object
#' @param rule trading rule definition
#'
#' @return returns trading strategy object with added rule
#' @export addRule
#'
#' @examples addRule(CCI_pullback,
#'                          list(category = "condition",
#'                               indicator = TTR::CCI,
#'                               arguments = list(n = "n1", maType = "SMA", c = 0.015),
#'                               type = "on-off",
#'                               on = 100,
#'                               off = -100)
#'                    )
addRule <- function (strategy, rule) {

  rule_category <- as.character(rule$category)
  rule_pos <- grep(paste0("^", rule_category), names(strategy))

  str(strategy)

  if (length(rule_pos) == 0) {
    strategy_update <- rlist::list.append(strategy, list(rule))
    names(strategy_update)[length(strategy_update)] <- paste(rule_category)

  } else {

    strategy_trunc <- strategy[!(names(strategy) %in% rule_category)]
    strategy_category <- strategy[(names(strategy) %in% rule_category)]
    strategy_category <- unlist(strategy_category,
                                recursive = FALSE, use.names = FALSE)

    str(strategy_trunc)
    str(strategy_category)
    str(rule)

    if (length(strategy_category) >= 3) {
      strategy_category <- list(strategy_category, rule)
    } else {
      strategy_category <- rlist::list.append(strategy_category, rule)
    }

    str(strategy_category)

    strategy_update <- rlist::list.append(strategy_trunc,
                                          strategy_category)
    names(strategy_update)[length(strategy_update)] <- paste(rule_category)

    str(strategy_update)
  }

  return(strategy_update)
}



# setStrategyParameter ----------------------------------------------------


#' Set Trading Strategy Parameter
#'
#' @param file name of the R file which contains trategy initiation and definition
#' @param ... further variables to pass to strategy definition
#'
#' @return returns trading strategy object with parameters
#' @export updateStrategyParameter
#'
#' @examples CCIpullback1 <- updateStrategyParameter("./CCIpullback.R", nCCI1 = 6,
#' nCCI2 = 85, SL = 0.03, TS = 0.20)
updateStrategyParameter <- function (file, ...) {

  args <- list(...)
  if (!is.null(length(args))) {
    for (i in seq_len(length(args))) {
      eval(parse(text=paste0(names(args[i]), "<-", args[i])), envir=.GlobalEnv)
    }
  }
  source(file)

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