@@ -26,19 +26,11 @@ You can pass your API key via the environment variable :code:`POLYGON_API_KEY` o
26
26
27
27
For non-paginated endpoints call :code: `get_* `:
28
28
29
- .. code-block :: python
30
-
31
- aggs = client.get_aggs(" AAPL" , 1 , " day" , " 2022-04-01" , " 2022-04-04" )
32
- print (aggs)
29
+ .. literalinclude :: ../../examples/rest/simple-get.py
33
30
34
31
For paginated endpoints call :code: `list_* ` and use the provided iterator:
35
32
36
- .. code-block :: python
37
-
38
- trades = []
39
- for t in client.list_trades(" AAA" , timestamp = " 2022-04-20" , limit = 5 , sort = Sort.ASC )
40
- trades.append(t)
41
- print (trades)
33
+ .. literalinclude :: ../../examples/rest/simple-list.py
42
34
43
35
.. note ::
44
36
The number of network requests made by the iterator depends on the value of the parameter :code: `limit `.
@@ -55,125 +47,11 @@ For endpoints that have a set of parameters you can use the provided :doc:`enums
55
47
56
48
To handle the raw `urllib3 response <https://urllib3.readthedocs.io/en/stable/reference/urllib3.response.html?highlight=response#response) yourself, pass `raw=True> `_ yourself pass :code: `raw=True `:
57
49
58
- .. code-block :: python
59
-
60
- aggs = client.get_aggs(" AAPL" , 1 , " day" , " 2022-04-01" , " 2022-04-04" , raw = True )
61
- print (aggs.geturl())
62
- # https://api.polygon.io/v2/aggs/ticker/AAPL/range/1/day/2022-04-01/2022-04-04
63
- print (aggs.status)
64
- # 200
65
- print (aggs.data)
66
- # b'{
67
- # "ticker": "AAPL",
68
- # "queryCount": 2,
69
- # "resultsCount": 2,
70
- # "adjusted": true,
71
- # "results": [
72
- # {
73
- # "v": 78251328,
74
- # "vw": 173.4143,
75
- # "o": 174.03,
76
- # "c": 174.31,
77
- # "h": 174.88,
78
- # "l": 171.94,
79
- # "t": 1648785600000,
80
- # "n": 661160
81
- # },
82
- # {
83
- # "v": 76545983,
84
- # "vw": 177.4855,
85
- # "o": 174.57,
86
- # "c": 178.44,
87
- # "h": 178.49,
88
- # "l": 174.44,
89
- # "t": 1649044800000,
90
- # "n": 630374
91
- # }
92
- # ],
93
- # "status": "OK",
94
- # "request_id": "d8882a9d5194978819777f49c44b09c6",
95
- # "count": 2
96
- # }'
50
+ .. literalinclude :: ../../examples/rest/raw-get.py
97
51
98
52
If it is a paginated :code: `list_* ` response it's up to you to handle the "next_url" iteration:
99
53
100
- .. code-block :: python
101
-
102
- trades = client.list_trades(" AAA" , timestamp = " 2022-04-20" , limit = 5 )
103
- print (aggs.data)
104
- # b'{
105
- # "results": [
106
- # {
107
- # "conditions": [
108
- # 15
109
- # ],
110
- # "exchange": 11,
111
- # "id": "52983575627601",
112
- # "participant_timestamp": 1650499200029279200,
113
- # "price": 24.875,
114
- # "sequence_number": 1591291,
115
- # "sip_timestamp": 1650499200029316600,
116
- # "size": 100,
117
- # "tape": 1
118
- # },
119
- # {
120
- # "conditions": [
121
- # 38,
122
- # 41
123
- # ],
124
- # "exchange": 11,
125
- # "id": "52983575627600",
126
- # "participant_timestamp": 1650499200029279200,
127
- # "price": 24.875,
128
- # "sequence_number": 1591290,
129
- # "sip_timestamp": 1650499200029316600,
130
- # "tape": 1
131
- # },
132
- # {
133
- # "conditions": [
134
- # 15
135
- # ],
136
- # "exchange": 11,
137
- # "id": "52983575622470",
138
- # "participant_timestamp": 1650493800003024000,
139
- # "price": 24.875,
140
- # "sequence_number": 1571279,
141
- # "sip_timestamp": 1650493800003645400,
142
- # "size": 100,
143
- # "tape": 1
144
- # },
145
- # {
146
- # "conditions": [
147
- # 38,
148
- # 41
149
- # ],
150
- # "exchange": 11,
151
- # "id": "52983575622469",
152
- # "participant_timestamp": 1650493800003024000,
153
- # "price": 24.875,
154
- # "sequence_number": 1571276,
155
- # "sip_timestamp": 1650493800003635500,
156
- # "tape": 1
157
- # },
158
- # {
159
- # "conditions": [
160
- # 15
161
- # ],
162
- # "exchange": 11,
163
- # "id": "52983575556178",
164
- # "participant_timestamp": 1650485400002987800,
165
- # "price": 24.875,
166
- # "sequence_number": 1536223,
167
- # "sip_timestamp": 1650485400003870000,
168
- # "size": 100,
169
- # "tape": 1
170
- # }
171
- # ],
172
- # "status": "OK",
173
- # "request_id": "618bb99e7a632ed9f55454a541404b44",
174
- # "next_url": "https://api.polygon.io/v3/trades/AAA?cursor=YXA9NSZhcz0mbGltaXQ9NSZvcmRlcj1kZXNjJnNvcnQ9dGltZXN0YW1wJnRpbWVzdGFtcC5ndGU9MjAyMi0wNC0yMFQwNCUzQTAwJTNBMDBaJnRpbWVzdGFtcC5sdGU9MjAyMi0wNC0yMFQyMCUzQTEwJTNBMDAuMDAzODY5OTUyWg"
175
- # }'
176
-
54
+ .. literalinclude :: ../../examples/rest/raw-list.py
177
55
178
56
WebSocket client usage
179
57
----------------------
@@ -182,93 +60,23 @@ WebSocket client usage
182
60
183
61
The simplest way to use the websocket client is to just provide a callback:
184
62
185
- .. code-block :: python
186
-
187
- from polygon import WebSocketClient
188
- from polygon.websocket.models import WebSocketMessage
189
- from typing import List
190
-
191
- c = WebSocketClient(subscriptions = [' T.AAPL' ])
192
-
193
- def handle_msg (msgs : List[WebSocketMessage]):
194
- for m in msgs:
195
- print (m)
196
-
197
- c.run(handle_msg)
63
+ .. literalinclude :: ../../examples/websocket/simple.py
198
64
199
65
.. note ::
200
66
Raises :code: `AuthError ` if invalid API key is provided.
201
67
202
68
If you want to capture state you can use a global variable inside the callback.
203
69
Alternatively, you can wrap a class method in a closure.
204
70
205
- .. code-block :: python
206
-
207
- from polygon import WebSocketClient
208
- from polygon.websocket.models import WebSocketMessage
209
- from typing import List
210
-
211
- class MessageHandler :
212
- count = 0
213
-
214
- def handle_msg (self , msgs : List[WebSocketMessage]):
215
- for m in msgs:
216
- if type (m) == EquityTrade:
217
- print (self .count, m)
218
- self .count += 1
219
-
220
- h = MessageHandler()
221
-
222
- def handle_msg (msgs : List[WebSocketMessage]):
223
- h.handle_msg(msgs)
224
-
225
- c.run(handle_msg)
71
+ .. literalinclude :: ../../examples/websocket/aggs.py
226
72
227
73
Under the hood our client uses an asynchronous runtime. To manage the runtime
228
- yourself (including unsubscribing and subscribing) you'll need to use asyncio
229
- and the :code: `.connect ` method:
230
-
231
- .. code-block :: python
232
-
233
- from polygon import WebSocketClient
234
- from polygon.websocket.models import WebSocketMessage
235
- from typing import List
236
-
237
- c = WebSocketClient(subscriptions = [' T.AAPL' ]) # Uses POLYGON_API_KEY env var. Can optionally supply your key.
74
+ yourself (including unsubscribing and subscribing) you can use asyncio and the
75
+ :code: `.connect ` method:
238
76
239
- async def handle_msg (msgs : List[WebSocketMessage]):
240
- for m in msgs:
241
- print (m)
242
-
243
- async def timeout ():
244
- await asyncio.sleep(1 )
245
- print (' unsubscribe_all' )
246
- c.unsubscribe_all()
247
- await asyncio.sleep(1 )
248
- print (' close' )
249
- await c.close()
250
-
251
- async def main ():
252
- await asyncio.gather(
253
- c.connect(handle_msg),
254
- timeout()
255
- )
256
-
257
- asyncio.run(main())
77
+ .. literalinclude :: ../../examples/websocket/async.py
258
78
259
79
To handle raw messages yourself pass `raw=True `:
260
80
261
- .. code-block :: python
262
-
263
- from polygon import WebSocketClient
264
- from polygon.websocket.models import WebSocketMessage
265
- from typing import Union
266
- import json
267
-
268
- c = WebSocketClient(subscriptions = [' T.*' ], raw = True )
269
-
270
- def handle_msg (msgs : Union[str , bytes ]):
271
- print (json.loads(msgs))
272
-
273
- c.run(handle_msg)
81
+ .. literalinclude :: ../../examples/websocket/raw.py
274
82
0 commit comments