简体   繁体   English

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

[英]how to add add_TA with the correct datacolumn

I have 2 symbols in my backtest.我的回测中有 2 个符号。 I am adding indicator donchianchannel.我正在添加指标 donchianchannel。 However when i plot it, add_TA( mktdata$high.DCH, col = 6, lwd = 1.5,on=TRUE) does not pass the associated symbol's data and i end up getting the same data plotted for both the symbols.但是,当我 plot 它时, 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 # $10,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)    ")
}

在此处输入图像描述

在此处输入图像描述

Try this instead, at the end of your example:请在示例末尾尝试此操作:

 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)    ")
}

Explanation: mktdata only contains the OHLC data for the last symbol run inside the applyStrategy loop over symbols .说明: mktdata仅包含在applyStrategy循环中运行的最后一个symbols的 OHLC 数据。 See for yourself by print(tail(mktdata)) , after running the strategy.运行策略后,请通过print(tail(mktdata))亲自查看。 ie quantstrat does not save the calcluations from the backtest.即 quantstrat 不保存来自回测的计算。

If you want to see your indicators and signals, re-generating them as above is a straightforward way.如果您想查看指标和信号,如上所述重新生成它们是一种简单的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM