← back

Implement Volume Bars Sampling

#300 · Machine Learning · Medium

⊣ Solve on deep-ml.com

Problem

Implement volume bars sampling for financial time series. Volume bars sample a new bar every time a fixed volume threshold is reached, providing bars that normalize for trading volume.

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
30
31
32
33
34
35
36
37
38
39
def volume_bars(trades: list[dict], volume_threshold: float) -> list[dict]:
    """
    trades: list of dicts with keys 'price' and 'volume'
    volume_threshold: cumulative volume required to form a bar
    """
    bars = []
    current_prices = []
    current_volume = 0.0

    for trade in trades:
        current_prices.append(trade["price"])
        current_volume += trade["volume"]

        if current_volume >= volume_threshold:
            bar = {
                "open": current_prices[0],
                "high": max(current_prices),
                "low": min(current_prices),
                "close": current_prices[-1],
                "volume": current_volume,
                "num_ticks": len(current_prices),
            }
            bars.append(bar)
            current_prices = []
            current_volume = 0.0

    # Handle remaining trades
    if current_prices:
        bar = {
            "open": current_prices[0],
            "high": max(current_prices),
            "low": min(current_prices),
            "close": current_prices[-1],
            "volume": current_volume,
            "num_ticks": len(current_prices),
        }
        bars.append(bar)

    return bars

Explanation

  1. Accumulate trade volumes until the cumulative volume meets or exceeds the threshold.
  2. When the threshold is reached, emit a bar with OHLCV statistics and reset the accumulator.
  3. Any remaining trades after the last full bar form a partial bar.
  4. Volume bars provide a more uniform information content per bar compared to time bars, since periods with high volume (and thus more information) produce more bars.

Complexity

  • Time: O(n) where n is the number of trades
  • Space: O(n) worst case for price accumulation within a single bar