Skip to content

Commit f60bfe4

Browse files
Add futures beta support (#884)
* Add futures beta support * Fix lint * Fix indent * Add websocket support * Fix lint * Fix lint errors * Make sure we support launchpad * Removed order param and added IEX back * Clean up imports * Sync futures client and models with spec * Added pagination flag and fixed base url parsing * Added note about pagination flag
1 parent 0bbcb67 commit f60bfe4

File tree

10 files changed

+917
-54
lines changed

10 files changed

+917
-54
lines changed

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,41 @@ for quote in quotes:
6262
print(quote)
6363
```
6464

65+
### Pagination Behavior
66+
67+
By default, the client paginates results for endpoints like `list_trades` and `list_quotes` behind the scenes for you. Understanding how pagination interacts with the `limit` parameter is key.
68+
69+
#### Default (Pagination Enabled)
70+
71+
Pagination is enabled by default (`pagination=True`):
72+
73+
* `limit` controls the page size, not the total number of results.
74+
* The client automatically fetches all pages, yielding results until none remain.
75+
76+
Here's an example:
77+
78+
```python
79+
client = RESTClient(api_key="<API_KEY>")
80+
trades = [t for t in client.list_trades(ticker="TSLA", limit=100)]
81+
```
82+
83+
This fetches all TSLA trades, 100 per page.
84+
85+
#### Disabling Pagination
86+
87+
To return a fixed number of results and stop, disable pagination:
88+
89+
```python
90+
client = RESTClient(api_key="<API_KEY>", pagination=False)
91+
trades = [t for t in client.list_trades(ticker="TSLA", limit=100)]
92+
```
93+
94+
This returns at most 100 total trades, no additional pages.
95+
96+
### Performance Tip
97+
98+
If you're fetching large datasets, always use the maximum supported limit for the API endpoint. This reduces the number of API calls and improves overall performance.
99+
65100
### Additional Filter Parameters
66101

67102
Many of the APIs in this client library support the use of additional filter parameters to refine your queries. Please refer to the specific API documentation for details on which filter parameters are supported for each endpoint. These filters can be applied using the following operators:

polygon/rest/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from .aggs import AggsClient
2+
from .futures import FuturesClient
23
from .trades import TradesClient
34
from .quotes import QuotesClient
45
from .snapshot import SnapshotClient
@@ -23,6 +24,7 @@
2324

2425
class RESTClient(
2526
AggsClient,
27+
FuturesClient,
2628
TradesClient,
2729
QuotesClient,
2830
SnapshotClient,
@@ -44,6 +46,7 @@ def __init__(
4446
num_pools: int = 10,
4547
retries: int = 3,
4648
base: str = BASE,
49+
pagination: bool = True,
4750
verbose: bool = False,
4851
trace: bool = False,
4952
custom_json: Optional[Any] = None,
@@ -55,6 +58,7 @@ def __init__(
5558
num_pools=num_pools,
5659
retries=retries,
5760
base=base,
61+
pagination=pagination,
5862
verbose=verbose,
5963
trace=trace,
6064
custom_json=custom_json,
@@ -66,6 +70,7 @@ def __init__(
6670
num_pools=num_pools,
6771
retries=retries,
6872
base=base,
73+
pagination=pagination,
6974
verbose=verbose,
7075
trace=trace,
7176
custom_json=custom_json,

polygon/rest/base.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from .models.request import RequestOptionBuilder
1111
from ..logging import get_logger
1212
import logging
13-
from urllib.parse import urlencode
13+
from urllib.parse import urlencode, urlparse
1414
from ..exceptions import AuthError, BadResponse
1515

1616
logger = get_logger("RESTClient")
@@ -30,6 +30,7 @@ def __init__(
3030
num_pools: int,
3131
retries: int,
3232
base: str,
33+
pagination: bool,
3334
verbose: bool,
3435
trace: bool,
3536
custom_json: Optional[Any] = None,
@@ -41,6 +42,7 @@ def __init__(
4142

4243
self.API_KEY = api_key
4344
self.BASE = base
45+
self.pagination = pagination
4446

4547
self.headers = {
4648
"Authorization": "Bearer " + self.API_KEY,
@@ -227,11 +229,14 @@ def _paginate_iter(
227229
return []
228230
for t in decoded[result_key]:
229231
yield deserializer(t)
230-
if "next_url" in decoded:
231-
path = decoded["next_url"].replace(self.BASE, "")
232-
params = {}
233-
else:
232+
if not self.pagination or "next_url" not in decoded:
234233
return
234+
next_url = decoded["next_url"]
235+
parsed = urlparse(next_url)
236+
path = parsed.path
237+
if parsed.query:
238+
path += "?" + parsed.query
239+
params = {}
235240

236241
def _paginate(
237242
self,

0 commit comments

Comments
 (0)