Skip to main content

Crate weirdnet_exchange

Crate weirdnet_exchange 

Source
Expand description

Exchange-neutral trading types and traits for Weirdnet apps.

  • num: exact decimal Price and Qty (no floats), with increment rounding.
  • instrument: venues, products, markets and instrument kinds (spot, dated futures, perpetuals), with order checks against a product’s increments and limits.
  • order: sides, order types, time in force, client order ids, requests, and an Order state machine that accounts fills.
  • account: fills, balances, fee tiers, positions (with a PositionBook) and margin.
  • market: trades, tickers, order book snapshots and deltas (an OrderBook that applies them with gap checks), candles and a CandleBuilder.
  • exchange: the async adapter traits, MarketData (no credentials) and Trading, with Capabilities and paging; Exchange is both.
  • cost: a CostModel for fees, spread and slippage from a live book.
  • rate: a shared token-bucket RateLimiter.
  • credentials: secrets for adapters, typed by environment so a live adapter can only be built from Live credentials.
  • paper: Paper, an in-memory simulated exchange for tests and dry runs.
  • Feature vault: vault::VaultSecret, credentials from weirdnet-vault.
  • Feature service: feeds::MarketHubs, market data as weirdnet-service hub topics.

This crate has no exchange adapters and makes no network calls. Adapters implement the traits in their own crates.

Timestamps are milliseconds since the Unix epoch (UTC). Amounts are Decimal.

use weirdnet_exchange::*;

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);

Re-exports§

pub use account::Balance;
pub use account::FeeTier;
pub use account::Fill;
pub use account::Liquidity;
pub use account::MarginSummary;
pub use account::Position;
pub use account::PositionBook;
pub use cost::BookCostModel;
pub use cost::Cost;
pub use cost::CostModel;
pub use credentials::Credential;
pub use credentials::EnvKind;
pub use credentials::Live;
pub use credentials::SecretSource;
pub use credentials::StaticSecret;
pub use credentials::Test;
pub use error::ExchangeError;
pub use error::RejectReason;
pub use error::Result;
pub use exchange::CandleQuery;
pub use exchange::Capabilities;
pub use exchange::Environment;
pub use exchange::EventStream;
pub use exchange::Exchange;
pub use exchange::FillQuery;
pub use exchange::MarketData;
pub use exchange::OrderQuery;
pub use exchange::OrderRef;
pub use exchange::Page;
pub use exchange::Preview;
pub use exchange::Trading;
pub use exchange::UserEvent;
pub use instrument::AccountId;
pub use instrument::Asset;
pub use instrument::InstrumentKind;
pub use instrument::Market;
pub use instrument::Product;
pub use instrument::ProductId;
pub use instrument::ProductStatus;
pub use instrument::VenueId;
pub use market::BookChange;
pub use market::BookDelta;
pub use market::BookSnapshot;
pub use market::Candle;
pub use market::CandleBuilder;
pub use market::Channel;
pub use market::Granularity;
pub use market::Level;
pub use market::MarketEvent;
pub use market::OrderBook;
pub use market::Ticker;
pub use market::Trade;
pub use num::Price;
pub use num::Qty;
pub use num::Rounding;
pub use order::ClientOrderId;
pub use order::Order;
pub use order::OrderAck;
pub use order::OrderEvent;
pub use order::OrderId;
pub use order::OrderRequest;
pub use order::OrderSize;
pub use order::OrderStatus;
pub use order::OrderType;
pub use order::PositionSide;
pub use order::Side;
pub use order::TimeInForce;
pub use paper::Paper;
pub use paper::PaperOptions;
pub use rate::BucketSpec;
pub use rate::RateLimiter;

Modules§

account
Fills, balances, fees, positions and margin.
cost
What a trade costs: fees, spread, slippage and carry.
credentials
Credentials typed by environment.
error
Errors adapters and helpers return.
exchange
The adapter traits.
feeds
Market data as weirdnet-service hub topics (feature service).
instrument
Venues, assets, products and markets.
market
Market data: trades, tickers, order books and candles.
num
Exact decimal prices and quantities.
order
Orders: requests, acknowledgements, and the order state machine.
paper
Paper: an in-memory exchange for tests and dry runs.
rate
A token-bucket rate limiter for adapters.
vault
Credentials from weirdnet-vault (feature vault).

Structs§

Decimal
Decimal represents a 128 bit representation of a fixed-precision decimal number. The finite set of values of type Decimal are of the form m / 10e, where m is an integer such that -296 < m < 296, and e is an integer between 0 and 28 inclusive.

Functions§

now_ms
Milliseconds since the Unix epoch, from the system clock.