Skip to content
All library documents

Constructing Tick-Count OHLC Bars from Tick Data in R

Article Quant Q&A · Author: Lionel

Summary

The document shows how to aggregate an ordered sequence of trade-price observations into OHLC bars based on a fixed number of ticks rather than elapsed time. It assigns successive observations to bar groups, then calculates each group’s first, maximum, minimum, and last prices as its open, high, low, and close. The example uses groups of 500 ticks and demonstrates assembling the results into a table.

A follow-up example adds timestamps drawn from the source data, makes duplicate timestamps unique for charting, and plots the resulting bars. The method is a compact alternative to time-based aggregation when working with tick data in R. It assumes the observations are sorted chronologically and treats the final incomplete group as a bar, since the number of groups is rounded up. The example uses prices as the input and does not discuss volume bars, trade filtering, session boundaries, missing observations, or validation against a market-data convention; timestamp assignment may therefore need adjustment for a particular dataset or use case.

Key ideas

  • Group chronologically sorted tick observations into consecutive batches of a chosen size.
  • Calculate each batch’s open, high, low, and close from its first, maximum, minimum, and last prices.
  • A rounded-up group count preserves a final bar even when the tick total is not divisible by the target size.
  • Attach suitable timestamps to bars before plotting or further analysis.
  • The example does not address session boundaries or alternative bar definitions such as volume bars.

Tags

Full text
# Create Tick Bars with R


# Create Tick Bars with R












How do I get OHLC bars with tick count (i.e. 500 ticks) instead of time?

I prefer quantmod. Currently I have tick data and can already convert to minute bars using xts.

```
# I'd like this to be 500 ticks, instead of 5 seconds
CL.x <- to.period(CL.tick, "seconds", k=5)
```

## Answer by Enrico Schumann (score 2, accepted)

https://quant.stackexchange.com/a/41643

I don't use `quantmod`, but you can aggregate the data using R's `tapply`. Assume you have your tick data, and these are sorted in time. Let's make up some data.

```
ticks <- cumprod(1 + rnorm(100020, sd = 0.001))
```

Compute the number of bars.

```
n <- ceiling(length(ticks)/500)
bars <- rep(1:n, each = 500)[seq_along(ticks)]
```

Compute open, high, low close for each bar and combine them into a matrix.

```
ohlc.list <- tapply(ticks, bars,
                    function(x) c(x[1], max(x), min(x), x[length(x)]))
ohlc <- do.call(rbind, ohlc.list)
colnames(ohlc) <- c("open", "high", "low", "close")
```

You may now process these bars as you like; perhaps attach a timestamp to each bar, and plot them.

## Answer by Lionel (score 1)

https://quant.stackexchange.com/a/41648

Here's what I ended up using, thanks to Enrico.

My data had CL.ticks (DateTime and Open) e.g. CL.ticks <- CL[c("DateTime", "Open")]

Where DateTime is POSIXct

```
# Combine -----
tickCount = 500
ticks = CL.ticks$Open
## get num bars
n <- ceiling(length(ticks)/tickCount)
bars <- rep(1:n, each = tickCount)[seq_along(ticks)]

## make a bar
ohlc.list <- tapply(ticks, bars, function(x) c(x[1], max(x), min(x), x[length(x)]))
ohlc <- do.call(rbind, ohlc.list)
colnames(ohlc) <- c("open", "high", "low", "close")

# Grab the opening time
bars.dt <- CL$DateTime[seq(1, length(CL$DateTime), tickCount)]
ohlc.df <- data.frame(ohlc)

## Add time to rows
ohlc.df$DateTime <- bars.dt
ohlc.df$DateTime <- make.time.unique(bars.dt, eps=0.00001)
rownames(ohlc.df) = ohlc.df$DateTime
ohlc.df$DateTime = NULL

## Plot
chartSeries(ohlc.df)
```

Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.