Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion 47 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 28 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
Morty Proxy This is a proxified and sanitized view of the page, visit original site.