ralgorithmic-tradingquantitative-financequantstrat

how to add add_TA with the correct datacolumn


I have 2 symbols in my backtest. I am adding indicator 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.

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

enter image description here

enter image description here


Solution

  • 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. See for yourself by print(tail(mktdata)), after running the strategy. i.e. quantstrat does not save the calcluations from the backtest.

    If you want to see your indicators and signals, re-generating them as above is a straightforward way.