OrderBook
An OrderBook is a representation of the currently known Ask
and Bid
levels. Essentially, these are the quantities of a given Asset
that people want to sell (Ask
) or buy (Bid
) at certain prices.
In Trading.jl
they are implemented in the AssetLedger
as 2 TreeComponents
. These are standard Components
that are also internally backed by a Red-Black Tree
(facilitating easy search type operations, see ceil
, floor
, maximum
and minimum
) of LinkedLists
of individual Asks
and Bids
.
By default, when starting a Trader
it will listen to updates on new Bids
Asks
, Trades
and latest_quote
updates. The latter signifies the L1
OrderBook updates. The former 3 are stored in a manner that facilitates L3
OrderBook interactions (each ask/bid is stored individually in the linked lists). sum(x -> x.quantity, limit)
can be used to extract L2
type market data from the OrderBook.
Behavior
While Ask
, Bid
, Trade
and latest_quote
data comes in, the Trades
are matched with the Ask
and Bid
levels and clear these. The matching works as follows:
- we assume that we have incomplete
Trade
data, and no directBid
orAsk
canceling data - a
Trade
is assumed to always happen on the bestBid
orAsk
price level - if there exist
Bid
orAsk
levels between theTrade
and the OrderBook center, they will be cleared assuming they are stale - if a level exists with the exact
Trade
price, it will be cleared until the cleared quantity matches theTrade
quantity
This means that when a sell trade comes in at price 5.0
then all Bids
with a higher price than 5.0
will be assumed outdated and thus removed.
Example
See OrderBook Imbalance for an example usecase in a simple Strategy
.
References
Trading.Ask
— TypeRepresents an ask order with a price
and quantity
.
Trading.Bid
— TypeRepresents a bid order with a price
and quantity
.
Trading.Trade
— TypeRepresents an executed trade with a price
, quantity
and side
.
Trading.latest_quote
— FunctionReturns the latest NBBO quote.
Trading.TreeComponent
— TypeA Component backed by a red-black tree.
Indexing into a component with an Entity
will return the data linked to that entity, indexing with a regular Int
will return directly the data that is stored in the data vector at that index, i.e. generally not the storage linked to the Entity
with that Int
as id.
To register a Type
to be stored in a TreeComponent
see @tree_component
. Every Type
needs to define <
and ==
operators.
Trading.@tree_component
— MacroDefines that a struct should be used with a TreeComponent
.
@tree_component struct Example
d
end
Base.ceil
— MethodReturns the first level (i.e. a LinkedList
with EntityPtr
) with a higher value than v
.
Base.floor
— MethodReturns the first level (i.e. a LinkedList
with EntityPtr
) with a lower value than v
.
Base.maximum
— MethodReturns the first [EntityPtr
] in the LinkedList
with the maximum value in the TreeComponent
. See maximum_node
if the full list is desired.
Base.minimum
— MethodReturns the first [EntityPtr
] in the LinkedList
with the minimum value in the TreeComponent
. See minimum_node
if the full list is desired.
Trading.minimum_node
— Functionminimum_node(tree::Tree, node::TreeNode)
Returns the TreeNode with minimum value in subtree of node
.
Returns the LinkedList
of EntityPtrs
containing the minimum value in the `TreeComponent.
Trading.maximum_node
— Functionmaximum_node(tree::Tree, node::TreeNode)
Returns the TreeNode with maximum value in subtree of node
.
Returns the LinkedList
of EntityPtrs
containing the maximum value in the `TreeComponent.
Trading.levels
— Functionlevels(c; rev=false)
Iterate through the levels (each a LinkedList
of EntityPtrs
) of the TreeComponent
c
, either inorder if rev == false
or in reverse order if rev == true
.
Trading.LinkedList
— TypeA classic linked list implementation, mostly used to represent value levels in a TreeComponent
Trading.EntityPtr
— TypeHolds the direct reference of an Entity
into an array of associated data (usually in an AbstractComponent
).