gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 01:45:01 26 4
gpt4 key购买 nike

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

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

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

对于我做错了什么有什么想法吗?

最佳答案

而不是写 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

关于r - 如何在 quantmod 包中创建技术指标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20192913/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com