[R] Fibonacci technical analysis using data from getSymbols and quantmod
Bert Gunter
bgunter@4567 @end|ng |rom gm@||@com
Sun Aug 17 11:07:07 CEST 2025
I think you would do better posting on the R finance SIG list,
https://stat.ethz.ch/mailman/listinfo/r-sig-finance,
if it's still active rather than here for such a specific and technical
post.
Alternatively, and better, would be to check to see if the quantmod package
has its own mailing list and post there, if so.
(Unless Eric's ChatGPT code works for you of course).
-- Bert
On Sun, Aug 17, 2025 at 12: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