-
Notifications
You must be signed in to change notification settings - Fork 539
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #379 from alpacahq/advanced_streamconn_usage
Advanced streamconn usage
- Loading branch information
Showing
8 changed files
with
234 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
""" | ||
In this example code we wrap the ws connection to make sure we reconnect | ||
in case of ws disconnection. | ||
""" | ||
|
||
import logging | ||
import threading | ||
import asyncio | ||
import time | ||
from alpaca_trade_api import StreamConn | ||
from alpaca_trade_api.common import URL | ||
|
||
logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO) | ||
|
||
ALPACA_API_KEY = "<YOUR-API-KEY>" | ||
ALPACA_SECRET_KEY = "<YOUR-SECRET-KEY>" | ||
USE_POLYGON = False | ||
|
||
|
||
def run_connection(conn, channels): | ||
try: | ||
conn.run(channels) | ||
except Exception as e: | ||
print(f'Exception from websocket connection: {e}') | ||
finally: | ||
send_msg(f"Trying to re-establish connection") | ||
time.sleep(3) | ||
run_connection(conn, channels) | ||
|
||
if __name__ == '__main__': | ||
channels = ['alpacadatav1/Q.GOOG'] | ||
|
||
conn = StreamConn( | ||
ALPACA_API_KEY, | ||
ALPACA_SECRET_KEY, | ||
base_url=URL('https://paper-api.alpaca.markets'), | ||
data_url=URL('https://data.alpaca.markets'), | ||
# data_url=URL('http://127.0.0.1:8765'), | ||
data_stream='polygon' if USE_POLYGON else 'alpacadatav1' | ||
) | ||
|
||
|
||
@conn.on(r'^AM\..+$') | ||
async def on_minute_bars(conn, channel, bar): | ||
print('bars', bar) | ||
|
||
|
||
@conn.on(r'Q\..+') | ||
async def on_quotes(conn, channel, quote): | ||
print('quote', quote) | ||
|
||
|
||
@conn.on(r'T\..+') | ||
async def on_trades(conn, channel, trade): | ||
print('trade', trade) | ||
|
||
run_connection(conn, channels) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
""" | ||
In this example code we will show a pattern that allows a user to change | ||
the websocket subscriptions as they please. | ||
""" | ||
import logging | ||
import threading | ||
import asyncio | ||
import time | ||
from alpaca_trade_api import StreamConn | ||
from alpaca_trade_api.common import URL | ||
|
||
ALPACA_API_KEY = "<YOUR-API-KEY>" | ||
ALPACA_SECRET_KEY = "<YOUR-SECRET-KEY>" | ||
USE_POLYGON = False | ||
|
||
conn: StreamConn = None | ||
|
||
def consumer_thread(): | ||
|
||
try: | ||
# make sure we have an event loop, if not create a new one | ||
loop = asyncio.get_event_loop() | ||
loop.set_debug(True) | ||
except RuntimeError: | ||
asyncio.set_event_loop(asyncio.new_event_loop()) | ||
|
||
global conn | ||
conn = StreamConn( | ||
ALPACA_API_KEY, | ||
ALPACA_SECRET_KEY, | ||
base_url=URL('https://paper-api.alpaca.markets'), | ||
data_url=URL('https://data.alpaca.markets'), | ||
# data_url=URL('http://127.0.0.1:8765'), | ||
data_stream='polygon' if USE_POLYGON else 'alpacadatav1' | ||
) | ||
|
||
@conn.on(r'^AM\..+$') | ||
async def on_minute_bars(conn, channel, bar): | ||
print('bars', bar) | ||
|
||
|
||
@conn.on(r'Q\..+') | ||
async def on_quotes(conn, channel, quote): | ||
print('quote', quote) | ||
|
||
|
||
@conn.on(r'T\..+') | ||
async def on_trades(conn, channel, trade): | ||
print('trade', trade) | ||
|
||
conn.run(['alpacadatav1/Q.GOOG']) | ||
|
||
if __name__ == '__main__': | ||
logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO) | ||
threading.Thread(target=consumer_thread).start() | ||
|
||
loop = asyncio.get_event_loop() | ||
|
||
time.sleep(5) # give the initial connection time to be established | ||
subscriptions = [['alpacadatav1/AM.TSLA'], ['alpacadatav1/Q.GOOG'], | ||
['alpacadatav1/T.AAPL']] | ||
|
||
while 1: | ||
for channels in subscriptions: | ||
loop.run_until_complete(conn.subscribe(channels)) | ||
if "AM." in channels[0]: | ||
time.sleep(60) # aggs are once every minute. give it time | ||
else: | ||
time.sleep(20) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
""" | ||
In this example code we will show how to shut the streamconn websocket | ||
connection down and then up again. it's the ability to stop/start the | ||
connection | ||
""" | ||
import logging | ||
import threading | ||
import asyncio | ||
import time | ||
from alpaca_trade_api import StreamConn | ||
from alpaca_trade_api.common import URL | ||
|
||
ALPACA_API_KEY = "<YOUR-API-KEY>" | ||
ALPACA_SECRET_KEY = "<YOUR-SECRET-KEY>" | ||
USE_POLYGON = False | ||
|
||
conn: StreamConn = None | ||
|
||
def consumer_thread(): | ||
|
||
try: | ||
# make sure we have an event loop, if not create a new one | ||
loop = asyncio.get_event_loop() | ||
loop.set_debug(True) | ||
except RuntimeError: | ||
asyncio.set_event_loop(asyncio.new_event_loop()) | ||
|
||
global conn | ||
conn = StreamConn( | ||
ALPACA_API_KEY, | ||
ALPACA_SECRET_KEY, | ||
base_url=URL('https://paper-api.alpaca.markets'), | ||
data_url=URL('https://data.alpaca.markets'), | ||
# data_url=URL('http://127.0.0.1:8765'), | ||
data_stream='polygon' if USE_POLYGON else 'alpacadatav1' | ||
) | ||
|
||
@conn.on(r'^AM\..+$') | ||
async def on_minute_bars(conn, channel, bar): | ||
print('bars', bar) | ||
|
||
|
||
@conn.on(r'Q\..+') | ||
async def on_quotes(conn, channel, quote): | ||
print('quote', quote) | ||
|
||
|
||
@conn.on(r'T\..+') | ||
async def on_trades(conn, channel, trade): | ||
print('trade', trade) | ||
|
||
conn.run(['alpacadatav1/Q.GOOG']) | ||
|
||
if __name__ == '__main__': | ||
logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO) | ||
|
||
loop = asyncio.get_event_loop() | ||
|
||
while 1: | ||
threading.Thread(target=consumer_thread).start() | ||
time.sleep(5) | ||
loop.run_until_complete(conn.stop_ws()) | ||
time.sleep(20) |
File renamed without changes.