如何使用正确的数据列添加 add_TA

how to add add_TA with the correct datacolumn

我的回测中有 2 个品种。我正在添加指标 donchianchannel。然而,当我绘制它时,add_TA( mktdata$high.DCH, col = 6, lwd = 1.5,on=TRUE) 没有传递相关符号的数据,我最终得到了为两者绘制的相同数据符号。

    library(quantstrat)

# source("R/symbols.R")
# source("R/functions.R")
debug
portfolio.st <- "Port.Luxor"
account.st <- "Acct.Luxor"
strategy.st <- "Strat.Luxor"
init_date <- "2007-12-31"
start_date <- "2008-01-01"
end_date <- "2009-12-31"
adjustment <- TRUE
init_equity <- 1e4 # ,000

Sys.setenv(TZ = "UTC")

currency('USD')
basic_symbols <- function() {
  symbols <- c(
    "IWM", # iShares Russell 2000 Index ETF
    "QQQ" # PowerShares QQQ TRust, Series 1 ETF
  )
}

symbols <- basic_symbols()

getSymbols(Symbols = symbols,
           src = "yahoo",
           index.class = "POSIXct",
           from = start_date,
           to = end_date,
           adjust = adjustment)

stock(symbols,
      currency = "USD",
      multiplier = 1)

rm.strat(portfolio.st)
rm.strat(account.st)

initPortf(name = portfolio.st,
          symbols = symbols,
          initDate = init_date)

initAcct(name = account.st,
         portfolios = portfolio.st,
         initDate = init_date,
         initEq = init_equity)

initOrders(portfolio = portfolio.st,
           symbols = symbols,
           initDate = init_date)

strategy(strategy.st, store = TRUE)

add.indicator(strategy = strategy.st,
              name = "SMA",
              arguments = list(x = quote(Cl(mktdata)),
                               n = 10),
              label = "nFast")

add.indicator(strategy = strategy.st,
              name = "SMA",
              arguments = list(x = quote(Cl(mktdata)),
                               n = 30),
              label = "nSlow")

add.indicator(strategy = strategy.st, 
              # correct name of function:
              name = "DonchianChannel",
              arguments = list(HL = quote(HLC(mktdata)[, 1:2]), 
                               n = 20),
              label = "DCH")


add.signal(strategy = strategy.st,
           name="sigCrossover",
           arguments = list(columns = c("nFast", "nSlow"),
                            relationship = "gte"),
           label = "long")

add.signal(strategy = strategy.st,
           name="sigCrossover",
           arguments = list(columns = c("nFast", "nSlow"),
                            relationship = "lt"),
           label = "short")

add.rule(strategy = strategy.st,
         name = "ruleSignal",
         arguments = list(sigcol = "long",
                          sigval = TRUE,
                          orderqty = 100,
                          ordertype = "stoplimit",
                          orderside = "long",
                          threshold = 0.0005,
                          prefer = "High",
                          TxnFees = -10,
                          replace = FALSE),
         type = "enter",
         label = "EnterLONG")





add.rule(strategy.st,
         name = "ruleSignal",
         arguments = list(sigcol = "long",
                          sigval = TRUE,
                          orderside = "short",
                          ordertype = "market",
                          orderqty = "all",
                          TxnFees = -10,
                          replace = TRUE),
         type = "exit",
         label = "Exit2LONG")





results <- applyStrategy(strategy.st, portfolios = portfolio.st, verbose = TRUE)
updatePortf(portfolio.st)
updateAcct(account.st)
updateEndEq(account.st)

for(symbol in symbols) {
  
  
  chart.Posn(portfolio.st, Symbol = symbol, 
             TA = "add_SMA(n = 10, col = 4); add_SMA(n = 30, col = 2) ;
               add_TA( mktdata$high.DCH, col = 6, lwd = 1.5,on=TRUE)    ")
}

试试这个,在你的例子的最后:

 results <- applyStrategy(strategy.st, portfolios = portfolio.st, verbose = TRUE)
updatePortf(portfolio.st)
updateAcct(account.st)
updateEndEq(account.st)

for(symbol in symbols) {
  
  inds <- applyIndicators(strategy.st, get(symbol))
  # Optionally, if you also want the strategy signals per symbol, do this:
  sigs <- applySignals(strategy.st, inds)

  
  chart.Posn(portfolio.st, Symbol = symbol, 
             TA = "add_SMA(n = 10, col = 4); add_SMA(n = 30, col = 2) ;
               add_TA(inds$high.DCH, col = 6, lwd = 1.5,on=TRUE)    ")
}

解释:mktdata 仅包含 applyStrategy 循环 symbols 中最后一个交易品种 运行 的 OHLC 数据。在 运行 制定策略后,通过 print(tail(mktdata)) 亲自查看。即 quantstrat 不保存回测的计算结果。

如果您想查看您的指标和信号,按上述方式重新生成它们是一种直接的方法。