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:
PriceandQtywrap a decimal and serialize as strings; rounding to a product's increments is explicit (Rounding::Down,Up,Nearest). - Instruments:
Market(venue and product) andProductwith increments, size and notional limits, status and kind (spot, dated future, perpetual);Product::check_ordermakes the checks a venue would. - Orders: market, limit (with post-only), stop-limit and stop-market orders; GTC, GTD, IOC and FOK; validated
ClientOrderIds; anOrderstate machine that refuses impossible transitions and overfills and ignores replayed fills. - Accounts:
Fill,Balance,FeeTier,PositionandPositionBook(weighted-average entry, realized and unrealized PnL, flips),MarginSummary. - Market data: trades, tickers, book snapshots and deltas, an
OrderBookthat reports sequence gaps,CandleandCandleBuilder. Candle widths are theGranularityvaluesOneMinute,FiveMinutes,FifteenMinutes,OneHour,FourHours(0.2.0),SixHoursandOneDay;Granularity::ALLlists them narrowest first. - Adapters:
MarketDataandTrading(balances, fees, preview, idempotent placement, cancel, queries, positions, user streams) withCapabilities;Exchangeis both. Errors say whether a retry is safe. - Helpers:
CostModelandBookCostModel, and a shared token-bucketRateLimiter. - Credentials:
Credential<Live>andCredential<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::FourHoursand makesGranularity#[non_exhaustive], so later widths can arrive without another break. Depend onweirdnet-exchange = "0.2", add a_ =>arm to anymatchonGranularity, and iterateGranularity::ALL, now a&'static [Granularity]slice, withfor &g in Granularity::ALL. Serialized names are unchanged.
Key types and modules
PriceandQty: exact decimals with explicit rounding to a product's increments.- Instruments (
Market,Product) and orders with anOrderstate machine that refuses impossible transitions and overfills. - Fills, balances, fee tiers, positions and a
PositionBookwith realized and unrealized PnL. - Market data: trades, tickers, an
OrderBookthat 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::ALLlists them narrowest first, and thePaperexchange builds candles at every width. MarketDataandTradingadapter traits, cost models, a shared rate limiter, environment-checked credentials and thePaperexchange.
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
- Builds on weirdnet-vault, weirdnet-service.
- License: BSD-3-Clause.
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.