Quantstrat 动态订单大小百分比

Quantstrat dynamic order sizing in percents

我不想以绝对值(手数或美元)而是以百分比来设置订单大小。例如。我将订单大小设置为 <- 0.3,然后将必要的手数计算为当前资产的 30%。我应该使用 osMaxPos / osMaxDollar 还是以某种方式编写自定义大小调整函数?

add.rule(
  strategy.st, name = 'ruleSignal',
  label = 'EnterLONG', type = 'enter',
  arguments = list(
    sigcol = signal$long$enter$label, sigval = TRUE,
    replace = TRUE, orderset = 'ocolong', orderqty = 1,
    ordertype = 'market', orderside = 'long'
  )
)
add.rule(
  strategy.st, name = 'ruleSignal',
  label = 'ExitLONG', type = 'exit',
  arguments = list(
    sigcol = signal$long$exit$label, sigval = TRUE,
    replace = TRUE, orderset = 'ocolong', orderqty = 'all',
    ordertype = 'market', orderside = 'long'
  )
)

对于像我一样兴奋的人,这里有一个解决方案:

我在创建自定义订单大小函数方面找到了一个完美的开端 here, Tim Trice referenced to comments section in Ilya Kipnis' blog. Also i found out that a portfolio needs to be updated to get actual equity in a Joshua Ulrich's answer

leverage <- 10 # 1:10
tradeSize <- 0.3 # 30%
osFixedPercent <- function(timestamp, orderqty, portfolio, symbol, ruletype, ...) {
  if(!exists("tradeSize")) stop("You must set trade size")

  updatePortf(portfolio)
  portfolio <- getPortfolio(portfolio)
  equity <- initEq + sum(portfolio$summary$Period.Realized.PL)

  ClosePrice <- as.numeric(mktdata[timestamp,]$close)
  maxPos <- equity * tradeSize
  initialMargin <- ClosePrice / leverage
  orderqty <- sign(orderqty) * floor(maxPos / initialMargin)

  return(orderqty)
}