0

I am having this error while running a strategy back-testing in the R, using Quantstrat package. Whenever, I try to use applySignals function to test the signals, it shows the logical error. I tried to remove the NAs by na.omit(FB) command, but when you calculate Simple Moving Average, you will have the NAS in the beginning. Can somebody suggest me the solution?

Thanks,

require(PerformanceAnalytics)
require(quantstrat)
require(quantmod)
require(blotter)

initDate="2015-01-01"
from="2015-01-02"
to="2015-06-30"

options(width=100)

currency('USD')
Sys.setenv(TZ="UTC")

symbols = c("SPY", "FB", "TWTR")

getSymbols(symbols, from=from, to=to, src="yahoo", adjust=TRUE) 
stock(symbols, currency="USD", multiplier=1)



suppressWarnings(rm("account.MAC","portfolio.MAC",pos=.blotter))
suppressWarnings(rm("order_book.MAC",pos=.strategy))


tradeSize <- 1000
initEq <- tradeSize

strategy.st <- portfolio.st <- account.st <- "MAC"
rm.strat(strategy.st)
initPortf(portfolio.st, symbols=symbols, initDate=initDate, currency='USD')
initAcct(account.st, portfolios=portfolio.st, initDate=initDate, currency='USD', initEq=initEq)
initOrders(portfolio.st, initDate=initDate)
strategy(strategy.st, store=TRUE)

#parameters


nFast = 10
nSlow = 30


#indicators

add.indicator(strategy.st, name="SMA",
              arguments=list(x=quote(Cl(mktdata)[,1]), n=nFast),
              label="nFast")

add.indicator(strategy.st, name="SMA",
              arguments=list(x=quote(Cl(mktdata)[,1]), n=nSlow),
              label="nSlow")

test <- applyIndicators(strategy.st, mktdata=Cl(FB))
head(test, 5)


#signals


add.signal(strategy.st, name="sigCrossover",
           arguments=list(columns=c("nFast", "nSlow"), relationship="gt"),
           label="longEntry")

add.signal(strategy.st, name="sigCrossover",
           arguments=list(columns=c("nFast", "nSlow"), relationship="lt"),
           label="longExit")

test2 <- applySignals(strategy.st, mktdata=Cl(FB))

Error: Error in if (length(j) == 0 || (length(j) == 1 && j == 0)) { : 
  missing value where TRUE/FALSE needed

1 Answers1

0

I was able to solve the issue with my code.

So, if I only use mktdata object instead of mktdata = Cl(FB), Quantstrat is working fine. I am not able to understand fully why it is working that way, but somehow it worked fine.

test2 <- applySignals(strategy.st, mktdata)
  • What is happening here is that you're unrolling the functionality of the applyStrategy function. The `mktdata` object is available after you call applyIndicators. Inside applyStrategy, it would be passed as the 'mktdata' argument to applySignals. You could also have passed mktdata=test into your applySignals call. – Brian G. Peterson Jun 04 '16 at 11:58