Skip to content

Commit

Permalink
implement list aggs (#369)
Browse files Browse the repository at this point in the history
  • Loading branch information
morningvera authored Jan 30, 2023
1 parent 0971ba2 commit 72b1a9e
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 1 deletion.
47 changes: 46 additions & 1 deletion polygon/rest/aggs.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
28 changes: 28 additions & 0 deletions test_rest/test_aggs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down

0 comments on commit 72b1a9e

Please sign in to comment.