newbie
newbie

Reputation: 1

How to add Simple Moving Average to highchart in R?

I want to add Simple Moving Average on this candlestick chart (using highchart, not chartSeries) but I keep failing. How can I add this SMA to my chart?

add_SMA(n = 200, on = 1, col = "red"), add_SMA(n = 50, on = 1, col = "black")

Any help?


start <- format(as.Date("2015-01-01"), "%Y-%m-%d")
end <- format(as.Date("2024-10-03"), "%Y-%m-%d")

ticker <- "PFE"

price <- getSymbols(ticker, auto.assign = FALSE, from = start, to = end)

head(price)

highchart(type = "stock") %>% 
  hc_add_series(price) %>% 
  hc_title(text = "<b>Price Candlestick Chart 2015-2024</b>")

Upvotes: 0

Views: 32

Answers (1)

Edward
Edward

Reputation: 18493

You could use the SMA function, with your choice of time periods to average over.

library(highcharter)
library(TTR)

highchart(type = "stock") %>% 
  hc_add_series(price) %>% 
  hc_add_series(SMA(price$PFE.Close, n=10)) %>% 
  hc_title(text = "Price Candlestick Chart 2015-2024")

enter image description here

Upvotes: 0

Related Questions