我在使用 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