Native support for candlestick charts in Plotly and R
[This article was first published on R – Modern Data, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Plotly.js now supports candlestick charts as a chart-type and in this post we’ll highlight how to use this feature in R. We’ll use the quantmod
package to retrieve data as well as generate some technical trading signals.
Don’t forget to install the latest version of plotly from github:
devtools::install_github("ropensci/plotly")
Syntax
The syntax is straightforward:
plot_ly(x = ..., open = ..., high = ..., low = ..., close = ..., type = "candlestick")
Data
# Get data stock <- getSymbols("MSFT", auto.assign = F) dts <- index(stock) df <- data.frame(stock, row.names = NULL) df$dates <- dts names(df) <- c("Open", "High", "Low", "Close", "Volume", "Adjusted", "dates") # Subset to after Jan 2016 df <- subset(df, dates > "2016-01-01") # Color or volume bars barcols <- c() for (i in 1:length(df$dates)) { if (i == 1) {barcols[i] <- "#F95959"} if (i > 1) { x <- ifelse(df$Close[i] > df$Close[i - 1], "#455D7A", "#F95959") barcols[i] <- x } } # Moving Avg line MA <- runMean(df$Close) # Range selector rangeselectorlist = list( x = 0, y = 0.9, bgcolor = "#0099cc", font = list(color = "white"), buttons = list( list(count = 1, label = "reset", step = "all"), list(count = 1, label = "1yr", step = "year", stepmode = "backward"), list(count = 3, label = "3 mo", step = "month", stepmode = "backward"), list(count = 1, label = "1 mo", step = "month", stepmode = "backward"), list(step = "all") ) )
Base candlestick chart with volume panel
plot_ly(df, type = "candlestick", x = ~dates, open = ~Open, high = ~High, low = ~Low, close = ~Close, yaxis = "y", increasing = list(line = list(color = "#455D7A")), decreasing = list(line = list(color = "#F95959")), name = "Price", height = 600, width = 1024) %>% add_bars(data = df, x = ~dates, y = ~Volume, marker = list(color = barcols), yaxis = "y2", inherit = F, name = "Vol") %>% add_lines(x = df$dates, y = MA, line = list(width = 3, dash = "5px", color = "#33bbff"), inherit = F, name = "Mov Avg") %>% layout( plot_bgcolor = "rgb(250,250,250)", xaxis = list(title = "", domain = c(0,0.95), rangeslider = list(visible = F), rangeselector = rangeselectorlist), yaxis = list(domain = c(0.22, 0.9)), yaxis2 = list(domain = c(0, 0.18), side = "right"), showlegend = F, annotations = list( list(x = 0, y = 1, xanchor = "left", yanchor = "top", xref = "paper", yref = "paper", text = paste0("<b>Microsoft</b>"), font = list(size = 30, family = "serif"), showarrow = FALSE), list(x = 0.8, y = 0.95, xanchor = "left", yanchor = "top", xref = "paper", yref = "paper", text = paste0("[", paste(range(df$dates),collapse = " / "), "]"), font = list(size = 15, family = "serif"), showarrow = FALSE), list(x = 0, y = 0.18, xanchor = "left", yanchor = "top", xref = "paper", yref = "paper", text = paste0("<b>Volume</b>"), font = list(size = 15, family = "serif"), showarrow = FALSE) ) )
Base candlestick chart with Bollinger bands
BB <- as.data.frame(BBands(df$Close)) plot_ly(df, type = "candlestick", x = ~dates, open = ~Open, high = ~High, low = ~Low, close = ~Close, yaxis = "y", increasing = list(line = list(color = "#455D7A")), decreasing = list(line = list(color = "#F95959")), name = "Price", height = 600, width = 1024) %>% add_bars(data = df, x = ~dates, y = ~Volume, marker = list(color = barcols), yaxis = "y2", inherit = F, name = "Vol") %>% # MA add_lines(x = df$dates, y = BB$mavg, line = list(width = 3, dash = "5px", color = "#33bbff"), inherit = F, name = "Mov Avg") %>% # Upper and Lower bounds add_lines(x = df$dates, y = BB$up, line = list(width = 1, dash = "5px", color = "#737373"), fill = "tonexty", fillcolor = "rgba(194, 240, 240, 0.2)", inherit = F, name = "Bollinger") %>% add_lines(x = df$dates, y = BB$dn, line = list(width = 1, dash = "5px", color = "#737373"), fill = "tonexty", fillcolor = "rgba(194, 240, 240, 0.2)", inherit = F, name = "Bollinger") %>% layout( plot_bgcolor = "rgb(250,250,250)", xaxis = list(title = "", domain = c(0,0.95), rangeslider = list(visible = F), rangeselector = rangeselectorlist), yaxis = list(domain = c(0.22, 0.9)), yaxis2 = list(domain = c(0, 0.18), side = "right"), showlegend = F, annotations = list( list(x = 0, y = 1, xanchor = "left", yanchor = "top", xref = "paper", yref = "paper", text = paste0("<b>Microsoft</b>"), font = list(size = 30, family = "serif"), showarrow = FALSE), list(x = 0.8, y = 0.95, xanchor = "left", yanchor = "top", xref = "paper", yref = "paper", text = paste0("[", paste(range(df$dates),collapse = " / "), "]"), font = list(size = 15, family = "serif"), showarrow = FALSE), list(x = 0, y = 0.18, xanchor = "left", yanchor = "top", xref = "paper", yref = "paper", text = paste0("<b>Volume</b>"), font = list(size = 15, family = "serif"), showarrow = FALSE) ) )
Base candlestick chart with Chaikin Money Flow
CKD <- CMF(df[,c("High", "Low", "Close")], volume = df$Volume) CKD.pos <- CKD CKD.pos[CKD.pos < 0] <- 0 CKD.neg <- CKD CKD.neg[CKD.neg > 0] <- 0 plot_ly(df, type = "candlestick", x = ~dates, open = ~Open, high = ~High, low = ~Low, close = ~Close, yaxis = "y", increasing = list(line = list(color = "#455D7A")), decreasing = list(line = list(color = "#F95959")), name = "Price", height = 600, width = 1024) %>% # MA add_lines(x = df$dates, y = BB$mavg, line = list(width = 3, dash = "5px", color = "#33bbff"), inherit = F, name = "Mov Avg") %>% # CKD add_lines(x = df$dates, y = CKD.pos, yaxis = "y2", line = list(width = 1, color = "black"), fill = "tozeroy", fillcolor = "#47d147", inherit = FALSE) %>% add_lines(x = df$dates, y = CKD.neg, yaxis = "y2", line = list(width = 1, color = "black"), fill = "tozeroy", fillcolor = "#ff6666", inherit = FALSE) %>% layout( plot_bgcolor = "rgb(250,250,250)", xaxis = list(title = "", domain = c(0,0.95), rangeslider = list(visible = F), rangeselector = rangeselectorlist), yaxis = list(domain = c(0.22, 0.9)), yaxis2 = list(domain = c(0, 0.18), side = "right"), showlegend = F, annotations = list( list(x = 0, y = 1, xanchor = "left", yanchor = "top", xref = "paper", yref = "paper", text = paste0("<b>Microsoft</b>"), font = list(size = 30, family = "serif"), showarrow = FALSE), list(x = 0.8, y = 0.95, xanchor = "left", yanchor = "top", xref = "paper", yref = "paper", text = paste0("[", paste(range(df$dates),collapse = " / "), "]"), font = list(size = 15, family = "serif"), showarrow = FALSE), list(x = 0, y = 0.18, xanchor = "left", yanchor = "top", xref = "paper", yref = "paper", text = paste0("<b>Chaikin Money Flow</b>"), font = list(size = 15, family = "serif"), showarrow = FALSE) ) )
Base candlestick chart with MACD
macd <- data.frame(TTR::MACD(df$Close, 12, 26, 9)) macd$diff <- macd$macd - macd$signal plot_ly(df, type = "candlestick", x = ~dates, open = ~Open, high = ~High, low = ~Low, close = ~Close, yaxis = "y", increasing = list(line = list(color = "#455D7A")), decreasing = list(line = list(color = "#F95959")), name = "Price", height = 600, width = 1024) %>% # MA add_lines(x = df$dates, y = BB$mavg, line = list(width = 3, dash = "5px", color = "#33bbff"), inherit = F, name = "Mov Avg") %>% # MACD add_lines(x = df$dates, y = macd$macd, yaxis = "y2", line = list(width = 1, color = "#8c8c8c"), inherit = FALSE) %>% add_lines(x = df$dates, y = macd$signal, yaxis = "y2", line = list(width = 1, color = "#ff6666"), inherit = FALSE) %>% add_bars(x = df$dates, y = macd$diff, marker = list(color = "#bfbfbf"), yaxis = "y2", inherit = FALSE) %>% layout( plot_bgcolor = "rgb(250,250,250)", xaxis = list(title = "", domain = c(0,0.95), rangeslider = list(visible = F), rangeselector = rangeselectorlist), yaxis = list(domain = c(0.22, 0.9)), yaxis2 = list(domain = c(0, 0.18), side = "right"), showlegend = F, annotations = list( list(x = 0, y = 1, xanchor = "left", yanchor = "top", xref = "paper", yref = "paper", text = paste0("<b>Microsoft</b>"), font = list(size = 30, family = "serif"), showarrow = FALSE), list(x = 0.8, y = 0.95, xanchor = "left", yanchor = "top", xref = "paper", yref = "paper", text = paste0("[", paste(range(df$dates),collapse = " / "), "]"), font = list(size = 15, family = "serif"), showarrow = FALSE), list(x = 0, y = 0.18, xanchor = "left", yanchor = "top", xref = "paper", yref = "paper", text = paste0("<b>MACD (12, 26, 9)</b>"), font = list(size = 15, family = "serif"), showarrow = FALSE) ) )
To leave a comment for the author, please follow the link and comment on their blog: R – Modern Data.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.