r - # Quantstrat 错误地?警告缺少现有仪器



我们使用基础 R、MATLAB、SAS 和电子表格开发了我们最初的回测器,但正在探索转向 quantstrat。 尽管我们对本文之前试图理解的结果有一些疑问,但我刚刚将R更新到v3.6.0并且代码停止工作,并警告说:"在getInstrument(symbol):找不到仪器Cityso.xts,请先创建它。事实上,"Cityso.xts"存在并用于初始化投资组合。

我在网上搜索了答案,包括:定量警告,但这篇文章解决的一点略有不同。我搜索的其他返回是 更不切中要害。

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将能够正确访问它。

最新更新