我们使用大量的基础R,MATLAB,SAS和电子表格开发了原始的回测器,但正在探索仅移动到Quantstrat的方法。尽管在发布本文之前,我们对要理解的结果有一些疑问,但我只是将R更新到v3.6.0,并且代码停止工作,并警告:“在getInstrument(symbol)中:未找到仪器Cityso.xts ,请先创建它。”实际上,存在“ Cityso.xts”并用于初始化投资组合。
我已经在线搜索了答案,包括:Warning in quantstrat,但是这篇文章的重点稍有不同。我搜索的其他回报是 甚至更少。
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时,此代码为何停止工作?
答案 0 :(得分:1)
Quantstrat找不到对象Cityso.xts
的原因是因为在stock()
函数中未正确定义该对象,错误地传递了C.xts
(不存在)通过传递正确的对象,Quantstrat将能够正确访问它。