lucyd-backtest 0.1.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.
- lucyd_backtest-0.1.0/.gitignore +8 -0
- lucyd_backtest-0.1.0/AGENTS.md +62 -0
- lucyd_backtest-0.1.0/Cargo.lock +2117 -0
- lucyd_backtest-0.1.0/Cargo.toml +47 -0
- lucyd_backtest-0.1.0/LICENSE +21 -0
- lucyd_backtest-0.1.0/PKG-INFO +152 -0
- lucyd_backtest-0.1.0/README.md +308 -0
- lucyd_backtest-0.1.0/docs/data-format.md +167 -0
- lucyd_backtest-0.1.0/docs/execution.md +94 -0
- lucyd_backtest-0.1.0/examples/basic.rs +47 -0
- lucyd_backtest-0.1.0/examples/ladder_mm.rs +39 -0
- lucyd_backtest-0.1.0/examples/late_side.rs +31 -0
- lucyd_backtest-0.1.0/examples/strategies/ladder_mm.rs +138 -0
- lucyd_backtest-0.1.0/examples/strategies/late_side.rs +98 -0
- lucyd_backtest-0.1.0/lucyd_backtest/__init__.py +477 -0
- lucyd_backtest-0.1.0/lucyd_backtest/_native.pyi +34 -0
- lucyd_backtest-0.1.0/lucyd_backtest/py.typed +0 -0
- lucyd_backtest-0.1.0/pyproject.toml +31 -0
- lucyd_backtest-0.1.0/python/Cargo.toml +21 -0
- lucyd_backtest-0.1.0/python/LICENSE +21 -0
- lucyd_backtest-0.1.0/python/README.md +129 -0
- lucyd_backtest-0.1.0/python/examples/late_side.py +44 -0
- lucyd_backtest-0.1.0/python/src/lib.rs +672 -0
- lucyd_backtest-0.1.0/python/tests/test_package.py +96 -0
- lucyd_backtest-0.1.0/src/book.rs +512 -0
- lucyd_backtest-0.1.0/src/cache.rs +167 -0
- lucyd_backtest-0.1.0/src/engine/html/report.css +173 -0
- lucyd_backtest-0.1.0/src/engine/html/report.js +399 -0
- lucyd_backtest-0.1.0/src/engine/html.rs +386 -0
- lucyd_backtest-0.1.0/src/engine/mod.rs +373 -0
- lucyd_backtest-0.1.0/src/engine/params.rs +134 -0
- lucyd_backtest-0.1.0/src/engine/report.rs +919 -0
- lucyd_backtest-0.1.0/src/engine/risk.rs +390 -0
- lucyd_backtest-0.1.0/src/engine/strategy.rs +543 -0
- lucyd_backtest-0.1.0/src/engine/tests.rs +863 -0
- lucyd_backtest-0.1.0/src/engine/world.rs +1150 -0
- lucyd_backtest-0.1.0/src/error.rs +63 -0
- lucyd_backtest-0.1.0/src/event.rs +538 -0
- lucyd_backtest-0.1.0/src/export.rs +461 -0
- lucyd_backtest-0.1.0/src/fills.rs +441 -0
- lucyd_backtest-0.1.0/src/http.rs +184 -0
- lucyd_backtest-0.1.0/src/lib.rs +57 -0
- lucyd_backtest-0.1.0/src/loader.rs +1217 -0
- lucyd_backtest-0.1.0/src/main.rs +494 -0
- lucyd_backtest-0.1.0/src/market.rs +267 -0
- lucyd_backtest-0.1.0/src/network.rs +123 -0
- lucyd_backtest-0.1.0/src/pq.rs +78 -0
- lucyd_backtest-0.1.0/src/prices.rs +125 -0
- lucyd_backtest-0.1.0/src/request.rs +660 -0
- lucyd_backtest-0.1.0/src/scaffold.rs +57 -0
- lucyd_backtest-0.1.0/src/trades.rs +315 -0
- lucyd_backtest-0.1.0/templates/new/Cargo.toml.tmpl +16 -0
- lucyd_backtest-0.1.0/templates/new/gitignore +2 -0
- lucyd_backtest-0.1.0/templates/new/main.rs.tmpl +162 -0
- lucyd_backtest-0.1.0/tests/live_engine.rs +76 -0
- lucyd_backtest-0.1.0/tests/live_testnet.rs +197 -0
- lucyd_backtest-0.1.0/tests/new_project.rs +94 -0
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# Notes for coding agents
|
|
2
|
+
|
|
3
|
+
How to work with this repo and with strategies built on it. Human docs: [README.md](README.md), [docs/execution.md](docs/execution.md), [docs/data-format.md](docs/data-format.md).
|
|
4
|
+
|
|
5
|
+
## What this is
|
|
6
|
+
|
|
7
|
+
A Rust crate and CLI: a loader that downloads historical data of Lucyd prediction markets (L2 order book, order book fills, on-chain trades, Pyth BTC/USD prices) and an engine that backtests a strategy on it. Markets: "will BTC be higher at the end of the period than at the start", periods 5m, 15m, 30m, 1h, 4h, 1d. Outcome 0 is up, 1 is down. Tokens trade from 0.0025 to 0.9975 USDC in steps of 0.0025; the winner pays 1, a voided market pays 0.5 per token. Testnet: liquidity is mostly the platform's own market maker, so results are not real-money estimates.
|
|
8
|
+
|
|
9
|
+
## Writing a strategy in Python
|
|
10
|
+
|
|
11
|
+
`pip install lucyd-backtest`, then `lb.load(...)` for data as pandas DataFrames and a subclass of `lb.Strategy` run with `lb.backtest(data, strategy, **params)`. Same handlers, context and parameters as the Rust API below; outcomes may be `"up"` / `"down"`, sides `"buy"` / `"sell"`. Guide: [python/README.md](python/README.md), example: [python/examples/late_side.py](python/examples/late_side.py). An exception in a handler stops the run and is raised from `backtest`.
|
|
12
|
+
|
|
13
|
+
## Writing a strategy in Rust
|
|
14
|
+
|
|
15
|
+
Fastest path, outside this repo:
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
cargo install --git https://github.com/lucyd-trade/lucyd-backtest
|
|
19
|
+
lucyd-backtest new my-strategy && cd my-strategy
|
|
20
|
+
cargo run --release # summary in the terminal, reports/report.json and reports/report.html
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Edit `src/main.rs` of the new project. A strategy implements `engine::Strategy`; all handlers have empty defaults:
|
|
24
|
+
|
|
25
|
+
- `subscriptions()` picks what to receive: book (every change, `BookFeed::Throttle(ms)` or none), fills, prices, a timer.
|
|
26
|
+
- Handlers: `on_start`, `on_market_open`, `on_book`, `on_fill` (others' recorded fills), `on_price`, `on_timer`, `on_market_close` (result unknown yet), `on_resolution`, `on_order` (own order updates), `on_finish`.
|
|
27
|
+
- `Ctx` sees only the past: `now_ms`, `open_markets`, `market`, `book`, `best_bid` / `best_ask`, `market_price`, `usdc`, `position`, `open_orders`.
|
|
28
|
+
- Orders: `ctx.limit(market, outcome, side, price, tokens)` (GTC), `ctx.market_order(market, outcome, side, tokens, Some(worst_price))` (FAK), `ctx.cancel(id)`. Tokens are whole numbers; prices must be on the 0.0025 grid.
|
|
29
|
+
|
|
30
|
+
Worked examples: [examples/strategies/late_side.rs](examples/strategies/late_side.rs) (taker near the end of a market), [examples/strategies/ladder_mm.rs](examples/strategies/ladder_mm.rs) (maker ladder around the mid).
|
|
31
|
+
|
|
32
|
+
Without writing code, the CLI runs the built-in examples:
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
lucyd-backtest run --strategy late-side --period 5m --from 2026-10-27 --to 2026-10-28 --report out/r.json --html
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Reading results
|
|
39
|
+
|
|
40
|
+
`Report` JSON has `summary` (whole run) and the fit / validation split (`validation_fraction`, last 30% of the range by default). Judge a strategy on the validation sample, and look at `warnings`: too few own fills, missing data. Same data, params and seed give the same report, so compare runs by changing one thing at a time.
|
|
41
|
+
|
|
42
|
+
## Picking data
|
|
43
|
+
|
|
44
|
+
- Fills (default) exist from about 2026-10-20, Pyth prices from 2026-10-27, order books from early October 2026, on-chain trades from 2026-10-07.
|
|
45
|
+
- Start with one period and one day; a day of 5m markets is 288 markets.
|
|
46
|
+
- `lucyd-backtest list ...` shows what a request selects before downloading. `--fills-only` drops markets nobody traded.
|
|
47
|
+
- Data is cached; finished markets are never downloaded twice.
|
|
48
|
+
|
|
49
|
+
## Working on the crate itself
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
cargo fmt --check
|
|
53
|
+
cargo clippy --all-targets -- -D warnings
|
|
54
|
+
cargo test
|
|
55
|
+
cargo test -- --ignored # hits the live testnet and builds a `new` project; needs network
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
CI runs the first three on Linux, macOS and Windows. Python bindings: `python/` (PyO3 crate `lucyd-backtest-py` in `src/lib.rs`, package `lucyd_backtest/`); build locally with `maturin develop --release -m python/Cargo.toml` in a virtualenv, test with `pytest python/tests` (`LUCYD_LIVE=1` for the testnet test). Workflow `.github/workflows/python.yml` builds wheels and publishes to PyPI on a `py-v<version>` tag. Layout: `src/loader.rs` + `request.rs` (what to load), `book.rs`, `fills.rs`, `trades.rs`, `prices.rs` (sources), `event.rs` (event stream), `engine/` (execution, report, risk, HTML), `scaffold.rs` + `templates/new/` (the `new` command), `main.rs` (CLI). If you change the public API or the report format, update README.md and the docs in the same change.
|
|
59
|
+
|
|
60
|
+
## Live data in an agent
|
|
61
|
+
|
|
62
|
+
For current markets, order books and trades (not history), connect the [Lucyd MCP server](https://github.com/lucyd-trade/mcp-server): hosted read-only at `https://mcp.lucyd.trade/mcp`, or locally with `npx -y @lucyd/mcp`.
|