2

I am getting this error when applying strategy in quantstrat:

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

My code is as follows:

.blotter <- new.env()
.strategy <- new.env()
Sys.setenv(TZ="UTC")
STRATEGY<-'PFReplicate'

try(rm.strat(STRATEGY))

n<-130

#SMA signals and rules
LONG.ENTRY.SIGNAL.SMA<-"CLOSE_GT_SMA_SIG_LONG"
LONG.EXIT.SIGNAL.SMA<-"CLOSE_LT_SMA_SIG_LONG"
SHORT.ENTRY.SIGNAL.SMA<-"CLOSE_LT_SMA_SIG_SHORT"
SHORT.EXIT.SIGNAL.SMA<-"CLOSE_GT_SMA_SIG_SHORT"
LONG.ENTRY.RULE.SMA<-'L_ENTRY_SMA_RULE'
LONG.EXIT.RULE.SMA<-'L_EXIT_SMA_RULE'
SHORT.ENTRY.RULE.SMA<-'S_ENTRY_SMA_RULE'
SHORT.EXIT.RULE.SMA<-'S_EXIT_SMA_RULE'


LONG.ORDERSET.NAME<-'CLOSELONGSMA'
SHORT.ORDERSET.NAME<-'CLOSESHORTSMA'

strategy(STRATEGY,store=TRUE)


Set up SMA indicator
add.indicator(strategy = STRATEGY,name='SMA',
              arguments=list(x=quote(mktdata),n),
              label='SMA')

#Set up signals 
#SMA signals
add.signal(strategy = STRATEGY,name="sigCrossover",
           arguments=list(columns=c('Close','SMA'),
                          relationship="gt"),
           label=LONG.ENTRY.SIGNAL.SMA)
add.signal(strategy = STRATEGY,name="sigCrossover",
           arguments=list(columns=c('Close','SMA'),
                          relationship="lt"),
           label=LONG.EXIT.SIGNAL.SMA)
add.signal(strategy = STRATEGY,name="sigCrossover",
           arguments=list(columns=c('Close','SMA'),
                          relationship="lt"),
           label=SHORT.ENTRY.SIGNAL.SMA)
add.signal(strategy = STRATEGY,name="sigCrossover",
           arguments=list(columns=c('Close','SMA'),
                          relationship="gt"),
           label=SHORT.EXIT.SIGNAL.SMA)


#Add our SMA rules (enabled)
add.rule(strategy = STRATEGY,name="ruleSignal",
         arguments=list(sigcol=LONG.ENTRY.SIGNAL.SMA,sigval=TRUE,
                        orderqty=100,ordertype="market",
                        TxnFees=0,orderside="long",
                        orderset=LONG.ORDERSET.NAME),
         type="enter",label=LONG.ENTRY.RULE.SMA)

add.rule(strategy = STRATEGY,name="ruleSignal",
         arguments=list(sigcol=LONG.EXIT.SIGNAL.SMA,sigval=TRUE,
                        orderqty='all',ordertype="market",
                        TxnFees=0,orderside="long",
                        orderset=LONG.ORDERSET.NAME),
         type="exit",label=LONG.EXIT.RULE.SMA)

add.rule(strategy = STRATEGY,name="ruleSignal",
         arguments=list(sigcol=SHORT.ENTRY.SIGNAL.SMA,sigval=TRUE,
                        orderqty=100,ordertype="market",
                        TxnFees=0,orderside="short",
                        orderset=SHORT.ORDERSET.NAME),
         type="enter",label=SHORT.ENTRY.RULE.SMA)

add.rule(strategy = STRATEGY,name="ruleSignal",
         arguments=list(sigcol=SHORT.EXIT.SIGNAL.SMA,sigval=TRUE,
                        orderqty='all',ordertype="market",
                        TxnFees=0,orderside="short",
                        orderset=SHORT.ORDERSET.NAME),
         type="exit",label=SHORT.EXIT.RULE.SMA)



symbol <- mar.rep
port <- 'mar.rep'

currency("USD")
stock(primary_id = symbol,currency = "USD",multiplier = 1)
Sys.setenv(TZ="UTC")

initDate <- '1971-01-05'
startDate <- '1972-01-06'
endDate<- '2010-12-31'
initEq <- 1e6



initPortf(name = port,symbols = symbol,initDate=initDate)
initAcct(name = port,portfolios = port,initDate=initDate,initEq=initEq)
initOrders(portfolio = port,initDate=initDate)
applyStrategy(strategy =STRATEGY,portfolios = port,debug = TRUE)

I've tried to keep the code simple, so as to avoid dumb errors, but I still get this one. The applyStrategy runs and lists thousands of transactions, and after 30 minutes, I get this error. I am guessing the fix is simple, but I am not seeing it. Thanks for your help!

Sean Sinykin
  • 542
  • 4
  • 22
  • Your code doesn't run because `symbol` isn't defined. Even if it did, it's a bit much to ask people to run something that takes 30 minutes. – Joshua Ulrich Jul 19 '15 at 05:24

1 Answers1

4

I figured out the problem after posting my question. For anyone else who runs across this error when running quantstrat, check your data for NAs. Plus make sure all assets have explicitly-defined columns that match exactly the columns referenced in add.signal.

This may sound obvious, but data management was the biggest obstacle to getting results in my case. My data came from various data providers, with varying column formats (csv files, primarily). After spending a few hours cleaning and setting up my data to run through the strategy, it is working (I'm at hour 7 so far of processing).

Quantstrat can be difficult to debug, as some error messages are not easy to interpret. Note that this error message is telling you that one or more logical comparisons in an if statement results in NA. If you see this error, check your data for NA to see if this may be the problem.

nrow(na.omit(data)) == nrow(data)

If this is not true, you have NAs. You can remove them with

data_cleaned <- na.omit(data)

but it will depend on your data format.

Sorry if this is a remedial error for everyone. I just wanted to post a detailed answer to this error, as it seems to come up a fair amount for people. If I had seen an explanation like this yesterday, I would have saved several hours of frustration!

Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
Sean Sinykin
  • 542
  • 4
  • 22