Skip to main content

Crate troy

Crate troy 

Source
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 Dec is represented, what the NaN state means, and the rules every operation follows. This page records the decisions behind Dec and 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 Dec is 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 testing feature.

Macros§

dec
Builds a Dec from a decimal literal at compile time.

Structs§

Dec
A decimal carrying a fixed Dec::SCALE decimal places, backed by an i128 holding the value scaled by 10^SCALE.
OrderBook
A level 2 order book.
OrderBookDiff
A batch of level updates to apply to a book.
OrderBookSide
One side of a level 2 book.
OrderBookTop
The best bid and ask: the smallest snapshot of a book that still prices a trade, and what a top-of-book feed carries.
PriceAmount
A struct representing a price and amount pair.

Enums§

ParseDecError
Why a decimal failed to parse.