# Quantstrat 错误?警告现有仪器丢失

# Quantstrat Mistakenly? Warns An Existing Instrument Is Missing

我们使用 base R、MATLAB、SAS 和电子表格的组合开发了我们最初的回测器,但正在探索仅转向 quantstrat。尽管我们对在此之前试图了解的结果有一些疑问 post,但我刚刚将 R 更新到 v3.6.0,代码停止工作,警告:"In getInstrument(symbol) : instrument Cityso.xts not found, please create it first." 事实上,"Cityso.xts" 存在并用于初始化投资组合。

我在网上搜索了答案,包括:Warning in quantstrat,但是这个 post 解决了一个稍微不同的问题。我搜索的其他 returns 是 更不用说了。

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 

目前,我们将交易规模和初始净值定义为小而简单的,以帮助理解输出(尽管输出有问题,但代码 运行 没有警告):

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)

[此处显示交易]

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

当然,后面的代码不起作用,坚持认为 Cityso.xts 不存在,当它存在时。
为什么当我迁移到 R 版本 3.6.0 时此代码停止工作?

Quantstrat 找不到对象 Cityso.xts 的原因是它在 stock() 函数中没有正确定义,错误地 C.xts (不存在)传入。通过传递正确的对象,Quantstrat 将能够正确访问它。