weirdnet-exchange 0.2.0

Weirdnet exchange types and traits: instruments, exact decimal prices and sizes, orders with a state machine, fills, balances, positions, market data, an async adapter trait, cost and rate-limit helpers, and an in-memory paper exchange.
Documentation

weirdnet-exchange

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. This crate has no exchange adapters and makes no network calls.

  • Exact numbers. Price and Qty wrap a decimal (no floats) and serialize as strings. Rounding to a product's increments is explicit (Rounding::Down, Up, Nearest).
  • Instruments. Market (venue and product), Product with increments, size and notional limits, status, and kind (spot, dated future, perpetual). Product::check_order makes the checks a venue would.
  • Orders. Side, OrderType (market, limit with post-only, stop-limit, stop-market), TimeInForce (GTC, GTD, IOC, FOK), base or quote sizes, validated ClientOrderIds, and an Order state machine that refuses impossible transitions and overfills and ignores replayed fills.
  • Accounts. Fill, Balance (available and held), FeeTier, Position and PositionBook (weighted-average entry, realized and unrealized PnL, flips), MarginSummary.
  • Market data. Trade, Ticker, BookSnapshot and BookDelta, an OrderBook that applies deltas and reports sequence gaps, Candle and CandleBuilder (1m, 5m, 15m, 1h, 4h, 6h and 1d Granularity widths), and MarketEvent.
  • Adapter traits. MarketData (public data and streams) and Trading (balances, fees, preview, idempotent placement, cancel, queries, positions and user streams), with Capabilities; Exchange is both. Errors say whether a retry is safe.
  • Helpers. CostModel and BookCostModel (fees, spread, slippage, carry), and a shared token-bucket RateLimiter with retry-after back-off.
  • 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 exchange. 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.
use weirdnet_exchange::*;

async fn demo() -> Result<()> {
    let paper = Paper::default();
    let m = paper.market("BTC-USD");
    paper.add_product(Product::spot(m.clone(), "BTC", "USD").with_increments(Decimal::new(1, 2), Decimal::new(1, 4)))?;
    paper.deposit("USD", Decimal::from(1000));
    paper.set_book(BookSnapshot {
        market: m.clone(),
        bids: vec![Level::new(Price::parse("99")?, Qty::parse("1")?)],
        asks: vec![Level::new(Price::parse("101")?, Qty::parse("1")?)],
        sequence: 1,
        at_ms: 0,
    })?;

    let x: &dyn Exchange = &paper;
    let order = OrderRequest::market(ClientOrderId::generate("demo"), m, Side::Buy, OrderSize::Base(Qty::parse("0.5")?));
    let ack = x.place_order(&order).await?;
    assert_eq!(ack.status, OrderStatus::Filled);
    Ok(())
}

The design notes explain the choices. API documentation is on docs.rs.

Features

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

Upgrading from 0.1

0.2.0 adds 4-hour candles (Granularity::FourHours) and makes two breaking changes so later widths can ship without another break: Granularity is #[non_exhaustive], so a match on it needs a _ => arm, and Granularity::ALL is a &'static [Granularity] slice instead of a fixed-size array (iterate with for &g in Granularity::ALL). Serialized names are unchanged. See the changelog.

Status

The API may change before 1.0. The paper exchange is a simulation: resting orders fill optimistically (no queue position) and margin is not modelled.

License

BSD-3-Clause, copyright Weird Brains LLC. See LICENSE.


A Weird Brains project.