Real-time processing: 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 you set start/end times accordingly (see the commented out line).
# timestamps appear in GMT
cb = GoldenCrossCallback()
otp.run(trd, symbols=['SPY'],
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:02:16.717255 {'PRICE': 405.54, 'short': 405.70794973568997, 'long': 405.70836495181135, 'buy': 0.0, 'sell': 1.0}
SELL
..............................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
...................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................