Error while applyingStrategy

I'm new to R and recently ran into the following error after running a applyStrategy

function from a package quantstrat

:

Error in eval(expr, envir, enclos) : object 'signal' not found
Error in `colnames<-`(`*tmp*`, value = integer(0)) : 
  attempt to set 'colnames' on an object with less than two dimensions

      

Can anyone explain to me how I can debug this error and report if I used the right way sigFormula

to combine the indicators MACD

and RSI

?

Thank you for your help and refer to the below code:

# 1. Load R Packages
library(quantstrat)

# 2. Stock Instrument Initialisation

# 2.1 Stock Instrument Initialisation
inital.portfolio.date <- '2013-12-31'
start.date <- '2014-01-01'
end.date <- '2017-05-31'
initial.equity <- 10000
Sys.setenv(TZ="UTC")

# 2.2 Download data
getSymbols(Symbols = "SPY", src = "google", from = start.date, to = end.date, adjust = T, index.class="POSIXct")

# 2.3 Initialise currency
currency(primary_id = "USD")

# 2.4 Initialise Stock Instrument
stock(primary_id = "SPY", currency  = "USD", multiplier = 1)

# 3. Strategy Visualisation and Details
# MACD & RSI Strategy
lineChart(SPY, theme = "white")
addMACD(fast = 12, slow = 26, signal =9, type = "EMA")
addRSI(n = 14, maType = "EMA")

# 4. Strategy Initialisation

# 4.1 Strategy Name
WT.Strat1 <- "Wen Ting MACD & RSI Strategy"

# 4.2 Clear Strategy Data
rm.strat(WT.Strat1)

# 4.3 Strategy Object
strategy(name = WT.Strat1, store = TRUE)

# 4.4 Summary of Completed Strategy Object
summary(getStrategy(WT.Strat1))

# 5. Strategy Definition

# 5.1 Add Indicators
add.indicator(strategy = WT.Strat1, name = "MACD", arguments = list(x = quote(Cl(SPY)), nFast = 12, nSlow = 26, nSig = 9), label = "MACD")
add.indicator(strategy = WT.Strat1, name = "RSI", arguments = list(price = quote(Cl(SPY)), n = 14), label = "RSI")

# 5.2 Add Strategy Signals
add.signal(strategy = WT.Strat1, name = "sigFormula", arguments = list(columns = c("macd","signal","RSI"), formula ="(signal > macd) & (RSI < 30)", cross = FALSE, label = "trigger"), label = "BuySignal")

add.signal(strategy = WT.Strat1, name = "sigFormula", arguments = list(columns = c("macd","signal","RSI"), formula ="(signal < macd) & (RSI > 70)", cross = FALSE, label = "trigger"), label = "SellSignal")

# 5.3 Add Buy Rule
add.rule(strategy = WT.Strat1, name = "ruleSignal", arguments = list(sigcol = "BuySignal", sigval = TRUE, orderqty = 10, ordertype = "market", orderside = "long"), type = "enter", label = "BuyRule", enabled = TRUE)

# 5.4 Add Sell Rule
add.rule(strategy = WT.Strat1, name = "ruleSignal", arguments = list(sicol = "SellSignal", sigval = TRUE, orderqty = all, ordertype = "market", orderside = "long", TxnFees = -6), type = "exit", label = "SellRule", enabled = TRUE)

# 5.5 Completed Strategy Object
summary(getStrategy(WT.Strat1))

# 6. Portfolio Initialisation

# 6.1 Portfolio Name
WT.Portfolio1 <- "Wen Ting Portfolio 1"

# 6.2 Clear Portfolio Data
rm.strat(WT.Portfolio1)

# 6.3 Initialise Portfolio Object
initPortf(name = WT.Portfolio1, symbols = "SPY", initDate = inital.portfolio.date)

# 6.4 Initialise Account Object
initAcct(name = WT.Strat1, portfolios = WT.Portfolio1, initDate = inital.portfolio.date, initEq = initial.equity)

# 6.5 Initialise Orders Object
initOrders(portfolio = WT.Portfolio1, initDate = inital.portfolio.date)

# 7. Strategy Application

# 7.1 Strategy Application to Market Data
applyStrategy(strategy = WT.Strat1, portfolios = WT.Portfolio1)

      

+3


source to share


1 answer


Try to fix your code:

add.signal(strategy = WT.Strat1, name = "sigFormula", arguments = list(columns = c("macd.MACD","signal.MACD","EMA.RSI"), formula ="(signal.MACD > macd.MACD) & (EMA.RSI < 30)", cross = FALSE, label = "trigger"), label = "BuySignal")

add.signal(strategy = WT.Strat1, name = "sigFormula", arguments = list(columns = c("macd.MACD","signal.MACD","EMA.RSI"), formula ="(signal.MACD < macd.MACD) & (EMA.RSI > 70)", cross = FALSE, label = "trigger"), label = "SellSignal")

      

How do I debug this error you are asking? Here's a quick way:

First try to call traceback()

> traceback()
8: stop("attempt to set 'colnames' on an object with less than two dimensions")
7: `colnames<-`(`*tmp*`, value = seq(ncol(tmp_val)))
6: applySignals(strategy = strategy, mktdata = mktdata, parameters = parameters, 
       ...)
5: applyStrategy(strategy = WT.Strat1, portfolios = WT.Portfolio1) at .active-rstudio-document#86
4: eval(ei, envir)
3: eval(ei, envir)
2: withVisible(eval(ei, envir))
1: source("~/.active-rstudio-document", echo = TRUE)

      

You can see applySignals

what is wrong in your call applyStrategy

(which runs the backtest), so you know it has something to do with your signal-related data. (If it was applyIndicators

, it would have to do with your indicator logic, if it did applyRules

, you know it probably has to do with your rule logic)



applyStrategy

adds an object to the global environment mktdata

that you can inspect even if a full simulation is not done.

try head(mktdata)

:

> head(mktdata)
           SPY.Open SPY.High SPY.Low SPY.Close SPY.Volume macd.MACD signal.MACD EMA.RSI
2014-01-02   183.98   184.07  182.48    182.92  119636836        NA          NA      NA
2014-01-03   183.21   183.60  182.63    182.88   81390502        NA          NA      NA
2014-01-06   183.47   183.56  182.08    182.36  108028139        NA          NA      NA
2014-01-07   183.09   183.79  182.95    183.48   86144169        NA          NA      NA
2014-01-08   183.45   183.83  182.89    183.52   96582234        NA          NA      NA
2014-01-09   184.10   184.13  182.80    183.64   90683302        NA          NA      NA

      

And you can see the names of your columns does not really signal

, macd

but rather signal.MACD

and macd.MACD

. You have to specify the column names correctly in your logic sigFormula

.

And this is one of the quickest ways to debug Quantstrat. Insert browser()

in the code - is the next level of detail that you could use if you can not understand the problem.

If you try to run the backtest with the correct column names everything works.

+2


source







All Articles