Table of Contents

Class L2OrderBook<A, L>

Namespace
Virtufin.Base.OrderBooks
Assembly
Virtufin.Base.dll

In-memory Level-2 order book: two sorted ladders of aggregate size keyed by price. Pure state — no I/O, no CloudEvents, not thread-safe.

public sealed class L2OrderBook<A, L> : IL2OrderBook<A, L>, IOrderBook, IL2OrderBookWriter<A, L> where A : IFixedPointAmount where L : IFixedPointAmount

Type Parameters

A

Depth (size) amount type.

L

Level (price) amount type.

Inheritance
L2OrderBook<A, L>
Implements
Inherited Members

Remarks

The price (level) and size (depth) dimensions carry independent fixed scales supplied at construction. Every value written must already be at the corresponding scale; the sorted comparer compares price amounts via CompareTo(IAmount), which throws PrecisionMismatchException on a stray mismatch, so a mis-normalized producer fails loudly rather than corrupting the ladder.

Bids are enumerated highest price first, asks lowest price first. Live projections (BidsDescending/AsksAscending) are snapshots and cannot tear mid-update.

Constructors

L2OrderBook(int, int, int, int)

Construct a book with no symbol.

public L2OrderBook(int levelBase, int levelPrecision, int depthBase, int depthPrecision)

Parameters

levelBase int

Base of the price dimension (e.g. 10).

levelPrecision int

Precision of the price dimension.

depthBase int

Base of the size dimension (e.g. 10).

depthPrecision int

Precision of the size dimension.

L2OrderBook(Symbol?, int, int, int, int)

Construct a book for a symbol.

public L2OrderBook(Symbol? symbol, int levelBase, int levelPrecision, int depthBase, int depthPrecision)

Parameters

symbol Symbol?

Instrument the book tracks, if known.

levelBase int

Base of the price dimension (e.g. 10).

levelPrecision int

Precision of the price dimension.

depthBase int

Base of the size dimension (e.g. 10).

depthPrecision int

Precision of the size dimension.

Properties

AppliedFinalUpdateId

Final update id of the most recently applied diff, or -1.

public long AppliedFinalUpdateId { get; }

Property Value

long

AppliedFirstUpdateId

First update id of the most recently applied diff, or -1.

public long AppliedFirstUpdateId { get; }

Property Value

long

AsksAscending

Ask levels, lowest price first.

public IReadOnlyList<(L Price, A Size)> AsksAscending { get; }

Property Value

IReadOnlyList<(L Price, A Size)>

BidsDescending

Bid levels, highest price first.

public IReadOnlyList<(L Price, A Size)> BidsDescending { get; }

Property Value

IReadOnlyList<(L Price, A Size)>

DepthBase

Base of the size (depth) dimension.

public int DepthBase { get; }

Property Value

int

DepthPrecision

Precision of the size (depth) dimension.

public int DepthPrecision { get; }

Property Value

int

LastUpdateId

The last applied update id (or the bootstrapping snapshot's id).

public long LastUpdateId { get; }

Property Value

long

LevelBase

Base of the price (level) dimension.

public int LevelBase { get; }

Property Value

int

LevelPrecision

Precision of the price (level) dimension.

public int LevelPrecision { get; }

Property Value

int

State

Current lifecycle state.

public OrderBookState State { get; }

Property Value

OrderBookState

Symbol

The instrument the book tracks, if known.

public Symbol? Symbol { get; }

Property Value

Symbol?

Methods

ApplyLevel(bool, L, A)

Apply one level mutation: a zero size removes the price, otherwise the size is assigned (absolute, not incremental).

public void ApplyLevel(bool isBid, L price, A size)

Parameters

isBid bool

true for the bid side, false for asks.

price L

Level price.

size A

New aggregate size; zero removes the level.

Clear()

Remove every level.

public void Clear()

ResetAppliedRange()

Clear the recorded applied id range (e.g. after a snapshot).

public void ResetAppliedRange()

SetAppliedRange(long, long)

Record the id range of the most recently applied diff.

public void SetAppliedRange(long firstUpdateId, long finalUpdateId)

Parameters

firstUpdateId long

Diff's first update id.

finalUpdateId long

Diff's final update id.

SetLastUpdateId(long)

Set the last applied update id.

public void SetLastUpdateId(long lastUpdateId)

Parameters

lastUpdateId long

New update id.

SetState(OrderBookState)

Set the lifecycle state.

public void SetState(OrderBookState state)

Parameters

state OrderBookState

New state.

TryGetAsk(L, out A)

Look up the aggregate size at an ask price.

public bool TryGetAsk(L price, out A size)

Parameters

price L

Price at the book's level scale.

size A

The level's aggregate size when present.

Returns

bool

TryGetBid(L, out A)

Look up the aggregate size at a bid price.

public bool TryGetBid(L price, out A size)

Parameters

price L

Price at the book's level scale.

size A

The level's aggregate size when present.

Returns

bool