diff --git a/polygon/rest/aggs.py b/polygon/rest/aggs.py index 4c92a990..71d05d18 100644 --- a/polygon/rest/aggs.py +++ b/polygon/rest/aggs.py @@ -1,5 +1,5 @@ from .base import BaseClient -from typing import Optional, Any, Dict, List, Union +from typing import Optional, Any, Dict, List, Union, Iterator from .models import Agg, GroupedDailyAgg, DailyOpenCloseAgg, PreviousCloseAgg, Sort from urllib3 import HTTPResponse from datetime import datetime, date @@ -8,6 +8,51 @@ class AggsClient(BaseClient): + def list_aggs( + self, + ticker: str, + multiplier: int, + timespan: str, + # "from" is a keyword in python https://www.w3schools.com/python/python_ref_keywords.asp + from_: Union[str, int, datetime, date], + to: Union[str, int, datetime, date], + adjusted: Optional[bool] = None, + sort: Optional[Union[str, Sort]] = None, + limit: Optional[int] = None, + params: Optional[Dict[str, Any]] = None, + raw: bool = False, + options: Optional[RequestOptionBuilder] = None, + ) -> Union[Iterator[Agg], HTTPResponse]: + """ + List aggregate bars for a ticker over a given date range in custom time window sizes. + + :param ticker: The ticker symbol. + :param multiplier: The size of the timespan multiplier. + :param timespan: The size of the time window. + :param from_: The start of the aggregate time window as YYYY-MM-DD, a date, Unix MS Timestamp, or a datetime. + :param to: The end of the aggregate time window as YYYY-MM-DD, a date, Unix MS Timestamp, or a datetime. + :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. + :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. + :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. + :param params: Any additional query params + :param raw: Return raw object instead of results object + :return: Iterator of aggregates + """ + if isinstance(from_, datetime): + from_ = int(from_.timestamp() * self.time_mult("millis")) + + if isinstance(to, datetime): + to = int(to.timestamp() * self.time_mult("millis")) + url = f"/v2/aggs/ticker/{ticker}/range/{multiplier}/{timespan}/{from_}/{to}" + + return self._paginate( + path=url, + params=self._get_params(self.list_aggs, locals()), + raw=raw, + deserializer=Agg.from_dict, + options=options, + ) + def get_aggs( self, ticker: str, diff --git a/test_rest/mocks/v2/aggs/ticker/AAPL/range/1/day/2005-04-02/2005-04-04.json b/test_rest/mocks/v2/aggs/ticker/AAPL/range/1/day/2005-04-02/2005-04-04.json new file mode 100644 index 00000000..82c09f8b --- /dev/null +++ b/test_rest/mocks/v2/aggs/ticker/AAPL/range/1/day/2005-04-02/2005-04-04.json @@ -0,0 +1,31 @@ +{ + "ticker": "AAPL", + "queryCount": 2, + "resultsCount": 2, + "adjusted": true, + "results": [ + { + "v": 642646396.0, + "vw": 1.469, + "o": 1.5032, + "c": 1.4604, + "h": 1.5064, + "l": 1.4489, + "t": 1112331600000, + "n": 82132 + }, + { + "v": 578172308.0, + "vw": 1.4589, + "o": 1.4639, + "c": 1.4675, + "h": 1.4754, + "l": 1.4343, + "t": 1112587200000, + "n": 65543 + } + ], + "status": "OK", + "request_id": "12afda77aab3b1936c5fb6ef4241ae42", + "count": 2 +} \ No newline at end of file diff --git a/test_rest/test_aggs.py b/test_rest/test_aggs.py index 08706c53..a589fe33 100644 --- a/test_rest/test_aggs.py +++ b/test_rest/test_aggs.py @@ -8,6 +8,34 @@ class AggsTest(BaseTest): + def test_list_aggs(self): + aggs = [ + a for a in self.c.list_aggs("AAPL", 1, "day", "2005-04-02", "2005-04-04") + ] + expected = [ + Agg( + open=1.5032, + high=1.5064, + low=1.4489, + close=1.4604, + volume=642646396.0, + vwap=1.469, + timestamp=1112331600000, + transactions=82132, + ), + Agg( + open=1.4639, + high=1.4754, + low=1.4343, + close=1.4675, + volume=578172308.0, + vwap=1.4589, + timestamp=1112587200000, + transactions=65543, + ), + ] + self.assertEqual(aggs, expected) + def test_get_aggs(self): aggs = self.c.get_aggs("AAPL", 1, "day", "2005-04-01", "2005-04-04") expected = [