Skip to main content

lucyd-backtest for Python

Backtest trading strategies on Lucyd prediction markets from Python or Jupyter. Load the historical L2 order book, order book fills, on-chain trades and Pyth BTC/USD prices as pandas DataFrames. Then run a strategy written in Python on an execution engine that models latency, slippage on the recorded book and queue position. The engine is written in Rust and ships as a compiled wheel, so there is nothing to build.

Lucyd currently runs on the Ethereum Sepolia testnet with test USDC. Most testnet liquidity comes from the platform's own market maker, so use the data to build and test strategies, not to estimate real-money returns.

pip install lucyd-backtest

Python 3.9 or newer on Linux, macOS or Windows. Dependencies: pandas and pyarrow.

The markets

Every market asks one question: will BTC be higher at the end of its period than at the start? The price at the start is the strike. The market settles on the Pyth BTC/USD price at the end. There are two outcome tokens, up (0) and down (1). They trade from 0.0025 to 0.9975 USDC in steps of 0.0025, and the winning token pays 1 USDC. A market that gets no oracle price in time is voided, and each token then pays 0.5. Series run back to back: 5m, 15m, 30m, 1h, 4h and 1d.

Data

import lucyd_backtest as lb

data = lb.load(period="15m", start="2026-10-26", end="2026-10-28")

data.markets          # one row per market: start, end, strike, resolved price, winner, volume
data.fills()          # every match of the order book: price, size, side, taker, maker, settlement
data.prices()         # Pyth ticks: price, conf, publish time
data.book_updates()   # recorded book snapshots and deltas, one row per level
data.events()         # everything above as one time-ordered stream

book = data.book_at(data.markets.address[0], "2026-10-26T00:05:00Z")
book.best_bid("up"), book.asks("down")

load arguments:

  • period: "5m", "15m", "30m", "1h", "4h" or "1d", or a list of them. The default is all.
  • start, end: "2026-10-27", "2026-10-27T12:00:00Z", unix seconds, a date or a datetime, all in UTC. A market is selected if it overlaps the range.
  • data: any of "book", "fills", "trades" and "prices", or "all". The default is book, fills and prices.
  • fills_only=True: leaves out markets nobody traded.
  • market_ids: specific market addresses, used instead of a period and range.

lb.markets(...) takes the same arguments and returns only the market list, without downloading data.

Downloads are cached, and a finished market is never downloaded twice.

What is available from when:

Data Since
Fills about 2026-10-20
Order book early October 2026
On-chain trades 2026-10-07
Pyth prices 2026-10-27

Gaps in the data show up as warnings in data.warnings; they are never errors.

Backtest

class LateSide(lb.Strategy):
    timer_ms = 1000

    def __init__(self, seconds_before=30, max_price=0.95):
        self.seconds_before, self.max_price, self.done = seconds_before, max_price, set()

    def on_timer(self, ctx):
        for m in ctx.open_markets():
            if m.address in self.done or m.time_to_end_ms > self.seconds_before * 1000:
                continue
            btc = ctx.market_price(m.address)
            if m.strike is None or btc is None:
                continue
            side = "up" if btc.price > m.strike else "down"
            ask = ctx.best_ask(m.address, side)
            if ask and ask.price <= self.max_price:
                ctx.market_order(m.address, side, "buy", 20, limit=self.max_price)
                self.done.add(m.address)

report = lb.backtest(data, LateSide(), initial_usdc=1000, submit_latency_ms=100)
report                       # in Jupyter: the full interactive report
report.summary["pnl"], report.validation
report.equity, report.orders, report.markets   # DataFrames
report.save_html("report.html")                # or report.show() outside Jupyter

Handlers. Implement only the ones you need; the engine calls only what you implement:

  • on_start, on_finish;
  • on_market_open(ctx, market), on_market_close(ctx, market);
  • on_resolution(ctx, market, result);
  • on_book(ctx, market): the book changed. Set book_throttle_ms to get it at most once per that many ms;
  • on_fill(ctx, fill): a recorded fill of another participant;
  • on_price(ctx, feed, price);
  • on_timer(ctx): needs timer_ms;
  • on_order(ctx, update): your order was accepted, filled, cancelled or rejected.

Context. ctx shows the state as of now and nothing later:

  • markets: ctx.open_markets(), ctx.market(address);
  • book and prices: ctx.book(address), ctx.best_bid(address, outcome), ctx.best_ask(...), ctx.market_price(address);
  • account: ctx.usdc, ctx.position(address, outcome), ctx.open_orders();
  • orders: ctx.limit(address, outcome, side, price, tokens) places a GTC order, ctx.market_order(address, outcome, side, tokens, limit=None) a FAK order, and ctx.cancel(order_id) cancels.

An outcome is "up" / 0 or "down" / 1. A side is "buy" or "sell". Prices are on the 0.0025 grid, and tokens are whole numbers.

Execution parameters. Pass these as keywords to backtest:

Parameter Default
initial_usdc 1000
submit_latency_ms, cancel_latency_ms 100
latency_jitter_ms 0
queue_model "pessimistic" (or "proportional")
fee_bps 0
revert_probability 0
seed 0
validation_fraction 0.3
risk_interval_ms 3600000
annualize False

The same data, parameters and seed give the same report.

Validation sample. The last 30% of the range is reported separately as report.validation. Judge a strategy on that sample.

How orders execute, and the limits of the model: docs/execution.md. Event columns: docs/data-format.md. A full example: examples/late_side.py.

License

MIT

Metadata

Release files for lucyd-backtest 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for lucyd-backtest 0.1.0
File Size Uploaded Provenance Actions
lucyd_backtest-0.1.0.tar.gz 161.8 kB With provenance Details for lucyd_backtest-0.1.0.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for lucyd-backtest 0.1.0
File Actions
lucyd_backtest-0.1.0-cp39-abi3-win_amd64.whl CPython 3.9 abi3 Windows x86-64 Details for lucyd_backtest-0.1.0-cp39-abi3-win_amd64.whl
lucyd_backtest-0.1.0-cp39-abi3-manylinux_2_28_aarch64.whl CPython 3.9 abi3 Linux glibc 2.28+ ARM64 Details for lucyd_backtest-0.1.0-cp39-abi3-manylinux_2_28_aarch64.whl
lucyd_backtest-0.1.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.9 abi3 Linux glibc 2.17+ x86-64 Details for lucyd_backtest-0.1.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
lucyd_backtest-0.1.0-cp39-abi3-macosx_11_0_arm64.whl CPython 3.9 abi3 macOS 11.0+ ARM64 Details for lucyd_backtest-0.1.0-cp39-abi3-macosx_11_0_arm64.whl
lucyd_backtest-0.1.0-cp39-abi3-macosx_10_12_x86_64.whl CPython 3.9 abi3 macOS 10.12+ x86-64 Details for lucyd_backtest-0.1.0-cp39-abi3-macosx_10_12_x86_64.whl

Total release size: 22.9 MB

Release files / lucyd_backtest-0.1.0.tar.gz

Download URL lucyd_backtest-0.1.0.tar.gz
Size 161.8 kB
Tags Source
SHA-256 checksum
How to use checksums
34bf9a7d611ce609ecd6e2f9e2e610ad041b8da7ed81732cb68f0f3e3afe6d5c
BLAKE2b-256 checksum
How to use checksums
06072c3c8e3e08904b6afab2a5e4f9829f12f6c4167a1ab302c3012fde83eef2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 28, 2026.

Transparency log

Release files / lucyd_backtest-0.1.0-cp39-abi3-win_amd64.whl

Download URL lucyd_backtest-0.1.0-cp39-abi3-win_amd64.whl
Size 4.5 MB
Tags CPython 3.9 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
e2865374376c74ecc08555c8bd1b0c5892fc679e8d52add80913d3bd88226e13
BLAKE2b-256 checksum
How to use checksums
c2185773126929d4574e9d8cfc27889792d076d0a171152d9e10828e31709f54
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 28, 2026.

Transparency log

Release files / lucyd_backtest-0.1.0-cp39-abi3-manylinux_2_28_aarch64.whl

Download URL lucyd_backtest-0.1.0-cp39-abi3-manylinux_2_28_aarch64.whl
Size 4.6 MB
Tags CPython 3.9 Linux glibc 2.28+ ARM64 abi3
SHA-256 checksum
How to use checksums
54ec7834670fe70a1b0d1d397c84c3abc33d6a0705165db8f260dd81a4065095
BLAKE2b-256 checksum
How to use checksums
8c5cecefa44805e74b397a4eda7b3dd24d98a10b556b64cf2eb42e285437321a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 28, 2026.

Transparency log

Release files / lucyd_backtest-0.1.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL lucyd_backtest-0.1.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 4.8 MB
Tags CPython 3.9 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
9789401bfa72aae41050454a3ac6a78920d25efc705bcbdcaf114acf41a427be
BLAKE2b-256 checksum
How to use checksums
23d9b14fc99f496c96a6e83bd47aa85ba8d96105b88597dd85cecb1b7f8dd24d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 28, 2026.

Transparency log

Release files / lucyd_backtest-0.1.0-cp39-abi3-macosx_11_0_arm64.whl

Download URL lucyd_backtest-0.1.0-cp39-abi3-macosx_11_0_arm64.whl
Size 4.3 MB
Tags CPython 3.9 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
c7ae5767bc36de6ab52ad2f91fb054d8a06e9f9ac13969037d10f14b7bfda766
BLAKE2b-256 checksum
How to use checksums
4237428af3362d91b4c8c72e269789794c318864840a68e84a8e40840827c300
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 28, 2026.

Transparency log

Release files / lucyd_backtest-0.1.0-cp39-abi3-macosx_10_12_x86_64.whl

Download URL lucyd_backtest-0.1.0-cp39-abi3-macosx_10_12_x86_64.whl
Size 4.6 MB
Tags CPython 3.9 abi3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
06918e28a2e05d5445d00dbeecac2f15c3fc50d26de8303f26fd316193c080d6
BLAKE2b-256 checksum
How to use checksums
49105b3656fd9b2a16f0444f8125d05be94a67a38b729e64ec12a2b941036a7f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 28, 2026.

Transparency log

Security for lucyd-backtest 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 6 files in this release have been uploaded with provenance metadata.

Select from 6 files

Attestation for lucyd_backtest-0.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 28, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

6 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page