如何在quantmod包中创建技术指标

时间:2013-11-25 12:19:15

标签: r quantmod

我是R的新手,我在创建技术指标方面遇到了一些问题。更具体地说,我想创建一个指标Fibonacci,它将被添加到chartSeries并由5条水平线组成。我正在处理的数据是股票的收盘价。因此,我想创建的图表将在最高收盘价的点上有一条水平线,在最低收盘价的点上有一条水平线,在前两条之间有三条水平线。我为了获取股票收盘价时间序列的五个值而编写的代码如下:

Fibonacci <- function(x) {
x <- try.xts(x, error = as.matrix)
n <- nrow(x)
min <- runMin(x,n=n)
max <- runMax(x,n=n)
high <- 0.62*(max-min) + min
middle <- 0.5*(max-min) + min
low <- 0.38*(max-min) + min
res <-cbind(na.spline(min),na.spline(max),na.spline(high),
            na.spline(middle),na.spline(low))
colnames(res)<- c("min","max","high","middle","low") 
reclass (res, x)
}

我还编写了以下代码,以便将技术指标添加到chartSeries包的现有quantmod图中:

addFibonacci<- function(col = "red",overlay = TRUE){
stopifnot("package:TTR" %in% search() || require("TTR", quietly = TRUE))
lchob <- quantmod:::get.current.chob() 
x <- as.matrix(lchob@xdata)
chobTA <- new("chobTA")
chobTA@new <- !overlay
if (!is.OHLC(x)) 
    stop("Fibonacci requires HL series")
else {
fibon <- Fibonacci(Cl(x))
}
chobTA@TA.values <- fibon[lchob@xsubset]
chobTA@call <- match.call()
chobTA@on <- 1
chobTA@params <- list(xrange = lchob@xrange, colors = lchob@colors, 
    color.vol = lchob@color.vol, multi.col = lchob@multi.col, 
    spacing = lchob@spacing, width = lchob@width, bp = lchob@bp, 
    x.labels = lchob@x.labels, time.scale = lchob@time.scale, 
    col = col)
if (is.null(sys.call(-1))) {
    TA <- lchob@passed.args$TA
    lchob@passed.args$TA <- c(TA, chobTA)
    lchob@windows <- lchob@windows + ifelse(chobTA@new, 1, 
        0)
    chartSeries.chob <- quantmod:::chartSeries.chob
    do.call(chartSeries.chob, list(lchob))
    invisible(chobTA)
}
else {
    return(chobTA)
}
}

问题是指标没有添加到图表中,我也收到以下错误消息:

Error in do.call(x@passed.args$TA[[j]]@name, list(x@passed.args$TA[[j]])) :
  'what' must be a character string or a function

对于我做错了什么想法?

1 个答案:

答案 0 :(得分:7)

不是从头开始编写add*函数,而是使用newTA

> library(quantmod)
> getSymbols("AAPL")
[1] "AAPL"
> addFibonacci <- newTA(Fibonacci,on=1)
> chartSeries(AAPL, TA="addFibonacci()")
Error in addFibonacci() : could not find function "get.current.chob"

嗯,显然get.current.chob没有导出......没关系,我们可以自己更改功能。致电addFibonacci <- newTA(Fibonacci,on=1)后,addFibonacci定义为:

addFibonacci <- function (..., on = 1, legend = "auto") 
{
    #lchob <- get.current.chob()
    lchob <- quantmod:::get.current.chob()
    x <- as.matrix(lchob@xdata)
    x <- Fibonacci(x = x)
    yrange <- NULL
    chobTA <- new("chobTA")
    if (NCOL(x) == 1) {
        chobTA@TA.values <- x[lchob@xsubset]
    }
    else chobTA@TA.values <- x[lchob@xsubset, ]
    chobTA@name <- "chartTA"
    if (any(is.na(on))) {
        chobTA@new <- TRUE
    }
    else {
        chobTA@new <- FALSE
        chobTA@on <- on
    }
    chobTA@call <- match.call()
    legend.name <- gsub("^add", "", deparse(match.call()))
    gpars <- c(list(...), list())[unique(names(c(list(), list(...))))]
    chobTA@params <- list(xrange = lchob@xrange, yrange = yrange, 
        colors = lchob@colors, color.vol = lchob@color.vol, multi.col = lchob@multi.col, 
        spacing = lchob@spacing, width = lchob@width, bp = lchob@bp, 
        x.labels = lchob@x.labels, time.scale = lchob@time.scale, 
        isLogical = is.logical(x), legend = legend, legend.name = legend.name, 
        pars = list(gpars))
    if (is.null(sys.call(-1))) {
        TA <- lchob@passed.args$TA
        lchob@passed.args$TA <- c(TA, chobTA)
        lchob@windows <- lchob@windows + ifelse(chobTA@new, 1, 
            0)
        chartSeries.chob <- chartSeries.chob
        do.call("chartSeries.chob", list(lchob))
        invisible(chobTA)
    }
    else {
        return(chobTA)
    }
}

您可以看到我将get.current.chob()的来电替换为quantmod:::get.current.chob()的位置。现在应该可以了。

chartSeries(AAPL, TA="addFibonacci()")

成功!

enter image description here