The pieces / weirdnet-exchange

weirdnet-exchange

Exchange-neutral trading types and traits: instruments, exact decimal prices and sizes, orders with a state machine, fills, positions, market data, adapter traits and an in-memory paper exchange.

v0.2.0 GitHub crates.io docs.rs weirdnet-exchange = "0.2.0"

What it brings

Use weirdnet-exchange to write trading code once and run it against any venue: real adapters implement its traits, and the in-memory Paper exchange stands in for tests and dry runs. Prices and sizes are exact decimals, never floats.

What it is

Exchange-neutral trading types and traits for Weird Brains apps on Weirdnet. Adapters for real venues live in their own crates and implement the traits here; apps talk to the traits and can swap in the in-memory Paper exchange for tests and dry runs. The crate has no exchange adapters and makes no network calls.

How it works

  • Numbers: Price and Qty wrap a decimal and serialize as strings; rounding to a product's increments is explicit (Rounding::Down, Up, Nearest).
  • Instruments: Market (venue and product) and Product with increments, size and notional limits, status and kind (spot, dated future, perpetual); Product::check_order makes the checks a venue would.
  • Orders: market, limit (with post-only), stop-limit and stop-market orders; GTC, GTD, IOC and FOK; validated ClientOrderIds; an Order state machine that refuses impossible transitions and overfills and ignores replayed fills.
  • Accounts: Fill, Balance, FeeTier, Position and PositionBook (weighted-average entry, realized and unrealized PnL, flips), MarginSummary.
  • Market data: trades, tickers, book snapshots and deltas, an OrderBook that reports sequence gaps, Candle and CandleBuilder. Candle widths are the Granularity values OneMinute, FiveMinutes, FifteenMinutes, OneHour, FourHours (0.2.0), SixHours and OneDay; Granularity::ALL lists them narrowest first.
  • Adapters: MarketData and Trading (balances, fees, preview, idempotent placement, cancel, queries, positions, user streams) with Capabilities; Exchange is both. Errors say whether a retry is safe.
  • Helpers: CostModel and BookCostModel, and a shared token-bucket RateLimiter.
  • Credentials: Credential<Live> and Credential<Test> are checked against their source's environment, so a live adapter can't be built from test credentials.
  • Paper: matches market, limit, post-only, IOC, FOK, GTC and GTD orders against the books and trades you give it, holds funds, charges fees and tracks positions. Resting orders fill optimistically (no queue position) and margin is not modelled.
  • Upgrading from 0.1: 0.2.0 adds Granularity::FourHours and makes Granularity #[non_exhaustive], so later widths can arrive without another break. Depend on weirdnet-exchange = "0.2", add a _ => arm to any match on Granularity, and iterate Granularity::ALL, now a &'static [Granularity] slice, with for &g in Granularity::ALL. Serialized names are unchanged.

Key types and modules

  • Price and Qty: exact decimals with explicit rounding to a product's increments.
  • Instruments (Market, Product) and orders with an Order state machine that refuses impossible transitions and overfills.
  • Fills, balances, fee tiers, positions and a PositionBook with realized and unrealized PnL.
  • Market data: trades, tickers, an OrderBook that reports sequence gaps, candles.
  • Candle widths (Granularity): 1 minute, 5 minutes, 15 minutes, 1 hour, 4 hours (FourHours, new in 0.2.0), 6 hours and 1 day. Granularity::ALL lists them narrowest first, and the Paper exchange builds candles at every width.
  • MarketData and Trading adapter traits, cost models, a shared rate limiter, environment-checked credentials and the Paper exchange.

Feature flags

  • vault: vault::VaultSecret, credentials from weirdnet-vault, scoped per account and venue, test and live kept apart.
  • service: feeds::MarketHubs, books, trades, tickers and candles as weirdnet-service hub topics.

Usage

[dependencies]
weirdnet-exchange = "0.2.0"

Where it fits

Version 0.2.0 is on crates.io (docs.rs, repository, tag v0.2.0). weirdnet-vault and weirdnet-service are optional features. 0.2.0 adds 4-hour candles (Granularity::FourHours, serialized as "four_hours") and breaks 0.1 in three small ways: Granularity is #[non_exhaustive], so a match on it needs a _ => arm; Granularity::ALL is a &'static [Granularity] slice instead of a six-element array; and the implicit discriminants of SixHours and OneDay move up by one. Serialized names of the existing widths are unchanged.