2013-01-20 190 views
1

我试图用价格和一些技术指标(如ADX,RSI和OBV)绘制图表。我无法弄清楚为什么addOBV提供了一个错误,为什么addADX根本没有显示在图表的图表中?addOBV投掷错误

这里我的代码:

tmp <- read.csv(paste("ProcessedQuotes/",Nifty[x,],".csv", sep=""), 
    as.is=TRUE, header=TRUE, row.names=NULL) 
tmp$Date<-as.Date(tmp$Date) 
ydat = xts(tmp[,-1],tmp$Date) 
lineChart(ydat, TA=NULL, name=paste(Nifty[x,]," Technical Graph")) 
plot(addSMA(10)) 
plot(addEMA(10)) 
plot(addRSI()) 
plot(addADX()) 
plot(addOBV()) 

误差addOBV是:

Error in try.xts(c(2038282, 1181844, -1114409, 1387404, 3522045, 4951254, : 
    Error in as.xts.double(x, ..., .RECLASS = TRUE) : 
     order.by must be either 'names()' or otherwise specified 

下面你可以参见DIN未在图尽显。

enter image description here

> class(ydat) 
[1] "xts" "zoo" 
> head(ydat) 
    Open High Low Close Volume Trades Sma20 Sma50 DIp DIn DX ADX aroonUp aroonDn oscillator macd signal RSI14 
+3

请让你的例子可重现... –

+0

我同意@PaulHiemstra。问题可能与您的数据有关。使用'getSymbols'从Yahoo Finance提取数据,并且请将'sessionInfo()'的输出添加到您的问题中。 –

+0

addOBV使用close和volume,这是在ydat中存在的头像(ydat)所示。所有其他技术指标都在工作..我在互联网上显示一个链接,说有一个quantmod的补丁,为了解决这个错误,但试图应用该补丁时,说不能应用于我的版本R – user1848880

回答

3

我不知道为什么这个补丁不适合你的工作,但你可以创建一个新的函数(或者你可以屏蔽该quantmod的一个)。我们只需制作一个名为addOBV2的新修补版本,它是addOBV的代码,除了一条修补线。 (x <- as.matrix([email protected])替换为x <- try.xts([email protected], error=FALSE))。

addOBV2 <- function (..., on = NA, legend = "auto") 
{ 
    stopifnot("package:TTR" %in% search() || require("TTR", quietly = TRUE)) 
    lchob <- quantmod:::get.current.chob() 
    x <- try.xts([email protected], error=FALSE) 
    #x <- as.matrix([email protected]) 
    x <- OBV(price = Cl(x), volume = Vo(x)) 
    yrange <- NULL 
    chobTA <- new("chobTA") 
    if (NCOL(x) == 1) { 
    [email protected] <- x[[email protected]] 
    } 
    else [email protected] <- x[[email protected], ] 
    [email protected] <- "chartTA" 
    if (any(is.na(on))) { 
    [email protected] <- TRUE 
    } 
    else { 
    [email protected] <- FALSE 
    [email protected] <- on 
    } 
    [email protected] <- match.call() 
    legend.name <- gsub("^.*[(]", " On Balance Volume (", deparse(match.call()))#, 
    #extended = TRUE) 
    gpars <- c(list(...), list(col=4))[unique(names(c(list(col=4), list(...))))] 
    [email protected] <- list(xrange = [email protected], yrange = yrange, 
         colors = [email protected], color.vol = [email protected], multi.col = [email protected], 
         spacing = [email protected], width = [email protected], bp = [email protected], 
         x.labels = [email protected], time.scale = [email protected], 
         isLogical = is.logical(x), legend = legend, legend.name = legend.name, 
         pars = list(gpars)) 
    if (is.null(sys.call(-1))) { 
    TA <- [email protected]$TA 
    [email protected]$TA <- c(TA, chobTA) 
    [email protected] <- [email protected] + ifelse([email protected], 1, 
              0) 
    chartSeries.chob <- quantmod:::chartSeries.chob 
    do.call("chartSeries.chob", list(lchob)) 
    invisible(chobTA) 
    } 
    else { 
    return(chobTA) 
    } 
} 

现在有效。

# reproduce your data 
ydat <- getSymbols("ZEEL.NS", src="yahoo", from="2012-09-11", 
        to="2013-01-18", auto.assign=FALSE) 

lineChart(ydat, TA=NULL, name=paste("ZEEL Technical Graph")) 
plot(addSMA(10)) 
plot(addEMA(10)) 
plot(addRSI()) 
plot(addADX()) 
plot(addOBV2()) 

enter image description here

+0

非常感谢..修补的obv2工作正常... – user1848880

+0

我刚刚提交此修补程序quantmod R-Forge,r603。 –

3

此代码重现错误:

library(quantmod) 
getSymbols("AAPL") 
lineChart(AAPL, 'last 6 months') 
addOBV() 

会议信息:

sessionInfo() 
R version 2.15.0 (2012-03-30) 
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit) 

locale: 
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8 

attached base packages: 
[1] stats  graphics grDevices utils  datasets methods base  

other attached packages: 
[1] quantmod_0.3-17 TTR_0.21-1  xts_0.9-1  zoo_1.7-9  Defaults_1.1-1 rgeos_0.2-11 
[7] sp_1.0-5  sos_1.3-5  brew_1.0-6  

loaded via a namespace (and not attached): 
[1] grid_2.15.0 lattice_0.20-6 tools_2.15.0 

周围的Googling,错误似乎与这样的事实:addOBV转换数据转换成矩阵,这会导致问题。补丁已被posted on RForge

+0

@ user1848880,要应用该补丁,您必须签出或下载源代码,然后单独下载补丁程序,并修补代码(例如,在Linux上遵循[这些说明](http://www.cyberciti.biz /常见问题/ APPY-补丁文件使用补丁命令/))。然后,一旦你在本地修补了源代码,你就必须构建并安装补丁的修补版本。 – GSee

+0

install.packages(“quantmod.patch”) 警告消息: 软件包'quantmod.patch'不可用(对于R版本2.15.2) – user1848880