Upticks / Downticks#

Let’s mark each trade as an uptick if its price is above the last trade’s price and as a downtick if it’s below.

import onetick.py as otp

def uptick(t):
    if t['PRICE'] == otp.nan or t['PRICE'][-1] == otp.nan:
        return otp.nan
    if t['PRICE'] > t['PRICE'][-1]:
        return 1
    elif t['PRICE'] < t['PRICE'][-1]:
        return -1
    else:
        return 0

trd = otp.DataSource('NYSE_TAQ', tick_type='TRD')
trd = trd[['PRICE']]
trd['UPTICK'] = trd.apply(uptick)
otp.run(
    trd,
    symbols=['SPY'],
    start=otp.dt(2023, 5, 15, 9, 30), 
    end=otp.dt(2023, 5, 15, 9, 30, 1),
)
Time PRICE UPTICK
0 2023-05-15 09:30:00.000178688 412.22 NaN
1 2023-05-15 09:30:00.000776704 412.22 0.0
2 2023-05-15 09:30:00.003603456 412.22 0.0
3 2023-05-15 09:30:00.006352128 412.24 1.0
4 2023-05-15 09:30:00.007128064 412.24 0.0
... ... ... ...
310 2023-05-15 09:30:00.934032640 412.27 0.0
311 2023-05-15 09:30:00.975609344 412.24 -1.0
312 2023-05-15 09:30:00.980264448 412.27 1.0
313 2023-05-15 09:30:00.985391616 412.28 1.0
314 2023-05-15 09:30:00.985394944 412.28 0.0

315 rows × 3 columns