<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/native-support-for-candlestick-charts-in-plotly-and-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://moderndata.plot.ly/native-support-for-candlestick-charts-in-plotly-and-r/"> R – Modern Data</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>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 <code>quantmod</code> package to retrieve data as well as generate some technical trading signals. </p>
<p>Don’t forget to install the latest version of plotly from github: </p>
<pre>devtools::install_github(&quot;ropensci/plotly&quot;)</pre>

<h3>Syntax</h3>
<p>The syntax is straightforward:</p>
<pre>plot_ly(x = ..., open = ..., high = ..., low = ..., close = ..., type = &quot;candlestick&quot;)</pre>
<p></p>
<h3>Data</h3>

<pre># Get data
stock &lt;- getSymbols(&quot;MSFT&quot;, auto.assign = F)
dts &lt;- index(stock)
df &lt;- data.frame(stock, row.names = NULL)
df$dates &lt;- dts
names(df) &lt;- c(&quot;Open&quot;, &quot;High&quot;, &quot;Low&quot;, &quot;Close&quot;, &quot;Volume&quot;, &quot;Adjusted&quot;, &quot;dates&quot;)

# Subset to after Jan 2016
df &lt;- subset(df, dates > &quot;2016-01-01&quot;)

# Color or volume bars
barcols &lt;- c()
for (i in 1:length(df$dates)) {
  
  if (i == 1) {barcols[i] &lt;- &quot;#F95959&quot;}
  
  if (i > 1) {
    x &lt;- ifelse(df$Close[i] > df$Close[i - 1], &quot;#455D7A&quot;, &quot;#F95959&quot;)
    barcols[i] &lt;- x
  }
}

# Moving Avg line
MA &lt;- runMean(df$Close)

# Range selector
rangeselectorlist = list(
  x = 0, y = 0.9,
  bgcolor = &quot;#0099cc&quot;,
  font = list(color = &quot;white&quot;),
  
  buttons = list(
    list(count = 1, label = &quot;reset&quot;, step = &quot;all&quot;),
    list(count = 1, label = &quot;1yr&quot;, step = &quot;year&quot;, stepmode = &quot;backward&quot;),
    list(count = 3, label = &quot;3 mo&quot;, step = &quot;month&quot;, stepmode = &quot;backward&quot;),
    list(count = 1, label = &quot;1 mo&quot;, step = &quot;month&quot;, stepmode = &quot;backward&quot;),
    list(step = &quot;all&quot;)
  )
)</pre>

<h3>Base candlestick chart with volume panel</h3>

<pre>plot_ly(df, type = &quot;candlestick&quot;,
        x = ~dates,
        open = ~Open, high = ~High, low = ~Low, close = ~Close,
        yaxis = &quot;y&quot;,
        increasing = list(line = list(color = &quot;#455D7A&quot;)),
        decreasing = list(line = list(color = &quot;#F95959&quot;)),
        name = &quot;Price&quot;,
        height = 600, width = 1024) %>%
  
  add_bars(data = df, x = ~dates, y = ~Volume,
           marker = list(color = barcols),
           yaxis = &quot;y2&quot;, inherit = F, name = &quot;Vol&quot;) %>%
  
  add_lines(x = df$dates, y = MA,
            line = list(width = 3, dash = &quot;5px&quot;, color = &quot;#33bbff&quot;),
            inherit = F, name = &quot;Mov Avg&quot;) %>%
  
  layout(
    plot_bgcolor = &quot;rgb(250,250,250)&quot;,
    xaxis = list(title = &quot;&quot;, 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 = &quot;right&quot;),
    
    showlegend = F,
    
    annotations = list(
      list(x = 0, y = 1, xanchor = &quot;left&quot;, yanchor = &quot;top&quot;,
           xref = &quot;paper&quot;, yref = &quot;paper&quot;,
           text = paste0(&quot;&lt;b>Microsoft&lt;/b>&quot;),
           font = list(size = 30, family = &quot;serif&quot;),
           showarrow = FALSE),
      
      list(x = 0.8, y = 0.95, xanchor = &quot;left&quot;, yanchor = &quot;top&quot;,
           xref = &quot;paper&quot;, yref = &quot;paper&quot;,
           text = paste0(&quot;[&quot;, paste(range(df$dates),collapse = &quot; / &quot;), &quot;]&quot;),
           font = list(size = 15, family = &quot;serif&quot;),
           showarrow = FALSE),
      
      list(x = 0, y = 0.18, xanchor = &quot;left&quot;, yanchor = &quot;top&quot;,
           xref = &quot;paper&quot;, yref = &quot;paper&quot;,
           text = paste0(&quot;&lt;b>Volume&lt;/b>&quot;),
           font = list(size = 15, family = &quot;serif&quot;),
           showarrow = FALSE)
    )
  )</pre>
<p><iframe width="450" src="http://plot.ly/~RPlotBot/4352.embed"></iframe></p>
<h3>Base candlestick chart with Bollinger bands</h3>

<pre>BB &lt;- as.data.frame(BBands(df$Close))
plot_ly(df, type = &quot;candlestick&quot;,
        x = ~dates,
        open = ~Open, high = ~High, low = ~Low, close = ~Close,
        yaxis = &quot;y&quot;,
        increasing = list(line = list(color = &quot;#455D7A&quot;)),
        decreasing = list(line = list(color = &quot;#F95959&quot;)),
        name = &quot;Price&quot;,
        height = 600, width = 1024) %>%
  
  add_bars(data = df, x = ~dates, y = ~Volume,
           marker = list(color = barcols),
           yaxis = &quot;y2&quot;, inherit = F, name = &quot;Vol&quot;) %>%
  
  # MA
  add_lines(x = df$dates, y = BB$mavg,
            line = list(width = 3, dash = &quot;5px&quot;, color = &quot;#33bbff&quot;),
            inherit = F, name = &quot;Mov Avg&quot;) %>%
  
  # Upper and Lower bounds
  add_lines(x = df$dates, y = BB$up,
            line = list(width = 1, dash = &quot;5px&quot;, color = &quot;#737373&quot;),
            fill = &quot;tonexty&quot;, fillcolor = &quot;rgba(194, 240, 240, 0.2)&quot;,
            inherit = F, name = &quot;Bollinger&quot;) %>%
  
  add_lines(x = df$dates, y = BB$dn,
            line = list(width = 1, dash = &quot;5px&quot;, color = &quot;#737373&quot;),
            fill = &quot;tonexty&quot;, fillcolor = &quot;rgba(194, 240, 240, 0.2)&quot;,
            inherit = F, name = &quot;Bollinger&quot;) %>%
  
  layout(
    plot_bgcolor = &quot;rgb(250,250,250)&quot;,
    xaxis = list(title = &quot;&quot;, 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 = &quot;right&quot;),
    showlegend = F,
    
    annotations = list(
      list(x = 0, y = 1, xanchor = &quot;left&quot;, yanchor = &quot;top&quot;,
           xref = &quot;paper&quot;, yref = &quot;paper&quot;,
           text = paste0(&quot;&lt;b>Microsoft&lt;/b>&quot;),
           font = list(size = 30, family = &quot;serif&quot;),
           showarrow = FALSE),
      
      list(x = 0.8, y = 0.95, xanchor = &quot;left&quot;, yanchor = &quot;top&quot;,
           xref = &quot;paper&quot;, yref = &quot;paper&quot;,
           text = paste0(&quot;[&quot;, paste(range(df$dates),collapse = &quot; / &quot;), &quot;]&quot;),
           font = list(size = 15, family = &quot;serif&quot;),
           showarrow = FALSE),
      
      list(x = 0, y = 0.18, xanchor = &quot;left&quot;, yanchor = &quot;top&quot;,
           xref = &quot;paper&quot;, yref = &quot;paper&quot;,
           text = paste0(&quot;&lt;b>Volume&lt;/b>&quot;),
           font = list(size = 15, family = &quot;serif&quot;),
           showarrow = FALSE)
    )
  )</pre>
<p><iframe width="450" src="http://plot.ly/~RPlotBot/4352.embed"></iframe></p>
<h3>Base candlestick chart with Chaikin Money Flow</h3>
<p><code>
<pre>CKD &lt;- CMF(df[,c(&quot;High&quot;, &quot;Low&quot;, &quot;Close&quot;)], volume = df$Volume)
CKD.pos &lt;- CKD
CKD.pos[CKD.pos &lt; 0] &lt;- 0

CKD.neg &lt;- CKD
CKD.neg[CKD.neg > 0] &lt;- 0

plot_ly(df, type = &quot;candlestick&quot;,
        x = ~dates,
        open = ~Open, high = ~High, low = ~Low, close = ~Close,
        yaxis = &quot;y&quot;,
        increasing = list(line = list(color = &quot;#455D7A&quot;)),
        decreasing = list(line = list(color = &quot;#F95959&quot;)),
        name = &quot;Price&quot;,
        height = 600, width = 1024) %>%
  
  # MA
  add_lines(x = df$dates, y = BB$mavg,
            line = list(width = 3, dash = &quot;5px&quot;, color = &quot;#33bbff&quot;),
            inherit = F, name = &quot;Mov Avg&quot;) %>%
  
  # CKD
  add_lines(x = df$dates, y = CKD.pos,
            yaxis = &quot;y2&quot;,
            line = list(width = 1, color = &quot;black&quot;),
            fill = &quot;tozeroy&quot;, fillcolor = &quot;#47d147&quot;,
            inherit = FALSE) %>%
  
  add_lines(x = df$dates, y = CKD.neg,
            yaxis = &quot;y2&quot;,
            line = list(width = 1, color = &quot;black&quot;),
            fill = &quot;tozeroy&quot;, fillcolor = &quot;#ff6666&quot;,
            inherit = FALSE) %>%
  
  layout(
    plot_bgcolor = &quot;rgb(250,250,250)&quot;,
    xaxis = list(title = &quot;&quot;, 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 = &quot;right&quot;),
    showlegend = F,
    
    annotations = list(
      list(x = 0, y = 1, xanchor = &quot;left&quot;, yanchor = &quot;top&quot;,
           xref = &quot;paper&quot;, yref = &quot;paper&quot;,
           text = paste0(&quot;&lt;b>Microsoft&lt;/b>&quot;),
           font = list(size = 30, family = &quot;serif&quot;),
           showarrow = FALSE),
      
      list(x = 0.8, y = 0.95, xanchor = &quot;left&quot;, yanchor = &quot;top&quot;,
           xref = &quot;paper&quot;, yref = &quot;paper&quot;,
           text = paste0(&quot;[&quot;, paste(range(df$dates),collapse = &quot; / &quot;), &quot;]&quot;),
           font = list(size = 15, family = &quot;serif&quot;),
           showarrow = FALSE),
      
      list(x = 0, y = 0.18, xanchor = &quot;left&quot;, yanchor = &quot;top&quot;,
           xref = &quot;paper&quot;, yref = &quot;paper&quot;,
           text = paste0(&quot;&lt;b>Chaikin Money Flow&lt;/b>&quot;),
           font = list(size = 15, family = &quot;serif&quot;),
           showarrow = FALSE)
    )
  )</pre>
<p><iframe width="450" src="http://plot.ly/~RPlotBot/4354.embed"></iframe></p>
<h3>Base candlestick chart with MACD</h3>
<p><code>
<pre>macd &lt;- data.frame(TTR::MACD(df$Close, 12, 26, 9))
macd$diff &lt;- macd$macd - macd$signal

plot_ly(df, type = &quot;candlestick&quot;,
        x = ~dates,
        open = ~Open, high = ~High, low = ~Low, close = ~Close,
        yaxis = &quot;y&quot;,
        increasing = list(line = list(color = &quot;#455D7A&quot;)),
        decreasing = list(line = list(color = &quot;#F95959&quot;)),
        name = &quot;Price&quot;,
        height = 600, width = 1024) %>%
  
  # MA
  add_lines(x = df$dates, y = BB$mavg,
            line = list(width = 3, dash = &quot;5px&quot;, color = &quot;#33bbff&quot;),
            inherit = F, name = &quot;Mov Avg&quot;) %>%
  
  # MACD
  add_lines(x = df$dates, y = macd$macd,
            yaxis = &quot;y2&quot;,
            line = list(width = 1, color = &quot;#8c8c8c&quot;),
            inherit = FALSE) %>%
  
  add_lines(x = df$dates, y = macd$signal,
            yaxis = &quot;y2&quot;,
            line = list(width = 1, color = &quot;#ff6666&quot;),
            inherit = FALSE) %>%
  
  add_bars(x = df$dates, y = macd$diff,
           marker = list(color = &quot;#bfbfbf&quot;),
           yaxis = &quot;y2&quot;,
           inherit = FALSE) %>%
  
  layout(
    plot_bgcolor = &quot;rgb(250,250,250)&quot;,
    xaxis = list(title = &quot;&quot;, 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 = &quot;right&quot;),
    showlegend = F,
    
    annotations = list(
      list(x = 0, y = 1, xanchor = &quot;left&quot;, yanchor = &quot;top&quot;,
           xref = &quot;paper&quot;, yref = &quot;paper&quot;,
           text = paste0(&quot;&lt;b>Microsoft&lt;/b>&quot;),
           font = list(size = 30, family = &quot;serif&quot;),
           showarrow = FALSE),
      
      list(x = 0.8, y = 0.95, xanchor = &quot;left&quot;, yanchor = &quot;top&quot;,
           xref = &quot;paper&quot;, yref = &quot;paper&quot;,
           text = paste0(&quot;[&quot;, paste(range(df$dates),collapse = &quot; / &quot;), &quot;]&quot;),
           font = list(size = 15, family = &quot;serif&quot;),
           showarrow = FALSE),
      
      list(x = 0, y = 0.18, xanchor = &quot;left&quot;, yanchor = &quot;top&quot;,
           xref = &quot;paper&quot;, yref = &quot;paper&quot;,
           text = paste0(&quot;&lt;b>MACD (12, 26, 9)&lt;/b>&quot;),
           font = list(size = 15, family = &quot;serif&quot;),
           showarrow = FALSE)
    )
  )</pre>
<p><iframe width="450" src="http://plot.ly/~RPlotBot/4356.embed"></iframe></p>

<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://moderndata.plot.ly/native-support-for-candlestick-charts-in-plotly-and-r/"> R – Modern Data</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/8WFMZ4lF8CE"></a></div></code></code>