1
- [ ![ Build Status] ( https://drone.polygon.io/api/badges/ polygon-io/client-python/status .svg )] ( https://drone.polygon.io/polygon-io/client-python )
1
+ [ ![ Build Status] ( https://github.com/ polygon-io/client-python/actions/workflows/test/badge .svg )] ( )
2
2
[ ![ PyPI version] ( https://badge.fury.io/py/polygon-api-client.svg )] ( https://badge.fury.io/py/polygon-api-client )
3
+ [ ![ Docs] ( https://readthedocs.org/projects/polygon-api-client/badge/?version=latest )] ( https://polygon-api-client.readthedocs.io/en/latest/ )
3
4
4
5
# Polygon Python Client - WebSocket & RESTful APIs
5
6
@@ -11,105 +12,30 @@ For a basic product overview, check out our [setup and use documentation](https:
11
12
12
13
### Install
13
14
14
- ` pip install polygon-api-client `
15
-
16
- ` polygon-api-client ` supports python version >= 3.6
17
-
18
- ## Simple WebSocket Demo
19
- ``` python
20
- import time
21
-
22
- from polygon import WebSocketClient, STOCKS_CLUSTER
23
-
24
-
25
- def my_custom_process_message (message ):
26
- print (" this is my custom message processing" , message)
27
-
28
-
29
- def my_custom_error_handler (ws , error ):
30
- print (" this is my custom error handler" , error)
31
-
15
+ Requires python version >= 3.7
32
16
33
- def my_custom_close_handler (ws ):
34
- print (" this is my custom close handler" )
35
-
36
-
37
- def main ():
38
- key = ' your api key'
39
- my_client = WebSocketClient(STOCKS_CLUSTER , key, my_custom_process_message)
40
- my_client.run_async()
41
-
42
- my_client.subscribe(" T.MSFT" , " T.AAPL" , " T.AMD" , " T.NVDA" )
43
- time.sleep(1 )
44
-
45
- my_client.close_connection()
46
-
47
-
48
- if __name__ == " __main__" :
49
- main()
50
- ```
17
+ ` pip install polygon-api-client `
51
18
52
- ## Simple REST Demo
19
+ ## REST Demos
20
+ ### Getting aggs
53
21
``` python
54
22
from polygon import RESTClient
55
23
56
-
57
- def main ():
58
- key = " your api key"
59
-
60
- # RESTClient can be used as a context manager to facilitate closing the underlying http session
61
- # https://requests.readthedocs.io/en/master/user/advanced/#session-objects
62
- with RESTClient(key) as client:
63
- resp = client.stocks_equities_daily_open_close(" AAPL" , " 2021-06-11" )
64
- print (f " On: { resp.from_} Apple opened at { resp.open} and closed at { resp.close} " )
65
-
66
-
67
- if __name__ == ' __main__' :
68
- main()
69
-
24
+ client = RESTClient() # Uses POLYGON_API_KEY env var. Can optionally supply your key as first parameter.
25
+ aggs = client.get_aggs(" AAPL" , 1 , " day" , " 2005-04-01" , " 2005-04-04" )
70
26
```
71
27
72
- ### Query parameters for REST calls
73
-
74
- Every function call under our RESTClient has the ` query_params ` kwargs. These kwargs are passed along and mapped 1:1
75
- as query parameters to the underling HTTP call. For more information on the different query parameters please reference
76
- our [ API Docs] ( https://polygon.io/docs/ ) .
77
-
78
- #### Example with query parameters
79
-
28
+ ### Getting trades
80
29
``` python
81
- import datetime
82
-
83
30
from polygon import RESTClient
31
+ from polygon.rest.models import Sort
84
32
33
+ client = RESTClient() # Uses POLYGON_API_KEY env var. Can optionally supply your key as first parameter.
85
34
86
- def ts_to_datetime (ts ) -> str :
87
- return datetime.datetime.fromtimestamp(ts / 1000.0 ).strftime(' %Y-%m-%d %H:%M' )
88
-
89
-
90
- def main ():
91
- key = " your api key"
92
-
93
- # RESTClient can be used as a context manager to facilitate closing the underlying http session
94
- # https://requests.readthedocs.io/en/master/user/advanced/#session-objects
95
- with RESTClient(key) as client:
96
- from_ = " 2021-01-01"
97
- to = " 2021-02-01"
98
- resp = client.stocks_equities_aggregates(" AAPL" , 1 , " minute" , from_, to, unadjusted = False )
99
-
100
- print (f " Minute aggregates for { resp.ticker} between { from_} and { to} . " )
101
-
102
- for result in resp.results:
103
- dt = ts_to_datetime(result[" t" ])
104
- print (f " { dt} \n\t O: { result[' o' ]} \n\t H: { result[' h' ]} \n\t L: { result[' l' ]} \n\t C: { result[' c' ]} " )
105
-
106
-
107
- if __name__ == ' __main__' :
108
- main()
109
- ```
110
-
111
- ## Notes about the REST Client
112
-
113
- We use swagger as our API spec and we used this swagger to generate most of the code that defines the REST client.
114
- We made this decision due to the size of our API, many endpoints and object definitions, and to accommodate future changes.
35
+ trades = []
36
+ for t in client.list_trades(" AAA" , timestamp = " 2022-04-20" , limit = 5 , sort = Sort.ASC ):
37
+ trades.append(t)
38
+ ```
115
39
40
+ ### Getting raw response
41
+ To handle the raw [ urllib3 response] ( https://urllib3.readthedocs.io/en/stable/reference/urllib3.response.html?highlight=response#response ) yourself, pass ` raw=True ` .
0 commit comments