Skip to content
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

Stock charts #10

Closed
ziyadsaeed opened this issue Jan 14, 2016 · 9 comments
Closed

Stock charts #10

ziyadsaeed opened this issue Jan 14, 2016 · 9 comments
Milestone

Comments

@ziyadsaeed
Copy link

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
Copy link
Owner

jbkunst commented Jan 14, 2016

@jbkunst jbkunst added this to the v0.2.0 milestone Jan 18, 2016
@jbkunst
Copy link
Owner

jbkunst commented Jan 19, 2016

@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
Copy link

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
Copy link
Owner

jbkunst commented Jan 19, 2016

@TonyDIRL

You pass an xts objects transforming the data, same as #8 (comment).

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
Copy link

Awesome, I confirm it works.

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

@jbkunst
Copy link
Owner

jbkunst commented Jan 19, 2016

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

@jbkunst
Copy link
Owner

jbkunst commented Jan 20, 2016

@TonyDIRL
Copy link

Awesome, that is really great.

@jbkunst
Copy link
Owner

jbkunst commented Jan 21, 2016

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

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

No branches or pull requests

3 participants