From e2793e9f8552b9ac7ec5a816650e9264a7959632 Mon Sep 17 00:00:00 2001 From: Shlomi Kushchi Date: Sun, 20 Sep 2020 18:40:24 +0300 Subject: [PATCH 1/4] messages from polygon and alpaca are different let's split it for readability --- examples/websocket_example.py | 60 +++++++++++++++++++++++++---------- 1 file changed, 44 insertions(+), 16 deletions(-) diff --git a/examples/websocket_example.py b/examples/websocket_example.py index c6611ab1..b98fe3ee 100644 --- a/examples/websocket_example.py +++ b/examples/websocket_example.py @@ -23,26 +23,54 @@ 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 + + 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 - @conn.on(r'^AM\..+$') - async def on_minute_bars(conn, channel, bar): - print('bars', bar) + 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 - 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 + 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: From baee34075a4588e1b512837b3d290d525c2f3834 Mon Sep 17 00:00:00 2001 From: Shlomi Kushchi Date: Mon, 21 Sep 2020 09:00:47 +0300 Subject: [PATCH 2/4] added examples for the alpaca websocket --- examples/websocket_example.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/examples/websocket_example.py b/examples/websocket_example.py index b98fe3ee..8c3ee25c 100644 --- a/examples/websocket_example.py +++ b/examples/websocket_example.py @@ -76,4 +76,18 @@ async def on_trades(conn, channel, trade): if USE_POLYGON: conn.run(['trade_updates', 'AM.AAPL', 'Q.AA', 'T.*']) 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(['alpacadatav1/T.TSLA']) + From d5802eb0b4871e618627f69141eca4cc9d77ea32 Mon Sep 17 00:00:00 2001 From: Shlomi Kushchi Date: Mon, 21 Sep 2020 09:50:08 +0300 Subject: [PATCH 3/4] polygon example of using the websocket. --- examples/websocket_example.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/examples/websocket_example.py b/examples/websocket_example.py index 8c3ee25c..95ece2bf 100644 --- a/examples/websocket_example.py +++ b/examples/websocket_example.py @@ -74,7 +74,14 @@ async def on_trades(conn, channel, 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: # these won't work: # conn.run(['T.*']) From 88048cb3b1c68aeeee1e3f2ca2b5f7d662ddfd56 Mon Sep 17 00:00:00 2001 From: Shlomi Kushchi Date: Wed, 23 Sep 2020 18:44:40 +0300 Subject: [PATCH 4/4] combined example for the alapca stream --- examples/websocket_example.py | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/websocket_example.py b/examples/websocket_example.py index 95ece2bf..3266b6a6 100644 --- a/examples/websocket_example.py +++ b/examples/websocket_example.py @@ -96,5 +96,6 @@ async def on_trades(conn, channel, trade): # 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'])