0

I'm running an R quantstrat based script, taken from Backtesting Strategies with R). It works. Until I add ADX as an indicator and signal. If so, then I get the following error:

Error in `colnames<-`(`*tmp*`, value = "ADXsig") : 
length of 'dimnames' [2] not equal to array extent

The supposed solution, discussed here in quantstrattrader and here in r.789695.n4.nabble.com, would be to change the ADX add.indicator code from x=quote(Cl(mktdata)) to quote(Cl(mktdata)[,1]). It doesn't work, maybe because ADX uses HLC rather than Cl, and HLC references three columns rather than only one. To be clear: changing HLC=quote(HLC(mktdata)) to quote(HLC(mktdata)[,1]) did not work.

Full working code below, and ADX code separately further below:

# INSTALL PACKAGES
# install.packages("devtools")
# require(devtools)
# install_github("braverock/FinancialInstrument")
# install_github("joshuaulrich/xts") 
# install_github("braverock/blotter")
# install.packages("quantstrat", repos="http://R-Forge.R-project.org")
# install_github("braverock/PerformanceAnalytics")

# LIBRARIES
library(quantstrat)   

# INITIAL SETUP
Sys.setenv(TZ = "EST")
currency('USD') 
start_date <- "2015-01-01"
end_date <- "2016-12-31"
init_equity <- 1e4 # $10,000
adjustment <- FALSE

# GET DATA
basic_symbols <- function() {symbols <- c("SPY")}
symbols <- basic_symbols()
getSymbols(Symbols = symbols, src = "google", index.class = "POSIXct", 
           from = start_date, to = end_date, adjust = adjustment)
stock(symbols,currency = "USD", multiplier = 1)

# DEFINE STRATEGY/PORTFOLIO/ACCOUNT NAMES
portfolio.st <- "Port.Luxor"
account.st <- "Acct.Luxor"
strategy.st <- "Strat.Luxor"

# REMOVE PRIOR STRATEGY/PORTFOLIO, INITIALIZE PORTFOLIO/ACCOUNT/STRATEGY, STORE STRATEGY
rm.strat(portfolio.st)
rm.strat(account.st)
initPortf(name = portfolio.st, symbols = symbols)
initAcct(account.st, portfolios = portfolio.st, initEq = init_equity)
initOrders(portfolio.st)
strategy(strategy.st, store = TRUE)

# INDICATORS & SIGNALS

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

# TRADING RULES
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")

# APPLY STRATEGY 
applyStrategy(strategy.st, portfolios = portfolio.st)

The error is produced when the following code (for ADX indicator & signal) is added. Even if not referenced by the strategy - its presence in the mktdata xts object is what produces the error. The presence of add.indicator by itself does not cause the error, its rather the presence of add.signal that does. Thinking the error may be caused by add.signal referencing column = "ADX", but not knowing which ADX to reference because add.indicator created three ADX columns.

add.indicator(strategy.st, name="ADX", 
              arguments=list(HLC=quote(HLC(mktdata)), n=14), 
              label="ADX")

add.signal(strategy.st, name = "sigThreshold",
           arguments = list(column = "ADX",
                            threshold = 30,
                            relationship = "gt",      
                            cross = TRUE),            
           label = "ADXsig")
Krug
  • 1,003
  • 13
  • 33

1 Answers1

0

Found the solution: changed column = "ADX" to column = "ADX.ADX". As follows:

add.indicator(strategy.st, name="ADX", 
          arguments=list(HLC=quote(HLC(mktdata)), n=14), 
          label="ADX")

add.signal(strategy.st, name = "sigThreshold",
       arguments = list(column = "ADX.ADX",
                        threshold = 30,
                        relationship = "gt",      
                        cross = TRUE),            
       label = "ADXsig")
Krug
  • 1,003
  • 13
  • 33