Point-in-time benchmarks: BBO at different markouts#

Find the prevailing quote at different time intervals (markouts) before/after each trade.

import onetick.py as otp

markouts = [-1, 0, 1, 5, 60, 600] 

trd = otp.DataSource('NYSE_TAQ', tick_type='TRD')
trd = trd[['PRICE', 'SIZE']]

qte_by_markout = []
for m in markouts:
    mr = str(m).replace('-', 'm')
    qte = otp.DataSource('TAQ_NBBO',
                         tick_type='NBBO',
                         back_to_first_tick=otp.Hour(24))
    qte = qte[['ASK_PRICE', 'BID_PRICE']]
    qte = qte.rename({'ASK_PRICE': f'ASK_PRICE_{mr}',
                      'BID_PRICE': f'BID_PRICE_{mr}'})
    qte[f'quote_time_{mr}'] = qte['Time']

    # shift the data by m seconds
    qte = qte.time_interval_shift(m * 1000)
    qte_by_markout.append(qte)

trd = otp.join_by_time([trd] + qte_by_markout)
otp.run(
    trd,
    symbols=['SPY'],
    start=otp.dt(2023, 5, 15, 9, 30), 
    end=otp.dt(2023, 5, 15, 9, 30, 1),
    timezone='EST5EDT',
)
Time PRICE SIZE ASK_PRICE_m1 BID_PRICE_m1 quote_time_m1 ASK_PRICE_0 BID_PRICE_0 quote_time_0 ASK_PRICE_1 BID_PRICE_1 quote_time_1 ASK_PRICE_5 BID_PRICE_5 quote_time_5 ASK_PRICE_60 BID_PRICE_60 quote_time_60 ASK_PRICE_600 BID_PRICE_600 quote_time_600
0 2023-05-15 09:30:00.000178688 412.22 100 412.26 412.23 2023-05-15 09:29:59.000000000 412.25 412.22 2023-05-15 09:30:00.000174080 412.28 412.27 2023-05-15 09:30:01.000000000 412.23 412.21 2023-05-15 09:30:05.000000000 412.06 412.05 2023-05-15 09:31:00.000105984 411.45 411.44 2023-05-15 09:40:00.000000000
1 2023-05-15 09:30:00.000776704 412.22 247 412.26 412.23 2023-05-15 09:29:59.000000000 412.24 412.21 2023-05-15 09:30:00.000715520 412.28 412.27 2023-05-15 09:30:01.000000000 412.23 412.21 2023-05-15 09:30:05.000000000 412.06 412.05 2023-05-15 09:31:00.000772608 411.45 411.44 2023-05-15 09:40:00.000000000
2 2023-05-15 09:30:00.003603456 412.22 100 412.26 412.23 2023-05-15 09:29:59.000000000 412.24 412.22 2023-05-15 09:30:00.003562496 412.28 412.27 2023-05-15 09:30:01.002818816 412.23 412.21 2023-05-15 09:30:05.000000000 412.06 412.05 2023-05-15 09:31:00.001634816 411.45 411.44 2023-05-15 09:40:00.000000000
3 2023-05-15 09:30:00.006352128 412.24 1 412.26 412.23 2023-05-15 09:29:59.005259520 412.25 412.22 2023-05-15 09:30:00.006343936 412.28 412.27 2023-05-15 09:30:01.004251904 412.23 412.21 2023-05-15 09:30:05.000000000 412.06 412.05 2023-05-15 09:31:00.005624320 411.45 411.44 2023-05-15 09:40:00.006328576
4 2023-05-15 09:30:00.007128064 412.24 3 412.26 412.23 2023-05-15 09:29:59.007053824 412.25 412.22 2023-05-15 09:30:00.007110656 412.28 412.27 2023-05-15 09:30:01.004251904 412.23 412.21 2023-05-15 09:30:05.000000000 412.05 412.04 2023-05-15 09:31:00.007117824 411.45 411.44 2023-05-15 09:40:00.007125760
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
310 2023-05-15 09:30:00.934032640 412.27 160 412.26 412.22 2023-05-15 09:29:59.837682688 412.28 412.26 2023-05-15 09:30:00.934030080 412.28 412.26 2023-05-15 09:30:01.931170560 412.25 412.23 2023-05-15 09:30:05.898093824 412.07 412.06 2023-05-15 09:31:00.927816448 411.44 411.43 2023-05-15 09:40:00.928655104
311 2023-05-15 09:30:00.975609344 412.24 2 412.25 412.22 2023-05-15 09:29:59.970543872 412.28 412.27 2023-05-15 09:30:00.970691840 412.29 412.26 2023-05-15 09:30:01.972468480 412.25 412.24 2023-05-15 09:30:05.973397760 412.07 412.06 2023-05-15 09:31:00.972016640 411.44 411.43 2023-05-15 09:40:00.954621952
312 2023-05-15 09:30:00.980264448 412.27 1 412.25 412.22 2023-05-15 09:29:59.970543872 412.28 412.27 2023-05-15 09:30:00.979763456 412.29 412.27 2023-05-15 09:30:01.978472704 412.26 412.24 2023-05-15 09:30:05.978229504 412.07 412.06 2023-05-15 09:31:00.972016640 411.44 411.43 2023-05-15 09:40:00.954621952
313 2023-05-15 09:30:00.985391616 412.28 100 412.25 412.22 2023-05-15 09:29:59.970543872 412.28 412.27 2023-05-15 09:30:00.985296640 412.29 412.27 2023-05-15 09:30:01.985324032 412.26 412.24 2023-05-15 09:30:05.978229504 412.07 412.06 2023-05-15 09:31:00.972016640 411.44 411.43 2023-05-15 09:40:00.954621952
314 2023-05-15 09:30:00.985394944 412.28 100 412.25 412.22 2023-05-15 09:29:59.970543872 412.28 412.27 2023-05-15 09:30:00.985296640 412.29 412.27 2023-05-15 09:30:01.985324032 412.26 412.24 2023-05-15 09:30:05.978229504 412.07 412.06 2023-05-15 09:31:00.972016640 411.44 411.43 2023-05-15 09:40:00.954621952

315 rows × 21 columns