# The Open Deviation Bar — A Complete Explanation

**From first principles to bit-exact empirical proof. All numbers verified against 866,341 bars in the production ClickHouse oracle on bigblack (BTCUSDT, 2018-01-16 → 2026-03-13).**

---

## Part I — Layman's Explanation

### What is a "bar"?

Before understanding an Open Deviation Bar, you need to understand what a bar is at all.

In financial markets, a **bar** (also called a candle) is a snapshot of price activity compressed into a single record. It has four prices:

- **Open**: the first price when the bar started
- **High**: the highest price reached during the bar's life
- **Low**: the lowest price reached during the bar's life
- **Close**: the last price when the bar ended

The question is: _what decides when a bar ends and the next one starts?_

Traditional bar types answer this differently:

| Bar Type        | Ends When...                           | Problem                                    |
| --------------- | -------------------------------------- | ------------------------------------------ |
| Time bar (1min) | Clock ticks over                       | Most bars are "nothing happened" noise     |
| Tick bar        | N trades occur                         | Treats a $1 trade the same as a $1M trade  |
| Volume bar      | N units of volume accumulate           | Ignores directionality — up vs down volume |
| Range bar       | Price moves N dollars from high or low | Asymmetric; anchors drift as price moves   |

### The Open Deviation Bar: a trip-wire anchored to the open

An Open Deviation Bar ends when price moves a fixed **percentage** away from **where that bar started (the open price)**.

Specifically, a bar starts at some price `O` (the open). It then waits for price to move **at least 0.25%** (or whatever the configured threshold is) away from `O` — either up or down. The moment that happens, the bar closes and the next one starts.

Think of it as a trip-wire:

- When the bar opens at `O`, two invisible lines are drawn:
  - An **upper line** at `O × 1.0025` (0.25% above open)
  - A **lower line** at `O × 0.9975` (0.25% below open)
- Every incoming trade is checked: has price touched either line?
- **The instant price touches or crosses either line**, the bar closes at that price

This is why it is called an "Open Deviation Bar": it measures **deviation from the open**, not from any dynamic high/low.

### Why this is useful

**It is symmetric.** An up-bar and a down-bar represent exactly the same magnitude of price movement, just in opposite directions. Classic range bars are not symmetric — they reset their anchor whenever high or low changes.

**It is noise-adaptive.** When BTC is at $10,000, a 0.25% threshold is $25. When BTC is at $70,000, the same 0.25% threshold is $175. The bar always represents "a meaningful percentage move," regardless of absolute price level. A time-based 1-minute bar cannot do this — when markets are slow, 95% of bars are meaningless.

**It preserves market structure.** Each bar spans a coherent period of price activity. You don't get 1,000 empty bars at 3 AM and then miss the entire flash crash in a single overcrowded bar.

**It is anchored to information arrival.** A new bar starts the moment the previous threshold is breached. The very first trade of the new bar sets the open, and the clock starts fresh. This mirrors how market participants actually react: a 0.25% move means something happened; the market's next move is a fresh decision from that new level.

---

## Part II — Precise Definition

### Threshold notation

Thresholds are stored in **decimal basis points (dbps)**:

```
1 dbps = 0.00001 = 0.001%
250 dbps = 0.0025 = 0.25%
500 dbps = 0.0050 = 0.50%
750 dbps = 0.0075 = 0.75%
1000 dbps = 0.0100 = 1.00%
```

In formula: `threshold_fraction = threshold_decimal_bps / 100000`

### Bar formation rules

Let `O` = open price of the current bar, `T` = `threshold_decimal_bps / 100000`.

**Upper threshold line**: `U = O × (1 + T)`
**Lower threshold line**: `L = O × (1 - T)`

For each incoming aggTrade at price `P`:

```
if P >= U  →  bar closes UP   at P  (close = P ≥ U)
if P <= L  →  bar closes DOWN at P  (close = P ≤ L)
```

The condition is **inclusive** (`>=` / `<=`): price touching the line closes the bar immediately.

After closure, the next bar opens at the **very next aggTrade's price**. The open of bar `i+1` is not the close of bar `i` — it is the first trade that arrived after bar `i` closed.

### Continuous streaming (Aion mode)

In production, bars are constructed as a continuous stream with no resets. The processor maintains state across all time boundaries, producing an unbroken chain of bars. There are no artificial reset points and no incomplete "orphan" bars.

The processor can resume from any point using the checkpoint API (`create_checkpoint()` / `from_checkpoint()`), and in production resumes from the last committed trade ID in ClickHouse.

### Stathera trade-ID continuity

Every bar records the `first_agg_trade_id` and `last_agg_trade_id` of the aggTrades it consumed. For any two consecutive bars:

```
bar[i].last_agg_trade_id + 1 = bar[i+1].first_agg_trade_id
```

This invariant (called **Stathera**) means the bars form an unbroken chain: not a single aggTrade is skipped, and not a single one is double-counted across bars.

---

## Part III — Bit-Exact Oracle Examples

All data below is queried live from the production ClickHouse oracle on bigblack. Every number is verifiable by re-running the exact SQL shown.

### Scale of the dataset

```sql
SELECT threshold_decimal_bps, count() AS total_bars,
       median(duration_us) / 1000000 AS median_duration_s,
       avg(agg_record_count) AS avg_trades_per_bar
FROM opendeviationbar_cache.open_deviation_bars FINAL
WHERE symbol = 'BTCUSDT'
GROUP BY threshold_decimal_bps ORDER BY threshold_decimal_bps
```

| Threshold | Total Bars | Median Duration | Avg Trades/Bar | Date Range              |
| --------- | ---------- | --------------- | -------------- | ----------------------- |
| 250 dbps  | 866,341    | 22 s            | 4,499          | 2018-01-16 → 2026-03-13 |
| 500 dbps  | 205,897    | 125 s           | 18,931         | 2018-01-16 → 2026-03-13 |
| 750 dbps  | 89,556     | 316 s           | 43,523         | 2018-01-16 → 2026-03-13 |
| 1000 dbps | 49,501     | 602 s           | 78,739         | 2018-01-16 → 2026-03-13 |

Observations from first principles:

- Doubling the threshold (250→500) reduces bar count by ~4.2×. Bars are rarer at higher thresholds because price must move further.
- Larger thresholds consume more trades per bar (4,499 → 78,739) because each bar spans a longer, more eventful price journey.

---

### Example 1 — An UP bar that closed exactly at the threshold

This bar from 2018-02-17 is the simplest possible case: price opened, rose precisely to the threshold line, and triggered closure at that exact tick. No overshoot.

**Query:**

```sql
SELECT symbol, threshold_decimal_bps, open, high, low, close,
       round(open * (1 + threshold_decimal_bps / 100000.0), 2) AS threshold_line,
       close - round(open * (1 + threshold_decimal_bps / 100000.0), 2) AS overshoot_dollars,
       agg_record_count, individual_trade_count, duration_us,
       first_agg_trade_id, last_agg_trade_id,
       last_agg_trade_id - first_agg_trade_id + 1 AS trade_id_span,
       toDateTime(open_time_ms / 1000, 'UTC') AS bar_open_utc,
       toDateTime(close_time_ms / 1000, 'UTC') AS bar_close_utc,
       (close/open - 1.0) / (threshold_decimal_bps / 100000.0) AS deviation_ratio
FROM opendeviationbar_cache.open_deviation_bars FINAL
WHERE symbol = 'BTCUSDT' AND threshold_decimal_bps = 250
  AND first_agg_trade_id = 16515649
```

**Result:**

| Field                    | Value                                    |
| ------------------------ | ---------------------------------------- |
| `symbol`                 | `BTCUSDT`                                |
| `threshold_decimal_bps`  | `250` (= 0.25%)                          |
| `open`                   | `$10,680.00`                             |
| `high`                   | `$10,706.70`                             |
| `low`                    | `$10,677.96`                             |
| `close`                  | `$10,706.70`                             |
| `threshold_line`         | `$10,706.70` = 10,680 × 1.0025           |
| `overshoot_dollars`      | `$0.00` ← **landed exactly on the line** |
| `agg_record_count`       | `785` aggTrades consumed                 |
| `individual_trade_count` | `924` individual fills                   |
| `duration_us`            | `257,513,000 µs` = **4 min 17 sec**      |
| `first_agg_trade_id`     | `16,515,649`                             |
| `last_agg_trade_id`      | `16,516,433`                             |
| `trade_id_span`          | `785` = (16,516,433 − 16,515,649 + 1)    |
| `bar_open_utc`           | `2018-02-17 20:28:09 UTC`                |
| `bar_close_utc`          | `2018-02-17 20:32:27 UTC`                |
| `deviation_ratio`        | `1.0000000000000675` ≈ exactly 1.0       |

**What this proves:**

```
O = $10,680.00
T = 250/100000 = 0.0025

Upper line = 10,680 × (1 + 0.0025)
           = 10,680 × 1.0025
           = $10,706.70

close = $10,706.70  ←  price touched the line exactly
high  = $10,706.70  ←  the threshold price was the highest price in the bar
                        (price arrived there and immediately triggered closure)

overshoot = $10,706.70 − $10,706.70 = $0.00
```

The `high = close = threshold_line` combination means: price climbed steadily, touched the upper line, and the bar snapped shut. One tick further up was never traded — the algorithm is `>=`, not `>`.

The `deviation_ratio = 1.0000000000000675` is IEEE 754 float64 for exactly 1.0 (the 6.75e-16 is numerical precision noise, 14 decimal places of accuracy).

---

### Example 2 — A DOWN bar that closed exactly at the threshold

**Result (2018-02-12 02:18:19 UTC):**

| Field                | Value                                    |
| -------------------- | ---------------------------------------- |
| `open`               | `$8,404.00`                              |
| `high`               | `$8,412.00`                              |
| `low`                | `$8,382.99`                              |
| `close`              | `$8,382.99`                              |
| `threshold_line`     | `$8,382.99` = 8,404 × (1 − 0.0025)       |
| `overshoot_dollars`  | `$0.00` ← **landed exactly on the line** |
| `agg_record_count`   | `143` aggTrades                          |
| `duration_us`        | `21,958,000 µs` = **21.9 seconds**       |
| `first_agg_trade_id` | `14,237,339`                             |
| `last_agg_trade_id`  | `14,237,481`                             |
| `trade_id_span`      | `143`                                    |
| `bar_open_utc`       | `2018-02-12 02:18:19 UTC`                |
| `bar_close_utc`      | `2018-02-12 02:18:41 UTC`                |
| `deviation_ratio`    | `1.000000000000023` ≈ exactly 1.0        |

**What this proves:**

```
O = $8,404.00
T = 250/100000 = 0.0025

Lower line = 8,404 × (1 − 0.0025)
           = 8,404 × 0.9975
           = $8,382.99

close = $8,382.99  ←  price touched the lower line exactly
low   = $8,382.99  ←  the threshold was the lowest price reached

overshoot = $8,382.99 − $8,382.99 = $0.00
```

The fast 21.9-second duration shows how violently price can move — this bar consumed 143 aggTrades in under half a minute before hitting the 0.25% floor.

---

### Example 3 — A Stathera chain: six consecutive bars, 2018-02-17

This shows the unbroken trade-ID chain across six consecutive bars on the same UTC day. Every aggTrade is accounted for — none skipped, none double-counted.

**Query:**

```sql
SELECT open, high, low, close, agg_record_count,
       first_agg_trade_id, last_agg_trade_id,
       multiIf(close > open, 'UP', close < open, 'DOWN', 'FLAT') AS direction,
       round(duration_us / 1000000.0, 1) AS duration_s,
       toDateTime(bar_open_utc, 'UTC'), toDateTime(bar_close_utc, 'UTC')
FROM opendeviationbar_cache.open_deviation_bars FINAL
WHERE symbol = 'BTCUSDT' AND threshold_decimal_bps = 250
  AND open_time_ms BETWEEN ... AND ...  -- 2018-02-17 20:00–22:00 UTC
ORDER BY first_agg_trade_id ASC LIMIT 6
```

**Result:**

| #   | Direction | Open       | Close      | Duration | Trades | first_agg_trade_id | last_agg_trade_id | Continuity Check              |
| --- | --------- | ---------- | ---------- | -------- | ------ | ------------------ | ----------------- | ----------------------------- |
| 1   | UP        | $10,678.00 | $10,705.00 | 150.1 s  | 289    | **16,513,664**     | **16,513,952**    | — (first in chain)            |
| 2   | DOWN      | $10,705.00 | $10,678.10 | 739.6 s  | 1,696  | **16,513,953**     | **16,515,648**    | 16,513,952 + 1 = 16,513,953 ✓ |
| 3   | UP        | $10,680.00 | $10,706.70 | 257.5 s  | 785    | **16,515,649**     | **16,516,433**    | 16,515,648 + 1 = 16,515,649 ✓ |
| 4   | UP        | $10,707.00 | $10,734.00 | 559.7 s  | 1,327  | **16,516,434**     | **16,517,760**    | 16,516,433 + 1 = 16,516,434 ✓ |
| 5   | UP        | $10,734.44 | $10,761.53 | 212.5 s  | 974    | **16,517,761**     | **16,518,734**    | 16,517,760 + 1 = 16,517,761 ✓ |
| 6   | DOWN      | $10,762.00 | $10,735.05 | 654.7 s  | 1,833  | **16,518,735**     | **16,520,567**    | 16,518,734 + 1 = 16,518,735 ✓ |

**Stathera invariant verified:** `last_agg_trade_id[i] + 1 = first_agg_trade_id[i+1]` holds for all 5 consecutive pairs. Every single aggTrade from trade #16,513,664 to #16,520,567 appears in exactly one bar.

**The open-to-open chain:** Bar 3 (`open = $10,680`) is our bit-exact threshold-touch example above. Notice bar 3's open `$10,680` is close to but not equal to bar 2's close `$10,678.10` — this is correct. The close of bar 2 (the last trade that triggered the DOWN breach) is **not** the open of bar 3. Bar 3's open is the **next** aggTrade that arrived after bar 2 closed: the very first trade of trade ID 16,515,649 which happened to be at `$10,680`.

---

### Example 4 — The >= proof: scale of exact-threshold touches

We established with two individual bars above that `>=` (not `>`) is the breach condition. Here is the population-level evidence:

```sql
SELECT
    countIf(close > open AND (close/open - 1.0) / (threshold_decimal_bps / 100000.0) >= 1.0
            AND (close/open - 1.0) / (threshold_decimal_bps / 100000.0) < 1.0001) AS up_at_exact_threshold,
    countIf(close < open AND (1.0 - close/open) / (threshold_decimal_bps / 100000.0) >= 1.0
            AND (1.0 - close/open) / (threshold_decimal_bps / 100000.0) < 1.0001) AS down_at_exact_threshold,
    min(multiIf(close > open AND (close/open - 1.0) / (threshold_decimal_bps / 100000.0) >= 0.999,
                (close/open - 1.0) / (threshold_decimal_bps / 100000.0), NULL)) AS min_up_ratio
FROM opendeviationbar_cache.open_deviation_bars FINAL
WHERE symbol = 'BTCUSDT' AND threshold_decimal_bps = 250
```

| Metric                                            | Value                               |
| ------------------------------------------------- | ----------------------------------- |
| UP bars closing within 0.01% of exact threshold   | **79,585** (out of 436,289) = 18.2% |
| DOWN bars closing within 0.01% of exact threshold | **82,782** (out of 430,046) = 19.3% |
| Minimum UP deviation ratio (all 866K bars)        | `0.99999999999997...` ≈ `1.0`       |

**Interpretation:** 18–19% of all threshold-breaching bars landed within 0.01% of the exact threshold price. The absolute minimum ratio across 866,341 bars is floating-point-indistinguishable from exactly 1.0.

If the condition were strictly `>` (greater than, not equal), a bar could never close at exactly `open × (1 + threshold)`. Price would have to exceed it by at least one tick ($0.01 at BTC prices). That would produce a minimum ratio of approximately `1 + 0.01/(BTC_price × threshold)` ≈ `1.00004` for BTC at $25,000. Instead we see `1.0000000000000675` — a number that is, to 14 significant figures, exactly 1. The condition is `>=`.

---

## Part IV — Summary Reference

### The three outcomes of a bar

| Outcome  | Condition                       | `close`     | `duration_us` | Meaning                              |
| -------- | ------------------------------- | ----------- | ------------- | ------------------------------------ |
| **UP**   | price touched `open × (1 + T)`  | = threshold | > 0           | Buyers won; full upward move made    |
| **DOWN** | price touched `open × (1 − T)`  | = threshold | > 0           | Sellers won; full downward move made |
| **Flat** | `close = open` (extremely rare) | = open      | 0 or > 0      | Price frozen at open (degenerate)    |

### Key formula sheet

```
threshold_fraction    = threshold_decimal_bps / 100_000
upper_threshold_line  = open × (1 + threshold_fraction)
lower_threshold_line  = open × (1 − threshold_fraction)

bar closes UP   when: any price P ≥ upper_threshold_line
bar closes DOWN when: any price P ≤ lower_threshold_line

Stathera invariant: bar[i].last_agg_trade_id + 1 = bar[i+1].first_agg_trade_id
```

### Threshold quick reference (BTCUSDT production, bigblack)

| Threshold | Fraction | Bars (8+ years) | Median Duration | Avg Trades/Bar |
| --------- | -------- | --------------- | --------------- | -------------- |
| 250 dbps  | 0.25%    | 866,341         | 22 seconds      | 4,499          |
| 500 dbps  | 0.50%    | 205,897         | 125 seconds     | 18,931         |
| 750 dbps  | 0.75%    | 89,556          | 316 seconds     | 43,523         |
| 1000 dbps | 1.00%    | 49,501          | 602 seconds     | 78,739         |

### How it differs from classic range bars

The term "range bar" typically refers to the Nicolellis bar (1995), which closes when `High − Low ≥ N` for some fixed dollar amount `N`. That is fundamentally different:

| Property              | Open Deviation Bar                         | Classic Nicolellis Range Bar              |
| --------------------- | ------------------------------------------ | ----------------------------------------- |
| Anchor                | Fixed to **open** of each bar              | Shifts with high/low as bar evolves       |
| Direction             | Bidirectional: only up or only down        | Symmetric around high/low range           |
| Price relativity      | Percentage of open (scale-invariant)       | Fixed dollar amount (not scale-invariant) |
| BTC@$10K vs BTC@$70K  | Same 0.25% threshold, proportionally equal | Same $N, proportionally 7× smaller        |
| Bar can go both ways? | No — close direction is determined once    | Yes — price can oscillate inside range    |

---

_Document generated 2026-03-13. All examples verified by direct ClickHouse query against the production oracle on bigblack. Raw trade data source: Binance SPOT aggTrades (`data.binance.vision/data/spot/daily/aggTrades/BTCUSDT/`)._
