Quantstrat不适用于ADX

时间:2017-06-25 01:45:17

标签: r quantstrat

我正在运行基于R quantstrat的脚本,取自Backtesting Strategies with R)。有用。直到我添加 ADX 作为指标和信号。如果是这样,那么我得到以下错误:

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

我们讨论的here in quantstrattraderhere in r.789695.n4.nabble.com假设解决方案是将 ADX add.indicator代码从x=quote(Cl(mktdata))更改为quote(Cl(mktdata)[,1]) }。它不起作用,可能是因为ADX使用HLC而不是Cl,而HLC引用了三列而不是一列。需要明确的是:将HLC=quote(HLC(mktdata))更改为quote(HLC(mktdata)[,1])无效。

下面的完整工作代码,以及下面的ADX代码:

# 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)

添加以下代码( ADX 指示符和信号)时会产生错误。即使没有被策略引用 - 它在mktdata xts对象中的存在也会产生错误。 add.indicator本身的存在不会导致错误,而是add.signal的存在。认为错误可能是add.signal引用column = "ADX"引起的,但不知道要引用哪个ADX,因为add.indicator创建了三个ADX列。

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

1 个答案:

答案 0 :(得分:1)

找到解决方案:将column = "ADX"更改为column = "ADX.ADX"。如下:

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