Skip to content

Commit c2c40be

Browse files
authored
Quotes (#117)
1 parent 19926a9 commit c2c40be

File tree

4 files changed

+131
-1
lines changed

4 files changed

+131
-1
lines changed

polygon/rest/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from .aggs import AggsClient
22
from .trades import TradesClient
3+
from .quotes import QuotesClient
34

45

5-
class RESTClient(AggsClient, TradesClient):
6+
class RESTClient(AggsClient, TradesClient, QuotesClient):
67
pass

polygon/rest/models/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from .aggs import *
22
from .trades import *
3+
from .quotes import *
34
from enum import Enum
45

56

polygon/rest/models/quotes.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
from typing import Optional, List
2+
from dataclasses import dataclass
3+
4+
5+
@dataclass
6+
class Quote:
7+
"Quote contains quote data for a specified ticker symbol."
8+
ask_exchange: Optional[int] = None
9+
ask_price: Optional[float] = None
10+
ask_size: Optional[float] = None
11+
bid_exchange: Optional[int] = None
12+
bid_price: Optional[float] = None
13+
bid_size: Optional[float] = None
14+
conditions: Optional[List[int]] = None
15+
indicators: Optional[List[int]] = None
16+
participant_timestamp: Optional[int] = None
17+
sequence_number: Optional[int] = None
18+
sip_timestamp: Optional[int] = None
19+
tape: Optional[int] = None
20+
trf_timestamp: Optional[int] = None
21+
22+
@staticmethod
23+
def from_dict(d):
24+
return Quote(**d)
25+
26+
@dataclass
27+
class LastQuote:
28+
"LastQuote contains data for the most recent NBBO (Quote) tick for a given stock."
29+
ticker: Optional[str] = None
30+
trf_timestamp: Optional[int] = None
31+
sequence_number: Optional[int] = None
32+
sip_timestamp: Optional[int] = None
33+
participant_timestamp: Optional[int] = None
34+
ask_price: Optional[float] = None
35+
ask_size: Optional[int] = None
36+
ask_exchange: Optional[int] = None
37+
conditions: Optional[List[int]] = None
38+
indicators: Optional[List[int]] = None
39+
bid_price: Optional[float] = None
40+
bid_size: Optional[int] = None
41+
bid_exchange: Optional[int] = None
42+
tape: Optional[int] = None
43+
44+
@staticmethod
45+
def from_dict(d):
46+
return LastQuote(
47+
ticker=d.get("T", None),
48+
trf_timestamp=d.get("f", None),
49+
sequence_number=d.get("q", None),
50+
sip_timestamp=d.get("t", None),
51+
participant_timestamp=d.get("y", None),
52+
ask_price=d.get("P", None),
53+
ask_size=d.get("S", None),
54+
ask_exchange=d.get("X", None),
55+
conditions=d.get("c", None),
56+
indicators=d.get("i", None),
57+
bid_price=d.get("p", None),
58+
bid_size=d.get("s", None),
59+
bid_exchange=d.get("x", None),
60+
tape=d.get("z", None)
61+
)

polygon/rest/quotes.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
from .base import BaseClient
2+
from typing import Optional, Any, Dict, List, Union
3+
from .models import Quote, LastQuote, Sort, Order
4+
from urllib3 import HTTPResponse
5+
6+
# https://polygon.io/docs/stocks
7+
class QuotesClient(BaseClient):
8+
def list_quotes(
9+
self,
10+
ticker: str,
11+
timestamp: Optional[str] = None,
12+
timestamp_lt: Optional[str] = None,
13+
timestamp_lte: Optional[str] = None,
14+
timestamp_gt: Optional[str] = None,
15+
timestamp_gte: Optional[str] = None,
16+
limit: Optional[int] = None,
17+
sort: Optional[Union[str, Sort]] = None,
18+
order: Optional[Union[str, Order]] = None,
19+
params: Optional[Dict[str, Any]] = None,
20+
raw: bool = False
21+
) -> Union[List[Quote], HTTPResponse]:
22+
"""
23+
Get quotes for a ticker symbol in a given time range.
24+
25+
:param ticker: The ticker symbol to get quotes for.
26+
:param timestamp: Query by timestamp. Either a date with the format YYYY-MM-DD or a nanosecond timestamp.
27+
:param timestamp_lt: Timestamp less than
28+
:param timestamp_lte: Timestamp less than or equal to
29+
:param timestamp_gt: Timestamp greater than
30+
:param timestamp_gte: Timestamp greater than or equal to
31+
:param limit: Limit the number of results returned, default is 10 and max is 50000.
32+
:param sort: Sort field used for ordering.
33+
:param order: Order results based on the sort field.
34+
:param params: Any additional query params
35+
:param raw: Return HTTPResponse object instead of results object
36+
:return: List of quotes
37+
:rtype: List[Quote]
38+
"""
39+
url = f"/v3/quotes/{ticker}"
40+
41+
return self._paginate(
42+
path=url,
43+
params=self._get_params(self.list_quotes, locals()),
44+
raw=raw,
45+
deserializer=Quote.from_dict,
46+
)
47+
48+
def get_last_quote(
49+
self,
50+
ticker: str,
51+
params: Optional[Dict[str, Any]] = None,
52+
raw: bool = False
53+
):
54+
"""
55+
Get the most recent NBBO (Quote) tick for a given stock.
56+
57+
:param ticker: The ticker symbol of the stock/equity.
58+
:param params: Any additional query params
59+
:param raw: Return HTTPResponse object instead of results object
60+
:return: Last Quote
61+
:rtype: LastQuote
62+
"""
63+
url = f"/v2/last/nbbo/{ticker}"
64+
65+
return self._get(path=url, params=params, deserializer=LastQuote.from_dict, raw=raw)
66+
67+

0 commit comments

Comments
 (0)