jbkunst / highcharter

R wrapper for highcharts
http://jkunst.com/highcharter/
Other
720 stars 149 forks source link

Stock charts #10

Closed ziyadsaeed closed 8 years ago

ziyadsaeed commented 8 years ago

For future please add stock charting from the highstocks package. It works best for timeseries data. It is like google finance charts http://www.highcharts.com/stock/demo/

jbkunst commented 8 years ago

Yep, it's in https://github.com/jbkunst/highcharter#future-work ;)!

jbkunst commented 8 years ago

@TonyDIRL, @ziyadsaeed

Just an update. I'm just implemented a highstock parameter to get a Highstock instead of Highcharts. Works fine but it seems to be bugged the range selector and I need to do some test with real data via quantmod package.

You can check examples here: http://jkunst.com/highcharter/#time-series-highstock

TonyDIRL commented 8 years ago

Great thank you. So I see that it can handle ts objects

z <- ts(matrix(rnorm(300), 100, 1), start = c(1961, 1), frequency = 12)
highchart(highstock = TRUE) %>%  hc_add_series_ts2(z, color = "#26838E")

How would I pass it an xts object?

ProdData <- xts(runif(100,0,1), seq(as.POSIXct("2016-01-04 08:00:00"), as.POSIXct("2016-01-04 08:01:00"), length = 100),tz="GMT")

Also, is type="candlestick" from highstock exposed?

jbkunst commented 8 years ago

@TonyDIRL

You pass an xts objects transforming the data, same as https://github.com/jbkunst/highcharter/issues/8#issuecomment-171780923.

Theres no a function (like shorcut) to chart candlesticks in a quick way yet. Anyway, as same as all charts in highcharts/highstock you can replicate and make the options needes to plot. For example, if you want to chart this example http://www.highcharts.com/stock/demo/candlestick see the View options button.

Rebember, you can do everithing right know. The actual issue is you need transform your R data in jsonlike format (timestamps , in lists) to use them in highcharter. As I said you I'm working on shortcuts to chart quickly this objects ;)!

I can't run this example 100% because I have firewall issues in my work place. But something like this can work:

require("quantmod")

getSymbols("SPY", src = "google")

time <- time(SPY) %>% 
  zoo::as.Date() %>% 
  as.POSIXct() %>% 
  as.numeric() %>% 
  { 1000 * . }

spy <- SPY[,-5] %>% 
  as.data.frame() %>% 
  {cbind(time, .)} %>% 
  list.parse2()

highchart(highstock = TRUE) %>% 
  hc_add_series(type = "candlestick",
                data = spy,
                name = "SPY",
                dataGrouping = list(
                  units = list(
                    list("week", 1),
                    list("month", c(1,2,3,4,6))
                    )
                  )
                )
TonyDIRL commented 8 years ago

Awesome, I confirm it works.

To be able to combine this with the highcharts boost module would make it very useful

jbkunst commented 8 years ago

Yep it's the idea use that module! Need time to test :smile:

jbkunst commented 8 years ago

@TonyDIRL

http://jkunst.com/highcharter/#xts-objects-and-quantmod-package

Regards,

TonyDIRL commented 8 years ago

Awesome, that is really great.

jbkunst commented 8 years ago

I dont know if the module works on highsotck candlestick series (for example) But it was added:

http://rpubs.com/jbkunst/highcharter-no-boost http://rpubs.com/jbkunst/highcharter-boost

Code:

n <- 50000

x <- sin(4*2*pi*seq(n)/n) + rnorm(n)/10

plot(x)

highchart() %>% 
  hc_title(text = sprintf("Drawin %s points", n)) %>% 
  hc_chart(zoomType = "x") %>% 
  hc_tooltip(valueDecimals = 2) %>% 
  hc_add_series(data = x, lineWidth = 0.5)

Sadly, I cant replicate de scatter example yet. With n = 4999 works, but with n = 5000 produce

highstock.js:252 Uncaught TypeError: Cannot read property 'length' of undefined
library("MASS")

# n <- 4999
n <- 5000

sigma <- matrix(c(10,3,3,2),2,2)
sigma

mvr <- round(mvrnorm(n, rep(0, 2), sigma), 4)

ds <- list.parse2(as.data.frame(mvr))

head(ds)

highchart() %>% 
  hc_xAxis(min = min(mvr[, 1]), max = max(mvr[, 1])) %>% 
  hc_yAxis(min = min(mvr[, 2]), max = max(mvr[, 2])) %>% 
  hc_chart(zoomType = "xy") %>% 
  hc_legend(enabled = FALSE) %>% 
  hc_add_series(data = ds,
                type = "scatter",
                color = 'rgba(152,0,67,0.9)',
                marker = list(radius =  0.8),
                tooltip = list(
                  followPointer = FALSE,
                  pointFormat = '[{point.x:.1f}, {point.y:.1f}]'
                )) %>% 
  hc_plotOptions(series =
                   list(
                     turboThreshold = 5000,
                     enableMouseTracking = FALSE
                   )
  )

This is related to issue #8