# Converting time bars to tick bars or volume bars in python

Recently I've started reading Advances in Financial Machine Learning by Marcos Lopez de Prado. In the second chapter the author defines some essential financial data structures, like tick bars, volume bars, etc. I was wondering how I could transform a series of daily returns of forex data, acquired using yfinance lib for python 3.7, in to any kind of those bars de Prado mentions.

Below, I'll leave the snippet I used to get the data.

import yfinance as yf

df = yf.Ticker("BRL=X").history(period='max').Close.pct_change().dropna()


• I searched the first chapter to see what you are referring to. I can't find the words "tick" or "bars", and "volume" is in an unrelated chart. But "ticks" normally refer to individual quotes and trades anyway, not daily data as you get through Yahoo. May 28 '20 at 4:26
• how to convert tick bars (regular trade data) into volume bars instead? May 28 '20 at 9:53
• @chrisaycock, it's the second chapter, got confused May 28 '20 at 14:09

As mentioned in my comment, tick data is the individual quotes and trades; Yahoo only has daily data. As an analogy, you can always make a high-definition photo more blurry and pixelated, but you can't add detail and definition to a bad picture. Daily data is just an aggregate of individual ticks, so you can't get the individual ticks from daily data.

You will need a different source for tick data, and those are usually commercial. (Vendors sell to professional traders, after all.)

With that said, once you do get some tick data, aggregations are pretty straightforward. I've included some pandas code here for posterity; this assumes a trades Dataframe with price and size columns, indexed by timestamp.

### Time Bars

Just give the frequency you desire. Here is an example of five-minute bars:

trades.groupby(pd.Grouper(freq="5min")).agg({'price': 'ohlc', 'size': 'sum'})


### Tick Bars

We'll define a helper function to round-down to the nearest integer:

def bar(xs, y): return np.int64(xs / y) * y


Then group by the bars of the Dataframe's row number. Here's an example of 10-tick bars:

trades.groupby(bar(np.arange(len(trades)), 10)).agg({'price': 'ohlc', 'size': 'sum'})


### Volume Bars

Group by the bars of the cumulative volume. Here's an example for n shares traded:

trades.groupby(bar(np.cumsum(trades['size']), n)).agg({'price': 'ohlc', 'size': 'sum'}

• is that code converting time bars to volume bars, or tick bars to volume bars? It looks like it is just downsampling low frequency volume data (neither time nor tick bars) to lower frequency volume (which is not what volume bars are) May 30 '20 at 3:19
• Is there a easy way to get groupby "Percentage Change" Bars? Mar 1 at 5:58

a tick is a change in the price, it is not a second or minute at regular tine intervals, it's frequency is driven by market moves whilst daily data is aggregated, therefore by definition it is impossible to deduct tick data from any other time frequency.

to extract this in python, there are multiple ways, I personally use Dukascopy, where you can download free csv extract and feed them easily into your python program using pandas.

• what steps does dukascopy take exactly to make the conversion from tick bars to volume bars? May 28 '20 at 9:55
• as pointed out by chrisaycock in an earlier comment, tick prices and volume are two different things and you can't convert from one to the other, it is like trying to convert an apple into a mushroom
– John
May 28 '20 at 10:27
• so i guess my question shudve been how does dukascopy convert data to tick bars. And how does it convert data to volume bars May 28 '20 at 10:52
• @Jonat, ty for your response, would mind giving a little more info on the proccess plz? I'm really interested in learning from you. May 28 '20 at 14:11