在 quantstrat 中应用策略时出现此错误:
if (length(j) == 0 || (length(j) == 1 && j == 0)) { 中的错误:需要 TRUE/FALSE 的缺失值
我的代码如下:
.blotter <- new.env()
.strategy <- new.env()
Sys.setenv(TZ="UTC")
STRATEGY<-'PFReplicate'
try(rm.strat(STRATEGY))
n<-130
#SMA signals and rules
LONG.ENTRY.SIGNAL.SMA<-"CLOSE_GT_SMA_SIG_LONG"
LONG.EXIT.SIGNAL.SMA<-"CLOSE_LT_SMA_SIG_LONG"
SHORT.ENTRY.SIGNAL.SMA<-"CLOSE_LT_SMA_SIG_SHORT"
SHORT.EXIT.SIGNAL.SMA<-"CLOSE_GT_SMA_SIG_SHORT"
LONG.ENTRY.RULE.SMA<-'L_ENTRY_SMA_RULE'
LONG.EXIT.RULE.SMA<-'L_EXIT_SMA_RULE'
SHORT.ENTRY.RULE.SMA<-'S_ENTRY_SMA_RULE'
SHORT.EXIT.RULE.SMA<-'S_EXIT_SMA_RULE'
LONG.ORDERSET.NAME<-'CLOSELONGSMA'
SHORT.ORDERSET.NAME<-'CLOSESHORTSMA'
strategy(STRATEGY,store=TRUE)
Set up SMA indicator
add.indicator(strategy = STRATEGY,name='SMA',
arguments=list(x=quote(mktdata),n),
label='SMA')
#Set up signals
#SMA signals
add.signal(strategy = STRATEGY,name="sigCrossover",
arguments=list(columns=c('Close','SMA'),
relationship="gt"),
label=LONG.ENTRY.SIGNAL.SMA)
add.signal(strategy = STRATEGY,name="sigCrossover",
arguments=list(columns=c('Close','SMA'),
relationship="lt"),
label=LONG.EXIT.SIGNAL.SMA)
add.signal(strategy = STRATEGY,name="sigCrossover",
arguments=list(columns=c('Close','SMA'),
relationship="lt"),
label=SHORT.ENTRY.SIGNAL.SMA)
add.signal(strategy = STRATEGY,name="sigCrossover",
arguments=list(columns=c('Close','SMA'),
relationship="gt"),
label=SHORT.EXIT.SIGNAL.SMA)
#Add our SMA rules (enabled)
add.rule(strategy = STRATEGY,name="ruleSignal",
arguments=list(sigcol=LONG.ENTRY.SIGNAL.SMA,sigval=TRUE,
orderqty=100,ordertype="market",
TxnFees=0,orderside="long",
orderset=LONG.ORDERSET.NAME),
type="enter",label=LONG.ENTRY.RULE.SMA)
add.rule(strategy = STRATEGY,name="ruleSignal",
arguments=list(sigcol=LONG.EXIT.SIGNAL.SMA,sigval=TRUE,
orderqty='all',ordertype="market",
TxnFees=0,orderside="long",
orderset=LONG.ORDERSET.NAME),
type="exit",label=LONG.EXIT.RULE.SMA)
add.rule(strategy = STRATEGY,name="ruleSignal",
arguments=list(sigcol=SHORT.ENTRY.SIGNAL.SMA,sigval=TRUE,
orderqty=100,ordertype="market",
TxnFees=0,orderside="short",
orderset=SHORT.ORDERSET.NAME),
type="enter",label=SHORT.ENTRY.RULE.SMA)
add.rule(strategy = STRATEGY,name="ruleSignal",
arguments=list(sigcol=SHORT.EXIT.SIGNAL.SMA,sigval=TRUE,
orderqty='all',ordertype="market",
TxnFees=0,orderside="short",
orderset=SHORT.ORDERSET.NAME),
type="exit",label=SHORT.EXIT.RULE.SMA)
symbol <- mar.rep
port <- 'mar.rep'
currency("USD")
stock(primary_id = symbol,currency = "USD",multiplier = 1)
Sys.setenv(TZ="UTC")
initDate <- '1971-01-05'
startDate <- '1972-01-06'
endDate<- '2010-12-31'
initEq <- 1e6
initPortf(name = port,symbols = symbol,initDate=initDate)
initAcct(name = port,portfolios = port,initDate=initDate,initEq=initEq)
initOrders(portfolio = port,initDate=initDate)
applyStrategy(strategy =STRATEGY,portfolios = port,debug = TRUE)
我试图保持代码简单,以避免愚蠢的错误,但我仍然得到这个。applyStrategy 运行并列出数千个事务,30 分钟后,我收到此错误。我猜修复很简单,但我没有看到它。谢谢你的帮助!