Skip to content
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
6 changes: 4 additions & 2 deletions polygon/rest/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,10 @@ def _get(

if result_key:
obj = obj[result_key]
else:
# If the result_key does not exist, still need to put the results in a list

# If obj is not yet a list, need to turn it into a list
# This is for the Daily Open/Close and Last Trade endpoints
if type(obj) != list:
obj = [obj]

if deserializer:
Expand Down
65 changes: 65 additions & 0 deletions polygon/rest/models/trades.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,68 @@ class Trade:
@staticmethod
def from_dict(d):
return Trade(**d)


@dataclass
class LastTrade:
ticker: str
trf_timestamp: int
sequence_number: float
sip_timestamp: int
participant_timestamp: int
conditions: List[int]
correction: int
id: str
price: float
trf_id: int
size: float
exchange: int
tape: int

@staticmethod
def from_dict(d):
return LastTrade(
d.get("T", None),
d.get("f", None),
d.get("q", None),
d.get("t", None),
d.get("y", None),
d.get("c", None),
d.get("e", None),
d.get("i", None),
d.get("p", None),
d.get("r", None),
d.get("s", None),
d.get("x", None),
d.get("z", None),
)


@dataclass
class Last:
conditions: List[int]
exchange: int
price: float
size: float
timestamp: int

@staticmethod
def from_dict(d):
return Last(**d)


@dataclass
class LastTradeCrypto:
last: Last
ticker: str
status: str
request_id: str

@staticmethod
def from_dict(d):
return LastTradeCrypto(
d.get("last", None),
d.get("symbol", None),
d.get("status", None),
d.get("request_id", None),
)
50 changes: 49 additions & 1 deletion polygon/rest/trades.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from .base import BaseClient
from typing import Optional, Any, Dict, Union, Iterator
from .models import Trade, Sort, Order
from .models import Trade, LastTrade, LastTradeCrypto, Sort, Order
from urllib3 import HTTPResponse
from datetime import datetime, date

Expand Down Expand Up @@ -44,3 +44,51 @@ def list_trades(
raw=raw,
deserializer=Trade.from_dict,
)

def get_last_trade(
self,
ticker: str,
params: Optional[Dict[str, Any]] = None,
raw: bool = False,
) -> Union[LastTrade, HTTPResponse]:
"""
Get the most recent trade for a ticker.

:param ticker: The ticker symbol of the asset
:param params: Any additional query params
:param raw: Return raw object instead of results object
:return: Last trade
"""
url = f"/v2/last/trade/{ticker}"

return self._get(
path=url,
params=self._get_params(self.get_last_trade, locals()),
result_key="results",
deserializer=LastTrade.from_dict,
raw=raw,
)

def get_last_trade_crypto(
self,
from_: str,
to: str,
params: Optional[Dict[str, Any]] = None,
raw: bool = False,
) -> Union[LastTrade, HTTPResponse]:
"""
Get the most recent trade for a ticker.

:param ticker: The ticker symbol of the asset
:param params: Any additional query params
:param raw: Return raw object instead of results object
:return: Last trade
"""
url = f"/v1/last/crypto/{from_}/{to}"

return self._get(
path=url,
params=self._get_params(self.get_last_trade_crypto, locals()),
deserializer=LastTradeCrypto.from_dict,
raw=raw,
)
7 changes: 0 additions & 7 deletions rest-example.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,3 @@
print(aggs)
aggs = client.get_aggs("AAPL", 1, "day", date(2005, 4, 4), datetime(2005, 4, 4))
print(aggs)

trades = []
for t in client.list_trades("AAA", timestamp="2022-04-20", limit=5, sort=Sort.ASC):
trades.append(t)
print(trades)

print(client.list_trades("AAA", timestamp="2022-04-20", limit=5, raw=True))