应用量子策略时,错误消息'缺少值需要TRUE / FALSE'

时间:2011-08-05 17:08:25

标签: r quantstrat

我正在尝试运行此代码

OBVMA <- function(price,volume,n) {
price <- try.xts(price, error = as.matrix)
volume <- try.xts(volume, error = as.matrix)
if (!(is.xts(price) && is.xts(volume))) {
price <- as.vector(price)
volume <- as.vector(volume)
}
obvma <- c(volume[1], ifelse(ROC(price) > 0, volume, -volume)[-1])
obvma <- cumsum(obvma)
obvma <- runMean(obvma, n)
if (is.xts(obvma)) {
obvma <- xts(obvma, index(price))
colnames(obvma) <- "obvma"
}
reclass(obvma, price)
}

require(quantstrat)
suppressWarnings(rm("order_book.obvcross",pos=.strategy))
suppressWarnings(rm("account.obvcross","portfolio.obvcross",pos=.blotter))
suppressWarnings(rm("account.st","portfolio.st","stock.str","stratOBVCROSS","initDate","initEq",'start_t','end_t'))
stock.str='ALPHA.AT' 
currency('EUR')
stock(stock.str,currency='EUR',multiplier=1)
initDate='2001-12-31'
initEq=1000000
portfolio.st='obvcross'
account.st='obvcross'
initPortf(portfolio.st,symbols=stock.str, initDate=initDate)
initAcct(account.st,portfolios=portfolio.st, initDate=initDate)
initOrders(portfolio=portfolio.st,initDate=initDate)

stratOBVCROSS<- strategy(portfolio.st)

stratOBVCROSS <- add.indicator(strategy = stratOBVCROSS, name = "OBV", arguments = list(price = quote(Cl(mktdata)),volume = quote(Vo(mktdata))),label= "obv")
stratOBVCROSS <- add.indicator(strategy = stratOBVCROSS, name = "OBVMA", arguments = list(price = quote(Cl(mktdata)),volume = quote(Vo(mktdata)), n=20),label="obvma20")

stratOBVCROSS <- add.signal(strategy = stratOBVCROSS,name="sigCrossover",arguments = list(column=c("obv","obvma20"),relationship="gte"),label="obv.gte.obvma20")
stratOBVCROSS <- add.signal(strategy = stratOBVCROSS,name="sigCrossover",arguments = list(column=c("obv","obvma20"),relationship="lt"),label="obv.lt.obvma20")

stratOBVCROSS <- add.rule(strategy = stratOBVCROSS,name='ruleSignal', arguments = list(sigcol="obv.gte.obvma20",sigval=TRUE, orderqty=100, ordertype='market', orderside='long'),type='enter')
stratOBVCROSS <- add.rule(strategy = stratOBVCROSS,name='ruleSignal', arguments = list(sigcol="obv.lt.obvma20",sigval=TRUE, orderqty=-100, ordertype='market', orderside='long'),type='exit')

stratOBVCROSS <- add.rule(strategy = stratOBVCROSS,name='ruleSignal', arguments = list(sigcol="obv.lt.obvma20",sigval=TRUE, orderqty=-100, ordertype='market', orderside='short'),type='enter')
stratOBVCROSS <- add.rule(strategy = stratOBVCROSS,name='ruleSignal', arguments = list(sigcol="obv.gte.obvma20",sigval=TRUE, orderqty=100, ordertype='market', orderside='short'),type='exit')

getSymbols(stock.str,from=initDate)
for(i in stock.str)
  assign(i, adjustOHLC(get(i),use.Adjusted=TRUE))

start_t<-Sys.time()
out<-try(applyStrategy(strategy=stratOBVCROSS,portfolios=portfolio.st))

但是,当我应用代码的最后一行时,我收到此错误消息

Error in if (length(j) == 0 || (length(j) == 1 && j == 0)) { : 
  missing value where TRUE/FALSE needed

有人可以帮助我找到此错误消息的解决方案

提前致谢

2 个答案:

答案 0 :(得分:3)

您的问题是您正在寻找列'obvma20',但您的函数会创建一个名为'obvma'的列。

简单的答案是将add.signal定义更改为使用'obvma'。

quantstrat不会覆盖它们存在的列名,假设函数作者(您)希望列标签是某种方式。我们可能会在将来更改它以查找重复的列名并在这种情况下应用标签,但在这种情况下,您仍然会查找错误的列名。

答案 1 :(得分:0)

我得到了类似的错误。现在,例如,当我运行bbands.demo时,似乎applyIndi​​cator实际上按照下面的代码更改了列名称(看起来这个行为是新的,因为我能够在另一台没有这个的机器上找到一个旧包问题)。

在运行applyIndi​​cator的代码后立即执行:

** * ** 请注意原始列名:

浏览[2]&GT;头(tmp_val)            dn mavg up pctB 2007-01-03 NA NA NA NA 2007-01-04 NA NA NA NA 2007-01-05 NA NA NA NA 2007-01-08 NA NA NA NA 2007-01-09 NA NA NA NA 2007-01-10 NA NA NA NA

看起来这是“新代码”粘贴现有代码。 (参见下面的旧代码)

浏览[2]&GT;

debug: if (ncol(tmp_val) == 1) {
    colnames(tmp_val) <- indicator$label
} else {
    colnames(tmp_val) <- paste(indicator$label, seq(1, ncol(tmp_val)), 
        sep = ".")
}

导致:

浏览[2]&GT;头(tmp_val)

**** 通知列名称如何更改

       BBands.1 BBands.2 BBands.3 BBands.4

2007-01-03 NA NA NA NA 2007-01-04 NA NA NA NA 2007-01-05 NA NA NA NA 2007-01-08 NA NA NA NA 2007-01-09 NA NA NA NA 2007-01-10 NA NA NA NA

* 在调用时会导致以下错误:

if(length(j)== 0 ||(length(j)== 1&amp;&amp; j == 0)){:   缺少需要TRUE / FALSE的值 另外:警告信息: 在download.file中(粘贴(yahoo.URL,“s =”,Symbols.name,“&amp; a =”,from.m,:   下载长度83602!=报告长度200

通知vs旧代码:

浏览[2]&GT;

 debug: if (is.null(colnames(tmp_val))) {
        if (ncol(tmp_val) == 1) {
            colnames(tmp_val) <- indicator$label
        }
        else {
            colnames(tmp_val) <- paste(indicator$label, seq(1, ncol(tmp_val)),
                sep = ".")
        }
    } else {
        if (ncol(tmp_val) > 1)
            colnames(tmp_val) <- paste(indicator$label, colnames(tmp_val),
                sep = ".")
    }

从bbands的演示中,up替换为BBands.ind.1(但是如果我用BBands.ind.1替换'up',我似乎无法工作。)

stratBBands <- add.signal(stratBBands,name="sigCrossover",arguments = list(columns=c("Close","up"),relationship="gt"),label="Cl.gt.UpperBand")

 stratBBands <- add.signal(stratBBands,name="sigCrossover",arguments = list(columns=c("Close","dn"),relationship="lt"),label="Cl.lt.LowerBand")

 stratBBands <- add.signal(stratBBands,name="sigCrossover",arguments = list(columns=c("High","Low","mavg"),relationship="op"),label="Cross.Mid")