[R] Fibonacci technical analysis using data from getSymbols and quantmod
Eric Berger
er|cjberger @end|ng |rom gm@||@com
Sun Aug 17 11:03:42 CEST 2025
Hi Andre,
I have used quantmod but not these functions. I dumped your code into
ChatGPT5 and it gave the following answer which will hopefully be of help:
You’re getting that error because AAPL is an *OHLCV xts with multiple
columns*. runMin() / runMax() only accept a *single column (univariate)
series*. Two solid ways to fix it:
library(quantmod)
Fibonacci <- function(x, n = 55) {
x <- try.xts(x, error = as.matrix)
x <- Cl(x) # <- make it univariate
lo <- runMin(x, n = n)
hi <- runMax(x, n = n)
high <- 0.618 * (hi - lo) + lo
middle <- 0.5 * (hi - lo) + lo
low <- 0.382 * (hi - lo) + lo
res <- cbind(na.spline(lo), na.spline(hi),
na.spline(high), na.spline(middle), na.spline(low))
colnames(res) <- c("min","max","high","middle","low")
reclass(res, x)
}
getSymbols("AAPL")
addFibonacci <- newTA(function(x) Fibonacci(x, n = 55), on = 1)
chartSeries(AAPL, TA = "addFibonacci()")
ALTERNATIVE FIX
library(quantmod)
Fibonacci <- function(x, n = 55) {
x <- try.xts(x, error = as.matrix)
lo <- runMin(Lo(x), n = n) # rolling min of Lows
hi <- runMax(Hi(x), n = n) # rolling max of Highs
high <- 0.618 * (hi - lo) + lo
middle <- 0.5 * (hi - lo) + lo
low <- 0.382 * (hi - lo) + lo
res <- cbind(na.spline(lo), na.spline(hi),
na.spline(high), na.spline(middle), na.spline(low))
colnames(res) <- c("min","max","high","middle","low")
reclass(res, x)
}
getSymbols("AAPL")
addFibonacci <- newTA(function(x) Fibonacci(x, n = 55), on = 1)
chartSeries(AAPL, TA = "addFibonacci()")
HTH,
Eric
On Sun, Aug 17, 2025 at 10:56 AM André Luiz Tietböhl Ramos <
andreltramos using gmail.com> wrote:
> Hello,
>
> I'd like to integrate the Fibonacci graph as a TA indicator for stock
> analysis. So fat I wasn't able to do so. From the web, I found something
> (below) but it didn't work either.
>
> My goal is to develop a function that uses the price column of a data frame
> along with the start and end dates of the period of interest, which are
> obtained from either its index or a given data frame column. From these
> data the Fibonacci levels from the indicator are plotted.
>
>
> https://stackoverflow.com/questions/20192913/how-to-create-a-technical-indicator-in-quantmod-package/79737478#79737478
>
> The Fibonacci function and indicator are below,
>
> 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)}
>
> addFibonacci <- function (..., on = 1, legend = "auto") {
> #lchob <- get.current.chob()
> lchob <- quantmod:::get.current.chob()
> x <- as.matrix(lchob using xdata)
> x <- Fibonacci(x = x)
> yrange <- NULL
> chobTA <- new("chobTA")
> if (NCOL(x) == 1) {
> chobTA using TA.values <- x[lchob using xsubset]
> }
> else chobTA using TA.values <- x[lchob using xsubset, ]
> chobTA using name <- "chartTA"
> if (any(is.na(on))) {
> chobTA using new <- TRUE
> }
> else {
> chobTA using new <- FALSE
> chobTA using on <- on
> }
> chobTA using call <- match.call()
> legend.name <- gsub("^add", "", deparse(match.call()))
> gpars <- c(list(...), list())[unique(names(c(list(), list(...))))]
> chobTA using params <- list(xrange = lchob using xrange, yrange = yrange,
> colors = lchob using colors, color.vol = lchob using color.vol, multi.col
> = lchob using multi.col,
> spacing = lchob using spacing, width = lchob using width, bp = lchob using bp,
> x.labels = lchob using x.labels, time.scale = lchob using time.scale,
> isLogical = is.logical(x), legend = legend, legend.name =
> legend.name,
> pars = list(gpars))
> if (is.null(sys.call(-1))) {
> TA <- lchob using passed.args$TA
> lchob using passed.args$TA <- c(TA, chobTA)
> lchob using windows <- lchob using windows + ifelse(chobTA using new, 1,
> 0)
> chartSeries.chob <- chartSeries.chob
> do.call("chartSeries.chob", list(lchob))
> invisible(chobTA)
> }
> else {
> return(chobTA)
> }}
>
> Using the TA indicator function suggested I got,
>
> R> getSymbols("AAPL")
>
> [1] "AAPL"
> R> addFibonacci <- newTA(Fibonacci,on=1)
> R> chartSeries(AAPL, TA="addFibonacci()")
> Error in runMin(x, n = n) (from #4) :
> ncol(x) > 1. runMin only supports univariate 'x'
> R> R> Fibonacci(AAPL)
> Error in runMin(x, n = n) (from #4) :
> ncol(x) > 1. runMin only supports univariate 'x'
> R>
>
> Any help is greatly appreciated.
>
>
> Regards,
>
> --
> André Luiz Tietbohl Ramos, PhD
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> https://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
[[alternative HTML version deleted]]
More information about the R-help
mailing list