Skip to content

Commit

Permalink
Merge pull request #311 from alpacahq/extend_websocket_example
Browse files Browse the repository at this point in the history
Extend the websocket usage example
  • Loading branch information
Shlomi Kushchi authored Oct 7, 2020
2 parents 8944547 + 88048cb commit 5317f2c
Showing 1 changed file with 68 additions and 18 deletions.
86 changes: 68 additions & 18 deletions examples/websocket_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,79 @@
async def on_status(conn, channel, data):
print('polygon status update', data)

@conn.on(r'^A*$')
async def on_second_bars(conn, channel, bar):
print('bars', bar)
sec_agg_count = 0 # don't print too much quotes
@conn.on(r'A.*')
async def on_agg(conn, channel, bars):
global sec_agg_count
if sec_agg_count % 1000 == 0:
print('sec bars', bars)
sec_agg_count += 1

quote_count = 0 # don't print too much quotes
@conn.on(r'Q.*')
async def on_quotes(conn, channel, quote):
global quote_count
if quote_count % 1000 == 0:
print('quote', quote)
quote_count += 1

@conn.on(r'^AM\..+$')
async def on_minute_bars(conn, channel, bar):
print('bars', bar)
agg_count = 0 # don't print too much quotes
@conn.on(r'AM.*')
async def on_agg(conn, channel, bars):
global agg_count
if agg_count % 1000 == 0:
print('bars', bars)
agg_count += 1

quote_count = 0 # don't print too much quotes
@conn.on(r'Q\..+', ['AAPL'])
async def on_quotes(conn, channel, quote):
global quote_count
if quote_count % 10 == 0:
print('bars', quote)
quote_count += 1
trade_count = 0 # don't print too much quotes
@conn.on(r'T.*')
async def on_trades(conn, channel, trade):
global trade_count
if trade_count % 1000 == 0:
print('trade', trade)
trade_count += 1

else:
@conn.on(r'^AM\..+$')
async def on_minute_bars(conn, channel, bar):
print('bars', bar)

quote_count = 0 # don't print too much quotes
@conn.on(r'Q\..+')
async def on_quotes(conn, channel, quote):
global quote_count
if quote_count % 10 == 0:
print('quote', quote)
quote_count += 1

@conn.on(r'T\..+', ['AAPL'])
async def on_trades(conn, channel, trade):
print('trade', trade)
@conn.on(r'T\..+')
async def on_trades(conn, channel, trade):
print('trade', trade)


if USE_POLYGON:
conn.run(['trade_updates', 'AM.AAPL', 'Q.AA', 'T.*'])
# you could use either one of these:
# conn.run(['trade_updates', 'AM.AAPL', 'Q.AA', 'T.*'])
# conn.run(['trade_updates', 'AM.AAPL', 'Q.AA', 'T.*'])
# conn.run(['trade_updates', 'AM.*', 'A.*', 'Q.*', 'T.*'])
# conn.run(['trade_updates', 'Q.*', 'T.*'])
# conn.run(['trade_updates', 'AM.*', 'A.*'])
conn.run(['trade_updates', 'AM.*', 'A.*', 'Q.*', 'T.*'])
# conn.run(['Q.AAPL'])
else:
conn.run(['trade_updates', 'alpacadatav1/AM.AAPL'])
# these won't work:
# conn.run(['T.*'])
# conn.run(['Q.*'])
# conn.run(['alpacadatav1/Q.*'])
# conn.run(['T.TSLA'])
# conn.run(['Q.TSLA'])

# these are fine:
# conn.run(['AM.*'])
# conn.run(['alpacadatav1/AM.*'])

# conn.run(['alpacadatav1/AM.TSLA'])
# conn.run(['alpacadatav1/Q.GOOG'])
# conn.run(['trade_updates', 'alpacadatav1/Q.GOOG', 'alpacadatav1/AM.TSLA'])
conn.run(['alpacadatav1/T.TSLA'])

0 comments on commit 5317f2c

Please sign in to comment.