Skip to content

1.1 Order book

Marc Juchli edited this page Apr 20, 2018 · 6 revisions

Gdax order book Figure 1: https://www.bitfinex.com/t/BTC:USD

Properties

The order book consists of limit orders placed on either bid (representing potential buyers) or ask (representing potential sellers) side of the book. The two sides are separated by the spread. For example, Figure 1 illustrates the state of the order book “BTC/USD” at some time t. The current best bid-price, on the buyer side (left), is $14,910.00 and the best ask-price, on the seller side (right), is $14,930.00. Therefore, the spread is currently $20.00 wide.

Note that the exchanges, in this case Bitfinex, define a minimal spread per asset as well as a minimum quantity to be traded).

In order to appear in either of the side of the order book, a trader is required to place an Order of type limit. To the accumulation of unexecuted trades at the same price level in the same order book state, we refer to as order book entry. The order book entry resides on a certain level in either side of the book. Furthermore, to the state of the order book at time t we refer as the order book state. Moreover, for every executed order (e.g. trade), the order book state changes and a new state evolves. The same applies for new or amended limit orders. After all, the order book could simply be regarded as an ordered list of order book states.

Order Side

Implies whether the trader wants to buy or sell a given asset.

Buy

Produces a bid.

Sell

Produces an ask.

Order Types

Nowadays exchanges provide various order types and therefore allow traders great flexibility to buy and sell assets bound to certain conditions. In our case, consider two of the most commonly used order types:

Limit

Arguments:

  • Price
  • Side
  • Quantity

Implies that if the ask price on the opposing side of the book becomes lower or equals (respectively bid price becomes higher or equals, in case of a sell order), the match engine of the exchange will match those two orders, provided that both parties were the first in line (FIFO). As a result, a trade is being generated. The disadvantage of this order type is the uncertainty of whether the order gets executed in the first place. In case no trade from the opposing side appears, the order remains (possibly forever) unexecuted.

Market

Arguments:

  • Side
  • Quantity

Therefore, the market order allows the trader to express his desire to buy (respectively sell) for the best available price. Therefore the matching engine executes trades starting from the price of the opposing side that is closest to the spread, then traverses down the book. Hence, market orders tend to become expensive, especially for large orders.

Implementation

The file orderbook.py currently holds the prototype implementation of an order book.

OrderbookEntry
    price : float
    amount : float

OrderbookState
    lastTrade : Order
    buyers : [OrderbookEntry]
    sellers : [OrderbookEntry]
    timestamp : Datetime

Orderbook
    states : [OrderbookState]

The Orderbook holds a list of OrderbookState which should be ordered by their timestamps. The OrderbookState further contains the last Trade and a two lists of OrderbookEntry, representing the bids and asks of the particular state in time. The OrderbookEntry (e.g. bid or ask) is contains a price and amount of the asset the trader is willing to buy or sell.

Issue:

  • #1: A more stable and efficient implementation (PyLimitBook) should be used.

Import data

Currently there are two methods implemented in order to generate an order book from historical data given a .tsv file:

  • Trades: loadFromFile(fileName)
  • States: loadFromBitfinexFile(fileName)
  • Events: loadFromEvents(fileName)
Clone this wiki locally