Building OHLC Bars from Trades and Order Book Data
Summary
The document explains that standard open, high, low, and close bars are constructed from transaction prices. Best bid and ask quotes, quote sizes, and aggregate volume or book-size fields do not provide the actual sequence of trades needed for true OHLC bars. Quotes or their midpoint can serve as approximations, but should be recognized as different from transaction-based bars.
It outlines several sampling approaches: fixed-time bars, fixed-tick bars, fixed-volume bars, and event-based bars triggered by tick or volume imbalance or runs. Time bars are common but may sample information unevenly because trading activity changes over the day. Tick and volume bars aim to better reflect trade activity; the answer claims they can produce return series with improved statistical properties, while giving no supporting study or detailed conditions. More adaptive imbalance and run-based methods are presented as more complex. The example groups transaction ticks and calculates each bar's first, maximum, minimum, and last price, along with total volume. Bar choice depends on the analysis and available transaction data.
Key ideas
- True OHLC bars require transaction prices; order book quotes only offer approximations.
- Time bars group trades by fixed intervals, while tick bars group them by transaction count.
- Volume bars account for differences in the size of trades within a bar.
- Imbalance and tick-run bars use trade patterns to define sampling intervals.
- The example constructs OHLC values from grouped transaction prices and sums their volume.
Tags
Full text
# How do I convert order book data into OHCL( Open,High,Low,close) format?
# How do I convert order book data into OHCL( Open,High,Low,close) format?
The image represents the order book data with columns having following attributes:
a0: Best ASK price (i.e. the lowest posted price at which someone is willing to sell an asset)
b0: Best BID price (i.e. the highest posted price at which someone is willing to buy an asset)
az0: Best ASK size (i.e. the number of lots being offered for selling at the lowest ask price)
bz0: Best BID size (i.e. the number of lots that people are trying to buy at the bid price)
What I want is to convert order book data into OHCL formate (Open, High, Low and Close). The other information that I have is the following:
Features: atv: feature representing one fraction of trading volume ( in number of lots )
btv: feature representing another fraction of trading volume ( in number of lots )
(atv + btv = total number of trades in the day so far)
tbq: sum of all the BID ( buy ) sizes in the market
tsq: sum of all the ASK ( sell ) sizes in the market
All the above variables are for a derivative instrument 2.
## Answer by lagrange103 (score 14)
https://quant.stackexchange.com/a/40112
As Alex C. notes, OHLC bars are meant to be calculated using transaction ticks. However, you could try to make bars from bid/ask individually (or perhaps even the mean of the two as an approximation), but bear in mind that they are not the 'real thing'.
But assuming you acquire transaction data, there are a number of possible methods for forming OHLC bars (roughly in increasing order of quality, but also implementation difficulty).
- Time bars – this is the default method. Essentially, you decide on a fixed amount of time (for example we will use 1-min bars), then split your dataset into subsets corresponding to every 1 minute interval. Each 1 minute interval will have a different number of ticks. The first tick in each 1 minute bar is the Open, the last tick is the 'close', and high/low are self-explanatory. Although time bars are the most popular, time-sampled series often have poor statistical properties like serial correlation, non-normality etc.
- Tick bars – these sample every time x transactions occur (e.g every 1000 ticks). The main advantage over time bars is that market information is not produced at a constant frequency (e.g more trades occur at market open), so time bars do not accurately capture information flow. Tick bars don't suffer from this problem, and empirically produce timeseries with better statistical properties – the resulting returns are closer to i.i.d Gaussians, which is an assumption of many models.
- Volume bars – minor improvement over tick bars, in which you sample every time x units/dollars of the asset are traded: this takes into account the fact that ticks are different sizes. This will allow for better analysis of price-volume action, and return distributions should be closer to i.i.d Gaussians.
- Tick Imbalance bars – getting a little bit more complex now, but essentially these bars sample when there is asymmetric information. Roughly speaking, it samples every time there is a certain level of imbalance in a series of ticks (net buy or sell).
- Volume Imbalance bars – similar to tick imbalance bars, but sample when there is a volume imbalance.
- Tick Runs Bars – sample whenever the sequence of ticks diverges from expectations.
Assuming you have a dataframe of price/volume transaction tick data, here is some example python code (a bit ugly) to form tick bars where `period` is the number of ticks in a bar:
```
all_bars = []
for _, bar in price_volume.groupby(np.arange(len(price_volume)) // period):
open_price = bar['price'][0]
high_price = bar['price'].max()
close_price = bar['price'][-1]
low_price = bar['price'].min()
volume = bar['volume'].sum()
timestamp = bar.index[-1]
all_bars.append([timestamp, open_price,
high_price, close_price,
low_price, volume])
ohlc = pd.DataFrame(all_bars, columns=['timestamp', 'open',
'high', 'low',
'close', 'volume'])
```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.