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
ADepth (size) amount type.
LLevel (price) amount type.
- Inheritance
-
L2OrderBook<A, L>
- Implements
-
IL2OrderBook<A, L>IL2OrderBookWriter<A, L>
- 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
levelBaseintBase of the price dimension (e.g. 10).
levelPrecisionintPrecision of the price dimension.
depthBaseintBase of the size dimension (e.g. 10).
depthPrecisionintPrecision 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
symbolSymbol?Instrument the book tracks, if known.
levelBaseintBase of the price dimension (e.g. 10).
levelPrecisionintPrecision of the price dimension.
depthBaseintBase of the size dimension (e.g. 10).
depthPrecisionintPrecision of the size dimension.
Properties
AppliedFinalUpdateId
Final update id of the most recently applied diff, or -1.
public long AppliedFinalUpdateId { get; }
Property Value
AppliedFirstUpdateId
First update id of the most recently applied diff, or -1.
public long AppliedFirstUpdateId { get; }
Property Value
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
DepthPrecision
Precision of the size (depth) dimension.
public int DepthPrecision { get; }
Property Value
LastUpdateId
The last applied update id (or the bootstrapping snapshot's id).
public long LastUpdateId { get; }
Property Value
LevelBase
Base of the price (level) dimension.
public int LevelBase { get; }
Property Value
LevelPrecision
Precision of the price (level) dimension.
public int LevelPrecision { get; }
Property Value
State
Current lifecycle state.
public OrderBookState State { get; }
Property Value
Symbol
The instrument the book tracks, if known.
public Symbol? Symbol { get; }
Property Value
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
isBidbooltruefor the bid side,falsefor asks.priceLLevel price.
sizeANew 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
SetLastUpdateId(long)
Set the last applied update id.
public void SetLastUpdateId(long lastUpdateId)
Parameters
lastUpdateIdlongNew update id.
SetState(OrderBookState)
Set the lifecycle state.
public void SetState(OrderBookState state)
Parameters
stateOrderBookStateNew state.
TryGetAsk(L, out A)
Look up the aggregate size at an ask price.
public bool TryGetAsk(L price, out A size)
Parameters
priceLPrice at the book's level scale.
sizeAThe level's aggregate size when present.
Returns
TryGetBid(L, out A)
Look up the aggregate size at a bid price.
public bool TryGetBid(L price, out A size)
Parameters
priceLPrice at the book's level scale.
sizeAThe level's aggregate size when present.