我在使用Quantstrat软件包在R中运行策略回测时遇到此错误。每当我尝试使用applySignals函数来测试信号时,它会显示逻辑错误。我试图通过na.omit(FB)命令删除NA,但是当你计算简单移动平均线时,你将在开始时拥有NAS。有人可以建议我解决方案吗?
谢谢,
require(PerformanceAnalytics)
require(quantstrat)
require(quantmod)
require(blotter)
initDate="2015-01-01"
from="2015-01-02"
to="2015-06-30"
options(width=100)
currency('USD')
Sys.setenv(TZ="UTC")
symbols = c("SPY", "FB", "TWTR")
getSymbols(symbols, from=from, to=to, src="yahoo", adjust=TRUE)
stock(symbols, currency="USD", multiplier=1)
suppressWarnings(rm("account.MAC","portfolio.MAC",pos=.blotter))
suppressWarnings(rm("order_book.MAC",pos=.strategy))
tradeSize <- 1000
initEq <- tradeSize
strategy.st <- portfolio.st <- account.st <- "MAC"
rm.strat(strategy.st)
initPortf(portfolio.st, symbols=symbols, 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)
#parameters
nFast = 10
nSlow = 30
#indicators
add.indicator(strategy.st, name="SMA",
arguments=list(x=quote(Cl(mktdata)[,1]), n=nFast),
label="nFast")
add.indicator(strategy.st, name="SMA",
arguments=list(x=quote(Cl(mktdata)[,1]), n=nSlow),
label="nSlow")
test <- applyIndicators(strategy.st, mktdata=Cl(FB))
head(test, 5)
#signals
add.signal(strategy.st, name="sigCrossover",
arguments=list(columns=c("nFast", "nSlow"), relationship="gt"),
label="longEntry")
add.signal(strategy.st, name="sigCrossover",
arguments=list(columns=c("nFast", "nSlow"), relationship="lt"),
label="longExit")
test2 <- applySignals(strategy.st, mktdata=Cl(FB))
Error: Error in if (length(j) == 0 || (length(j) == 1 && j == 0)) { :
missing value where TRUE/FALSE needed
答案 0 :(得分:0)
我能用我的代码解决问题。
所以,如果我只使用mktdata对象而不是mktdata = Cl(FB),那么Quantstrat工作正常。我无法完全理解为什么它以这种方式工作,但不知何故它工作得很好。
test2 <- applySignals(strategy.st, mktdata)