Real-time processing#

All of the queries can run in real-time mode. Real-time processing is enabled via callbacks. We illustrate this on the signal generation use case.

Use case: Signal Generation#

We’ll compute golden cross signals using 50-second and 200-second moving averages

  • ‘Entries’ is set to 1 when the short-term moving average goes above the long term (i.e., a signal to buy)

  • ‘Exits’ is set to 1 on when the short-term moving average goes below the long term (i.e., a signal to sell)

import onetick.py as otp

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

trd = trd.agg({'short': otp.agg.mean('PRICE')}, bucket_interval=60, running=True, all_fields=True)
trd = trd.agg({'long': otp.agg.mean('PRICE')}, bucket_interval=60 * 5, running=True, all_fields=True)

trd['buy'] = (trd['short'][-1] < trd['long'][-1]) & (trd['short'] > trd['long']) 
trd['sell'] = (trd['short'][-1] > trd['long'][-1]) & (trd['short'] < trd['long']) 

We define a callback that for every tick (i.e., on every trade) will

  • print a ‘.’ if there is no signal

  • print out the tick followed by ‘BUY’ on an entry signal

  • print out the tick followed by ‘SELL’ on an exit signal

class GoldenCrossCallback(otp.CallbackBase):
    def process_tick(self, tick, time):
        if not tick['buy'] and not tick['sell']:
            print('.', end='')
            return
        print()
        print()
        print(time, tick)
        if tick['buy']:
            print('BUY')
        if tick['sell']:
            print('SELL')
        print()

The query will run continuously with the output printed as the events happen if we set start/end times accordingly (see the commented out line).

# timestamps appear in GMT
cb = GoldenCrossCallback()
otp.run(trd, symbols='AMZN',
        callback=cb, running=True,
        # start=otp.dt.now(), end=otp.dt.now() + otp.Day(1),
        start=otp.dt(2023, 3, 31, 10), end=otp.dt(2023, 3, 31, 10, 5),
)
..................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................

2023-03-31 14:01:00.702788 {'PRICE': 102.685, 'short': 102.55571394019401, 'long': 102.55571526781988, 'buy': 0.0, 'sell': 1.0}
SELL



2023-03-31 14:01:00.715928 {'PRICE': 102.69, 'short': 102.55577296703349, 'long': 102.5557718802702, 'buy': 1.0, 'sell': 0.0}
BUY

..

2023-03-31 14:01:00.859880 {'PRICE': 102.685, 'short': 102.55593452066896, 'long': 102.55593932631618, 'buy': 0.0, 'sell': 1.0}
SELL

..

2023-03-31 14:01:01.362596 {'PRICE': 102.6888, 'short': 102.55619792035449, 'long': 102.5561062237178, 'buy': 1.0, 'sell': 0.0}
BUY

.......................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................

2023-03-31 14:02:37.925538 {'PRICE': 102.51, 'short': 102.58265328102567, 'long': 102.58265371464864, 'buy': 0.0, 'sell': 1.0}
SELL

.......................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
...................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................