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()

  • 1
    $\begingroup$ 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. $\endgroup$ – chrisaycock May 28 at 4:26
  • $\begingroup$ how to convert tick bars (regular trade data) into volume bars instead? $\endgroup$ – develarist May 28 at 9:53
  • 1
    $\begingroup$ @chrisaycock, it's the second chapter, got confused $\endgroup$ – Occhima May 28 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'}
| improve this answer | |
  • $\begingroup$ 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) $\endgroup$ – develarist May 30 at 3:19

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.

| improve this answer | |
  • $\begingroup$ what steps does dukascopy take exactly to make the conversion from tick bars to volume bars? $\endgroup$ – develarist May 28 at 9:55
  • $\begingroup$ 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 $\endgroup$ – John May 28 at 10:27
  • $\begingroup$ so i guess my question shudve been how does dukascopy convert data to tick bars. And how does it convert data to volume bars $\endgroup$ – develarist May 28 at 10:52
  • 1
    $\begingroup$ @Jonat, ty for your response, would mind giving a little more info on the proccess plz? I'm really interested in learning from you. $\endgroup$ – Occhima May 28 at 14:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.