← back

Implement Tick Bars Sampling

#299 · Financial ML · Medium

⊣ Solve on deep-ml.com

Problem

Implement tick bars sampling for financial time series. Tick bars sample a new bar (OHLCV summary) every N ticks (trades), providing a time-invariant way to analyze market microstructure data.

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import numpy as np

def tick_bars(trades: list[dict], tick_size: int) -> list[dict]:
    """
    trades: list of dicts with keys 'price' and 'volume'
    tick_size: number of ticks per bar
    """
    bars = []
    n = len(trades)

    for i in range(0, n, tick_size):
        chunk = trades[i:i + tick_size]
        if len(chunk) == 0:
            break

        prices = [t["price"] for t in chunk]
        volumes = [t["volume"] for t in chunk]

        bar = {
            "open": prices[0],
            "high": max(prices),
            "low": min(prices),
            "close": prices[-1],
            "volume": sum(volumes),
            "num_ticks": len(chunk),
        }
        bars.append(bar)

    return bars

Explanation

  1. Divide the trade sequence into non-overlapping chunks of tick_size trades each.
  2. For each chunk, compute OHLCV: open is the first price, high/low are the max/min, close is the last price, and volume is the sum.
  3. Tick bars normalize for trading activity: during high-activity periods (many trades per minute), more bars are created, providing higher resolution. During low-activity periods, fewer bars are produced.
  4. This approach is preferred over time bars in financial ML because it better reflects information arrival.

Complexity

  • Time: O(n) where n is the number of trades
  • Space: O(n / tick_size) for the output bars