Interval Metrics (e.g., VWAP)#

import onetick.py as otp

query = otp.DataSource('NYSE_TAQ', tick_type='TRD')
query = query.agg({'market_vwap': otp.agg.vwap('PRICE', 'SIZE')})

otp.run(
    query,
    symbols=['SPY'],
    start=otp.dt(2023, 5, 15, 9, 30),
    end=otp.dt(2023, 5, 15, 9, 30, 1),
    timezone='EST5EDT'
)
Time market_vwap
0 2023-05-15 09:30:01 412.212012

Computing market VWAP for every order’s arrival/exit interval#

start = otp.dt(2023, 5, 15, 9, 30)
end = otp.dt(2023, 5, 15, 9, 30, 1)

orders = otp.Ticks(arrival=[start, start + otp.Milli(7934)],
                   exit=[end, end + otp.Milli(9556)],
                   sym=['SPY', 'QQQ'])
otp.run(orders, start=start, end=start + otp.Day(1))
Time arrival exit sym
0 2023-05-15 09:30:00.000 2023-05-15 09:30:00.000 2023-05-15 09:30:01.000 SPY
1 2023-05-15 09:30:00.001 2023-05-15 09:30:07.934 2023-05-15 09:30:10.556 QQQ
def vwap(symbol):
    q = otp.DataSource('NYSE_TAQ', tick_type='TRD')
    q = q.agg({'market_vwap': otp.agg.vwap('PRICE','SIZE')})
    return q

orders = orders.join_with_query(vwap, start=orders['arrival'], end=orders['exit'], symbol=orders['sym'])
otp.run(orders, start=start, end=end + otp.Day(1))
Time market_vwap arrival exit sym
0 2023-05-15 09:30:00.000 412.212012 2023-05-15 09:30:00.000 2023-05-15 09:30:01.000 SPY
1 2023-05-15 09:30:00.001 325.318988 2023-05-15 09:30:07.934 2023-05-15 09:30:10.556 QQQ

A more efficient implementation is also available with symbol parameters.