suretick 0.3.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.
Files changed (58) hide show
  1. suretick-0.3.0/LICENSE +21 -0
  2. suretick-0.3.0/PKG-INFO +295 -0
  3. suretick-0.3.0/README.md +267 -0
  4. suretick-0.3.0/pyproject.toml +56 -0
  5. suretick-0.3.0/setup.cfg +4 -0
  6. suretick-0.3.0/src/suretick/__init__.py +3 -0
  7. suretick-0.3.0/src/suretick/analyze.py +128 -0
  8. suretick-0.3.0/src/suretick/cli.py +117 -0
  9. suretick-0.3.0/src/suretick/conformal.py +101 -0
  10. suretick-0.3.0/src/suretick/data/__init__.py +0 -0
  11. suretick-0.3.0/src/suretick/data/binance.py +80 -0
  12. suretick-0.3.0/src/suretick/data/synthetic.py +157 -0
  13. suretick-0.3.0/src/suretick/demo.py +186 -0
  14. suretick-0.3.0/src/suretick/experiment.py +103 -0
  15. suretick-0.3.0/src/suretick/features.py +127 -0
  16. suretick-0.3.0/src/suretick/live.py +713 -0
  17. suretick-0.3.0/src/suretick/live_models/btcusdt.json.gz +0 -0
  18. suretick-0.3.0/src/suretick/live_models/ethusdt.json.gz +0 -0
  19. suretick-0.3.0/src/suretick/live_models/solusdt.json.gz +0 -0
  20. suretick-0.3.0/src/suretick/metrics.py +148 -0
  21. suretick-0.3.0/src/suretick/models.py +153 -0
  22. suretick-0.3.0/src/suretick/monitors.py +309 -0
  23. suretick-0.3.0/src/suretick/online.py +63 -0
  24. suretick-0.3.0/src/suretick/plots.py +156 -0
  25. suretick-0.3.0/src/suretick/realdata.py +60 -0
  26. suretick-0.3.0/src/suretick/report.py +77 -0
  27. suretick-0.3.0/src/suretick/screen.py +33 -0
  28. suretick-0.3.0/src/suretick/server.py +248 -0
  29. suretick-0.3.0/src/suretick/stream.py +461 -0
  30. suretick-0.3.0/src/suretick/web/app.js +585 -0
  31. suretick-0.3.0/src/suretick/web/data/both_seed0.json +1 -0
  32. suretick-0.3.0/src/suretick/web/data/concept_only_seed0.json +1 -0
  33. suretick-0.3.0/src/suretick/web/data/covariate_only_seed0.json +1 -0
  34. suretick-0.3.0/src/suretick/web/data/index.json +32 -0
  35. suretick-0.3.0/src/suretick/web/data/null_seed0.json +1 -0
  36. suretick-0.3.0/src/suretick/web/data/real_20261202-2300-a-calm-stretch-2-dec-23-00-utc.json +1 -0
  37. suretick-0.3.0/src/suretick/web/data/real_index.json +8 -0
  38. suretick-0.3.0/src/suretick/web/data/recurring_seed0.json +1 -0
  39. suretick-0.3.0/src/suretick/web/index.html +194 -0
  40. suretick-0.3.0/src/suretick/web/social.png +0 -0
  41. suretick-0.3.0/src/suretick/web/style.css +175 -0
  42. suretick-0.3.0/src/suretick.egg-info/PKG-INFO +295 -0
  43. suretick-0.3.0/src/suretick.egg-info/SOURCES.txt +56 -0
  44. suretick-0.3.0/src/suretick.egg-info/dependency_links.txt +1 -0
  45. suretick-0.3.0/src/suretick.egg-info/entry_points.txt +2 -0
  46. suretick-0.3.0/src/suretick.egg-info/requires.txt +16 -0
  47. suretick-0.3.0/src/suretick.egg-info/top_level.txt +1 -0
  48. suretick-0.3.0/tests/test_analyze.py +51 -0
  49. suretick-0.3.0/tests/test_app.py +110 -0
  50. suretick-0.3.0/tests/test_conformal.py +68 -0
  51. suretick-0.3.0/tests/test_features.py +57 -0
  52. suretick-0.3.0/tests/test_live.py +94 -0
  53. suretick-0.3.0/tests/test_metrics.py +35 -0
  54. suretick-0.3.0/tests/test_models.py +28 -0
  55. suretick-0.3.0/tests/test_monitors.py +76 -0
  56. suretick-0.3.0/tests/test_online.py +25 -0
  57. suretick-0.3.0/tests/test_stream.py +72 -0
  58. suretick-0.3.0/tests/test_synthetic.py +27 -0
suretick-0.3.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Team Triple torrent (Aditya Rai, Sarvesh Shrirao, Rushank Bhoir)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,295 @@
1
+ Metadata-Version: 2.4
2
+ Name: suretick
3
+ Version: 0.3.0
4
+ Summary: Order book forecasting that knows when it stops working: calibrated, shift-aware, live
5
+ Author: Aditya Rai, Sarvesh Shrirao, Rushank Bhoir
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://github.com/codingcrux0/conformal-prediction-lob
8
+ Project-URL: Demo, https://codingcrux0.github.io/conformal-prediction-lob/
9
+ Project-URL: Issues, https://github.com/codingcrux0/conformal-prediction-lob/issues
10
+ Requires-Python: >=3.10
11
+ Description-Content-Type: text/markdown
12
+ License-File: LICENSE
13
+ Requires-Dist: numpy>=1.26
14
+ Requires-Dist: pandas>=2.1
15
+ Requires-Dist: scipy>=1.11
16
+ Requires-Dist: scikit-learn>=1.4
17
+ Requires-Dist: lightgbm>=4.3
18
+ Requires-Dist: websockets>=12.0
19
+ Requires-Dist: certifi>=2024.2
20
+ Provides-Extra: research
21
+ Requires-Dist: matplotlib>=3.8; extra == "research"
22
+ Requires-Dist: pyyaml>=6.0; extra == "research"
23
+ Provides-Extra: dev
24
+ Requires-Dist: pytest>=8.0; extra == "dev"
25
+ Requires-Dist: ruff>=0.5; extra == "dev"
26
+ Requires-Dist: pip-audit>=2.7; extra == "dev"
27
+ Dynamic: license-file
28
+
29
+ # SureTick: a market forecast that knows when it stops working
30
+
31
+ [![CI](https://github.com/codingcrux0/conformal-prediction-lob/actions/workflows/ci.yml/badge.svg)](https://github.com/codingcrux0/conformal-prediction-lob/actions/workflows/ci.yml)
32
+ [![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)
33
+
34
+ **Calibrated, shift-aware order book forecasting, live.** SureTick predicts whether a price
35
+ will go down, stay flat or go up in the next second, and wraps every forecast in an answer
36
+ set that is right 90% of the time. It watches for market changes, tells **harmless**
37
+ changes from **harmful** ones, retrains only when it must, and **remembers past regimes**
38
+ so it can switch back to the right model within seconds when a regime returns. It runs on
39
+ simulated markets, on recorded Bitcoin order books, **live on Binance**, and on your own
40
+ data.
41
+
42
+ Built by Team Triple torrent (Aditya Rai, Sarvesh Shrirao, Rushank Bhoir) for the
43
+ IITB-Optiver Perceptron Hackathon 2026, Theme 3, Problem 4: *order book forecasting under
44
+ regime shifts*.
45
+
46
+ ![Live mode forecasting Bitcoin](docs/live.png)
47
+
48
+ **Try it:** open the online demo at <https://codingcrux0.github.io/conformal-prediction-lob/>
49
+ (simulations and recorded markets), or install it to get live mode and your own data.
50
+
51
+ ## Quick start
52
+
53
+ You need Python 3.10 or newer.
54
+
55
+ ```bash
56
+ pip install git+https://github.com/codingcrux0/conformal-prediction-lob.git
57
+ suretick serve
58
+ ```
59
+
60
+ Your browser opens at `http://127.0.0.1:8000`. On macOS, run `brew install libomp` once
61
+ first (LightGBM needs it; without it the app still opens but cannot train models).
62
+
63
+ | View | What you see |
64
+ |---|---|
65
+ | **Simulations** | Five markets with changes injected at known times; four approaches compared; run any random seed. |
66
+ | **Real market** | Recorded Bitcoin order books from Binance replayed through every approach. |
67
+ | **Live** | The whole system on Binance right now: a forecast every 100 ms, checked one second later, adapting as the market moves. No account or key. |
68
+ | **Your data** | Upload an order book CSV and get the same scoreboard and charts. |
69
+
70
+ From the terminal:
71
+
72
+ ```bash
73
+ suretick live --symbol ethusdt # the app, straight on the live page (BTC, ETH, SOL start instantly)
74
+ suretick sample-data book.csv # an example order book CSV
75
+ suretick analyze book.csv # analyse an order book without the browser
76
+ ```
77
+
78
+ Only live mode uses the internet, to read Binance's public order book. The server only
79
+ accepts connections from your own computer.
80
+
81
+ <details>
82
+ <summary>Docker, and running from a clone (tests, research scripts)</summary>
83
+
84
+ ```bash
85
+ docker build -t suretick https://github.com/codingcrux0/conformal-prediction-lob.git
86
+ docker run --rm -p 127.0.0.1:8000:8000 suretick # then open http://127.0.0.1:8000
87
+ ```
88
+
89
+ ```bash
90
+ git clone https://github.com/codingcrux0/conformal-prediction-lob.git
91
+ cd conformal-prediction-lob
92
+ python -m venv .venv && source .venv/bin/activate # Windows: .venv\Scripts\activate
93
+ pip install -e ".[dev,research]"
94
+ pytest # 60 tests, about 75 seconds
95
+ python scripts/run_mvp.py # one-minute command-line demo with a figure
96
+ ```
97
+ </details>
98
+
99
+ ## The idea in 60 seconds
100
+
101
+ Order books (the lists of waiting buy and sell orders) are predictable for a moment, and then
102
+ the market changes its behaviour around news, volatility spikes or thin liquidity. A model
103
+ trained on the old behaviour keeps producing confident forecasts that are now wrong. For a
104
+ market maker that means quoting on the wrong side: **adverse selection**.
105
+
106
+ ```
107
+ order book ─► features ─► LightGBM ─► conformal answer set ─► forecast, or "not sure"
108
+ ▲ ▲
109
+ stored models ◄──────┤ │ matured labels only (no peeking ahead)
110
+ (regime memory) │ │
111
+ ┌──────┴────────┐ ┌─────┴───────────┐
112
+ │ input monitor │ │ error monitor │
113
+ │ KS + domain │ │ CUSUM on how │
114
+ │ classifier │ │ wrong we are │
115
+ └──────┬────────┘ └─────┬───────────┘
116
+ ▼ ▼
117
+ inputs only changed: keep the model, the model got worse: switch to a stored
118
+ reweight the calibration data model that fits, or retrain a new one
119
+ ```
120
+
121
+ 1. **Forecast.** Gradient-boosted trees read multi-level order-flow imbalance, queue
122
+ imbalance, spread and depth.
123
+ 2. **Stay honest.** Conformal prediction (with adaptive coverage updates) gives a set of
124
+ answers that contains the truth about 90% of the time. One answer is a confident forecast;
125
+ several answers mean "not sure" and we abstain.
126
+ 3. **Tell shifts apart.** Input-only changes (a thinner book, but the same price rule) are
127
+ usually harmless, so we keep the model and reweight the calibration data. Changes that
128
+ hurt the model's errors trigger a fix.
129
+ 4. **Remember regimes.** Every adopted model is kept. Every second we score the stored models
130
+ on the last 30 seconds of settled outcomes and switch to a clearly better one at once.
131
+
132
+ ## What is new here
133
+
134
+ | Idea | Why it matters |
135
+ |---|---|
136
+ | **Retrain only when it hurts.** Input alarms reweight; only error alarms act. | About two in three input alarms are harmless, so "retrain on every alarm" wastes effort and can make things worse. |
137
+ | **Regime memory.** A bank of past models and instant switching. | A returning regime is fixed in seconds instead of minutes (the delay of retraining on fresh labels). |
138
+ | **Thresholds that respect the data.** Alarm limits come from the data's own autocorrelation and from how uncertain the error baseline is. | A textbook KS test fires on 97% of checks in a calm market. Ours: about one false error alarm per hour. |
139
+ | **A market-maker score.** Forecast quality as adverse-selection ticks. | Turns "accuracy" into the quantity a trading desk cares about. |
140
+ | **Live, with the same guarantees.** The full system on a real exchange feed, 0.06 ms per forecast. | Not only a backtest: the same rules run on Binance, with training off the hot path. |
141
+ | **No leakage by construction.** Labels reach every learner only after they settle; splits are purged with an embargo. | Prevents the silent look-ahead bugs that fake most backtests. Tests enforce it, live mode included. |
142
+
143
+ We are not aware of prior work combining shift-type-aware conformal adaptation with a
144
+ regime bank for order book forecasting, but we have not done an exhaustive literature search.
145
+
146
+ ## Results
147
+
148
+ ### Simulated markets
149
+
150
+ Shifts are injected at known times, so detection delay can be measured. Every policy uses
151
+ the same model and the same retraining routine, and they differ only in *when* and *how*
152
+ they react. Mean and 95% bootstrap interval over **20 random seeds**
153
+ (`python scripts/run_benchmark.py --seeds 20`).
154
+
155
+ ![Policies compared on the recurring-regime scenario](reports/figures/policies_recurring.png)
156
+
157
+ **A harmful regime comes, goes and returns** (the scenario where regime memory matters):
158
+
159
+ | | Static | Retrain on KS alarm | Retrain on Page-Hinkley | Shift-aware | **+ regime memory** |
160
+ |---|---|---|---|---|---|
161
+ | Coverage while the harmful regime returns (promise 0.90) | 0.62 | 0.71 | 0.70 | 0.90 | **0.90** [0.897, 0.902] |
162
+ | Accuracy while it returns | 0.22 | 0.29 | 0.27 | 0.27 | **0.49** [0.49, 0.50] |
163
+ | Coverage over the whole stream | 0.72 | 0.76 | 0.83 | 0.90 | **0.90** |
164
+ | Accuracy when it commits to one answer | 0.35 | 0.42 | 0.50 | 0.66 | **0.71** [0.71, 0.72] |
165
+ | Market-maker value (ticks per step) | −0.25 | −0.15 | −0.03 | +0.08 | **+0.15** [0.14, 0.16] |
166
+ | Retrains / switches to a stored model | 0 / 0 | 1.6 / 0 | 2.9 / 0 | 3.3 / 0 | 1.7 / 2.3 |
167
+
168
+ **A harmless shift, then a harmful one** (no regime returns):
169
+
170
+ | | Static | Retrain on KS alarm | Retrain on Page-Hinkley | **Shift-aware** |
171
+ |---|---|---|---|---|
172
+ | Coverage during the harmful shift | 0.59 | 0.65 | 0.63 | **0.90** |
173
+ | Accuracy after the harmful shift | 0.22 | 0.26 | 0.49 | **0.49** |
174
+ | Coverage over the whole stream | 0.76 | 0.80 | 0.85 | **0.90** |
175
+ | Harmful shift detected by | n/a | KS in 25% of runs | Page-Hinkley in 100%, in about 26 s | CUSUM in 100%, in about 14–18 s |
176
+
177
+ **False alarms on a calm market** (no shift at all): our error monitor raises 1.1 false
178
+ alarms per hour [0.5, 1.8] (target 0.7) and retrains 0.4 times per 30-minute run, while
179
+ matching the static model's accuracy (0.625 vs 0.626).
180
+
181
+ ### Real Bitcoin order books
182
+
183
+ A calm evening on Binance (2 Dec 2026, 21:20–00:00 UTC; trained on the first hour,
184
+ calibrated on the next 20 minutes, replayed for 80 minutes):
185
+
186
+ | | Static | Retrain on KS alarm | **Shift-aware (+ memory)** |
187
+ |---|---|---|---|
188
+ | Coverage (promise 0.90) | 0.79 | 0.79 | **0.90** |
189
+ | Accuracy of the top guess | 0.69 | 0.69 | 0.69 |
190
+ | Accuracy when it commits to one answer | 0.72 | 0.72 | **0.80** |
191
+ | Says "not sure" | 28% | 28% | 63% |
192
+ | Market-maker value (ticks per step) | **+22.6** | **+22.6** | +5.6 |
193
+ | Alarms / retrains | – | 0 / 0 | 0 / 0 |
194
+
195
+ Even a quiet real market drifts enough that a calibration made 20 minutes earlier stops
196
+ holding its promise (0.79 instead of 0.90); ours held it. The price is caution: it
197
+ abstains more often, so it earns less on this measuring stick. Event windows around the
198
+ US jobs report (4 Dec), the Fed decision (9 Dec) and CPI (10 Dec) are added to the app's
199
+ Real market page as they are recorded.
200
+
201
+ **Live:** a 26-minute live session on Bitcoin (4 Dec 2026, learned from the stream) checked
202
+ 7,680 forecasts and held 89.9% coverage, with 69% accuracy when it committed to one answer.
203
+
204
+ ### Honest limitations
205
+
206
+ * **Memory only helps when regimes return.** Without a return, "shift-aware" and "+ regime
207
+ memory" are equal (0.487 vs 0.487 accuracy after the harmful shift).
208
+ * **Page-Hinkley retraining matches us after a one-time shift** (0.486 vs 0.487 accuracy).
209
+ Our advantage over it is calibration (0.90 vs 0.85 coverage) and value, not raw accuracy.
210
+ * **A harmless-only shift still costs a little.** A static model is marginally better there
211
+ (accuracy 0.621 vs 0.618; value +0.25 vs +0.23).
212
+ * **Fewer false alarms cost some speed.** The error monitor now catches a harmful shift in
213
+ 14–24 s (it was about 10–16 s when it raised three times as many false alarms).
214
+ * **One-second direction is hard.** Our policies abstain 73–88% of the time in the
215
+ simulations and 63–92% on Bitcoin. This project claims calibrated uncertainty, not
216
+ trading profit.
217
+ * **Real data is still small:** one calm window and one live session so far. The
218
+ market-maker score is a toy measuring stick that assumes fills do not depend on the
219
+ forecast; it is not a profit claim.
220
+
221
+ About 67% of KS input alarms are *not* followed by an error alarm within five minutes, and KS
222
+ catches the harmful shift in only 25–95% of runs (after 4–7 minutes), while the error monitor
223
+ catches it in 100% of runs within seconds.
224
+
225
+ ### Why textbook tests fail here (`scripts/check_calibration.py`, calm market)
226
+
227
+ | Monitor | False alarms per hour |
228
+ |---|---|
229
+ | KS test with the textbook (independent rows) threshold | **34.4** (fires on 97% of checks) |
230
+ | KS test with our block-permutation threshold | 4.4 |
231
+ | Domain-classifier AUC, same method | 0.6 |
232
+ | CUSUM on errors, run-length calibrated with baseline uncertainty | 1.4 (was 2.4 before the fix; target 0.7) |
233
+
234
+ 10 calm runs of 30 minutes each (5 hours, so every single alarm adds 0.2 per hour); the
235
+ 20-seed benchmark measures 1.1 per hour [0.5, 1.8] for the error monitor inside the full system.
236
+
237
+ Order book features are autocorrelated, so tests that assume independent rows are unusable.
238
+
239
+ ### Speed (`scripts/bench_latency.py`, one CPU core, Apple M4)
240
+
241
+ | Hot path, per snapshot | P50 | P99 |
242
+ |---|---|---|
243
+ | Incremental features | 0.014 ms | 0.018 ms |
244
+ | LightGBM probabilities | 0.027 ms | 0.037 ms |
245
+ | Weighted conformal set | 0.004 ms | 0.006 ms |
246
+ | **Total** | **0.045 ms** | **0.058 ms** (budget: 1 ms) |
247
+
248
+ The monitors run off the hot path: a KS check takes 1.5 ms and a domain-classifier check
249
+ 3.4 ms. Retraining runs in a background thread in live mode.
250
+
251
+ ## Real data and live mode
252
+
253
+ `scripts/collect_binance.py` records live Binance order books (top 20 levels every 100 ms;
254
+ public data, no account or key). Two machines can record at once and the loader merges them.
255
+
256
+ ```bash
257
+ caffeinate -dims python scripts/collect_binance.py --symbols btcusdt ethusdt # leave running
258
+ python scripts/run_real.py --calm 4 # event study with the frozen rules in configs/events.yaml
259
+ python scripts/build_real_demo.py # put the recorded windows on the app's Real market page
260
+ python scripts/build_live_models.py # retrain the models live mode starts from
261
+ ```
262
+
263
+ ## Repository map
264
+
265
+ ```
266
+ src/suretick/ the library: data, features, models, conformal, monitors, stream engine
267
+ src/suretick/live.py live mode (LiveEngine + the Binance stream); live_models/ holds its shipped models
268
+ src/suretick/web/ the app (plain HTML, CSS, JavaScript; no build step, no tracking)
269
+ scripts/ benchmark, figures, calibration check, latency, collector, real-data runners
270
+ tests/ 60 tests (leakage, conformal coverage, monitors, live engine, uploads, server safety)
271
+ docs/ LEARN.md (every idea explained from zero), DEMO.md (finals script), TEAM.md
272
+ ```
273
+
274
+ * [`docs/LEARN.md`](docs/LEARN.md): a beginner's guide to every idea, file by file.
275
+ * [`docs/DEMO.md`](docs/DEMO.md): the 3-minute demo, the backup plan and judges' questions.
276
+ * [`CONTEXT.md`](CONTEXT.md): project rules, decisions and current state.
277
+
278
+ ## Feedback and contributing
279
+
280
+ Tried it? [Tell us what worked and what did not](https://github.com/codingcrux0/conformal-prediction-lob/issues/new?template=feedback.yml)
281
+ (a short form). Found a bug? [Open a bug report](https://github.com/codingcrux0/conformal-prediction-lob/issues/new?template=bug.yml).
282
+ Pull requests are welcome: run `pytest` and `ruff check .` first.
283
+
284
+ ## References
285
+
286
+ Cont, Kukanov & Stoikov (2014), *The price impact of order book events* ·
287
+ Gibbs & Candès (2021), *Adaptive conformal inference under distribution shift* ·
288
+ Tibshirani, Barber, Candès & Ramdas (2019), *Conformal prediction under covariate shift* ·
289
+ Page (1954), *Continuous inspection schemes* ·
290
+ Cao et al. (2022), *DSLOB: a synthetic limit order book dataset for benchmarking forecasting
291
+ algorithms under distributional shift*.
292
+
293
+ ## License
294
+
295
+ MIT. See [`LICENSE`](LICENSE).
@@ -0,0 +1,267 @@
1
+ # SureTick: a market forecast that knows when it stops working
2
+
3
+ [![CI](https://github.com/codingcrux0/conformal-prediction-lob/actions/workflows/ci.yml/badge.svg)](https://github.com/codingcrux0/conformal-prediction-lob/actions/workflows/ci.yml)
4
+ [![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)
5
+
6
+ **Calibrated, shift-aware order book forecasting, live.** SureTick predicts whether a price
7
+ will go down, stay flat or go up in the next second, and wraps every forecast in an answer
8
+ set that is right 90% of the time. It watches for market changes, tells **harmless**
9
+ changes from **harmful** ones, retrains only when it must, and **remembers past regimes**
10
+ so it can switch back to the right model within seconds when a regime returns. It runs on
11
+ simulated markets, on recorded Bitcoin order books, **live on Binance**, and on your own
12
+ data.
13
+
14
+ Built by Team Triple torrent (Aditya Rai, Sarvesh Shrirao, Rushank Bhoir) for the
15
+ IITB-Optiver Perceptron Hackathon 2026, Theme 3, Problem 4: *order book forecasting under
16
+ regime shifts*.
17
+
18
+ ![Live mode forecasting Bitcoin](docs/live.png)
19
+
20
+ **Try it:** open the online demo at <https://codingcrux0.github.io/conformal-prediction-lob/>
21
+ (simulations and recorded markets), or install it to get live mode and your own data.
22
+
23
+ ## Quick start
24
+
25
+ You need Python 3.10 or newer.
26
+
27
+ ```bash
28
+ pip install git+https://github.com/codingcrux0/conformal-prediction-lob.git
29
+ suretick serve
30
+ ```
31
+
32
+ Your browser opens at `http://127.0.0.1:8000`. On macOS, run `brew install libomp` once
33
+ first (LightGBM needs it; without it the app still opens but cannot train models).
34
+
35
+ | View | What you see |
36
+ |---|---|
37
+ | **Simulations** | Five markets with changes injected at known times; four approaches compared; run any random seed. |
38
+ | **Real market** | Recorded Bitcoin order books from Binance replayed through every approach. |
39
+ | **Live** | The whole system on Binance right now: a forecast every 100 ms, checked one second later, adapting as the market moves. No account or key. |
40
+ | **Your data** | Upload an order book CSV and get the same scoreboard and charts. |
41
+
42
+ From the terminal:
43
+
44
+ ```bash
45
+ suretick live --symbol ethusdt # the app, straight on the live page (BTC, ETH, SOL start instantly)
46
+ suretick sample-data book.csv # an example order book CSV
47
+ suretick analyze book.csv # analyse an order book without the browser
48
+ ```
49
+
50
+ Only live mode uses the internet, to read Binance's public order book. The server only
51
+ accepts connections from your own computer.
52
+
53
+ <details>
54
+ <summary>Docker, and running from a clone (tests, research scripts)</summary>
55
+
56
+ ```bash
57
+ docker build -t suretick https://github.com/codingcrux0/conformal-prediction-lob.git
58
+ docker run --rm -p 127.0.0.1:8000:8000 suretick # then open http://127.0.0.1:8000
59
+ ```
60
+
61
+ ```bash
62
+ git clone https://github.com/codingcrux0/conformal-prediction-lob.git
63
+ cd conformal-prediction-lob
64
+ python -m venv .venv && source .venv/bin/activate # Windows: .venv\Scripts\activate
65
+ pip install -e ".[dev,research]"
66
+ pytest # 60 tests, about 75 seconds
67
+ python scripts/run_mvp.py # one-minute command-line demo with a figure
68
+ ```
69
+ </details>
70
+
71
+ ## The idea in 60 seconds
72
+
73
+ Order books (the lists of waiting buy and sell orders) are predictable for a moment, and then
74
+ the market changes its behaviour around news, volatility spikes or thin liquidity. A model
75
+ trained on the old behaviour keeps producing confident forecasts that are now wrong. For a
76
+ market maker that means quoting on the wrong side: **adverse selection**.
77
+
78
+ ```
79
+ order book ─► features ─► LightGBM ─► conformal answer set ─► forecast, or "not sure"
80
+ ▲ ▲
81
+ stored models ◄──────┤ │ matured labels only (no peeking ahead)
82
+ (regime memory) │ │
83
+ ┌──────┴────────┐ ┌─────┴───────────┐
84
+ │ input monitor │ │ error monitor │
85
+ │ KS + domain │ │ CUSUM on how │
86
+ │ classifier │ │ wrong we are │
87
+ └──────┬────────┘ └─────┬───────────┘
88
+ ▼ ▼
89
+ inputs only changed: keep the model, the model got worse: switch to a stored
90
+ reweight the calibration data model that fits, or retrain a new one
91
+ ```
92
+
93
+ 1. **Forecast.** Gradient-boosted trees read multi-level order-flow imbalance, queue
94
+ imbalance, spread and depth.
95
+ 2. **Stay honest.** Conformal prediction (with adaptive coverage updates) gives a set of
96
+ answers that contains the truth about 90% of the time. One answer is a confident forecast;
97
+ several answers mean "not sure" and we abstain.
98
+ 3. **Tell shifts apart.** Input-only changes (a thinner book, but the same price rule) are
99
+ usually harmless, so we keep the model and reweight the calibration data. Changes that
100
+ hurt the model's errors trigger a fix.
101
+ 4. **Remember regimes.** Every adopted model is kept. Every second we score the stored models
102
+ on the last 30 seconds of settled outcomes and switch to a clearly better one at once.
103
+
104
+ ## What is new here
105
+
106
+ | Idea | Why it matters |
107
+ |---|---|
108
+ | **Retrain only when it hurts.** Input alarms reweight; only error alarms act. | About two in three input alarms are harmless, so "retrain on every alarm" wastes effort and can make things worse. |
109
+ | **Regime memory.** A bank of past models and instant switching. | A returning regime is fixed in seconds instead of minutes (the delay of retraining on fresh labels). |
110
+ | **Thresholds that respect the data.** Alarm limits come from the data's own autocorrelation and from how uncertain the error baseline is. | A textbook KS test fires on 97% of checks in a calm market. Ours: about one false error alarm per hour. |
111
+ | **A market-maker score.** Forecast quality as adverse-selection ticks. | Turns "accuracy" into the quantity a trading desk cares about. |
112
+ | **Live, with the same guarantees.** The full system on a real exchange feed, 0.06 ms per forecast. | Not only a backtest: the same rules run on Binance, with training off the hot path. |
113
+ | **No leakage by construction.** Labels reach every learner only after they settle; splits are purged with an embargo. | Prevents the silent look-ahead bugs that fake most backtests. Tests enforce it, live mode included. |
114
+
115
+ We are not aware of prior work combining shift-type-aware conformal adaptation with a
116
+ regime bank for order book forecasting, but we have not done an exhaustive literature search.
117
+
118
+ ## Results
119
+
120
+ ### Simulated markets
121
+
122
+ Shifts are injected at known times, so detection delay can be measured. Every policy uses
123
+ the same model and the same retraining routine, and they differ only in *when* and *how*
124
+ they react. Mean and 95% bootstrap interval over **20 random seeds**
125
+ (`python scripts/run_benchmark.py --seeds 20`).
126
+
127
+ ![Policies compared on the recurring-regime scenario](reports/figures/policies_recurring.png)
128
+
129
+ **A harmful regime comes, goes and returns** (the scenario where regime memory matters):
130
+
131
+ | | Static | Retrain on KS alarm | Retrain on Page-Hinkley | Shift-aware | **+ regime memory** |
132
+ |---|---|---|---|---|---|
133
+ | Coverage while the harmful regime returns (promise 0.90) | 0.62 | 0.71 | 0.70 | 0.90 | **0.90** [0.897, 0.902] |
134
+ | Accuracy while it returns | 0.22 | 0.29 | 0.27 | 0.27 | **0.49** [0.49, 0.50] |
135
+ | Coverage over the whole stream | 0.72 | 0.76 | 0.83 | 0.90 | **0.90** |
136
+ | Accuracy when it commits to one answer | 0.35 | 0.42 | 0.50 | 0.66 | **0.71** [0.71, 0.72] |
137
+ | Market-maker value (ticks per step) | −0.25 | −0.15 | −0.03 | +0.08 | **+0.15** [0.14, 0.16] |
138
+ | Retrains / switches to a stored model | 0 / 0 | 1.6 / 0 | 2.9 / 0 | 3.3 / 0 | 1.7 / 2.3 |
139
+
140
+ **A harmless shift, then a harmful one** (no regime returns):
141
+
142
+ | | Static | Retrain on KS alarm | Retrain on Page-Hinkley | **Shift-aware** |
143
+ |---|---|---|---|---|
144
+ | Coverage during the harmful shift | 0.59 | 0.65 | 0.63 | **0.90** |
145
+ | Accuracy after the harmful shift | 0.22 | 0.26 | 0.49 | **0.49** |
146
+ | Coverage over the whole stream | 0.76 | 0.80 | 0.85 | **0.90** |
147
+ | Harmful shift detected by | n/a | KS in 25% of runs | Page-Hinkley in 100%, in about 26 s | CUSUM in 100%, in about 14–18 s |
148
+
149
+ **False alarms on a calm market** (no shift at all): our error monitor raises 1.1 false
150
+ alarms per hour [0.5, 1.8] (target 0.7) and retrains 0.4 times per 30-minute run, while
151
+ matching the static model's accuracy (0.625 vs 0.626).
152
+
153
+ ### Real Bitcoin order books
154
+
155
+ A calm evening on Binance (2 Dec 2026, 21:20–00:00 UTC; trained on the first hour,
156
+ calibrated on the next 20 minutes, replayed for 80 minutes):
157
+
158
+ | | Static | Retrain on KS alarm | **Shift-aware (+ memory)** |
159
+ |---|---|---|---|
160
+ | Coverage (promise 0.90) | 0.79 | 0.79 | **0.90** |
161
+ | Accuracy of the top guess | 0.69 | 0.69 | 0.69 |
162
+ | Accuracy when it commits to one answer | 0.72 | 0.72 | **0.80** |
163
+ | Says "not sure" | 28% | 28% | 63% |
164
+ | Market-maker value (ticks per step) | **+22.6** | **+22.6** | +5.6 |
165
+ | Alarms / retrains | – | 0 / 0 | 0 / 0 |
166
+
167
+ Even a quiet real market drifts enough that a calibration made 20 minutes earlier stops
168
+ holding its promise (0.79 instead of 0.90); ours held it. The price is caution: it
169
+ abstains more often, so it earns less on this measuring stick. Event windows around the
170
+ US jobs report (4 Dec), the Fed decision (9 Dec) and CPI (10 Dec) are added to the app's
171
+ Real market page as they are recorded.
172
+
173
+ **Live:** a 26-minute live session on Bitcoin (4 Dec 2026, learned from the stream) checked
174
+ 7,680 forecasts and held 89.9% coverage, with 69% accuracy when it committed to one answer.
175
+
176
+ ### Honest limitations
177
+
178
+ * **Memory only helps when regimes return.** Without a return, "shift-aware" and "+ regime
179
+ memory" are equal (0.487 vs 0.487 accuracy after the harmful shift).
180
+ * **Page-Hinkley retraining matches us after a one-time shift** (0.486 vs 0.487 accuracy).
181
+ Our advantage over it is calibration (0.90 vs 0.85 coverage) and value, not raw accuracy.
182
+ * **A harmless-only shift still costs a little.** A static model is marginally better there
183
+ (accuracy 0.621 vs 0.618; value +0.25 vs +0.23).
184
+ * **Fewer false alarms cost some speed.** The error monitor now catches a harmful shift in
185
+ 14–24 s (it was about 10–16 s when it raised three times as many false alarms).
186
+ * **One-second direction is hard.** Our policies abstain 73–88% of the time in the
187
+ simulations and 63–92% on Bitcoin. This project claims calibrated uncertainty, not
188
+ trading profit.
189
+ * **Real data is still small:** one calm window and one live session so far. The
190
+ market-maker score is a toy measuring stick that assumes fills do not depend on the
191
+ forecast; it is not a profit claim.
192
+
193
+ About 67% of KS input alarms are *not* followed by an error alarm within five minutes, and KS
194
+ catches the harmful shift in only 25–95% of runs (after 4–7 minutes), while the error monitor
195
+ catches it in 100% of runs within seconds.
196
+
197
+ ### Why textbook tests fail here (`scripts/check_calibration.py`, calm market)
198
+
199
+ | Monitor | False alarms per hour |
200
+ |---|---|
201
+ | KS test with the textbook (independent rows) threshold | **34.4** (fires on 97% of checks) |
202
+ | KS test with our block-permutation threshold | 4.4 |
203
+ | Domain-classifier AUC, same method | 0.6 |
204
+ | CUSUM on errors, run-length calibrated with baseline uncertainty | 1.4 (was 2.4 before the fix; target 0.7) |
205
+
206
+ 10 calm runs of 30 minutes each (5 hours, so every single alarm adds 0.2 per hour); the
207
+ 20-seed benchmark measures 1.1 per hour [0.5, 1.8] for the error monitor inside the full system.
208
+
209
+ Order book features are autocorrelated, so tests that assume independent rows are unusable.
210
+
211
+ ### Speed (`scripts/bench_latency.py`, one CPU core, Apple M4)
212
+
213
+ | Hot path, per snapshot | P50 | P99 |
214
+ |---|---|---|
215
+ | Incremental features | 0.014 ms | 0.018 ms |
216
+ | LightGBM probabilities | 0.027 ms | 0.037 ms |
217
+ | Weighted conformal set | 0.004 ms | 0.006 ms |
218
+ | **Total** | **0.045 ms** | **0.058 ms** (budget: 1 ms) |
219
+
220
+ The monitors run off the hot path: a KS check takes 1.5 ms and a domain-classifier check
221
+ 3.4 ms. Retraining runs in a background thread in live mode.
222
+
223
+ ## Real data and live mode
224
+
225
+ `scripts/collect_binance.py` records live Binance order books (top 20 levels every 100 ms;
226
+ public data, no account or key). Two machines can record at once and the loader merges them.
227
+
228
+ ```bash
229
+ caffeinate -dims python scripts/collect_binance.py --symbols btcusdt ethusdt # leave running
230
+ python scripts/run_real.py --calm 4 # event study with the frozen rules in configs/events.yaml
231
+ python scripts/build_real_demo.py # put the recorded windows on the app's Real market page
232
+ python scripts/build_live_models.py # retrain the models live mode starts from
233
+ ```
234
+
235
+ ## Repository map
236
+
237
+ ```
238
+ src/suretick/ the library: data, features, models, conformal, monitors, stream engine
239
+ src/suretick/live.py live mode (LiveEngine + the Binance stream); live_models/ holds its shipped models
240
+ src/suretick/web/ the app (plain HTML, CSS, JavaScript; no build step, no tracking)
241
+ scripts/ benchmark, figures, calibration check, latency, collector, real-data runners
242
+ tests/ 60 tests (leakage, conformal coverage, monitors, live engine, uploads, server safety)
243
+ docs/ LEARN.md (every idea explained from zero), DEMO.md (finals script), TEAM.md
244
+ ```
245
+
246
+ * [`docs/LEARN.md`](docs/LEARN.md): a beginner's guide to every idea, file by file.
247
+ * [`docs/DEMO.md`](docs/DEMO.md): the 3-minute demo, the backup plan and judges' questions.
248
+ * [`CONTEXT.md`](CONTEXT.md): project rules, decisions and current state.
249
+
250
+ ## Feedback and contributing
251
+
252
+ Tried it? [Tell us what worked and what did not](https://github.com/codingcrux0/conformal-prediction-lob/issues/new?template=feedback.yml)
253
+ (a short form). Found a bug? [Open a bug report](https://github.com/codingcrux0/conformal-prediction-lob/issues/new?template=bug.yml).
254
+ Pull requests are welcome: run `pytest` and `ruff check .` first.
255
+
256
+ ## References
257
+
258
+ Cont, Kukanov & Stoikov (2014), *The price impact of order book events* ·
259
+ Gibbs & Candès (2021), *Adaptive conformal inference under distribution shift* ·
260
+ Tibshirani, Barber, Candès & Ramdas (2019), *Conformal prediction under covariate shift* ·
261
+ Page (1954), *Continuous inspection schemes* ·
262
+ Cao et al. (2022), *DSLOB: a synthetic limit order book dataset for benchmarking forecasting
263
+ algorithms under distributional shift*.
264
+
265
+ ## License
266
+
267
+ MIT. See [`LICENSE`](LICENSE).
@@ -0,0 +1,56 @@
1
+ [build-system]
2
+ requires = ["setuptools>=77"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "suretick"
7
+ version = "0.3.0"
8
+ description = "Order book forecasting that knows when it stops working: calibrated, shift-aware, live"
9
+ requires-python = ">=3.10"
10
+ readme = "README.md"
11
+ license = "MIT"
12
+ license-files = ["LICENSE"]
13
+ authors = [
14
+ { name = "Aditya Rai" },
15
+ { name = "Sarvesh Shrirao" },
16
+ { name = "Rushank Bhoir" },
17
+ ]
18
+ dependencies = [
19
+ "numpy>=1.26",
20
+ "pandas>=2.1",
21
+ "scipy>=1.11",
22
+ "scikit-learn>=1.4",
23
+ "lightgbm>=4.3",
24
+ "websockets>=12.0", # live mode and the recorder: Binance's public order book stream
25
+ "certifi>=2024.2",
26
+ ]
27
+
28
+ [project.scripts]
29
+ suretick = "suretick.cli:main"
30
+
31
+ [project.urls]
32
+ Homepage = "https://github.com/codingcrux0/conformal-prediction-lob"
33
+ Demo = "https://codingcrux0.github.io/conformal-prediction-lob/"
34
+ Issues = "https://github.com/codingcrux0/conformal-prediction-lob/issues"
35
+
36
+ [project.optional-dependencies]
37
+ research = ["matplotlib>=3.8", "pyyaml>=6.0"] # figures and the real-data runner
38
+ dev = ["pytest>=8.0", "ruff>=0.5", "pip-audit>=2.7"]
39
+
40
+ [tool.setuptools.packages.find]
41
+ where = ["src"]
42
+
43
+ [tool.setuptools.package-data]
44
+ suretick = ["web/*", "web/data/*.json", "live_models/*.json.gz"]
45
+
46
+ [tool.pytest.ini_options]
47
+ testpaths = ["tests"]
48
+ addopts = "-q"
49
+
50
+ [tool.ruff]
51
+ line-length = 110
52
+ target-version = "py310"
53
+
54
+ [tool.ruff.lint]
55
+ select = ["E", "F", "W", "I", "B", "UP", "NPY"]
56
+ ignore = ["B905"] # zip(strict=) needs Python 3.10+ style everywhere; not worth the noise
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,3 @@
1
+ """Calibrated, shift-aware limit order book forecasting."""
2
+
3
+ __version__ = "0.3.0"