Skip to content

Candlestick charts in R #630

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
debsush opened this issue Jun 19, 2016 · 4 comments
Closed

Candlestick charts in R #630

debsush opened this issue Jun 19, 2016 · 4 comments

Comments

@debsush
Copy link

debsush commented Jun 19, 2016

Hi,

I am new to plotly and was wondering if the following is doable with the package:

  1. Create a candle stick natively as done in the python example below:
    import plotly.plotly as py
    from plotly.tools import FigureFactory as FF
    from datetime import datetime

import pandas.io.data as web

df = web.DataReader("aapl", 'yahoo', datetime(2007, 10, 1), datetime(2009, 4, 1))
fig = FF.create_candlestick(df.Open, df.High, df.Low, df.Close, dates=df.index)
py.iplot(fig, filename='finance/aapl-candlestick', validate=False)

  1. Just like ggplot, can we extend this to plot.xts?
  2. Can we make the ChartSeries function from quantmod interactive with plotly?

Even if it is still not in place, was wondering if you could guide me as to how can I start developing these R charting functions within plotly?

Regards,
SD

@jackparmer
Copy link
Contributor

Hey @debsush -

Have you seen this?
http://moderndata.plot.ly/candlestick-charts-using-plotly-and-quantmod/

image

It might help to get you started.

@debsush
Copy link
Author

debsush commented Jun 21, 2016

Thanks Jack.

I have seen the implementation - it is definitely a good start. While there is a ton of work to be done on this chart which I can take up on my own, what bothers me is the render speed.

TODO:

  1. For 10 yrs and more daily data, feature to aggregate daily data into weekly and monthly data etc.
  2. When zoomed-in on daily data, the current chart continues to show date in MMM YYYY format etc.

All of the above is very much doable but I am a bit concerned about the render speed. Currently a 10 yr daily OHLC data takes 8-12 seconds to render. Compare this with ChartSeries in quantmod, render time is in milliseconds which is so cool.

@timelyportfolio Your feedback will be most welcome on this.

Regards
SD

@cpsievert cpsievert changed the title Implement Python Example in R Candlestick charts in R Jun 22, 2016
@cpsievert
Copy link
Collaborator

When #628 is finished, you'll be able to do something like:

library(plotly)
library(quantmod)
library(dplyr)

prices <- getSymbols("MSFT", auto.assign = F)
dat <- as.data.frame(prices)
dat$date <- index(prices)
names(dat) <- sub("^MSFT\\.", "", names(dat))
# create variable with hovertext displaying values of every variable
vars <- Map(function(x, y) paste0(x, ": ", y), names(dat), dat)
dat$txt <- Reduce(function(x, y) paste0(x, "<br />", y), vars)

plot_ly(dat, x = ~date, xend = ~date, color = ~Close > Open, 
        colors = c("red", "forestgreen"), hoverinfo = "none") %>%
  add_segments(y = ~Low, yend = ~High, line = list(width = 1)) %>%
  add_segments(y = ~Open, yend = ~Close, line = list(width = 3)) %>%
  add_markers(y = ~(Close + Open)/2, text = ~txt, hoverinfo = "text", opacity = 0) %>%
  toWebGL() %>%
  layout(showlegend = FALSE, yaxis = list(title = "Price")) 

@cpsievert
Copy link
Collaborator

Closed via d2bf2a1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants