Expand description
Superfast primitives and data structures for high frequency trading.
§Dec, the number
A fixed-scale decimal backed by an i128 carrying Dec::SCALE decimal
places. Arithmetic is exact within a finite range of roughly +/-1.7e20;
anything that leaves that range becomes Dec::NAN, which propagates
through every subsequent operation so the fault reaches the boundary where
Dec::is_finite is checked.
use troy::{Dec, dec};
let notional = dec!(104_237.25) * dec!(0.00135);
assert_eq!(notional, dec!(140.72028750));
assert!(notional.is_finite());
// overflow poisons rather than clamping or panicking
assert!((Dec::MAX + Dec::ONE).is_nan());§OrderBook, the structure
A level 2 book built on Dec. Each side keeps its levels best first, so
the best price, the n-th level and the depth cut are index arithmetic
rather than searches, and a level enters only if PriceAmount::is_valid
accepts it — a non-finite price would sort against every other level, and a
non-finite amount would poison the side’s running total for good.
use troy::{OrderBook, dec};
let mut book = OrderBook::new(Some(10));
book.bids.set_price_amount(dec!(104_237.25), dec!(3));
book.asks.set_price_amount(dec!(104_237.30), dec!(1));
assert_eq!(book.mid_price(), Some(dec!(104_237.275)));
assert_eq!(book.spread(), Some(dec!(0.05)));See the design module for the reasoning behind the representation, the
NaN state, and the ordering.
Modules§
- design
- How
Decis represented, what the NaN state means, and the rules every operation follows. This page records the decisions behindDecand the reasoning that led to them. It is the place to look before changing how a value is represented, how an operation reports failure, or how two values compare. - memory_
layout - How
Decis laid out in memory, and how that compares with other decimals. - release_
notes - Release notes, one section per tagged release.
- testing
- Building and simulating order books, for tests and for downstream crates
that enable the
testingfeature.
Macros§
Structs§
- Dec
- A decimal carrying a fixed
Dec::SCALEdecimal places, backed by ani128holding the value scaled by10^SCALE. - Order
Book - A level 2 order book.
- Order
Book Diff - A batch of level updates to apply to a book.
- Order
Book Side - One side of a level 2 book.
- Order
Book Top - The best bid and ask: the smallest snapshot of a book that still prices a trade, and what a top-of-book feed carries.
- Price
Amount - A struct representing a price and amount pair.
Enums§
- Parse
DecError - Why a decimal failed to parse.