2013-11-25 55 views
6

我是R的新手,我在創建技術指標時遇到了一些問題。更具體地說,我想創建一個指標Fibonacci,它將被添加到chartSeries,並將包含5條水平線。我正在使用的數據是股票的收盤價。因此,我想要創建的圖表將在最大收盤價的位置上有一條水平線,最低收盤價的一條水平線和前兩條之間的三條水平線。我爲了拿一個時間序列股票的收盤價的五個值寫的代碼如下:如何在quantmod軟件包中創建技術指標

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([email protected]) 
chobTA <- new("chobTA") 
[email protected] <- !overlay 
if (!is.OHLC(x)) 
    stop("Fibonacci requires HL series") 
else { 
fibon <- Fibonacci(Cl(x)) 
} 
[email protected] <- fibon[[email protected]] 
[email protected] <- match.call() 
[email protected] <- 1 
[email protected] <- list(xrange = [email protected], 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], 
    col = col) 
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) 
} 
} 

的問題是,所述指示器不獲取添加到圖表和我也得到以下錯誤消息:

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

我做錯了什麼想法?

回答

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([email protected]) 
    x <- Fibonacci(x = 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("^add", "", deparse(match.call())) 
    gpars <- c(list(...), list())[unique(names(c(list(), 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 <- 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

+0

非常感謝您的回答和幫助。我不知道我可以使用newTA功能。我只是想知道爲什麼我不能簡單地調用addFibonacci()並像我想要的那樣繪製所有的addTA函數,而是必須調用chartSeries函數?例如,當我調用addFibonacci我採取:錯誤在addFibonacci():對象'chartSeries.chob'找不到 – user3017291

相關問題