开发者

Error message 'missing value where TRUE/FALSE needed' when applying a quantstrat strategy

I am trying to run this code

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',开发者_如何学Python 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))

But when i apply the last line of the code i am receiving this error message

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

Could someone assist me to find a solution to this error message

Thanks in advance


Your issue is that you're looking for a column 'obvma20', but your function creates a column named 'obvma'.

The simple answer is to change your add.signal definitions to use 'obvma'.

quantstrat won't overwrite column names where they exist, assuming that the function author (you) wanted the column label to be a certain way. We may change that in the future to look for duplicated column names and apply the label in that case, but in this case you would still be looking for the wrong column name.


I get a similar error. when i run the bbands.demo now for example, it seems the applyIndicator actually changes the column names as per the code below (looks like this behavior is new as I was able to fish out an old package on another machine which did not have this problem).

Right after applying running the code for applyIndicator as in the example:

***** Notice the original column names:

Browse[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

it looks like this is the 'newer code' pastes over the existing code. (see older code below)

Browse[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 = ".")
}

Resulting in :

Browse[2]> head(tmp_val)

**** NOTICE HOW THE COLUMN NAMES have changed

       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

* results in the following error when invoked as below:

Error in if (length(j) == 0 || (length(j) == 1 && j == 0)) { : missing value where TRUE/FALSE needed In addition: Warning message: In download.file(paste(yahoo.URL, "s=", Symbols.name, "&a=", from.m, : downloaded length 83602 != reported length 200

notice vs Old code:

Browse[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 = ".")
    }

From the demo of bbands, up is replaced with BBands.ind.1 (but i cant seem to get it to work if I replace 'up' with BBands.ind.1).

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")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜