2013-11-25 15 views
6

Tôi là người mới tham gia R và tôi đang gặp một số vấn đề với việc tạo chỉ báo kỹ thuật. Cụ thể hơn, tôi muốn tạo chỉ báo, Fibonacci, sẽ được thêm vào chartSeries và sẽ bao gồm 5 đường ngang. Dữ liệu tôi đang làm việc là giá đóng cửa của một cổ phiếu. Vì vậy, đồ thị tôi muốn tạo sẽ có một đường ngang ở điểm giá đóng tối đa, một đường ngang ở điểm giá đóng cửa min và ba đường ngang giữa hai giá trị trước đó. Mã tôi đã viết trong trật tự để có những năm giá trị của một chuỗi thời gian của giá đóng cửa của một cổ phiếu như sau:Cách tạo chỉ báo kỹ thuật trong gói 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) 
} 

Tôi cũng đã viết đoạn mã sau để thêm các chỉ số kỹ thuật cho chartSeries cốt truyện hiện của quantmod gói:

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) 
} 
} 

vấn đề là chỉ số là không nhận được bổ sung vào bảng xếp hạng và tôi cũng nhận được thông báo lỗi sau:

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

Bất kỳ ý tưởng nào về những gì tôi đang làm sai?

Trả lời

7

Thay vì viết các add* chức năng từ đầu, bạn chỉ có thể sử dụng 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" 

Hmm, rõ ràng get.current.chob không xuất khẩu ... đó là okay, chúng tôi chỉ có thể thay đổi chức năng bản thân. Sau khi gọi addFibonacci <- newTA(Fibonacci,on=1), addFibonacci được định nghĩa là:

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) 
    } 
} 

Và bạn có thể nhìn thấy nơi tôi thay thế các cuộc gọi đến get.current.chob() với quantmod:::get.current.chob(). Bây giờ nó sẽ hoạt động.

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

Thành công!

enter image description here

+0

Cảm ơn bạn rất nhiều vì câu trả lời và trợ giúp. Tôi không biết rằng tôi có thể sử dụng chức năng newTA. Tôi chỉ tự hỏi tại sao tôi không thể chỉ đơn giản gọi addFibonacci() và lấy cốt truyện tôi muốn, giống như tất cả các hàm addTA, và thay vào đó tôi phải gọi hàm chartSeries? Ví dụ khi tôi gọi hàm addFibonacci i: Lỗi trong addFibonacci(): object 'chartSeries.chob' không tìm thấy – user3017291

Các vấn đề liên quan