Skip to content

Commit c28320c

Browse files
author
clickingbuttons
authored
allow datetime or int for aggs, lint tests (#123)
* allow datetime or int for aggs, lint tests * run --check in CI
1 parent 6c4e7dc commit c28320c

File tree

5 files changed

+39
-15
lines changed

5 files changed

+39
-15
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ help:
2222

2323
## Check code style
2424
style:
25-
poetry run black polygon
25+
poetry run black $(if $(CI),--check,) polygon tests
2626

2727
## Check static types
2828
static:
29-
poetry run mypy polygon
29+
poetry run mypy polygon tests
3030

3131
## Check code style and static types
3232
lint: style static

polygon/rest/aggs.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from typing import Optional, Any, Dict, List, Union
33
from .models import Agg, Sort
44
from urllib3 import HTTPResponse
5+
from datetime import datetime
56

67
# https://polygon.io/docs/stocks
78
class AggsClient(BaseClient):
@@ -11,8 +12,8 @@ def get_aggs(
1112
multiplier: int,
1213
timespan: str,
1314
# "from" is a keyword in python https://www.w3schools.com/python/python_ref_keywords.asp
14-
from_: str,
15-
to: str,
15+
from_: Union[str, int, datetime],
16+
to: Union[str, int, datetime],
1617
adjusted: Optional[bool] = None,
1718
sort: Optional[Union[str, Sort]] = None,
1819
limit: Optional[int] = None,
@@ -25,8 +26,8 @@ def get_aggs(
2526
:param ticker: The ticker symbol.
2627
:param multiplier: The size of the timespan multiplier.
2728
:param timespan: The size of the time window.
28-
:param _from: The start of the aggregate time window.
29-
:param to: The end of the aggregate time window.
29+
:param _from: The start of the aggregate time window as YYYY-MM-DD, Unix MS Timestamps, or a datetime.
30+
:param to: The end of the aggregate time window as YYYY-MM-DD, Unix MS Timestamps, or a datetime.
3031
: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.
3132
: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.
3233
: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.
@@ -35,6 +36,11 @@ def get_aggs(
3536
:return: List of aggregates
3637
:rtype: List[Agg]
3738
"""
39+
if isinstance(from_, datetime):
40+
from_ = int(from_.timestamp() * 1000)
41+
42+
if isinstance(to, datetime):
43+
to = int(to.timestamp() * 1000)
3844
url = f"/v2/aggs/ticker/{ticker}/range/{multiplier}/{timespan}/{from_}/{to}"
3945

4046
return self._get(

polygon/rest/reference.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ def list_tickers(
7474
) -> Union[Iterator[Ticker], HTTPResponse]:
7575
"""
7676
Query all ticker symbols which are supported by Polygon.io. This API currently includes Stocks/Equities, Crypto, and Forex.
77-
7877
:param ticker: Specify a ticker symbol. Defaults to empty string which queries all tickers.
7978
:param ticker_lt: Ticker less than
8079
:param ticker_lte: Ticker less than or equal to
@@ -113,7 +112,6 @@ def get_ticker_details(
113112
) -> Union[TickerDetails, HTTPResponse]:
114113
"""
115114
Get a single ticker supported by Polygon.io. This response will have detailed information about the ticker and the company behind it.
116-
117115
:param ticker: The ticker symbol of the asset.
118116
:param date: Specify a point in time to get information about the ticker available on that date. When retrieving information from SEC filings, we compare this date with the period of report date on the SEC filing.
119117
:param params: Any additional query params
@@ -143,7 +141,6 @@ def get_ticker_news(
143141
) -> Union[TickerDetails, HTTPResponse]:
144142
"""
145143
Get the most recent news articles relating to a stock ticker symbol, including a summary of the article and a link to the original source.
146-
147144
:param ticker: Return results that contain this ticker.
148145
:param published_utc: Return results published on, before, or after this date.
149146
:param limit: Limit the number of results returned, default is 10 and max is 1000.
@@ -168,7 +165,6 @@ def get_ticker_types(
168165
) -> Union[TickerTypes, HTTPResponse]:
169166
"""
170167
List all ticker types that Polygon.io has.
171-
172168
:param asset_class: Filter by asset class.
173169
:param locale: Filter by locale.
174170
:param params: Any additional query params

tests/mocks.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
from polygon import RESTClient
22
import unittest
3-
import httpretty
3+
import httpretty # type: ignore
44

55
mocks = [
66
(
77
"/v2/aggs/ticker/AAPL/range/1/day/2005-04-01/2005-04-04",
8-
'{"ticker":"AAPL","queryCount":2,"resultsCount":2,"adjusted":true,"results":[{"v":6.42646396e+08,"vw":1.469,"o":1.5032,"c":1.4604,"h":1.5064,"l":1.4489,"t":1112331600000,"n":82132},{"v":5.78172308e+08,"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}'
8+
'{"ticker":"AAPL","queryCount":2,"resultsCount":2,"adjusted":true,"results":[{"v":6.42646396e+08,"vw":1.469,"o":1.5032,"c":1.4604,"h":1.5064,"l":1.4489,"t":1112331600000,"n":82132},{"v":5.78172308e+08,"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}',
99
)
1010
]
1111

12+
1213
class BaseTest(unittest.TestCase):
1314
setup = False
15+
1416
def setUp(self):
1517
if self.setup:
1618
return
@@ -19,4 +21,3 @@ def setUp(self):
1921
for m in mocks:
2022
httpretty.register_uri(httpretty.GET, c.BASE + m[0], m[1])
2123
self.setup = True
22-

tests/test_aggs.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,31 @@
22
from polygon.rest.models import Agg
33
from mocks import BaseTest
44

5+
56
class AggsTest(BaseTest):
67
def test_get_aggs(self):
78
c = RESTClient("")
89
aggs = c.get_aggs("AAPL", 1, "day", "2005-04-01", "2005-04-04")
9-
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)]
10+
expected = [
11+
Agg(
12+
open=1.5032,
13+
high=1.5064,
14+
low=1.4489,
15+
close=1.4604,
16+
volume=642646396.0,
17+
vwap=1.469,
18+
timestamp=1112331600000,
19+
transactions=82132,
20+
),
21+
Agg(
22+
open=1.4639,
23+
high=1.4754,
24+
low=1.4343,
25+
close=1.4675,
26+
volume=578172308.0,
27+
vwap=1.4589,
28+
timestamp=1112587200000,
29+
transactions=65543,
30+
),
31+
]
1032
self.assertEqual(aggs, expected)
11-

0 commit comments

Comments
 (0)