clockbars 0.1.0__tar.gz → 1.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {clockbars-0.1.0 → clockbars-1.0.0}/.github/ISSUE_TEMPLATE/feature_request.md +1 -1
- {clockbars-0.1.0 → clockbars-1.0.0}/CHANGELOG.md +26 -22
- {clockbars-0.1.0 → clockbars-1.0.0}/CITATION.cff +4 -4
- {clockbars-0.1.0 → clockbars-1.0.0}/CONTRIBUTING.md +4 -3
- {clockbars-0.1.0 → clockbars-1.0.0}/PKG-INFO +137 -107
- clockbars-1.0.0/README.md +326 -0
- clockbars-1.0.0/docs/hero.png +0 -0
- clockbars-1.0.0/docs/paper.md +387 -0
- clockbars-1.0.0/examples/make_hero.py +173 -0
- {clockbars-0.1.0 → clockbars-1.0.0}/pyproject.toml +1 -1
- {clockbars-0.1.0 → clockbars-1.0.0}/src/clockbars/__init__.py +1 -1
- {clockbars-0.1.0 → clockbars-1.0.0}/tests/test_threshold_bars.py +12 -0
- {clockbars-0.1.0 → clockbars-1.0.0}/uv.lock +1 -1
- clockbars-0.1.0/README.md +0 -296
- clockbars-0.1.0/docs/hero.png +0 -0
- clockbars-0.1.0/docs/paper.md +0 -331
- clockbars-0.1.0/examples/make_hero.py +0 -124
- {clockbars-0.1.0 → clockbars-1.0.0}/.github/ISSUE_TEMPLATE/bug_report.md +0 -0
- {clockbars-0.1.0 → clockbars-1.0.0}/.github/ISSUE_TEMPLATE/config.yml +0 -0
- {clockbars-0.1.0 → clockbars-1.0.0}/.github/PULL_REQUEST_TEMPLATE.md +0 -0
- {clockbars-0.1.0 → clockbars-1.0.0}/.github/dependabot.yml +0 -0
- {clockbars-0.1.0 → clockbars-1.0.0}/.github/workflows/ci.yml +0 -0
- {clockbars-0.1.0 → clockbars-1.0.0}/.github/workflows/release.yml +0 -0
- {clockbars-0.1.0 → clockbars-1.0.0}/.gitignore +0 -0
- {clockbars-0.1.0 → clockbars-1.0.0}/LICENSE +0 -0
- {clockbars-0.1.0 → clockbars-1.0.0}/src/clockbars/builder.py +0 -0
- {clockbars-0.1.0 → clockbars-1.0.0}/src/clockbars/core.py +0 -0
- {clockbars-0.1.0 → clockbars-1.0.0}/src/clockbars/py.typed +0 -0
- {clockbars-0.1.0 → clockbars-1.0.0}/src/clockbars/synthetic.py +0 -0
- {clockbars-0.1.0 → clockbars-1.0.0}/tests/conftest.py +0 -0
- {clockbars-0.1.0 → clockbars-1.0.0}/tests/test_builder.py +0 -0
- {clockbars-0.1.0 → clockbars-1.0.0}/tests/test_pandas.py +0 -0
- {clockbars-0.1.0 → clockbars-1.0.0}/tests/test_sessions.py +0 -0
- {clockbars-0.1.0 → clockbars-1.0.0}/tests/test_synthetic.py +0 -0
- {clockbars-0.1.0 → clockbars-1.0.0}/tests/test_time_bars.py +0 -0
- {clockbars-0.1.0 → clockbars-1.0.0}/tests/test_validation.py +0 -0
|
@@ -13,6 +13,6 @@ description.
|
|
|
13
13
|
|
|
14
14
|
**Alternatives you considered**
|
|
15
15
|
|
|
16
|
-
Imbalance and run bars are already on the roadmap
|
|
16
|
+
Imbalance and run bars are already on the roadmap; if this request is one
|
|
17
17
|
of those, say what your preferred thresholding behavior would be, since
|
|
18
18
|
that is where implementations differ.
|
|
@@ -4,9 +4,9 @@ Notable changes to clockbars are documented here. The format follows
|
|
|
4
4
|
[Keep a Changelog](https://keepachangelog.com/en/1.1.0/); versions follow
|
|
5
5
|
[SemVer](https://semver.org/).
|
|
6
6
|
|
|
7
|
-
## [
|
|
7
|
+
## [1.0.0] - 2026-11-27
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
Initial public release.
|
|
10
10
|
|
|
11
11
|
### Added
|
|
12
12
|
|
|
@@ -20,46 +20,50 @@ First release.
|
|
|
20
20
|
and `flush()` for the trailing bar. `bars_to_frame()` collects bars with
|
|
21
21
|
exactly `resample()`'s schema and dtypes.
|
|
22
22
|
- Restart-from-zero threshold rule: the crossing trade closes its bar, a
|
|
23
|
-
closed bar's total always meets
|
|
24
|
-
of a segment can be partial
|
|
23
|
+
closed bar's total always meets its threshold, and only the final bar
|
|
24
|
+
of a segment can be partial: flagged, never dropped.
|
|
25
25
|
- Strict input validation: timezone-aware timestamps normalized to
|
|
26
26
|
`Datetime("ns", "UTC")`, sortedness required, and finiteness checked
|
|
27
27
|
separately from sign so NaN and inf cannot pass a sign check.
|
|
28
28
|
- Deterministic seeded trade generator (`synthetic_ticks`) with one
|
|
29
|
-
session per business day, U-shaped arrivals, size clustering at the
|
|
30
|
-
|
|
29
|
+
session per business day, U-shaped arrivals, size clustering at the open
|
|
30
|
+
and close, and volatility scaling with trade size.
|
|
31
31
|
- pandas input adapter, held to the same validation as Polars input.
|
|
32
|
-
-
|
|
32
|
+
- 172 tests: conservation, contiguity, threshold, partial-flag, OHLC and
|
|
33
33
|
VWAP consistency, determinism, validation, the pandas path, session
|
|
34
|
-
cuts, streaming parity with the batch
|
|
35
|
-
|
|
36
|
-
|
|
34
|
+
cuts, float-accumulation tolerance, streaming parity with the batch
|
|
35
|
+
path, and 200,000-trade scale runs.
|
|
36
|
+
- CI on Ubuntu and Windows across Python 3.11 through 3.14: ruff, strict
|
|
37
|
+
mypy, and pytest with statement coverage required at 100 percent.
|
|
37
38
|
- `py.typed` marker, so downstream type checkers see the package's types.
|
|
38
39
|
- `examples/make_hero.py` regenerates the README figure and its table
|
|
39
40
|
from a seed.
|
|
40
|
-
- `docs/paper.md`:
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
41
|
+
- `docs/paper.md`: the fifty-nine-year history of activity sampling, its
|
|
42
|
+
one genuinely contested claim, and the failure modes the test suite
|
|
43
|
+
pins, with published measurements cited alongside the library's own
|
|
44
|
+
reproducible numbers.
|
|
45
|
+
- `CITATION.cff`, `CONTRIBUTING.md`, issue and PR templates, and a
|
|
46
|
+
dependabot schedule.
|
|
44
47
|
|
|
45
48
|
### Design decisions worth knowing before depending on this
|
|
46
49
|
|
|
47
50
|
- Overshoot is not carried into the next bar. A global cumulative sum
|
|
48
51
|
would dock the following bar for the previous bar's overshoot and close
|
|
49
|
-
it below threshold mid-series;
|
|
52
|
+
it below threshold mid-series; clockbars restarts each bar at zero.
|
|
50
53
|
- Threshold comparisons use a `1e-9` relative tolerance, because summed
|
|
51
|
-
floats land on `499.9999999999999` and strict comparison would flag
|
|
54
|
+
floats land on `499.9999999999999` and a strict comparison would flag
|
|
52
55
|
phantom partial bars.
|
|
53
56
|
- A fractional tick threshold rounds up: 2.5 closes bars of 3 trades.
|
|
54
57
|
- Time-bar windows are half-open `[start, start + every)`, compound
|
|
55
58
|
intervals like `1h30m` are summed, and alignment is to the epoch, not
|
|
56
59
|
to the first trade.
|
|
57
60
|
- Batch and streaming must agree: exactly on bar boundaries, timestamps,
|
|
58
|
-
OHLC, counts, and partial flags; per-bar volume and
|
|
59
|
-
float-summation order (
|
|
60
|
-
and a sequential accumulator sum the same floats
|
|
61
|
+
OHLC, counts, and partial flags; per-bar volume, notional, and vwap to
|
|
62
|
+
within float-summation order (at most `1e-12` relative), because a
|
|
63
|
+
vectorised group sum and a sequential accumulator sum the same floats
|
|
64
|
+
in different orders.
|
|
61
65
|
- A trade exactly on a session boundary belongs to the new session, and a
|
|
62
|
-
session's final short bar is flagged partial
|
|
63
|
-
|
|
66
|
+
session's final short bar is flagged partial, never merged into the next
|
|
67
|
+
day.
|
|
64
68
|
|
|
65
|
-
[
|
|
69
|
+
[1.0.0]: https://github.com/zzionz/clockbars/releases/tag/v1.0.0
|
|
@@ -4,14 +4,14 @@ type: software
|
|
|
4
4
|
title: clockbars
|
|
5
5
|
authors:
|
|
6
6
|
- alias: zzionz
|
|
7
|
-
version:
|
|
7
|
+
version: 1.0.0
|
|
8
8
|
date-released: "2026-11-27"
|
|
9
9
|
license: MIT
|
|
10
10
|
repository-code: "https://github.com/zzionz/clockbars"
|
|
11
11
|
abstract: >-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
Bars on the market's clock: tick, volume, and dollar bars in Polars, one
|
|
13
|
+
call from DataFrame to DataFrame, with a streaming builder held to parity
|
|
14
|
+
with the batch path and session-aware construction.
|
|
15
15
|
keywords:
|
|
16
16
|
- quantitative-finance
|
|
17
17
|
- market-microstructure
|
|
@@ -25,12 +25,13 @@ uv run pytest -q --cov=clockbars # coverage stays at 100%
|
|
|
25
25
|
|
|
26
26
|
100% statement coverage is enforced deliberately, at the current size of the
|
|
27
27
|
library (167 statements). If a line cannot be tested, the honest options are
|
|
28
|
-
to delete it or to argue for it in a PR
|
|
28
|
+
to delete it or to argue for it in a PR. Lowering the threshold quietly is
|
|
29
|
+
neither.
|
|
29
30
|
|
|
30
31
|
## The one rule that matters
|
|
31
32
|
|
|
32
33
|
Conventions here are load-bearing. Whether overshoot leaks into the next bar,
|
|
33
|
-
how a partial final bar is flagged, what happens to naive timestamps
|
|
34
|
+
how a partial final bar is flagged, what happens to naive timestamps: each
|
|
34
35
|
is documented in the README, stated in the paper, and pinned by a named test.
|
|
35
36
|
If you change one of these behaviors, the PR changes all three artifacts in
|
|
36
37
|
the same commit: the test that pins it, the README section that states it,
|
|
@@ -46,6 +47,6 @@ both and commit the result, so the published numbers stay true.
|
|
|
46
47
|
## Reporting problems
|
|
47
48
|
|
|
48
49
|
Use the issue templates. For suspected wrong output, include the input, the
|
|
49
|
-
output, and the three versions (`python`, `polars`, `clockbars`)
|
|
50
|
+
output, and the three versions (`python`, `polars`, `clockbars`). Say
|
|
50
51
|
whether you believe it is a bug or an undocumented convention, since those
|
|
51
52
|
get very different fixes.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: clockbars
|
|
3
|
-
Version:
|
|
3
|
+
Version: 1.0.0
|
|
4
4
|
Summary: Bars sampled on the market's own clock: tick, volume, and dollar bars in Polars, batch and streaming with proven parity.
|
|
5
5
|
Project-URL: Homepage, https://github.com/zzionz/clockbars
|
|
6
6
|
Project-URL: Issues, https://github.com/zzionz/clockbars/issues
|
|
@@ -35,9 +35,10 @@ Description-Content-Type: text/markdown
|
|
|
35
35
|
|
|
36
36
|
# clockbars
|
|
37
37
|
|
|
38
|
-
**Bars
|
|
39
|
-
|
|
40
|
-
|
|
38
|
+
**Bars on the market's clock.** Every bar closes when a set amount of trading
|
|
39
|
+
has actually happened. Quiet minutes produce nothing, busy ones produce
|
|
40
|
+
plenty. Tick, volume, and dollar bars in Polars, built the same way in batch
|
|
41
|
+
and in a live stream.
|
|
41
42
|
|
|
42
43
|
[](https://github.com/zzionz/clockbars/actions/workflows/ci.yml)
|
|
43
44
|

|
|
@@ -46,42 +47,47 @@ both to the same contract.
|
|
|
46
47
|
|
|
47
48
|
---
|
|
48
49
|
|
|
49
|
-
Most
|
|
50
|
-
a
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
Sampling by activity
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
50
|
+
Most pipelines build bars by clock: take all the trades in each minute, call it
|
|
51
|
+
a row, repeat. Markets do not produce information at a constant rate, so the
|
|
52
|
+
result is uneven by construction. The first and last half hour of a session
|
|
53
|
+
carry several times the volume of midday, which means a 1-minute bar at 09:31
|
|
54
|
+
and a 1-minute bar at 12:31 agree in duration while differing by an order of
|
|
55
|
+
magnitude in content. Downstream code treats them as equal rows anyway, and
|
|
56
|
+
every statistic computed on the series inherits the unevenness.
|
|
57
|
+
|
|
58
|
+
Sampling by activity is the old fix. Mandelbrot and Taylor noted in 1967 that
|
|
59
|
+
price changes over a fixed number of transactions behave far better than price
|
|
60
|
+
changes over fixed time. Clark's 1973 model treated volume as the clock.
|
|
61
|
+
Easley, López de Prado, and O'Hara built a microstructure framework on
|
|
62
|
+
equal-volume buckets and named the idea: the volume clock.
|
|
63
|
+
|
|
64
|
+
The comparison has also been measured on real data, which is worth more than
|
|
65
|
+
any argument. Oomen (2006) compared calendar, business, and transaction-time
|
|
66
|
+
sampling on IBM transaction data from 2000 to 2004. Sampling in transaction
|
|
67
|
+
time cut the mean squared error of the realized variance estimator by about
|
|
68
|
+
5 percent on average, and by as much as 40 percent on days with irregular
|
|
69
|
+
trading. The clock choice is a measurable property of your estimator.
|
|
70
|
+
|
|
71
|
+
This problem has existing software; see [Related work](#related-work). What
|
|
64
72
|
none of it offers is a Polars-native path: one call from `DataFrame` to
|
|
65
73
|
`DataFrame` with a fixed schema, ready to drop into a pipeline that already
|
|
66
|
-
speaks Polars. That is the gap this fills
|
|
67
|
-
|
|
74
|
+
speaks Polars. That is the gap this fills. Two properties come with it that
|
|
75
|
+
the free ecosystem currently lacks: a streaming builder that provably
|
|
68
76
|
produces the same bars as the batch path, and session-aware construction so a
|
|
69
77
|
bar never straddles an exchange close.
|
|
70
78
|
|
|
71
79
|

|
|
72
80
|
|
|
73
|
-
Same 400,000-trade synthetic stream, 20 sessions. Top row: volume per bar is
|
|
74
|
-
long right tail for time bars and a spike for dollar bars. Bottom row:
|
|
75
|
-
|
|
76
|
-
flat. The stream is synthetic (seeded, reproducible)
|
|
77
|
-
licensed; the mechanism
|
|
81
|
+
Same 400,000-trade synthetic stream, 20 sessions. Top row: volume per bar is
|
|
82
|
+
a long right tail for time bars and a spike for dollar bars. Bottom row: both
|
|
83
|
+
schemes across one session, where time-bar volume traces the intraday U and
|
|
84
|
+
dollar bars hug a flat line. The stream is synthetic (seeded, reproducible)
|
|
85
|
+
because trade data is licensed; the mechanism is real.
|
|
78
86
|
|
|
79
87
|
## Install
|
|
80
88
|
|
|
81
|
-
Not on PyPI yet — install from source:
|
|
82
|
-
|
|
83
89
|
```bash
|
|
84
|
-
pip install
|
|
90
|
+
pip install clockbars
|
|
85
91
|
```
|
|
86
92
|
|
|
87
93
|
For development:
|
|
@@ -90,13 +96,13 @@ For development:
|
|
|
90
96
|
git clone https://github.com/zzionz/clockbars.git
|
|
91
97
|
cd clockbars
|
|
92
98
|
uv sync --all-extras
|
|
93
|
-
uv run pytest -q #
|
|
99
|
+
uv run pytest -q # 172 passed, 100% coverage enforced
|
|
94
100
|
```
|
|
95
101
|
|
|
96
102
|
CI runs the full gate set on every push: ruff, strict mypy, and the test
|
|
97
|
-
suite with coverage required at 100
|
|
98
|
-
3.11
|
|
99
|
-
under mypy you get its types, not `Any`.
|
|
103
|
+
suite with coverage required at 100 percent, on Ubuntu and Windows across
|
|
104
|
+
Python 3.11 through 3.14. The package ships a `py.typed` marker, so if you
|
|
105
|
+
use clockbars under mypy you get its types, not `Any`.
|
|
100
106
|
|
|
101
107
|
## Quickstart
|
|
102
108
|
|
|
@@ -104,7 +110,7 @@ under mypy you get its types, not `Any`.
|
|
|
104
110
|
import polars as pl
|
|
105
111
|
from clockbars import resample
|
|
106
112
|
|
|
107
|
-
trades = pl.read_parquet("trades.parquet") # ts, price, size
|
|
113
|
+
trades = pl.read_parquet("trades.parquet") # ts, price, size (sorted by ts)
|
|
108
114
|
|
|
109
115
|
dollar = resample(trades, kind="dollar", threshold=1_000_000)
|
|
110
116
|
volume = resample(trades, kind="volume", threshold=500)
|
|
@@ -112,8 +118,8 @@ ticks = resample(trades, kind="tick", threshold=250)
|
|
|
112
118
|
minute = resample(trades, kind="time", every="1m")
|
|
113
119
|
```
|
|
114
120
|
|
|
115
|
-
The same stream can be cut so that no bar straddles a session boundary
|
|
116
|
-
|
|
121
|
+
The same stream can be cut so that no bar straddles a session boundary. Pass
|
|
122
|
+
the rollover times and accumulation restarts at each one:
|
|
117
123
|
|
|
118
124
|
```python
|
|
119
125
|
from datetime import datetime, timezone
|
|
@@ -126,7 +132,7 @@ daily = resample(trades, kind="dollar", threshold=1_000_000,
|
|
|
126
132
|
session_starts=rollovers)
|
|
127
133
|
```
|
|
128
134
|
|
|
129
|
-
|
|
135
|
+
The same rule runs one trade at a time, for live streams:
|
|
130
136
|
|
|
131
137
|
```python
|
|
132
138
|
from clockbars import BarBuilder
|
|
@@ -141,14 +147,14 @@ trailing = builder.flush() # the final partial bar
|
|
|
141
147
|
```
|
|
142
148
|
|
|
143
149
|
A parity test asserts that `BarBuilder` over the batch input agrees with
|
|
144
|
-
`resample` on everything that decides what a bar
|
|
145
|
-
timestamps, OHLC, counts, partial flags
|
|
150
|
+
`resample` on everything that decides what a bar is: boundaries, ids,
|
|
151
|
+
timestamps, OHLC, counts, partial flags, at 200,000 trades, for all four
|
|
146
152
|
kinds, session cuts included. Per-bar volume and notional agree to within
|
|
147
|
-
float-summation order (
|
|
148
|
-
sequential accumulator sum the same numbers in different orders,
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
153
|
+
float-summation order (at most 1e-12 relative). A vectorised group sum and a
|
|
154
|
+
sequential accumulator sum the same numbers in different orders, so claiming
|
|
155
|
+
bit equality there would be a claim floating point cannot support. The bar
|
|
156
|
+
your backtest computed and the bar your live process computed are the same
|
|
157
|
+
object, or you find out in a test.
|
|
152
158
|
|
|
153
159
|
All four kinds return the same schema, so nothing downstream has to branch on
|
|
154
160
|
how the bars were made:
|
|
@@ -171,46 +177,46 @@ A bar closes on the trade that pushes its running total to the threshold, and
|
|
|
171
177
|
that trade is included in the closing bar. Each bar then restarts from zero.
|
|
172
178
|
Everything else follows:
|
|
173
179
|
|
|
174
|
-
1. **A closed bar's total is always at least its threshold.** It can overshoot
|
|
175
|
-
|
|
176
|
-
2. **Overshoot
|
|
177
|
-
|
|
178
|
-
each bar's overshoot to the
|
|
179
|
-
threshold in the middle of the series.
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
180
|
+
1. **A closed bar's total is always at least its threshold.** It can overshoot
|
|
181
|
+
only by the size of its own final trade.
|
|
182
|
+
2. **Overshoot stays in the bar that earned it.** The obvious vectorised
|
|
183
|
+
implementation is a global cumulative sum, and it has a defect: it charges
|
|
184
|
+
each bar's overshoot to the following bar, which then closes below
|
|
185
|
+
threshold in the middle of the series. In one 128-bar run built that way,
|
|
186
|
+
62 bars came out flagged partial. Restarting from zero keeps every bar
|
|
187
|
+
answerable on its own. `test_overshoot_is_not_carried_into_the_next_bar`
|
|
188
|
+
pins this.
|
|
183
189
|
3. **The final bar of any segment is kept, flagged, and never dropped.**
|
|
184
|
-
`is_partial=True` means "the segment ended before this bar filled"
|
|
185
|
-
|
|
186
|
-
one segment, so only the last bar can be partial. Throwing a
|
|
187
|
-
away would silently rewrite history.
|
|
190
|
+
`is_partial=True` means "the segment ended before this bar filled": the end
|
|
191
|
+
of the stream, or the end of a session. Without session cuts the whole
|
|
192
|
+
stream is one segment, so only the last bar can be partial. Throwing a
|
|
193
|
+
partial bar away would silently rewrite history.
|
|
188
194
|
4. **Naive timestamps are rejected.** If your data says 14:30 with no time
|
|
189
|
-
zone,
|
|
190
|
-
|
|
195
|
+
zone, you get an error telling you to declare the zone. Silently assuming
|
|
196
|
+
UTC is how a DST bug gets born.
|
|
191
197
|
5. **NaN and inf are rejected before they can look like data.** In
|
|
192
198
|
floating-point comparison, `NaN <= 0` is `False`, so a sign check alone
|
|
193
|
-
accepts NaN
|
|
199
|
+
accepts NaN, and one NaN price poisons every bar after it. Finiteness is
|
|
194
200
|
checked separately from sign.
|
|
195
201
|
6. **Threshold comparisons carry a `1e-9` relative tolerance.** Summing a few
|
|
196
202
|
thousand floats lands on `499.9999999999999` more often than anyone wants
|
|
197
|
-
to admit. Without the tolerance, those
|
|
198
|
-
7. **Duplicate timestamps are fine.** Trades share timestamps in real data
|
|
199
|
-
|
|
203
|
+
to admit. Without the tolerance, those become phantom partial bars.
|
|
204
|
+
7. **Duplicate timestamps are fine.** Trades share timestamps in real data.
|
|
205
|
+
Input order is preserved within a bar, so `open`/`close` are stable.
|
|
200
206
|
8. **Input must be sorted.** Unsorted input is an error, not a silent
|
|
201
207
|
`sort()`. Sorting implicitly is a policy decision about your data, and it
|
|
202
208
|
is not this library's to make.
|
|
203
209
|
|
|
204
210
|
Time bars follow the same schema with `is_partial` always `False`. Windows
|
|
205
211
|
are half-open `[start, start + every)` and align to wall-clock boundaries: a
|
|
206
|
-
frame that begins at 09:30:30 still produces windows that begin on the
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
212
|
+
frame that begins at 09:30:30 still produces windows that begin on the
|
|
213
|
+
minute. Compound intervals like `1h30m` are summed. Empty windows are never
|
|
214
|
+
emitted. Session cuts do not apply to time bars, since the clock already cuts
|
|
215
|
+
them; passing both raises instead of guessing a precedence.
|
|
210
216
|
|
|
211
217
|
## What the tests actually check
|
|
212
218
|
|
|
213
|
-
Not "the code runs"
|
|
219
|
+
Not "the code runs." The properties that make the output trustworthy:
|
|
214
220
|
|
|
215
221
|
- volume, notional, and trade counts are conserved exactly across bars;
|
|
216
222
|
- bars are chronological and non-overlapping, with `bar_id` contiguous from zero;
|
|
@@ -222,68 +228,87 @@ Not "the code runs" — the properties that make the output trustworthy:
|
|
|
222
228
|
- bars never straddle a session boundary, with the boundary trade going to
|
|
223
229
|
the new session;
|
|
224
230
|
- the streaming `BarBuilder` matches `resample` exactly on boundaries,
|
|
225
|
-
timestamps, OHLC, counts, and flags, and to
|
|
231
|
+
timestamps, OHLC, counts, and flags, and to 1e-12 relative on sums;
|
|
226
232
|
- the whole suite of invariants also runs at 200,000-trade scale, streaming
|
|
227
233
|
parity included.
|
|
228
234
|
|
|
229
235
|
Plus every validation rule above, each with the input that breaks it.
|
|
230
236
|
|
|
231
|
-
## What the
|
|
237
|
+
## What the evidence says
|
|
238
|
+
|
|
239
|
+
Three results from three sources. The first is ours and reproducible from a
|
|
240
|
+
seed. The other two are published studies on real data, and they point in
|
|
241
|
+
opposite directions. All three belong here.
|
|
232
242
|
|
|
233
243
|
`examples/make_hero.py` builds the figure and prints this table (seed 7,
|
|
234
|
-
threshold matched to median 1-minute notional so the comparison is
|
|
244
|
+
dollar threshold matched to the median 1-minute notional so the comparison is
|
|
245
|
+
fair):
|
|
235
246
|
|
|
236
247
|
| Sampling | Bars | Volume CV | Excess kurtosis |
|
|
237
248
|
| :--- | ---: | ---: | ---: |
|
|
238
249
|
| time (1m) | 7,800 | 0.7734 | 1.422 |
|
|
239
250
|
| dollar | 10,232 | 0.0255 | −0.007 |
|
|
240
251
|
|
|
241
|
-
Volume CV is the coefficient of variation of per-bar volume
|
|
242
|
-
each bar carries about the same amount of trading.
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
On
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
252
|
+
Volume CV is the coefficient of variation of per-bar volume: near zero means
|
|
253
|
+
each bar carries about the same amount of trading. On this stream, dollar
|
|
254
|
+
bars hold per-bar volume roughly 30 times steadier than time bars.
|
|
255
|
+
|
|
256
|
+
On real data, the stakes are higher than shape. Oomen (2006) measured
|
|
257
|
+
realized variance under calendar, business, and transaction-time sampling on
|
|
258
|
+
IBM transaction data, 2000 to 2004. Transaction time won on every single day
|
|
259
|
+
in the sample: the MSE of the estimator fell by about 5 percent on average
|
|
260
|
+
and by as much as 40 percent on days with irregular trading. That is the
|
|
261
|
+
cost of the old way, priced by someone else's measurement.
|
|
262
|
+
|
|
263
|
+
The newest study goes the other way, and citing it is the difference between
|
|
264
|
+
documentation and marketing. Grądzki, Wójcik, and Lessmann (2025) compared
|
|
265
|
+
sampling schemes for BTC and ETH algorithmic trading over 2018 to 2023 with
|
|
266
|
+
transaction costs, out of sample. CUSUM-filtered sampling was the only scheme
|
|
267
|
+
with consistently positive performance. Dollar bars were the weakest of
|
|
268
|
+
everything they tested. Their diagnosis: crypto exchange volume is inflated
|
|
269
|
+
by wash trading, so a dollar bar accumulates a partly fabricated signal.
|
|
270
|
+
|
|
271
|
+
Read together, the lesson is precise. The mechanical claim is settled:
|
|
272
|
+
sampling on an honest activity signal gives bars of near-constant information
|
|
273
|
+
content, and clock sampling does not. Whether a given activity signal is
|
|
274
|
+
honest, and whether an honest one is enough to help a given strategy, are
|
|
275
|
+
empirical questions with published answers on both sides. Clockbars makes
|
|
276
|
+
the choice cheap to test, correct by construction, and identical between
|
|
277
|
+
backtest and production. It does not promise the market will cooperate.
|
|
254
278
|
|
|
255
279
|
## pandas input
|
|
256
280
|
|
|
257
281
|
If pandas is installed, a `pandas.DataFrame` is accepted and held to the same
|
|
258
|
-
validation
|
|
282
|
+
validation, with one caveat: Polars converts pandas' timezone-aware datetime
|
|
259
283
|
columns through pyarrow, so pandas users need pyarrow too. Naive pandas
|
|
260
284
|
timestamps are rejected, same as naive Polars ones.
|
|
261
285
|
|
|
262
286
|
## Limitations
|
|
263
287
|
|
|
264
288
|
- Thresholds are absolute. Choosing one is still your job; a defensible
|
|
265
|
-
default is the median per-minute notional over your sample
|
|
266
|
-
the example does
|
|
289
|
+
default is the median per-minute notional over your sample, which is what
|
|
290
|
+
the example does.
|
|
267
291
|
- Threshold bars are not uniform in time. Anything that assumes a fixed bar
|
|
268
|
-
duration
|
|
292
|
+
duration (annualisation factors, some indicator definitions) needs
|
|
269
293
|
rethinking before it runs on them.
|
|
270
294
|
- Imbalance and run bars are not implemented. Fixed-threshold versions exist
|
|
271
295
|
in bar-forge and EWMA-adaptive versions in mlfinpy; a Polars-native
|
|
272
|
-
implementation is on the roadmap
|
|
273
|
-
- Session awareness means *your* boundaries: the library
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
- Bar segmentation scans over bars,
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
296
|
+
implementation is on the roadmap.
|
|
297
|
+
- Session awareness means *your* boundaries: the library ships no exchange
|
|
298
|
+
calendars. You provide the rollover times; `resample` guarantees the cut.
|
|
299
|
+
Rollover contract-adjustment is still your problem.
|
|
300
|
+
- Bar segmentation scans over bars, at `O(n_bars · log n_trades)`. With a
|
|
301
|
+
realistic threshold there are far fewer bars than trades. A tick threshold
|
|
302
|
+
of 1 on a million trades is inherently one bar per trade and will not be
|
|
303
|
+
fast; nothing makes that a good idea.
|
|
280
304
|
|
|
281
305
|
## Roadmap
|
|
282
306
|
|
|
283
307
|
- [ ] Imbalance bars, EWMA-adaptive (fixed-threshold versions exist in
|
|
284
|
-
bar-forge; adaptive ones in mlfinpy
|
|
285
|
-
|
|
308
|
+
bar-forge; adaptive ones in mlfinpy; a Polars port needs a reason
|
|
309
|
+
beyond porting)
|
|
286
310
|
- [ ] Run bars
|
|
311
|
+
- [ ] CUSUM-filtered sampling, the scheme that won in Grądzki et al. (2025)
|
|
287
312
|
- [ ] Out-of-core chunked batch construction
|
|
288
313
|
|
|
289
314
|
## Related work
|
|
@@ -293,12 +318,12 @@ sign of a promotional README.
|
|
|
293
318
|
|
|
294
319
|
**[bar-forge](https://github.com/bmouler/bar-forge)** (Mouler, 2026) is the
|
|
295
320
|
closest contemporary: tick, volume, dollar, and fixed-threshold imbalance
|
|
296
|
-
bars over NumPy trade records, plus strictly causal normalization
|
|
297
|
-
with 100
|
|
298
|
-
92.96
|
|
299
|
-
we read as both libraries deriving from the
|
|
300
|
-
sessions, streaming, or DataFrames, and it cites
|
|
301
|
-
first three and ship [a paper](docs/paper.md).
|
|
321
|
+
bars over NumPy trade records, plus strictly causal normalization
|
|
322
|
+
transforms, with 100 percent coverage, strict mypy, and a mutation-testing
|
|
323
|
+
run (1,208 mutants, 92.96 percent killed). Its bar conventions agree with
|
|
324
|
+
ours where they overlap, which we read as both libraries deriving from the
|
|
325
|
+
same source. It does not do sessions, streaming, or DataFrames, and it cites
|
|
326
|
+
no literature. We do the first three and ship [a paper](docs/paper.md).
|
|
302
327
|
|
|
303
328
|
**mlfinpy** is a free port of the mlfinlab data structures, including the
|
|
304
329
|
EWMA-adaptive imbalance and run bars from López de Prado ch. 2, which we do
|
|
@@ -308,11 +333,16 @@ not yet implement.
|
|
|
308
333
|
Thames) implements the full López de Prado catalogue; it began open source
|
|
309
334
|
and is now a commercial product.
|
|
310
335
|
|
|
336
|
+
Practitioner tooling has followed the same trail: Databento's 2024 guest
|
|
337
|
+
series from Double River Investments builds volume and dollar bars in Polars
|
|
338
|
+
with a Rust kernel, which is a good sign for the Polars-native path and a
|
|
339
|
+
complement to this library.
|
|
340
|
+
|
|
311
341
|
## Paper
|
|
312
342
|
|
|
313
|
-
A short paper
|
|
314
|
-
implementation errors this library exists to prevent, and why batch and
|
|
315
|
-
streaming must provably agree
|
|
343
|
+
A short paper covering why bar construction matters, the history behind it,
|
|
344
|
+
the implementation errors this library exists to prevent, and why batch and
|
|
345
|
+
streaming must provably agree, is in
|
|
316
346
|
[docs/paper.md](docs/paper.md).
|
|
317
347
|
|
|
318
348
|
If you use clockbars in research, cite it:
|