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(tick_type='TRD', schema={'PRICE': float})
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 the database and start/end times are set accordingly (see the commented out lines). With the US_COMP_SAMPLE database used and the time interval set in the past, the query will work in “historical” mode.

# timestamps appear in GMT
cb = GoldenCrossCallback()
otp.run(trd,
        callback=cb, running=True,
        symbols='US_COMP_SAMPLE::AMZN',
        start=otp.dt(2024, 2, 1, 10), end=otp.dt(2024, 2, 1, 11),
        # symbols='US_COMP::AMZN',
        # start=otp.dt.now(), end=otp.dt.now() + otp.Day(1),
)
2024-02-01 15:03:21.455144 {'PRICE': 158.2, 'short': 157.97445747563012, 'long': 157.97438842234772, 'buy': 1.0, 'sell': 0.0}
BUY

2024-02-01 15:07:24.788384 {'PRICE': 158.4599, 'short': 158.42578645640995, 'long': 158.42604791730056, 'buy': 0.0, 'sell': 1.0}
SELL
2024-02-01 15:08:07.403198 {'PRICE': 158.5901, 'short': 158.5014752299688, 'long': 158.5013617582309, 'buy': 1.0, 'sell': 0.0}
BUY

2024-02-01 15:08:07.957380 {'PRICE': 158.59, 'short': 158.50237916121398, 'long': 158.50239162999122, 'buy': 0.0, 'sell': 1.0}
SELL

2024-02-01 15:08:07.957813 {'PRICE': 158.59, 'short': 158.5024365422479, 'long': 158.5024008065252, 'buy': 1.0, 'sell': 0.0}
BUY

2024-02-01 15:08:09.768112 {'PRICE': 158.5813, 'short': 158.50784500979285, 'long': 158.50882239998944, 'buy': 0.0, 'sell': 1.0}
SELL

2024-02-01 15:08:15.312467 {'PRICE': 158.5888, 'short': 158.51707501562302, 'long': 158.51701274251764, 'buy': 1.0, 'sell': 0.0}
BUY

2024-02-01 15:08:15.486213 {'PRICE': 158.57, 'short': 158.51715277605268, 'long': 158.5174934711772, 'buy': 0.0, 'sell': 1.0}
SELL

2024-02-01 15:08:18.148820 {'PRICE': 158.59, 'short': 158.52095593846914, 'long': 158.52093938502517, 'buy': 1.0, 'sell': 0.0}
BUY

2024-02-01 15:08:21.482434 {'PRICE': 158.545, 'short': 158.52585352370136, 'long': 158.52632588073553, 'buy': 0.0, 'sell': 1.0}
SELL

2024-02-01 15:08:25.606591 {'PRICE': 158.562, 'short': 158.5309094890584, 'long': 158.53084700708925, 'buy': 1.0, 'sell': 0.0}
BUY
2024-02-01 15:11:54.801691 {'PRICE': 158.635, 'short': 158.84352976545534, 'long': 158.8439325129406, 'buy': 0.0, 'sell': 1.0}
SELL
2024-02-01 15:18:11.546537 {'PRICE': 158.275, 'short': 158.1583888832469, 'long': 158.1580828034444, 'buy': 1.0, 'sell': 0.0}
BUY

2024-02-01 15:21:37.218059 {'PRICE': 158.23, 'short': 158.26213875248894, 'long': 158.26214449824036, 'buy': 0.0, 'sell': 1.0}
SELL
2024-02-01 15:23:39.254873 {'PRICE': 158.3499, 'short': 158.29007506793593, 'long': 158.29003725197592, 'buy': 1.0, 'sell': 0.0}
BUY

2024-02-01 15:24:16.668463 {'PRICE': 158.12, 'short': 158.2709523117588, 'long': 158.27106276925767, 'buy': 0.0, 'sell': 1.0}
SELL

2024-02-01 15:27:40.097497 {'PRICE': 158.255, 'short': 158.1500660046751, 'long': 158.14984654192338, 'buy': 1.0, 'sell': 0.0}
BUY
2024-02-01 15:30:41.766369 {'PRICE': 158.072, 'short': 158.13806704225524, 'long': 158.13841187677446, 'buy': 0.0, 'sell': 1.0}
SELL

2024-02-01 15:32:03.366835 {'PRICE': 158.3, 'short': 158.16952092472414, 'long': 158.16950438942908, 'buy': 1.0, 'sell': 0.0}
BUY

2024-02-01 15:33:10.621053 {'PRICE': 158.0366, 'short': 158.168201979609, 'long': 158.16843030270553, 'buy': 0.0, 'sell': 1.0}
SELL

2024-02-01 15:34:26.027037 {'PRICE': 158.16, 'short': 158.15288958475796, 'long': 158.15275447146374, 'buy': 1.0, 'sell': 0.0}
BUY
2024-02-01 15:36:49.961798 {'PRICE': 158.22, 'short': 158.19172526169663, 'long': 158.191868475698, 'buy': 0.0, 'sell': 1.0}
SELL

2024-02-01 15:37:09.774374 {'PRICE': 158.2161, 'short': 158.1883632730942, 'long': 158.18835810319192, 'buy': 1.0, 'sell': 0.0}
BUY

2024-02-01 15:37:31.891598 {'PRICE': 158.095, 'short': 158.17807487714907, 'long': 158.1780754058365, 'buy': 0.0, 'sell': 1.0}
SELL

2024-02-01 15:37:32.103720 {'PRICE': 158.1, 'short': 158.1789522626737, 'long': 158.17803038295054, 'buy': 1.0, 'sell': 0.0}
BUY

2024-02-01 15:37:34.091985 {'PRICE': 158.0686, 'short': 158.17770723885636, 'long': 158.17774096550824, 'buy': 0.0, 'sell': 1.0}
SELL
2024-02-01 15:45:09.038441 {'PRICE': 157.98, 'short': 157.97695984200067, 'long': 157.97680557078928, 'buy': 1.0, 'sell': 0.0}
BUY

2024-02-01 15:45:24.460498 {'PRICE': 157.87, 'short': 157.9705107679523, 'long': 157.97056450306988, 'buy': 0.0, 'sell': 1.0}
SELL

2024-02-01 15:46:15.545864 {'PRICE': 158.035, 'short': 157.964221931041, 'long': 157.9642125673227, 'buy': 1.0, 'sell': 0.0}
BUY

2024-02-01 15:50:41.087986 {'PRICE': 158.09, 'short': 158.06068225220582, 'long': 158.06100264879095, 'buy': 0.0, 'sell': 1.0}
SELL