LimitOrderBook.jl

LimitOrderBook.AcctMapType
AcctMap{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.

source
LimitOrderBook.OrderType
Order{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. See OrderSide for more info.
  • size::Sz, the order size
  • price::Px, the price the order is set at
  • orderid::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.

source
LimitOrderBook.OrderBookType
OrderBook{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:

source
LimitOrderBook.OrderSideType
OrderSide(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

source
LimitOrderBook.OrderTraitsType
OrderTraits(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.

source
LimitOrderBook.book_depth_infoFunction
book_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.

source
LimitOrderBook.cancel_order!Method
cancel_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.

source
LimitOrderBook.clear_book!Method
clear_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.

source
LimitOrderBook.order_typesMethod
order_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).

source
LimitOrderBook.submit_limit_order!Method
submit_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::Order representing the order left in the book after matching. Is nothing if no order was inserted
  • order_match_lst::Vector{Order} representing the matched orders if the order crosses the book.
  • left_to_trade::Sz representing the size of the portion of the order which could neither inserted nor matched.
source
LimitOrderBook.submit_market_order!Method
submit_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_lst is a list of limit orders that market order matched with
  • left_to_trade is the remaining size of un-filled order ( ==0 if order is complete, >0 if incomplete)

Note: Only fill_mode.allornone will be used from fill_mode::OrderTraits. All other entries will be ignored.

source
LimitOrderBook.submit_market_order_byfunds!Function
submit_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_lst is a list of limit orders that market order matched with
  • funds_leftover is the amount of remaining funds if not enough liquidity was available ( ==0 if order is complete, >0 if incomplete)

Note: Only fill_mode.allornone will be considered from fill_mode::OrderTraits. All other entries will be ignored.

source
LimitOrderBook.write_csvMethod
write_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.

source