jbkunst / highcharter

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

Question: How to disable aggregation while plotting 1 minute OHLC charts #194

Closed SamoPP closed 8 years ago

SamoPP commented 8 years ago

I am trying to chart 1 minute OHLC bars data using highchartr. The issue is that I do not want to have aggregations, I want to see 1 minute bars exactly as they are in the data for every minute. (of course displayed at first in the top pane for 1, 2, ... days only). How can one disable aggregations and set up the chart s that just one (or a couple) days at the end of 1 minute time series are shown?

Below please find reproducible example of what I am trying to do. First I download 1 min bars data from Google. Then I plot it using quantmod and then using highchartr. Quantmod has no "aggregation" built in so the data is displayed correctly. I have no idea on how to make sure using highchartr that there is no aggergation on the chart and that initially you only display 1 or 2 or N days of 1 minute bars (not aggregated).

# Based on https://github.com/systematicinvestor/SIT/blob/master/R/data.r

getSymbol.intraday.google <- function(Symbol, interval=60, # 60 seconds period="1d", Exchange=NULL ) {

download Key Statistics from yahoo

url <- "" if (!is.null(Exchange)) { url <- paste('http://www.google.com/finance/getprices?q=', Symbol, '&x=', Exchange, '&i=', interval, '&p=', period, '&f=', 'd,o,h,l,c,v', sep='') } else { url <- paste('http://www.google.com/finance/getprices?q=', Symbol, '&i=', interval, '&p=', period, '&f=', 'd,o,h,l,c,v', sep='') }

tt <- tempfile() on.exit(unlink(tt), add=TRUE)

download.file(url, tt, method="auto", mode="wb", quiet=TRUE)

fileContent <- readChar(tt, file.info(tt)$size)

out <- fread(url, skip=7, stringsAsFactors=FALSE, verbose=TRUE)

out <- fread(tt, skip=7, stringsAsFactors=FALSE)

if(ncol(out) < 5) { cat('Error getting data from', url, '\n') return(NULL) }

colnames(out) <- c("Date", "Close", "High", "Low", "Open", "Volume")

date logic

date <- out$Date date.index <- substr(out$Date, 1, 1) == 'a' date <- as.double(gsub('a', '', date)) temp <- NA*date temp[date.index] <- date[date.index]

temp1 <- !is.na(temp)

in case y starts with NA

temp1[1] <- TRUE temp <- temp[cummax((1:length(temp))*temp1 )]

date <- temp + date*interval date[date.index] <- temp[date.index] class(date) <- c("POSIXt", "POSIXct")

date <- date - (as.double(format(date[1], '%H')) - 9)_60_60 # ... - 9)_60_60 times 60 times 60

ret <- xts(out[, -1, with=FALSE], order.by=date)

ret <- ret[, c("Open", "High", "Low", "Close", "Volume")] colnames(ret) <- paste(Symbol, colnames(ret), sep=".")

return(ret)

}

library(quantmod) library(data.table) library(highcharter)

SPY <- getSymbol.intraday.google("SPY", 60, "30d") head(SPY) tail(SPY)

subset <- "2016-09" chart_Series(SPY, subset=subset) add_Vo()

SPY.SMA.10 <- SMA(Cl(SPY), n=10) SPY.SMA.200 <- SMA(Cl(SPY), n=200) SPY.RSI.14 <- RSI(Cl(SPY), n=14) SPY.RSI.SellLevel <- xts(rep(70, NROW(SPY)), index(SPY)) SPY.RSI.BuyLevel <- xts(rep(30, NROW(SPY)), index(SPY))

highchart() %>%

create axis :)

hc_yAxis_multiples( list(title = list(text = NULL), height = "45%", top = "0%"), list(title = list(text = NULL), height = "25%", top = "47.5%", opposite = TRUE), list(title = list(text = NULL), height = "25%", top = "75%") ) %>%

series :D

hc_add_series_ohlc(SPY, yAxis = 0, name = "SPY") %>% hc_add_series_xts(SPY.SMA.10, yAxis = 0, name = "Fast MA") %>% hc_add_series_xts(SPY.SMA.200, yAxis = 0, name = "Slow MA") %>% hc_add_series_xts(SPY$SPY.Volume, color = "gray", yAxis = 1, name = "Volume", type = "column") %>% hc_add_series_xts(SPY.RSI.14, yAxis = 2, name = "Osciallator") %>% hc_add_series_xts(SPY.RSI.SellLevel, color = "red", yAxis = 2, name = "Sell level", enableMouseTracking = FALSE) %>% hc_add_series_xts(SPY.RSI.BuyLevel, color = "blue", yAxis = 2, name = "Buy level", enableMouseTracking = FALSE) %>%

I <3 themes

hc_add_theme(hc_theme_smpl())

jbkunst commented 8 years ago

Hi @SamoPP ,

Please, Can you write a minimal reproducible example? I can't replicate your example:

Error in setnames(out, spl("Date,Close,High,Low,Open,Volume")) : 
  could not find function "spl"
SamoPP commented 8 years ago

I have updated the reproducible example in my original question. Please have a look now. Thanks.

jbkunst commented 8 years ago

:wink:

54:   date <- date - (as.double(format(date[1], '%H')) - 9)6060

If I found something I tell you.

jbkunst commented 8 years ago

Not sure, but I think rangeSelector can help you:

  ...
  hc_add_theme(hc_theme_smpl()) %>% 
  hc_rangeSelector(
    buttons = list(
      list(type = 'hour', count = 1, text = '1h')
    )
  )

image

(http://www.highcharts.com/stock/demo/intraday-area)

SamoPP commented 8 years ago

So there is no way to fix "bar size" or "bar width" to 1 minute (for example)? Since I want to use this to potentially zoom in and out to look at several days of one minute data (for example I would look at this morning and then want to look for past three days and bar width should stay the same (1 min)...

jbkunst commented 8 years ago

I don't know much.

You need to search starting in www.highcharts.com/www.stackoverflow.com, if there a solution then is probably to implement it in highcharter

SamoPP commented 8 years ago

Also, the From and To fields at the top right corner do not have the possibility to enter time (only date).

SamoPP commented 8 years ago

OK, thanks. Will search and ask on stackoverflow and come back with what I find out. Thanks.

jbkunst commented 8 years ago

@SamoPP You have had any luck in this :wink: ?