Order Book Analytics#

onetick-py offers functions for analyzing tick-by-tick order book. There are three representations of an order book. We’ll show top 3 levels only for ease of exposition.

A book can be displayed with a tick per level per side. We refer to a level in the book as a ‘price level’ or ‘prl’.

import onetick.py as otp

snapshot_time=otp.dt(2023, 3, 2, 10)
prl = otp.ObSnapshot(db='CME', tick_type='PRL_FULL', max_levels=3) 
otp.run(prl, symbols='NQ\H23', start=snapshot_time, end=snapshot_time)
Time PRICE UPDATE_TIME SIZE LEVEL BUY_SELL_FLAG
0 2023-03-02 10:00:00 11865.00 2023-03-02 09:59:59.460445885 2 1 1
1 2023-03-02 10:00:00 11865.25 2023-03-02 09:59:50.159083669 4 2 1
2 2023-03-02 10:00:00 11865.50 2023-03-02 09:59:58.956216973 3 3 1
3 2023-03-02 10:00:00 11864.25 2023-03-02 09:59:59.473459109 3 1 0
4 2023-03-02 10:00:00 11864.00 2023-03-02 09:59:59.287701455 5 2 0
5 2023-03-02 10:00:00 11863.75 2023-03-02 09:59:59.696659135 5 3 0

Alternatively, a book can show a tick per level with both ask and bid price/size info.

snapshot_time=otp.dt(2023, 3, 2, 10)
prl = otp.ObSnapshotWide(db='CME', tick_type='PRL_FULL', max_levels=3)   
otp.run(prl, symbols='NQ\H23', start=snapshot_time, end=snapshot_time)
Time BID_PRICE BID_UPDATE_TIME BID_SIZE ASK_PRICE ASK_UPDATE_TIME ASK_SIZE LEVEL
0 2023-03-02 10:00:00 11864.25 2023-03-02 09:59:59.473459109 3 11865.00 2023-03-02 09:59:59.460445885 2 1
1 2023-03-02 10:00:00 11864.00 2023-03-02 09:59:59.287701455 5 11865.25 2023-03-02 09:59:50.159083669 4 2
2 2023-03-02 10:00:00 11863.75 2023-03-02 09:59:59.696659135 5 11865.50 2023-03-02 09:59:58.956216973 3 3

Finally, all levels can be displayed in one tick.

snapshot_time=otp.dt(2023, 3, 2, 10)
prl = otp.ObSnapshotFlat(db='CME', tick_type='PRL_FULL', max_levels=3) 
print(otp.run(prl, symbols='NQ\H23', start=snapshot_time, end=snapshot_time))
                 Time  BID_PRICE1              BID_UPDATE_TIME1  BID_SIZE1  ASK_PRICE1              ASK_UPDATE_TIME1  ASK_SIZE1  BID_PRICE2              BID_UPDATE_TIME2  BID_SIZE2  ASK_PRICE2              ASK_UPDATE_TIME2  ASK_SIZE2  BID_PRICE3              BID_UPDATE_TIME3  BID_SIZE3  ASK_PRICE3              ASK_UPDATE_TIME3  ASK_SIZE3
0 2023-03-02 10:00:00    11864.25 2023-03-02 09:59:59.473459109          3     11865.0 2023-03-02 09:59:59.460445885          2     11864.0 2023-03-02 09:59:59.287701455          5    11865.25 2023-03-02 09:59:50.159083669          4    11863.75 2023-03-02 09:59:59.696659135          5     11865.5 2023-03-02 09:59:58.956216973          3

We can output the book (in any of the three representation) on every change to price/size at any of the levels.

prl = otp.ObSnapshotFlat(db='CME', tick_type='PRL_FULL', max_levels=3, running=True)
prl = prl.drop(r".+TIME\d")
print(otp.run(prl, symbols='NQ\H23', start=otp.dt(2023, 3, 2, 10),  end=otp.dt(2023, 3, 2, 10)+otp.Milli(100)))
                             Time  BID_PRICE1  BID_SIZE1  ASK_PRICE1  ASK_SIZE1  BID_PRICE2  BID_SIZE2  ASK_PRICE2  ASK_SIZE2  BID_PRICE3  BID_SIZE3  ASK_PRICE3  ASK_SIZE3
0   2023-03-02 10:00:00.000000000    11864.25          3    11865.00          2    11864.00          5    11865.25          4    11863.75          5    11865.50          3
1   2023-03-02 10:00:00.002023081    11864.25          4    11865.00          2    11864.00          5    11865.25          4    11863.75          5    11865.50          3
2   2023-03-02 10:00:00.002135707    11864.50          1    11865.00          2    11864.25          4    11865.25          4    11864.00          5    11865.50          3
3   2023-03-02 10:00:00.013575033    11864.25          4    11865.00          2    11864.00          5    11865.25          4    11863.75          5    11865.50          3
4   2023-03-02 10:00:00.036656387    11864.25          3    11865.00          2    11864.00          5    11865.25          4    11863.75          5    11865.50          3
..                            ...         ...        ...         ...        ...         ...        ...         ...        ...         ...        ...         ...        ...
149 2023-03-02 10:00:00.059061723    11861.50          2    11862.25          1    11861.25          1    11862.50          5    11861.00          2    11862.75          3
150 2023-03-02 10:00:00.059285473    11861.50          2    11862.25          1    11861.25          1    11862.50          5    11861.00          2    11862.75          4
151 2023-03-02 10:00:00.059817175    11861.50          2    11862.25          2    11861.25          1    11862.50          5    11861.00          2    11862.75          4
152 2023-03-02 10:00:00.089526985    11861.50          2    11862.25          3    11861.25          1    11862.50          5    11861.00          2    11862.75          4
153 2023-03-02 10:00:00.089800939    11861.50          2    11862.25          3    11861.25          1    11862.50          5    11861.00          2    11862.75          3

[154 rows x 13 columns]

The ObSnapshot method doesn’t require specifying max_levels. The entire book is returned when the parameter is not specified.

snapshot_time=otp.dt(2023, 3, 2, 10)
prl = otp.ObSnapshot(db='CME', tick_type='PRL_FULL') 
otp.run(prl, symbols='NQ\H23', start=snapshot_time, end=snapshot_time)
Time PRICE UPDATE_TIME SIZE LEVEL BUY_SELL_FLAG
0 2023-03-02 10:00:00 11865.00 2023-03-02 09:59:59.460445885 2 1 1
1 2023-03-02 10:00:00 11865.25 2023-03-02 09:59:50.159083669 4 2 1
2 2023-03-02 10:00:00 11865.50 2023-03-02 09:59:58.956216973 3 3 1
3 2023-03-02 10:00:00 11865.75 2023-03-02 09:59:58.464531853 5 4 1
4 2023-03-02 10:00:00 11866.00 2023-03-02 09:59:58.393750319 6 5 1
... ... ... ... ... ... ...
1751 2023-03-02 10:00:00 643.75 2023-03-01 22:59:59.997000000 1 904 0
1752 2023-03-02 10:00:00 200.00 2023-03-01 22:59:59.997000000 1 905 0
1753 2023-03-02 10:00:00 123.50 2023-03-01 23:00:04.459000000 1 906 0
1754 2023-03-02 10:00:00 111.00 2023-03-01 22:59:59.997000000 1 907 0
1755 2023-03-02 10:00:00 1.00 2023-03-01 22:59:59.997000000 1 908 0

1756 rows × 6 columns

Book Imbalance#

Let’s find the time weighted book imbalance. The imbalance at a given time is defined as the sum of the bid sizes at the top x levels minus the sum of the ask sizes at the top x levels divided by the sum of these two terms: the values close to 1 mean the book is much heavier on the bid side, close to -1 – on the ask side, equal to zero means the sizes are the same.

x = 3

prl = otp.ObSnapshotWide(db='CME', tick_type='PRL_FULL', max_levels=x, running=True)
prls_df = otp.run(prl, symbols='NQ\H23', start=otp.dt(2023, 3, 2, 10), end=otp.dt(2023, 3, 2, 10)+otp.Milli(100))
print(prls_df.head(7))

prl = prl.agg({'ask_vol': otp.agg.sum('ASK_SIZE'), 'bid_vol': otp.agg.sum('BID_SIZE')}, bucket_units='ticks', bucket_interval=x)
prl['imb'] = (prl['bid_vol'] - prl['ask_vol']) / (prl['bid_vol'] + prl['ask_vol'])
prls_df = otp.run(prl, symbols='NQ\H23', start=otp.dt(2023, 3, 2, 10), end=otp.dt(2023, 3, 2, 10)+otp.Milli(100))
print(prls_df.head())

imb_stats = prl.agg({
    'tw_imb': otp.agg.tw_average('imb'),
    'mean':   otp.agg.average('imb'),
    'stdev':  otp.agg.stddev('imb'),
})
print(otp.run(imb_stats, symbols='NQ\H23', start=otp.dt(2023, 3, 2, 10), end=otp.dt(2023, 3, 2, 10)+otp.Milli(100)))
                           Time  BID_PRICE               BID_UPDATE_TIME  BID_SIZE  ASK_PRICE               ASK_UPDATE_TIME  ASK_SIZE  LEVEL
0 2023-03-02 10:00:00.000000000   11864.25 2023-03-02 09:59:59.473459109         3   11865.00 2023-03-02 09:59:59.460445885         2      1
1 2023-03-02 10:00:00.000000000   11864.00 2023-03-02 09:59:59.287701455         5   11865.25 2023-03-02 09:59:50.159083669         4      2
2 2023-03-02 10:00:00.000000000   11863.75 2023-03-02 09:59:59.696659135         5   11865.50 2023-03-02 09:59:58.956216973         3      3
3 2023-03-02 10:00:00.002023081   11864.25 2023-03-02 10:00:00.002023081         4   11865.00 2023-03-02 09:59:59.460445885         2      1
4 2023-03-02 10:00:00.002023081   11864.00 2023-03-02 09:59:59.287701455         5   11865.25 2023-03-02 09:59:50.159083669         4      2
5 2023-03-02 10:00:00.002023081   11863.75 2023-03-02 09:59:59.696659135         5   11865.50 2023-03-02 09:59:58.956216973         3      3
6 2023-03-02 10:00:00.002135707   11864.50 2023-03-02 10:00:00.002135707         1   11865.00 2023-03-02 09:59:59.460445885         2      1
                           Time  ask_vol  bid_vol       imb
0 2023-03-02 10:00:00.000000000        9       13  0.181818
1 2023-03-02 10:00:00.002023081        9       14  0.217391
2 2023-03-02 10:00:00.002135707        9       10  0.052632
3 2023-03-02 10:00:00.013575033        9       14  0.217391
4 2023-03-02 10:00:00.036656387        9       13  0.181818
                     Time    tw_imb     mean     stdev
0 2023-03-02 10:00:00.100 -0.105928 -0.05215  0.210483

Book sweep#

There are two version of book sweep: by price and by quantity. Book sweep by price, take a price as an input and returns the total quatity available at that price or better. Book sweep by quantity, takes a quantity as an input and returns the VWAP if the quantity were executed immediately.

def side_to_direction(side):
    return 1 if side == 'ASK' else -1

def sweep_by_price(side, price):
    prl = otp.ObSnapshot(db='CME', tick_type='PRL_FULL', side=side)
    direction = side_to_direction(side)
    prl, _ = prl[direction * prl['PRICE'] <= direction * price]
    prl = prl.agg({'total_qty': otp.agg.sum('SIZE')})
    return otp.run(prl, symbols='NQ\H23', start=otp.dt(2023, 3, 2, 10), end=otp.dt(2023, 3, 2, 10))

print(sweep_by_price('BID', 14075))
print(sweep_by_price('ASK', 14077))
                 Time  total_qty
0 2023-03-02 10:00:00          0
                 Time  total_qty
0 2023-03-02 10:00:00       3927
def sweep_by_qty(side, qty):
    prl = otp.ObSnapshot(db='CME', tick_type='PRL_FULL', side=side)
    prl = prl.agg({'total_qty': otp.agg.sum('SIZE')}, running=True, all_fields=True)
    direction = side_to_direction(side)
    prl, _ = prl[prl['total_qty'] - prl['SIZE'] < qty]
    # update the SIZE in the last tick only so that total_qty is exactly qty
    prl['SIZE'] = prl.apply(lambda tick: prl['SIZE'] - (prl['total_qty'] - qty) if prl['total_qty'] > qty else prl['SIZE'])
    prl = prl.agg({'VWAP': otp.agg.vwap('PRICE', 'SIZE')})
    return otp.run(prl, symbols='NQ\H23', start=otp.dt(2023, 3, 2, 10), end=otp.dt(2023, 3, 2, 10))
print(sweep_by_qty('BID', 10))
print(sweep_by_qty('ASK', 10))
                 Time       VWAP
0 2023-03-02 10:00:00  11864.025
                 Time       VWAP
0 2023-03-02 10:00:00  11865.325

Market By Order#

Order Book data may be annotated with ‘key’ fields lets us break down the book by each value of the ‘key’ fields. For example, a book could by keyed by market participant ID, allowing us to see the book with the orders of a given market participant only. Some exchanges provide ‘market-by-order’ data where the book is keyed by order id. Set show_full_detail to True to see the book broken down to the most granular level. The example below is a market-by-order book.

prl = otp.ObSnapshot('CME', tick_type='PRL_FULL', side='BID', show_full_detail=True)
prl = prl.first(5)
print(otp.run(prl, symbols='NQ\H23', start=otp.dt(2023, 3, 2, 10), end=otp.dt(2023, 3, 2, 10)))
                 Time UPDATE_TYPE       ORDER_ID  BUY_SELL_FLAG ORDER_TYPE     PRICE  SIZE  TIME_PRIORITY TRADE_ID  FILL_SIZE RECORD_TYPE DELETED_TIME  TICK_STATUS  OMDSEQ  LEVEL                   UPDATE_TIME
0 2023-03-02 10:00:00           A  6842037422329              0          L  11864.25     1    57339016343                   0           R   1970-01-01            0       0      1 2023-03-02 09:59:59.473459109
1 2023-03-02 10:00:00           A  6842037422316              0          L  11864.25     1    57339016315                   0           R   1970-01-01            0      11      1 2023-03-02 09:59:59.287650739
2 2023-03-02 10:00:00           A  6842037422310              0          L  11864.25     1    57339016308                   0           R   1970-01-01            0       3      1 2023-03-02 09:59:59.287537681
3 2023-03-02 10:00:00           A  6842037422308              0          L  11864.00     1    57339016306                   0           R   1970-01-01            0       0      2 2023-03-02 09:59:59.287427521
4 2023-03-02 10:00:00           A  6842037422143              0          L  11864.00     1    57339016050                   0           R   1970-01-01            0      36      2 2023-03-02 09:59:58.380503219

Market-by-order data can be used to analyze/validate the priority mechanism used by the exchange.``

prl = otp.ObSnapshot('CME', tick_type='PRL_FULL', side='BID', show_full_detail=True)

"""
ORDER_TYPE:
L = Limit order
I = Implied order

Implied liquidity doesn’t have priority as it's always last to execute at any price level. 
It also doesn’t have an order ID, so the IDs that we see in the db are synthetic 
(consisting of 1 or 2 for the 1st/2nd implied level, and E/F for the buy/sell side respectively).

In order to rank the orders within a given price point by priority, we need to sort first by ORDER_TYPE (“L” comes before “I”),
then by TIME_PRIORITY (lowest value comes first).
"""
prl = prl.sort(['LEVEL','ORDER_TYPE', 'TIME_PRIORITY'],ascending=[True,False, True])
orders = otp.run(prl, symbols='NQ\H23', start=otp.dt(2023, 3, 2, 10), end=otp.dt(2023, 3, 2, 10))
orders = orders[['ORDER_ID', 'PRICE', 'LEVEL', 'TIME_PRIORITY','SIZE', 'BUY_SELL_FLAG', 'ORDER_TYPE']]
orders.head()
ORDER_ID PRICE LEVEL TIME_PRIORITY SIZE BUY_SELL_FLAG ORDER_TYPE
0 6842037422310 11864.25 1 57339016308 1 0 L
1 6842037422316 11864.25 1 57339016315 1 0 L
2 6842037422329 11864.25 1 57339016343 1 0 L
3 6842037422105 11864.00 2 57339015994 1 0 L
4 6842037422131 11864.00 2 57339016030 1 0 L