LimitOrderBook.jl
LimitOrderBook.AcctMap — TypeAcctMap{Sz,Px,Oid,Aid}Collection of open orders by account.
(Sz,Px,Oid,Aid) characterize the type of Order present in the AcctMap. See documentation on Order for more information on the meaning of types.
The account map is implemented as a Dict containing AVLTrees. AcctMap{Sz,Px,Oid,Aid} = Dict{Aid,AVLTree{Oid,Order{Sz,Px,Oid,Aid}}} The outer key is the account id, mapping to an AVLTree of Orders keyed by order id.
LimitOrderBook.Order — TypeOrder{Sz<:Real,Px<:Real,Oid<:Integer,Aid<:Integer}Type representing a limit order.
An Order{Sz<:Real,Px<:Real,Oid<:Integer,Aid<:Integer} is a struct representing a resting Limit Order which contains
side::OrderSide, the side of the book the order will rest in. SeeOrderSidefor more info.size::Sz, the order sizeprice::Px, the price the order is set atorderid::Oid, a unique Order ID- (optional)
acctid::Union{Aid,Nothing}, which is set to nothing if the account is unknown or irrelevant.
One can create a new Order as
Order{Sz,Px,Pid,Aid}(side, size, price, orderid, order_mode [,acctid=nothing])where the types of size and price will be cast to the correct types. The orderid and acctid types will not be cast in order to avoid ambiguity.
LimitOrderBook.OrderBook — TypeOrderBook{Sz,Px,Oid,Aid}An OrderBook is a data structure containing limit orders represented as objects of type Order{Sz,Px,Oid,Aid}.
See documentation on Order for more information on this type.
How to use Orderbook:
- Initialize an empty limit order book as
OrderBook{Sz,Px,Oid,Aid}() - Submit or cancel limit orders with
submit_limit_order!andcancel_order!. - Submit market orders with
submit_market_order! - Retrieve order book state information with
printorshowmethods, as well asbook_depth_info,best_bid_ask,volume_bid_ask,n_orders_bid_askandget_acct - Write book state to
csvfile withwrite_csv.
LimitOrderBook.OrderSide — TypeOrderSide(is_buy::Bool)Type representing whether an order is a buy order or sell order. New instance can be generated with OrderSide(::Bool) or by using exported constants BUY_ORDER and SELL_ORDER
LimitOrderBook.OrderTraits — TypeOrderTraits(allornone::Bool,immediateorcancel::Bool,allow_cross::Bool)OrderTraits specify order traits which modify execution logic.
An instance can be initialized by using the keyword intializer or by using the exported constants VANILLA_FILLTYPE, IMMEDIATEORCANCEL_FILLTYPE, FILLORKILL_FILLTYPE.
The default execution logic is represented by VANILLA_FILLTYPE.
Note: This feature is not well supported yet. Other than the constants described above, use non-vanilla modes with caution.
LimitOrderBook.ask_orders — Methodask_orders(sb::OneSidedBook)Return an iterator over all ask orders by price/time priority.
LimitOrderBook.best_bid_ask — Methodbest_bid_ask(ob::OrderBook)Return best bid/ask prices in order book as a Tuple
LimitOrderBook.bid_orders — Methodbid_orders(sb::OneSidedBook)Return an iterator over all bid orders by price/time priority.
LimitOrderBook.book_depth_info — Functionbook_depth_info(ob::OrderBook, max_depth=5)Returns prices, volumes and order counts at bid and ask in ob::OrderBook until fixed depth max_depth as a nested Dict.
LimitOrderBook.cancel_order! — Methodcancel_order!(ob::OrderBook, o::Order)
cancel_order!(ob::OrderBook, orderid, side, price [, acct_id=nothing])Cancels Order o, or order with matching information from OrderBook.
Provide acct_id if known to guarantee correct account tracking.
LimitOrderBook.clear_book! — Methodclear_book!(ob::OrderBook,n_keep::Int64=10)Remove all orders beyond n_keep ≥ 0 from the best bid and best ask. When n_keep==0, all orders are cleared.
LimitOrderBook.get_acct — Methodget_acct(ob::OrderBook{Sz,Px,Oid,Aid},acct_id::Aid)Return all open orders assigned to account acct_id
LimitOrderBook.n_orders_bid_ask — Methodn_orders_bid_ask(ob::OrderBook)Return total number of orders on each side of order book, returned as a Tuple
LimitOrderBook.order_types — Methodorder_types(::Order{Sz,Px,Oid,Aid})
order_types(::OneSidedBook{Sz,Px,Oid,Aid})
order_types(::OrderBook{Sz,Px,Oid,Aid})Returns (Sz,Px,Oid,Aid).
LimitOrderBook.submit_limit_order! — Methodsubmit_limit_order!(
ob::OrderBook{Sz,Px,Oid,Aid},
orderid::Oid,
side::OrderSide,
limit_price::Real,
limit_size::Real,
[, acct_id::Aid, fill_mode::OrderTraits ]
)Enter limit order with size limit_size, price limit_price with side::OrderSide into ob::OrderBook.
If an account if acct_id is provided, account holdings are tracked in ob.acct_map.
Order execution logic can be modified according to the argument fill_mode::OrderTraits which defaults to fill_mode=VANILLA_FILLTYPE, representing the default order matching mode.
submit_limit_order! returns tuple of
new_open_order::Orderrepresenting the order left in the book after matching. Isnothingif no order was insertedorder_match_lst::Vector{Order}representing the matched orders if the order crosses the book.left_to_trade::Szrepresenting the size of the portion of the order which could neither inserted nor matched.
LimitOrderBook.submit_market_order! — Methodsubmit_market_order!(ob::OrderBook,side::OrderSide,mo_size[,fill_mode::OrderTraits])Submit market order to ob::OrderBook with side::OrderSide and size mo_size. Optionally fill_mode::OrderTraits may be provided to modify fill logic. Market orders are filled by price-time priority.
Returns tuple ( ord_lst::Vector{Order}, left_to_trade::Sz ) where
ord_lstis a list of limit orders that market order matched withleft_to_tradeis the remaining size of un-filled order (==0if order is complete,>0if incomplete)
Note: Only fill_mode.allornone will be used from fill_mode::OrderTraits. All other entries will be ignored.
LimitOrderBook.submit_market_order_byfunds! — Functionsubmit_market_order_byfunds!(ob::OrderBook,side::Symbol,funds[,mode::OrderTraits])Submit market order to ob::OrderBook side::OrderSide and available funds funds::Real. Optionally fill_mode::OrderTraits may be provided to modify fill logic. Market orders are filled by price-time priority.
Functionality is exactly the same as submit_market_order! except available funds (max total price paid on order) is provided, rather than number of shares (order size).
Returns tuple ( ord_lst::Vector{Order}, funds_leftover ) where
ord_lstis a list of limit orders that market order matched withfunds_leftoveris the amount of remaining funds if not enough liquidity was available (==0if order is complete,>0if incomplete)
Note: Only fill_mode.allornone will be considered from fill_mode::OrderTraits. All other entries will be ignored.
LimitOrderBook.volume_bid_ask — Methodvolume_bid_ask(ob::OrderBook)Return total bid and ask volume from order book as a Tuple.
LimitOrderBook.write_csv — Methodwrite_csv(
io::IO,
ob::OrderBook;
row_formatter = _order_to_csv,
header = "TRD,ID,SIDE,SIZE,PX,ACCT",
)Write OrderBook ob to an IO stream into csv format where each row corresponds to an order The formatting for each row is given by the function argument row_formatter(::Order)::String. The csv header can be provided as an argument where setting it to nothing writes no header.