I am new to R and have recently encountered the following error after running applyStrategy
function from quantstrat
package:
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
Could anyone please explain to me how I can debug this error and advise if I had used sigFormula
correctly to combine both MACD
and RSI
indicators?
Thank you for your help and please refer to the code below:
# 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's 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's 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)