2013-01-20 188 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