clockbars 0.1.0__tar.gz → 0.1.1__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-0.1.1}/.github/ISSUE_TEMPLATE/feature_request.md +1 -1
- {clockbars-0.1.0 → clockbars-0.1.1}/CHANGELOG.md +28 -4
- {clockbars-0.1.0 → clockbars-0.1.1}/CONTRIBUTING.md +4 -3
- {clockbars-0.1.0 → clockbars-0.1.1}/PKG-INFO +134 -105
- clockbars-0.1.1/README.md +325 -0
- clockbars-0.1.1/docs/hero.png +0 -0
- clockbars-0.1.1/docs/paper.md +387 -0
- clockbars-0.1.1/examples/make_hero.py +173 -0
- {clockbars-0.1.0 → clockbars-0.1.1}/pyproject.toml +1 -1
- {clockbars-0.1.0 → clockbars-0.1.1}/src/clockbars/__init__.py +1 -1
- {clockbars-0.1.0 → clockbars-0.1.1}/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-0.1.1}/.github/ISSUE_TEMPLATE/bug_report.md +0 -0
- {clockbars-0.1.0 → clockbars-0.1.1}/.github/ISSUE_TEMPLATE/config.yml +0 -0
- {clockbars-0.1.0 → clockbars-0.1.1}/.github/PULL_REQUEST_TEMPLATE.md +0 -0
- {clockbars-0.1.0 → clockbars-0.1.1}/.github/dependabot.yml +0 -0
- {clockbars-0.1.0 → clockbars-0.1.1}/.github/workflows/ci.yml +0 -0
- {clockbars-0.1.0 → clockbars-0.1.1}/.github/workflows/release.yml +0 -0
- {clockbars-0.1.0 → clockbars-0.1.1}/.gitignore +0 -0
- {clockbars-0.1.0 → clockbars-0.1.1}/CITATION.cff +0 -0
- {clockbars-0.1.0 → clockbars-0.1.1}/LICENSE +0 -0
- {clockbars-0.1.0 → clockbars-0.1.1}/src/clockbars/builder.py +0 -0
- {clockbars-0.1.0 → clockbars-0.1.1}/src/clockbars/core.py +0 -0
- {clockbars-0.1.0 → clockbars-0.1.1}/src/clockbars/py.typed +0 -0
- {clockbars-0.1.0 → clockbars-0.1.1}/src/clockbars/synthetic.py +0 -0
- {clockbars-0.1.0 → clockbars-0.1.1}/tests/conftest.py +0 -0
- {clockbars-0.1.0 → clockbars-0.1.1}/tests/test_builder.py +0 -0
- {clockbars-0.1.0 → clockbars-0.1.1}/tests/test_pandas.py +0 -0
- {clockbars-0.1.0 → clockbars-0.1.1}/tests/test_sessions.py +0 -0
- {clockbars-0.1.0 → clockbars-0.1.1}/tests/test_synthetic.py +0 -0
- {clockbars-0.1.0 → clockbars-0.1.1}/tests/test_threshold_bars.py +0 -0
- {clockbars-0.1.0 → clockbars-0.1.1}/tests/test_time_bars.py +0 -0
- {clockbars-0.1.0 → clockbars-0.1.1}/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,7 +4,30 @@ 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
|
-
## [0.1.
|
|
7
|
+
## [0.1.1] - 2026-11-27
|
|
8
|
+
|
|
9
|
+
### Changed
|
|
10
|
+
|
|
11
|
+
- Writing pass over the README, paper, changelog, and contributing guide,
|
|
12
|
+
measured with the humanize-ai-writing checker: 70 machine-style tells
|
|
13
|
+
(em-dash density, negative parallelism) reduced to zero. The one remaining
|
|
14
|
+
flag is the word "Paradigm" inside a cited paper's title, which stays
|
|
15
|
+
verbatim.
|
|
16
|
+
- New hero figure: volume histograms above, both sampling clocks overlaid
|
|
17
|
+
on a single session below, in one consistent palette.
|
|
18
|
+
- Motivation now cites published measurements alongside the synthetic demo:
|
|
19
|
+
Oomen (2006) on transaction-time sampling cutting realized-variance MSE
|
|
20
|
+
by about 5 percent on average and up to 40 percent on irregular days;
|
|
21
|
+
Grądzki, Wójcik, and Lessmann (2025) on dollar bars being the weakest
|
|
22
|
+
sampling scheme on wash-traded crypto volume; Griffin and Oomen (2008)
|
|
23
|
+
on tick time versus transaction time.
|
|
24
|
+
- Paper: abstract, sections 1 and 3, references, and limitations reworked
|
|
25
|
+
to carry the new evidence; software references regrouped.
|
|
26
|
+
|
|
27
|
+
No behavior changes. 171 tests, 100 percent coverage, strict mypy: all
|
|
28
|
+
unchanged.
|
|
29
|
+
|
|
30
|
+
## [0.1.0] - 2026-11-27
|
|
8
31
|
|
|
9
32
|
First release.
|
|
10
33
|
|
|
@@ -21,7 +44,7 @@ First release.
|
|
|
21
44
|
exactly `resample()`'s schema and dtypes.
|
|
22
45
|
- Restart-from-zero threshold rule: the crossing trade closes its bar, a
|
|
23
46
|
closed bar's total always meets the threshold, and only the final bar
|
|
24
|
-
of a segment can be partial
|
|
47
|
+
of a segment can be partial: flagged, never dropped.
|
|
25
48
|
- Strict input validation: timezone-aware timestamps normalized to
|
|
26
49
|
`Datetime("ns", "UTC")`, sortedness required, and finiteness checked
|
|
27
50
|
separately from sign so NaN and inf cannot pass a sign check.
|
|
@@ -59,7 +82,8 @@ First release.
|
|
|
59
82
|
float-summation order (≤ 1e-12 relative), because a vectorised group sum
|
|
60
83
|
and a sequential accumulator sum the same floats in different orders.
|
|
61
84
|
- A trade exactly on a session boundary belongs to the new session, and a
|
|
62
|
-
session's final short bar is flagged partial
|
|
63
|
-
|
|
85
|
+
session's final short bar is flagged partial, never merged into the next
|
|
86
|
+
day.
|
|
64
87
|
|
|
88
|
+
[0.1.1]: https://github.com/zzionz/clockbars/releases/tag/v0.1.1
|
|
65
89
|
[0.1.0]: https://github.com/zzionz/clockbars/releases/tag/v0.1.0
|
|
@@ -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: 0.1.
|
|
3
|
+
Version: 0.1.1
|
|
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
|
|
@@ -36,8 +36,8 @@ Description-Content-Type: text/markdown
|
|
|
36
36
|
# clockbars
|
|
37
37
|
|
|
38
38
|
**Bars sampled on the market's own clock.** Tick, volume, and dollar bars in
|
|
39
|
-
Polars
|
|
40
|
-
|
|
39
|
+
Polars. One explicit rule, two execution paths, one test suite that holds both
|
|
40
|
+
to the same contract.
|
|
41
41
|
|
|
42
42
|
[](https://github.com/zzionz/clockbars/actions/workflows/ci.yml)
|
|
43
43
|

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

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