修改包源代码

时间:2015-07-31 00:22:24

标签: r quantmod

我想修改quantmod包及其一些功能,如barChart和chartSeries。作为这次体验的一部分,我创建了一个名为“qmDATA”的xts对象,并将其传递给barChart函数和chartSeries函数的代码。

我目前在底部附近收到错误:

if (plot) 
+   library(quantmod)
> do.call("chartSeries.chob", list(chob))
Error in do.call("quantmod:::chartSeries.chob", list(chob)) : 
  could not find function "quantmod:::chartSeries.chob"
> #do.call("chartSeries.chob", list(chob), envir = thisEnv)
> 
> chob@device <- as.numeric(dev.cur())
> 
> write.chob(chob, chob@device)
Error: could not find function "write.chob"
> invisible(chob)

任何想法为什么错误说找不到chart_Series_chob。我在第一行中包含了对quantmod库的调用。以下是您可以运行的代码,它只是barChart和chartSeries的源代码

library(quantmod)
TIME = as.POSIXct(c("2015-01-01 13:14:15","2015-01-01 13:14:16","2015-01-01 13:14:17","2015-01-01 13:14:18","2015-01-01 13:14:19"),tz="GMT")
OPEN=c(10,10,10,10,13)
HIGH=c(15,16,17,14,15)
LOW=c(9,10,9,10,9)
CLOSE= c(12,12,12,12,12)
VOLUME= c(100,100,100,100,100)
MYVOLUME = c(60,50,40,30,20)
MYPRICE = c(10,11,12,13,14)
VOLUME_ORIG<- VOLUME
VOLUME<- VOLUME - MYVOLUME
d1<- data.frame(Open = OPEN, High = HIGH, Low = LOW,  Close = CLOSE, Volume = VOLUME, MYVOLUME = MYVOLUME, MYPRICE = MYPRICE, VOLUME_ORIG = VOLUME_ORIG)
d1$VOLUME_ORIG<- d1$VOLUME
d1$VOLUME<- d1$VOLUME - d1$MYVOLUME
head(d1)
d1<- with (d1, data.frame(Open = OPEN, High = HIGH, Low = LOW,  Close =CLOSE, Volume = VOLUME))
qmDATA<-as.xts(d1, tzone = Sys.getenv("GMT"), order.by =as.POSIXct(TIME))
head(qmDATA)
as.quantmod.OHLC(qmDATA,col.names = c("Open", "High", "Low", "Close","Volume")) 
#barChart(qmDATA)


############# start implementing code from getAnywhere(barChart)
x = qmDATA
subset = NULL
name = deparse(substitute(x)) 
type = "bars"
show.grid = TRUE
time.scale = NULL
log.scale = FALSE
TA = "addVo"
bar.type = "ohlc" 
theme = chartTheme("black")
major.ticks = "auto"
minor.ticks = TRUE
color.vol = TRUE
multi.col = TRUE
yrange = NULL
TAsep = ";"
plot= TRUE
##############start implementing code from      getAnywhere(chartSeries)
x <- try.xts(x, error = "chartSeries requires an xtsible object")
x <- na.omit(x)
indexClass(x) <- "POSIXct"

if (!is.null(subset) & is.character(subset)) {
  if (strsplit(subset, " ")[[1]][1] %in% c("first", "last")) {
    subsetvec <- strsplit(subset, " ")[[1]]
    if (length(subsetvec) < 3) {
      subset.n <- ifelse(length(subsetvec) == 1, 1L, 
                         as.numeric(subsetvec[2]))
    }
    else {
      subset.n <- paste(subsetvec[2:3], collapse = " ")
    }
    sub.index <- index(do.call(subsetvec[1], list(x, 
                                                  subset.n)))
    xsubset <- which(index(x) %in% sub.index)
  }else { xsubset <- which(index(x) %in% index(x[subset]))}
}else {xsubset <- 1:NROW(x)}
xdata <- x
x <- x[xsubset]



if (is.OHLC(x)) {
  Opens <- as.numeric(Op(x))
  Highs <- as.numeric(Hi(x))
  Lows <- as.numeric(Lo(x))
  Closes <- as.numeric(Cl(x))
}else {
  Lows <- min(x[, 1])
  Highs <- max(x[, 1])
  Closes <- as.numeric(x[, 1])
  type <- "line"
  color.vol <- FALSE
}



if (has.Vo(x)) {
  Volumes <- as.numeric(Vo(x))
  show.vol <- TRUE
}else show.vol <- FALSE
if (is.null(time.scale)) {
  time.scale <- periodicity(x)$scale
}

if (is.character(theme)) 
  theme <- chartTheme(theme)
# if (!missing(  up.col  )) 
#   theme$up.col <- up.col
# if (!missing(dn.col)) 
#   theme$dn.col <- dn.col
# if (missing(multi.col) | !multi.col) {
#   multi.col <- FALSE
#   theme$dn.up.col <- theme$up.col
#   theme$up.up.col <- theme$up.col
#   theme$dn.dn.col <- theme$dn.col
#   theme$up.dn.col <- theme$dn.col
# }else {
#   if (is.character(multi.col)) {
#     theme$dn.up.col <- multi.col[1]
#     theme$up.up.col <- multi.col[2]
#     theme$dn.dn.col <- multi.col[3]
#     theme$up.dn.col <- multi.col[4]
#   }
#   theme$up.col <- theme$up.up.col
#   theme$dn.col <- theme$dn.dn.col
#   multi.col <- TRUE
# }

chart.options <- c("auto", "candlesticks", "matchsticks", "line", "bars")
chart <- chart.options[pmatch(type, chart.options)]

if (chart[1] == "auto") {
  chart <- ifelse(NROW(x) > 300, "matchsticks", "candlesticks")
}
if (chart[1] == "candlesticks") {
  spacing <- 3
  width <- 3
}else if (chart[1] == "matchsticks" || chart[1] == "line") {
  spacing <- 1
  width <- 1
}else if (chart[1] == "bars") {
  spacing <- 4
  width <- 3
  if (NROW(x) > 60) 
    width <- 1
}


ep <- axTicksByTime(x, major.ticks)
x.labels <- names(ep)
chob <- new("chob")
#chob@call <- match.call(expand.dots = TRUE)
#if (is.null(name)) 
#  name <- as.character(match.call()$x)
chob@xdata <- xdata
chob@xsubset <- xsubset #1,2,3,4,5
chob@name <- name # "x"
chob@type <- chart[1] # bars
chob@xrange <- c(1, NROW(x)) # 1 5


if (is.OHLC(x)) { #get min and max ranges 
  chob@yrange <- c(min(Lo(x), na.rm = TRUE), max(Hi(x),  na.rm = TRUE))
}else {chob@yrange <- range(x[, 1], na.rm = TRUE)}


if (!is.null(yrange) && length(yrange) == 2)  # yrange is NULL
  # chob@yrange <- yrange
  # chob@log.scale <- log.scale
  # chob@color.vol <- color.vol
  # chob@multi.col <- multi.col
  # chob@show.vol <- show.vol
  # chob@bar.type <- bar.type
  # chob@line.type <- line.type
  # chob@spacing <- spacing
  # chob@width <- width
  # chob@bp <- ep
  # chob@x.labels <- x.labels
# chob@colors <- theme
# chob@layout <- layout
# chob@time.scale <- time.scale
# chob@minor.ticks <- minor.ticks
# chob@major.ticks <- major.ticks
# chob@length <- NROW(x)
# chob@passed.args <- as.list(match.call(expand.dots = TRUE)[-1])



if (!is.null(TA)) { #TA = addVo
  thisEnv <- environment()
  if (is.character(TA)) 
    TA <- as.list(strsplit(TA, TAsep)[[1]])
  chob@passed.args$TA <- list()

  for (ta in 1:length(TA)) {
    if (is.character(TA[[ta]])) {
      chob@passed.args$TA[[ta]] <- eval(parse(text = TA[[ta]]),  envir = thisEnv)
    }
    else chob@passed.args$TA[[ta]] <- eval(TA[[ta]], 
                                           envir = thisEnv)
  }


  poss.new <- sapply(chob@passed.args$TA, function(x) {
    if (isS4(x) && is(x, "chobTA")) 
      return(x@new)
    stop("improper TA argument/call in chartSeries",  call. = FALSE)
  })


  if (length(poss.new) > 0) 
    poss.new <- which(poss.new)
  chob@windows <- length(poss.new) + 1
  chob@passed.args$show.vol <- any(sapply(chob@passed.args$TA, function(x) x@name == "chartVo"))
}else chob@windows <- 1
chob@passed.args$TA <- sapply(chob@passed.args$TA, function(x) { eval(x@call)})


if (plot) 
  library(quantmod)
do.call("quantmod:::chartSeries.chob", list(chob)) ##############THIS IS THE ERROR
#do.call("quantmod:::chartSeries.chob", list(chob), envir = thisEnv)

chob@device <- as.numeric(dev.cur())

write.chob(chob, chob@device)
invisible(chob)

1 个答案:

答案 0 :(得分:0)

chartSeries.chob未从quantmod名称空间导出,因此您需要执行以下两项操作之一:

  1. 将您的功能包含在quantmod源的修改版本中,重建并重新安装修改后的软件包。
  2. 使用:::功能访问它:quantmod:::chartSeries.chob