-
Notifications
You must be signed in to change notification settings - Fork 1
/
streaming_example.py
57 lines (50 loc) · 1.67 KB
/
streaming_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# Expects venv like:
# python3 -mvenv venv
# source venv/bin/activate
# cd libraries/python/urllib3 && python setup.py install; cd -
# pip install requests
import requests, os, json
from pprint import pprint
import tradier_urllib3
from tradier_urllib3.models import StreamingQuote, StreamingTrade, StreamingQuote, StreamingSummary, StreamingTradex, StreamingTimesale
configuration = tradier_urllib3.Configuration(
host = "https://api.tradier.com/v1",
)
configuration.access_token = os.getenv('ACCESS_TOKEN', 'YOUR PRODUCTION ACCESS TOKEN')
api_client = tradier_urllib3.ApiClient(configuration)
api_instance = tradier_urllib3.DefaultApi(api_client)
session = api_instance.markets_events_session_post()
handlers = {
'trade': StreamingTrade,
'quote': StreamingQuote,
'summary': StreamingSummary,
'timesale': StreamingTimesale,
'tradex': StreamingTradex
}
def events(api_client, symbols, session, filter=None, linebreak=True, validOnly=True, advancedDetails=False):
symbols = symbols
if isinstance(symbols, list):
symbols = ','.join(symbols)
payload = {
'sessionid': session.stream.sessionid,
'symbols': symbols,
'linebreak': linebreak,
'validOnly': validOnly,
'advancedDetails': advancedDetails
}
if filter:
payload['filter'] = filter
headers = {
'Accept': 'application/json'
}
r = requests.post(session.stream.url, stream=True, params=payload, headers=headers)
r.raise_for_status()
for line in r.iter_lines():
if line:
data = json.loads(line)
dtype = data['type']
if dtype not in handlers:
raise Exception(f'Received unknown {dtype}')
yield api_client._ApiClient__deserialize(data, handlers[dtype])
for i in events(api_client, 'AAPL', session):
pprint(i)