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 direct Bid or Ask canceling data
  • a Trade is assumed to always happen on the best Bid or Ask price level
  • if there exist Bid or Ask levels between the Trade 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 the Trade 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.TreeComponentType

A 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.

source
Trading.EntityPtrType

Holds the direct reference of an Entity into an array of associated data (usually in an AbstractComponent).

source