← back

Implement Dollar Bars Sampling

#301 · Machine Learning · Medium

⊣ Solve on deep-ml.com

Problem

Implement dollar bars sampling for financial time series. Dollar bars sample a new bar every time a fixed dollar volume threshold (price * volume) is reached.

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
40
41
42
43
def dollar_bars(trades: list[dict], dollar_threshold: float) -> list[dict]:
    """
    trades: list of dicts with keys 'price' and 'volume'
    dollar_threshold: cumulative dollar volume required to form a bar
    """
    bars = []
    current_prices = []
    current_volume = 0.0
    current_dollar_volume = 0.0

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

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

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

    return bars

Explanation

  1. For each trade, accumulate the dollar volume (price * volume).
  2. When cumulative dollar volume reaches the threshold, emit a bar and reset.
  3. Dollar bars normalize for the dollar value exchanged, not just the number of shares. A stock at 100trading1000sharescarriesmoreinformationthanastockat100 trading 1000 shares carries more information than a stock at 1 trading 1000 shares.
  4. Dollar bars are considered the most robust of the three (tick, volume, dollar) for financial ML, as they account for both volume and price changes.

Complexity

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