Skip to content

Commit 72b1a9e

Browse files
authored
implement list aggs (#369)
1 parent 0971ba2 commit 72b1a9e

File tree

3 files changed

+105
-1
lines changed

3 files changed

+105
-1
lines changed

polygon/rest/aggs.py

+46-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from .base import BaseClient
2-
from typing import Optional, Any, Dict, List, Union
2+
from typing import Optional, Any, Dict, List, Union, Iterator
33
from .models import Agg, GroupedDailyAgg, DailyOpenCloseAgg, PreviousCloseAgg, Sort
44
from urllib3 import HTTPResponse
55
from datetime import datetime, date
@@ -8,6 +8,51 @@
88

99

1010
class AggsClient(BaseClient):
11+
def list_aggs(
12+
self,
13+
ticker: str,
14+
multiplier: int,
15+
timespan: str,
16+
# "from" is a keyword in python https://www.w3schools.com/python/python_ref_keywords.asp
17+
from_: Union[str, int, datetime, date],
18+
to: Union[str, int, datetime, date],
19+
adjusted: Optional[bool] = None,
20+
sort: Optional[Union[str, Sort]] = None,
21+
limit: Optional[int] = None,
22+
params: Optional[Dict[str, Any]] = None,
23+
raw: bool = False,
24+
options: Optional[RequestOptionBuilder] = None,
25+
) -> Union[Iterator[Agg], HTTPResponse]:
26+
"""
27+
List aggregate bars for a ticker over a given date range in custom time window sizes.
28+
29+
:param ticker: The ticker symbol.
30+
:param multiplier: The size of the timespan multiplier.
31+
:param timespan: The size of the time window.
32+
:param from_: The start of the aggregate time window as YYYY-MM-DD, a date, Unix MS Timestamp, or a datetime.
33+
:param to: The end of the aggregate time window as YYYY-MM-DD, a date, Unix MS Timestamp, or a datetime.
34+
:param adjusted: Whether or not the results are adjusted for splits. By default, results are adjusted. Set this to false to get results that are NOT adjusted for splits.
35+
:param sort: Sort the results by timestamp. asc will return results in ascending order (oldest at the top), desc will return results in descending order (newest at the top).The end of the aggregate time window.
36+
:param limit: Limits the number of base aggregates queried to create the aggregate results. Max 50000 and Default 5000. Read more about how limit is used to calculate aggregate results in our article on Aggregate Data API Improvements.
37+
:param params: Any additional query params
38+
:param raw: Return raw object instead of results object
39+
:return: Iterator of aggregates
40+
"""
41+
if isinstance(from_, datetime):
42+
from_ = int(from_.timestamp() * self.time_mult("millis"))
43+
44+
if isinstance(to, datetime):
45+
to = int(to.timestamp() * self.time_mult("millis"))
46+
url = f"/v2/aggs/ticker/{ticker}/range/{multiplier}/{timespan}/{from_}/{to}"
47+
48+
return self._paginate(
49+
path=url,
50+
params=self._get_params(self.list_aggs, locals()),
51+
raw=raw,
52+
deserializer=Agg.from_dict,
53+
options=options,
54+
)
55+
1156
def get_aggs(
1257
self,
1358
ticker: str,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"ticker": "AAPL",
3+
"queryCount": 2,
4+
"resultsCount": 2,
5+
"adjusted": true,
6+
"results": [
7+
{
8+
"v": 642646396.0,
9+
"vw": 1.469,
10+
"o": 1.5032,
11+
"c": 1.4604,
12+
"h": 1.5064,
13+
"l": 1.4489,
14+
"t": 1112331600000,
15+
"n": 82132
16+
},
17+
{
18+
"v": 578172308.0,
19+
"vw": 1.4589,
20+
"o": 1.4639,
21+
"c": 1.4675,
22+
"h": 1.4754,
23+
"l": 1.4343,
24+
"t": 1112587200000,
25+
"n": 65543
26+
}
27+
],
28+
"status": "OK",
29+
"request_id": "12afda77aab3b1936c5fb6ef4241ae42",
30+
"count": 2
31+
}

test_rest/test_aggs.py

+28
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,34 @@
88

99

1010
class AggsTest(BaseTest):
11+
def test_list_aggs(self):
12+
aggs = [
13+
a for a in self.c.list_aggs("AAPL", 1, "day", "2005-04-02", "2005-04-04")
14+
]
15+
expected = [
16+
Agg(
17+
open=1.5032,
18+
high=1.5064,
19+
low=1.4489,
20+
close=1.4604,
21+
volume=642646396.0,
22+
vwap=1.469,
23+
timestamp=1112331600000,
24+
transactions=82132,
25+
),
26+
Agg(
27+
open=1.4639,
28+
high=1.4754,
29+
low=1.4343,
30+
close=1.4675,
31+
volume=578172308.0,
32+
vwap=1.4589,
33+
timestamp=1112587200000,
34+
transactions=65543,
35+
),
36+
]
37+
self.assertEqual(aggs, expected)
38+
1139
def test_get_aggs(self):
1240
aggs = self.c.get_aggs("AAPL", 1, "day", "2005-04-01", "2005-04-04")
1341
expected = [

0 commit comments

Comments
 (0)