Time-Based Joins#
Time series analysis often involves looking up information from other time series for relevant time ranges. OneTick has built-in functions for this.
join_by_time
: Enhancing a time series with information from other time series at the time of each tick#
Below we’ll enhance the trades with the prevailing quote (i.e., best bid and ask) at the time of each trade. We’ll first look just at trades, then just at quotes, and finally we’ll join the two.
Let’s examine the trades first.
import onetick.py as otp
s = otp.dt(2023, 5, 15, 9, 30)
e = otp.dt(2023, 5, 15, 9, 30, 1)
trd = otp.DataSource('NYSE_TAQ', tick_type='TRD')
trd = trd[['PRICE', 'SIZE']]
otp.run(trd, start=s, end=e, symbols=['SPY'])
Time | PRICE | SIZE | |
---|---|---|---|
0 | 2023-05-15 09:30:00.000178688 | 412.22 | 100 |
1 | 2023-05-15 09:30:00.000776704 | 412.22 | 247 |
2 | 2023-05-15 09:30:00.003603456 | 412.22 | 100 |
3 | 2023-05-15 09:30:00.006352128 | 412.24 | 1 |
4 | 2023-05-15 09:30:00.007128064 | 412.24 | 3 |
... | ... | ... | ... |
310 | 2023-05-15 09:30:00.934032640 | 412.27 | 160 |
311 | 2023-05-15 09:30:00.975609344 | 412.24 | 2 |
312 | 2023-05-15 09:30:00.980264448 | 412.27 | 1 |
313 | 2023-05-15 09:30:00.985391616 | 412.28 | 100 |
314 | 2023-05-15 09:30:00.985394944 | 412.28 | 100 |
315 rows × 3 columns
Now let’s take a look at the quotes (or rather the ‘national best bid/offer’). Note the back_to_fist_tick
parameter which ensures that there is a quote at the start of the query (it’s actual timestamp is before the start).
qte = otp.DataSource('TAQ_NBBO', tick_type='NBBO', back_to_first_tick=60)
qte = qte[['BID_PRICE', 'ASK_PRICE']]
otp.run(qte, start=s, end=e, symbols=['SPY'])
Time | BID_PRICE | ASK_PRICE | |
---|---|---|---|
0 | 2023-05-15 09:30:00.000000000 | 412.22 | 412.25 |
1 | 2023-05-15 09:30:00.000174080 | 412.22 | 412.25 |
2 | 2023-05-15 09:30:00.000595200 | 412.22 | 412.24 |
3 | 2023-05-15 09:30:00.000715520 | 412.21 | 412.24 |
4 | 2023-05-15 09:30:00.000790784 | 412.22 | 412.24 |
... | ... | ... | ... |
1150 | 2023-05-15 09:30:00.979763456 | 412.27 | 412.28 |
1151 | 2023-05-15 09:30:00.981818880 | 412.27 | 412.28 |
1152 | 2023-05-15 09:30:00.983727872 | 412.27 | 412.28 |
1153 | 2023-05-15 09:30:00.983737856 | 412.27 | 412.28 |
1154 | 2023-05-15 09:30:00.985296640 | 412.27 | 412.28 |
1155 rows × 3 columns
We “enhance” the trades with the information from the quotes.
enh_trd = otp.join_by_time([trd, qte])
otp.run(enh_trd, start=s, end=e, symbols=['SPY'])
Time | PRICE | SIZE | BID_PRICE | ASK_PRICE | |
---|---|---|---|---|---|
0 | 2023-05-15 09:30:00.000178688 | 412.22 | 100 | 412.22 | 412.25 |
1 | 2023-05-15 09:30:00.000776704 | 412.22 | 247 | 412.21 | 412.24 |
2 | 2023-05-15 09:30:00.003603456 | 412.22 | 100 | 412.22 | 412.24 |
3 | 2023-05-15 09:30:00.006352128 | 412.24 | 1 | 412.22 | 412.25 |
4 | 2023-05-15 09:30:00.007128064 | 412.24 | 3 | 412.22 | 412.25 |
... | ... | ... | ... | ... | ... |
310 | 2023-05-15 09:30:00.934032640 | 412.27 | 160 | 412.26 | 412.28 |
311 | 2023-05-15 09:30:00.975609344 | 412.24 | 2 | 412.27 | 412.28 |
312 | 2023-05-15 09:30:00.980264448 | 412.27 | 1 | 412.27 | 412.28 |
313 | 2023-05-15 09:30:00.985391616 | 412.28 | 100 | 412.27 | 412.28 |
314 | 2023-05-15 09:30:00.985394944 | 412.28 | 100 | 412.27 | 412.28 |
315 rows × 5 columns
In other words, each trade is joined with the quote that was active at the time the trade took place. We can examine the quote time to make sure it’s before the trade time.
qte['quote_time'] = qte['Time']
enh_trd = otp.join_by_time([trd, qte])
otp.run(enh_trd, start=s, end=e, symbols=['SPY'])
Time | PRICE | SIZE | BID_PRICE | ASK_PRICE | quote_time | |
---|---|---|---|---|---|---|
0 | 2023-05-15 09:30:00.000178688 | 412.22 | 100 | 412.22 | 412.25 | 2023-05-15 09:30:00.000174080 |
1 | 2023-05-15 09:30:00.000776704 | 412.22 | 247 | 412.21 | 412.24 | 2023-05-15 09:30:00.000715520 |
2 | 2023-05-15 09:30:00.003603456 | 412.22 | 100 | 412.22 | 412.24 | 2023-05-15 09:30:00.003562496 |
3 | 2023-05-15 09:30:00.006352128 | 412.24 | 1 | 412.22 | 412.25 | 2023-05-15 09:30:00.006343936 |
4 | 2023-05-15 09:30:00.007128064 | 412.24 | 3 | 412.22 | 412.25 | 2023-05-15 09:30:00.007110656 |
... | ... | ... | ... | ... | ... | ... |
310 | 2023-05-15 09:30:00.934032640 | 412.27 | 160 | 412.26 | 412.28 | 2023-05-15 09:30:00.934030080 |
311 | 2023-05-15 09:30:00.975609344 | 412.24 | 2 | 412.27 | 412.28 | 2023-05-15 09:30:00.970691840 |
312 | 2023-05-15 09:30:00.980264448 | 412.27 | 1 | 412.27 | 412.28 | 2023-05-15 09:30:00.979763456 |
313 | 2023-05-15 09:30:00.985391616 | 412.28 | 100 | 412.27 | 412.28 | 2023-05-15 09:30:00.985296640 |
314 | 2023-05-15 09:30:00.985394944 | 412.28 | 100 | 412.27 | 412.28 | 2023-05-15 09:30:00.985296640 |
315 rows × 6 columns
Use case: Prevailing quote at the time of a trade#
The code snippet below implements this use case.
qte = otp.DataSource('TAQ_NBBO', tick_type='NBBO', back_to_first_tick=60)
qte = qte[['BID_PRICE', 'ASK_PRICE']]
qte['quote_time'] = qte['Time']
enh_trd = otp.join_by_time([trd, qte])
otp.run(enh_trd, start=otp.dt(2023, 3, 29, 10), end=otp.dt(2023, 3, 29, 10, 1), symbols=['AA'])
Time | PRICE | SIZE | BID_PRICE | ASK_PRICE | quote_time | |
---|---|---|---|---|---|---|
0 | 2023-03-29 10:00:00.734953984 | 40.9500 | 7 | 40.92 | 40.95 | 2023-03-29 10:00:00.000000000 |
1 | 2023-03-29 10:00:01.492248576 | 40.9500 | 100 | 40.92 | 40.95 | 2023-03-29 10:00:01.158007296 |
2 | 2023-03-29 10:00:03.348103936 | 40.9500 | 8 | 40.92 | 40.95 | 2023-03-29 10:00:03.347166976 |
3 | 2023-03-29 10:00:03.348121856 | 40.9500 | 391 | 40.92 | 40.95 | 2023-03-29 10:00:03.348119296 |
4 | 2023-03-29 10:00:03.348169984 | 40.9500 | 100 | 40.92 | 40.95 | 2023-03-29 10:00:03.348169472 |
... | ... | ... | ... | ... | ... | ... |
177 | 2023-03-29 10:00:53.957339392 | 41.0000 | 61 | 40.98 | 41.00 | 2023-03-29 10:00:53.957334528 |
178 | 2023-03-29 10:00:55.377122560 | 41.0100 | 5 | 40.98 | 41.01 | 2023-03-29 10:00:54.791796736 |
179 | 2023-03-29 10:00:55.612973568 | 40.9916 | 1 | 40.98 | 41.01 | 2023-03-29 10:00:54.791796736 |
180 | 2023-03-29 10:00:57.381350656 | 41.0000 | 10 | 40.99 | 41.03 | 2023-03-29 10:00:57.381330688 |
181 | 2023-03-29 10:00:57.382493184 | 41.0100 | 100 | 41.00 | 41.03 | 2023-03-29 10:00:57.381917184 |
182 rows × 6 columns
Use case: Computing Markouts#
A common TCA and quant research use case is to see what happens to the quote at certain intervals (aka markouts) before/after the trade. We can compute markouts efficiently by shifting the time series for each markout and then doing join_by_time
.
s = otp.dt(2023, 3, 8, 10)
e = otp.dt(2023, 3, 9, 10, 0, 1)
markouts = [-1, 1, 5]
trd = otp.DataSource('NYSE_TAQ', tick_type='TRD')
trd = trd[['PRICE', 'SIZE']]
qte_by_markout = []
for m in markouts:
qte = otp.DataSource('TAQ_NBBO', tick_type='NBBO', back_to_first_tick=86400)
qte = qte[['ASK_PRICE', 'BID_PRICE']]
qte = qte.rename({'ASK_PRICE': f'ASK_PRICE_{m}',
'BID_PRICE': f'BID_PRICE_{m}'})
qte[f'quote_time_{m}'] = 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, start=s, end=e, symbols='TSLA', apply_times_daily=True)
Time | PRICE | SIZE | ASK_PRICE_-1 | BID_PRICE_-1 | quote_time_-1 | ASK_PRICE_1 | BID_PRICE_1 | quote_time_1 | ASK_PRICE_5 | BID_PRICE_5 | quote_time_5 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 2023-03-08 10:00:00.014762069 | 181.4250 | 300 | 181.47 | 181.41 | 2023-03-08 09:59:59.002564435 | 181.49 | 181.48 | 2023-03-08 10:00:01.000000000 | 181.15 | 181.04 | 2023-03-08 10:00:05.012557243 |
1 | 2023-03-08 10:00:00.014764168 | 181.4400 | 12 | 181.47 | 181.41 | 2023-03-08 09:59:59.002564435 | 181.49 | 181.48 | 2023-03-08 10:00:01.000000000 | 181.15 | 181.04 | 2023-03-08 10:00:05.012557243 |
2 | 2023-03-08 10:00:00.014767570 | 181.4400 | 200 | 181.47 | 181.41 | 2023-03-08 09:59:59.002564435 | 181.49 | 181.48 | 2023-03-08 10:00:01.000000000 | 181.15 | 181.04 | 2023-03-08 10:00:05.012557243 |
3 | 2023-03-08 10:00:00.014799259 | 181.4500 | 2 | 181.47 | 181.41 | 2023-03-08 09:59:59.002564435 | 181.49 | 181.48 | 2023-03-08 10:00:01.000000000 | 181.15 | 181.04 | 2023-03-08 10:00:05.012557243 |
4 | 2023-03-08 10:00:00.015374686 | 181.4400 | 300 | 181.47 | 181.41 | 2023-03-08 09:59:59.002564435 | 181.49 | 181.48 | 2023-03-08 10:00:01.000000000 | 181.15 | 181.04 | 2023-03-08 10:00:05.012557243 |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
705 | 2023-03-09 10:00:00.956041264 | 183.5899 | 500 | 183.46 | 183.43 | 2023-03-09 09:59:59.911620614 | 183.59 | 183.54 | 2023-03-09 10:00:01.945769144 | 183.65 | 183.57 | 2023-03-09 10:00:05.954673686 |
706 | 2023-03-09 10:00:00.967881686 | 183.5800 | 25 | 183.46 | 183.43 | 2023-03-09 09:59:59.963372673 | 183.59 | 183.54 | 2023-03-09 10:00:01.963256371 | 183.66 | 183.57 | 2023-03-09 10:00:05.967652794 |
707 | 2023-03-09 10:00:00.978963708 | 183.4718 | 136 | 183.46 | 183.42 | 2023-03-09 09:59:59.971349853 | 183.59 | 183.54 | 2023-03-09 10:00:01.963256371 | 183.66 | 183.57 | 2023-03-09 10:00:05.967652794 |
708 | 2023-03-09 10:00:00.983358396 | 183.5500 | 20 | 183.46 | 183.41 | 2023-03-09 09:59:59.983288366 | 183.59 | 183.54 | 2023-03-09 10:00:01.963256371 | 183.66 | 183.57 | 2023-03-09 10:00:05.967652794 |
709 | 2023-03-09 10:00:00.990945922 | 183.5650 | 200 | 183.44 | 183.41 | 2023-03-09 09:59:59.988781014 | 183.59 | 183.54 | 2023-03-09 10:00:01.963256371 | 183.66 | 183.57 | 2023-03-09 10:00:05.967652794 |
710 rows × 12 columns
join_with_query
: Executing a query on each tick#
Sometimes we want to do a look up based on the information provided in a tick. For example, we may have a series of order ticks each containing an order arrival and exit times and we may want to find the market vwap during the interval. Let’s take this one step at a time.
Let’s find the market vwap for a given time range (i.e., for a given start
and end
).
q = otp.DataSource('NYSE_TAQ', tick_type='TRD')
q = q.agg({'market_vwap': otp.agg.vwap('PRICE', 'SIZE')})
otp.run(q, start=s, end=e, symbols=['SPY'])
Time | market_vwap | |
---|---|---|
0 | 2023-03-09 10:00:01 | 398.478916 |
Next let’s create a couple of orders each with its own values for start
/end
specified in the arrival
/exit
columns.
orders = otp.Ticks(arrival=[s, s+otp.Milli(7934)],
exit=[e, e+otp.Milli(9556)],
sym=['SPY', 'QQQ'])
otp.run(orders)
Time | arrival | exit | sym | |
---|---|---|---|---|
0 | 2003-12-01 00:00:00.000 | 2023-03-08 10:00:00.000 | 2023-03-09 10:00:01.000 | SPY |
1 | 2003-12-01 00:00:00.001 | 2023-03-08 10:00:07.934 | 2023-03-09 10:00:10.556 | QQQ |
We can wrap the code that finds vwap into a function and call it for each order while passing the relevant parameters for start
, end
, and symbol
.
def vwap(symbol):
q = otp.DataSource('NYSE_TAQ', tick_type='TRD')
q = q.agg({'market_vwap': otp.agg.vwap('PRICE','SIZE')})
return q
orders = otp.Ticks(arrival=[s, s+otp.Milli(7934)],
exit=[e, e+otp.Milli(9556)],
sym=['SPY', 'QQQ'])
orders = orders.join_with_query(vwap, start=orders['arrival'], end=orders['exit'], symbol=orders['sym'])
otp.run(orders, start=s, end=s+otp.Day(1))
Time | market_vwap | arrival | exit | sym | |
---|---|---|---|---|---|
0 | 2023-03-08 10:00:00.000 | 398.478916 | 2023-03-08 10:00:00.000 | 2023-03-09 10:00:01.000 | SPY |
1 | 2023-03-08 10:00:00.001 | 297.235723 | 2023-03-08 10:00:07.934 | 2023-03-09 10:00:10.556 | QQQ |
Interval VWAP: the efficient way#
The code above provides an implementation for this use case. However, a more efficient implementation may be useful when the number of orders is large. We provide a more efficient implementation below.
orders['_PARAM_START_TIME_NANOS'] = orders['arrival']
orders['_PARAM_END_TIME_NANOS'] = orders['exit']
orders['SYMBOL_NAME'] = orders['sym']
otp.run(vwap, symbols=orders, start=otp.dt(2022,12,1), end=otp.dt(2022,12,2))
{'SPY': Time market_vwap
0 2023-03-09 10:00:01 398.478916,
'QQQ': Time market_vwap
0 2023-03-09 10:00:10.556 297.235723}