rquantstrat

# Quantstrat Mistakenly? Warns An Existing Instrument Is Missing


We developed our original backtester using a kluge of base R, MATLAB, SAS and spreadsheets but are exploring moving just to quantstrat. Although we had some questions as to the results which we were trying to understand prior to this post, I just updated R to v3.6.0 and the code stopped working, warning that: "In getInstrument(symbol) : instrument Cityso.xts not found, please create it first." In fact, "Cityso.xts" is present and used to initialize the portfolio.

I've searched for answers online including: Warning in quantstrat, but this post addresses a slightly different point. Other returns for my search are even less on point.

getSymbols("C", from = "2017-04-17", to = "2017-05-11", src = "yahoo", adjust = TRUE)
C <- round (C, 2) # rounding
C <- C[ , -5:-6] # eliminate columns not used
l.ent <- c(0,1,0,0,0,0,0,1,1,1,0,0,1,0,0,0,0,0) # five long entry positions
l.exit <- c(0,0,0,0,1,0,0,0,0,0,1,1,1,0,0,1,0,0) # corresponding five long exit positions (3 day lag)
Cityso.xts <- merge(C,l.ent,l.exit) # merge indicators in last columns to the right
make.index.unique(Cityso.xts)  # source of code:  https://github.com/braverock/blotter/issues/51

initdate <- "1999-01-01"
from <- "2017-04-17"
to <- "2017-05-11"
Sys.setenv(TZ="UTC")
currency("USD")
stock("C.xts", currency = "USD") # Use stock() to initialize Cityso.xts and 

For now, we define trade size and initial equity small and simple to aid in understanding output (which, although there were questions as to output, the code ran without warnings):

tradesize <- 1
initeq <- 1
strategy.st <- "firststrat"
portfolio.st <- "firststrat"
account.st <- "firststrat"
rm.strat(strategy.st) # Remove the existing strategy if it exists
initPortf(portfolio.st, symbols = "Cityso.xts", 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)
add.signal(strategy.st, name = "sigThreshold", 
       arguments = list(column = "l.ent", # l.ent column contains indicators added external to quantstrat
          threshold = 0,
          relationship = "gt",
          cross = FALSE),
       label = "l.open.th")

add.signal(strategy.st, name = "sigThreshold", 
       arguments = list(column = "l.exit", # l.ent column contains indicators added external to quantstrat
          threshold = 0,
          relationship = "gt", 
          cross = FALSE), 
       label = "l.end.th")
test_init <- applyIndicators(strategy.st, mktdata = Cityso.xts)
test <- applySignals(strategy = strategy.st, mktdata = test_init)
add.rule(strategy.st, name = "ruleSignal", 
      arguments = list(sigcol = "l.open.th", 
                       sigval = TRUE, 
                       orderqty = 1,
                       ordertype = "market", 
                       orderside = "long", 
                       replace = FALSE, 
                       prefer = "Open"), 
      type = "enter")
add.rule(strategy.st, name = "ruleSignal", 
      arguments = list(sigcol = "l.end.th",
                       sigval = TRUE, 
                       orderqty = "all",
                       ordertype = "market", 
                       orderside = "long",
                       replace = FALSE, 
                       prefer = "Open"), 
      type = "exit")
add.rule(strategy = strategy.st, name = "ruleSignal",
      arguments = list(sigcol = "l.open.th", sigval = TRUE, ordertype = "market",
                       orderside = "long", replace = FALSE, prefer = "Open",
                       osFUN = osMaxDollar,
                       tradeSize = tradesize,
                       maxSize = tradesize),
      type = "enter")
out <- applyStrategy(strategy = strategy.st, portfolios = portfolio.st, debug = TRUE)

[The trades show here]

There were 17 warnings (use warnings() to see them)
warnings()
1: In getInstrument(symbol) : instrument Cityso.xts not found, please create it first.
2: In getInstrument(Symbol) :  instrument Cityso.xts not found, please create it first.
3: In addTxn(Portfolio = portfolio, Symbol = symbol, TxnDate = txntime,  ... :  Instrument Cityso.xts  not found, using contract multiplier of 1

Of course, later code does not work, insisting that Cityso.xts is not present, when it is.
Why did this code stop working when I moved to R version 3.6.0?


Solution

  • The reason that Quantstrat can't find the object Cityso.xts is because it was not defined properly in the stock() function, where mistakenly C.xts (which doesn't exist) is passed in. By passing the correct object, Quantstrat will be able to access it properly.