1

Hi I have data in xts format like this

> ITXxts 

  Time                  Price   Volume 
 2014-07-18 09:00:00    67.63   460
 2012-04-27 09:00:00    67.63   73
 2012-04-27 09:00:00    67.63   85
 2012-04-27 09:00:01    67.63   3
 2012-04-27 09:01:03    67.79   1
 2012-04-27 09:01:16    67.64   91
 2012-04-27 09:05:50    67.83   75

Now aggregated the series into 5 minute blocks

 tsagg5min =aggregatets(ITXxts,on="minutes",k=5)

 2014-07-18 09:05:00    67.45   43
 2014-07-18 09:10:00    67.82   12
 2014-07-18 09:15:00    67.91   341
 2014-07-18 09:20:00    67.70   275

chartSeries(tsagg5min) enter image description here

Now my question is I want to plot it in the OHLC format in such a way that It calculates the the Open High Low and Close of each 5 minute chunk in which the data was aggregated

Panchacookie
  • 427
  • 1
  • 6
  • 22
  • What is the ticker symbol used to get this data? Kind of hard for people to help you when you don't provide a [good example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – nrussell Jul 19 '14 at 15:10
  • 1
    What package does `aggregatets` come from and what does it do? It's hard to tell from your example. – GSee Jul 19 '14 at 16:42
  • Hi sorry for not posting the full code `aggregatets` function is from the Highfrequency package it aggregates unequally spaced tick HFT tick data – Panchacookie Jul 19 '14 at 21:25
  • Great question - I'd love to see OHLC plots for this type of data too. I was in fact wondering if there was a way to do it in lattice, which I find a much better plotting tool. Would love to see any more suggestions. – user1480926 Jul 19 '14 at 22:56
  • I think GSee answers what you want nicely. In regard to the plotting aspect, you might want to consider using `chart_Series`, instead of `chartSeries`. The plots look nicer. Even though the documentation says these `_` functions are in alpha, they work pretty well, except for automatically drawing gridlines on the plot for data on time scales less than a day. (But you can edit the source to fix these gridline issues) – FXQuantTrader Jul 20 '14 at 05:50
  • @user3311229 What is it about `aggregatets` that you like? Is it that it creates a strictly regular time series? I'm not familiar with the function, but it looks like it just calls `period.apply`, then makes the series strictly regular by using the idiomatic "`na.locf` on the results of a `merge` with a zero-width strictly regular xts." Maybe if you could say in words what you want (and be more specific than "aggregate," because that can mean anything), or provide the desired output that agrees with the shown input (or explain why your current output has values that aren't in the input) – GSee Jul 20 '14 at 14:28
  • I want the data to be like this – Panchacookie Jul 21 '14 at 23:23
  • `Time` `Open`( The opening price in the 5 min session ) `High` ( The highest price in the 5 min session ) `Low ` ( The lowest price in the 5 min session ) `Close`( The closing price in the 5 min session ) `Volume` (average vol in the 5 min session) – Panchacookie Jul 21 '14 at 23:49

1 Answers1

3

The xts package (which is loaded by quantmod) has functions for converting to OHLC bars.

tsagg5min <- to.minutes5(ITXxts)
#tsagg5min <- to.minutes(ITXxts, k=5) # identical
#tsagg5min <- to.period(ITXxts, endpoints(ITXxts, "minutes", k=5)) # identical

chartSeries(tsagg5min)
GSee
  • 48,880
  • 13
  • 125
  • 145