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(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::AAPL',
start=otp.dt(2024, 2, 1, 10), end=otp.dt(2024, 2, 1, 11),
# symbols='US_COMP:AAPL',
# start=otp.dt.now(), end=otp.dt.now() + otp.Day(1),
)
2024-02-01 15:01:11.979434 {'PRICE': 185.315, 'short': 185.1012592727279, 'long': 185.10124217054332, 'buy': 1.0, 'sell': 0.0}
BUY
2024-02-01 15:09:17.692410 {'PRICE': 185.83, 'short': 185.8235863521478, 'long': 185.82410441401197, 'buy': 0.0, 'sell': 1.0}
SELL
2024-02-01 15:09:54.236147 {'PRICE': 185.92, 'short': 185.8415095375788, 'long': 185.84149936037954, 'buy': 1.0, 'sell': 0.0}
BUY
2024-02-01 15:10:43.374575 {'PRICE': 185.82, 'short': 185.84715977974275, 'long': 185.84725242573356, 'buy': 0.0, 'sell': 1.0}
SELL
2024-02-01 15:15:08.838051 {'PRICE': 185.8019, 'short': 185.72218998083542, 'long': 185.72213659129, 'buy': 1.0, 'sell': 0.0}
BUY
2024-02-01 15:18:10.136909 {'PRICE': 185.84, 'short': 185.8203101170747, 'long': 185.8203847572364, 'buy': 0.0, 'sell': 1.0}
SELL
2024-02-01 15:23:22.049210 {'PRICE': 185.76, 'short': 185.73055907108136, 'long': 185.73054262537946, 'buy': 1.0, 'sell': 0.0}
BUY
2024-02-01 15:31:14.016747 {'PRICE': 185.97, 'short': 185.99928843829, 'long': 185.9993552988522, 'buy': 0.0, 'sell': 1.0}
SELL
2024-02-01 15:32:14.084341 {'PRICE': 186.04, 'short': 186.00823951937195, 'long': 186.00822931192164, 'buy': 1.0, 'sell': 0.0}
BUY
2024-02-01 15:32:14.089919 {'PRICE': 186.04, 'short': 186.00825011679626, 'long': 186.00825222519072, 'buy': 0.0, 'sell': 1.0}
SELL
2024-02-01 15:32:14.090346 {'PRICE': 186.04, 'short': 186.00826070715092, 'long': 186.00825381083524, 'buy': 1.0, 'sell': 0.0}
BUY
2024-02-01 15:32:48.617090 {'PRICE': 185.97, 'short': 186.00828649324842, 'long': 186.00829738070848, 'buy': 0.0, 'sell': 1.0}
SELL
2024-02-01 15:33:54.738241 {'PRICE': 186.02, 'short': 186.02067020869933, 'long': 186.02066442592587, 'buy': 1.0, 'sell': 0.0}
BUY
2024-02-01 15:40:13.957635 {'PRICE': 186.15, 'short': 186.18989411766114, 'long': 186.1899021301011, 'buy': 0.0, 'sell': 1.0}
SELL
2024-02-01 15:41:07.158301 {'PRICE': 186.26, 'short': 186.2036963288388, 'long': 186.20369213198435, 'buy': 1.0, 'sell': 0.0}
BUY
2024-02-01 15:41:50.002619 {'PRICE': 186.16, 'short': 186.20213928979697, 'long': 186.20214284271992, 'buy': 0.0, 'sell': 1.0}
SELL
2024-02-01 15:44:04.745022 {'PRICE': 186.16, 'short': 186.16751161396365, 'long': 186.16731067084527, 'buy': 1.0, 'sell': 0.0}
BUY
2024-02-01 15:44:17.078507 {'PRICE': 186.13, 'short': 186.16499118745529, 'long': 186.16499545709772, 'buy': 0.0, 'sell': 1.0}
SELL
2024-02-01 15:47:27.915190 {'PRICE': 186.175, 'short': 186.123525523029, 'long': 186.12347667562167, 'buy': 1.0, 'sell': 0.0}
BUY
2024-02-01 15:49:07.049957 {'PRICE': 186.03, 'short': 186.12888655586016, 'long': 186.12890134271402, 'buy': 0.0, 'sell': 1.0}
SELL