0

我正在尝试运行此代码

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

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

提前致谢

4

2 回答 2

3

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

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

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

于 2011-08-05T17:39:08.097 回答
0

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

在应用运行 applyIndi​​cator 的代码后,如示例所示:

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

浏览[2]> head(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]>

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]> 头(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呐呐呐呐呐

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

if (length(j) == 0 || (length(j) == 1 && j == 0)) { : 需要 TRUE/FALSE 时缺少值另外:警告消息:在 download.file(paste( yahoo.URL, "s=", Symbols.name, "&a=", from.m, : 下载长度 83602 != 报告长度 200

注意与旧代码:

浏览[2]>

 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")
于 2013-03-24T17:15:19.250 回答