Examples Catalog
Every example below is a file in this repository that you can execute. The point of this page is that you should not have to trust a snippet: each entry names the file, the command that runs it, and what it demonstrates.
All of them need the Python bindings built first — see
Python Bindings Setup. The Rust example needs
only cargo.
Examples that ship in the repo
Section titled “Examples that ship in the repo”| File | Run it with | Demonstrates |
|---|---|---|
crates/openquant/examples/research_notebook_smoke.rs | cargo run -p openquant --example research_notebook_smoke | The minimum Rust path: CUSUM event sampling → max-Sharpe allocation → VaR/ES/CDaR. ~30 lines, asserts its own invariants. |
notebooks/python/scripts/run_notebooks.py | just notebooks-run | Executes every research notebook in a Jupyter kernel and saves their tables and figures; fails on any cell error. CI runs it and checks the committed outputs are current. |
notebooks/python/scripts/smoke_all.py | just notebook-smoke | Plain-script mirror of the core notebook calls; a quick check that the Python surface works. |
experiments/run_pipeline.py | just exp-run | A config-driven pipeline run (experiments/configs/futures_oil_baseline.toml) that writes artifacts to experiments/artifacts. |
python/benchmarks/benchmark_pipeline.py | just py-bench | Times the mid-frequency pipeline over 30 iterations at 2048 bars. |
python/benchmarks/benchmark_data_processing.py | just py-bench-data | Time and throughput of openquant.data loading (CSV and Parquet), cleaning, calendar alignment and quality reporting at 200k rows × 4 symbols. The committed results are on the Performance page. |
python/tests/ | just py-test | 46 test modules, many of which double as usage examples — test_pipeline_api.py and test_bindings_contract.py are the two worth reading first. |
The thirteen notebooks under notebooks/python/ are numbered in reading
order, from 01_event_labeling_and_pipeline.ipynb to
14_fracdiff_features_classifier.ipynb (06 was removed; 11 replaces it). Notebooks 09 to 14 are
research runbooks, listed under Runbooks below and in the research gallery; 01 to 08 are
API tours that claim nothing (see the notebook contract). They are committed with their outputs,
and their figures are exported to docs-site/public/figures/notebooks/.
All of them run on synthetic data: 09 to 14 read the SYNTHETIC SYN_A..SYN_E
sample through openquant.data.fetch, and run on real data only when you pass your own source.
Figures from the executed notebooks
Section titled “Figures from the executed notebooks”just notebooks-run writes each notebook figure here in both site themes. These two come from
11_meta_labeling_triple_barrier.ipynb and describe SYNTHETIC price paths with a planted
signal, not a real market.
Runbooks
Section titled “Runbooks”Studies that state a hypothesis up front and end with a promotion decision, each with a page under Research. The research gallery shows every runbook’s hypothesis, figures, result and decision on one page:
- Fracdiff: stationarity versus memory —
09_fracdiff_stationarity_memory.ipynb, the FFD d-sweep (AFML Fig. 5.5) on SYNTHETIC data, checked against simulated series of known memory. - HRP vs IVP and CLA out of sample —
10_hrp_vs_ivp_cla_oos.ipynb, AFML §16.6’s Monte Carlo on SYNTHETIC data. - Triple-barrier labeling and meta-labeling —
11_meta_labeling_triple_barrier.ipynb, primary vs meta-labeled bets in purged k-fold, net of costs, deflated by a trial registry, on SYNTHETIC paths with a planted signal and a no-signal control. - CPCV backtest with PSR and deflated Sharpe —
12_cpcv_deflated_sharpe.ipynb, best-of-74 strategy selection judged by CPCV, PSR and DSR on a SYNTHETIC no-signal control and a planted signal. - Bet sizing from predicted probabilities —
13_bet_sizing_from_probabilities.ipynb, the pre-registered test of runbook 11’s post hoc finding: AFML Snippets 10.1-10.3 probability sizing against a flat 0.5 meta filter, with every sizing choice registered as a trial, on SYNTHETIC planted-signal paths and no-signal controls. - Fracdiff features against returns in a classifier —
14_fracdiff_features_classifier.ipynb, runbook 09’s open question: FFD features against returns and integer differences as purged-CV classifier inputs, net of costs, deflated by a trial registry, on SYNTHETIC paths with a planted signal and no-signal controls.
Worked example: cleaning a messy OHLCV file
Section titled “Worked example: cleaning a messy OHLCV file”The repository ships a deliberately awful CSV at
python/tests/fixtures/ohlcv_us_equities.csv — non-canonical column
names, two symbols interleaved, rows out of chronological order, and a
duplicated 2024-01-02 bar for AAPL:
Date,Ticker,Open,High,Low,Close,Volume,Adj Close2024-01-03,AAPL,186.10,187.00,185.50,186.30,5000,186.102024-01-01,AAPL,184.00,185.00,183.50,184.80,8000,184.702024-01-02,AAPL,185.00,186.20,184.70,185.90,7000,185.802024-01-02,AAPL,185.05,186.30,184.60,186.00,7100,185.902024-01-01,MSFT,370.10,372.00,369.90,371.50,6000,371.302024-01-03,MSFT,372.20,373.10,371.40,372.00,5500,371.90load_ohlcv canonicalises the header (Date → ts, Ticker →
symbol, Adj Close → adj_close), sorts by symbol and timestamp, and
drops the duplicate — keeping the last occurrence by default, which is
the convention you want when a vendor restates a bar:
from openquant import data
frame, report = data.load_ohlcv( "python/tests/fixtures/ohlcv_us_equities.csv", return_report=True,)print(frame)print(report)Ask for the report whenever the data is not yours. Silent deduplication is how a survivorship or restatement bug gets into a backtest without anyone noticing.
Worked example: the whole research loop
Section titled “Worked example: the whole research loop”The end-to-end loops are long enough to deserve their own pages, with the stages explained rather than just listed:
- Rust Core Workflow — one program: event sampling → triple-barrier labels → bet sizing → purged CV → risk and allocation, with its printed output.
- Python Core Workflow — the same ground in Python, ending in a promotion decision.
- Notebook Research Workflow — the notebook-driven version.
Where to start
Section titled “Where to start”| If you want to… | Go here |
|---|---|
| See OpenQuant produce a number, once | Quickstart |
| Understand the Rust API | Rust Core Workflow |
| Understand the Python API | Python Core Workflow |
| Look up one module | Modules by AFML chapter |
| Build and test the repo | Local Build Setup |
Worked examples on other pages
Section titled “Worked examples on other pages”This page works two examples itself. The others live next to the method they exercise, where their output is checked by the docs gates:
- Feature diagnostics (MDI, MDA and SFI on the same features, with purged folds): Feature diagnostics, and the Rust side in Feature importance.
- A CPCV backtest: Backtesting engine for the splits and paths, and runbook 12 for CPCV path Sharpe ratios next to PSR and the deflated Sharpe ratio.