Skip to content

Commit 0d875a3

Browse files
mcdayoubclickingbuttons
authored andcommitted
Trade endpoints (#134)
* added last trade and last trade crypto
1 parent 4057f24 commit 0d875a3

File tree

4 files changed

+118
-10
lines changed

4 files changed

+118
-10
lines changed

polygon/rest/base.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,10 @@ def _get(
6767

6868
if result_key:
6969
obj = obj[result_key]
70-
else:
71-
# If the result_key does not exist, still need to put the results in a list
70+
71+
# If obj is not yet a list, need to turn it into a list
72+
# This is for the Daily Open/Close and Last Trade endpoints
73+
if type(obj) != list:
7274
obj = [obj]
7375

7476
if deserializer:

polygon/rest/models/trades.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,68 @@ class Trade:
2121
@staticmethod
2222
def from_dict(d):
2323
return Trade(**d)
24+
25+
26+
@dataclass
27+
class LastTrade:
28+
ticker: str
29+
trf_timestamp: int
30+
sequence_number: float
31+
sip_timestamp: int
32+
participant_timestamp: int
33+
conditions: List[int]
34+
correction: int
35+
id: str
36+
price: float
37+
trf_id: int
38+
size: float
39+
exchange: int
40+
tape: int
41+
42+
@staticmethod
43+
def from_dict(d):
44+
return LastTrade(
45+
d.get("T", None),
46+
d.get("f", None),
47+
d.get("q", None),
48+
d.get("t", None),
49+
d.get("y", None),
50+
d.get("c", None),
51+
d.get("e", None),
52+
d.get("i", None),
53+
d.get("p", None),
54+
d.get("r", None),
55+
d.get("s", None),
56+
d.get("x", None),
57+
d.get("z", None),
58+
)
59+
60+
61+
@dataclass
62+
class Last:
63+
conditions: List[int]
64+
exchange: int
65+
price: float
66+
size: float
67+
timestamp: int
68+
69+
@staticmethod
70+
def from_dict(d):
71+
return Last(**d)
72+
73+
74+
@dataclass
75+
class LastTradeCrypto:
76+
last: Last
77+
ticker: str
78+
status: str
79+
request_id: str
80+
81+
@staticmethod
82+
def from_dict(d):
83+
return LastTradeCrypto(
84+
d.get("last", None),
85+
d.get("symbol", None),
86+
d.get("status", None),
87+
d.get("request_id", None),
88+
)

polygon/rest/trades.py

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from .base import BaseClient
22
from typing import Optional, Any, Dict, Union, Iterator
3-
from .models import Trade, Sort, Order
3+
from .models import Trade, LastTrade, LastTradeCrypto, Sort, Order
44
from urllib3 import HTTPResponse
55
from datetime import datetime, date
66

@@ -44,3 +44,51 @@ def list_trades(
4444
raw=raw,
4545
deserializer=Trade.from_dict,
4646
)
47+
48+
def get_last_trade(
49+
self,
50+
ticker: str,
51+
params: Optional[Dict[str, Any]] = None,
52+
raw: bool = False,
53+
) -> Union[LastTrade, HTTPResponse]:
54+
"""
55+
Get the most recent trade for a ticker.
56+
57+
:param ticker: The ticker symbol of the asset
58+
:param params: Any additional query params
59+
:param raw: Return raw object instead of results object
60+
:return: Last trade
61+
"""
62+
url = f"/v2/last/trade/{ticker}"
63+
64+
return self._get(
65+
path=url,
66+
params=self._get_params(self.get_last_trade, locals()),
67+
result_key="results",
68+
deserializer=LastTrade.from_dict,
69+
raw=raw,
70+
)
71+
72+
def get_last_trade_crypto(
73+
self,
74+
from_: str,
75+
to: str,
76+
params: Optional[Dict[str, Any]] = None,
77+
raw: bool = False,
78+
) -> Union[LastTrade, HTTPResponse]:
79+
"""
80+
Get the most recent trade for a ticker.
81+
82+
:param ticker: The ticker symbol of the asset
83+
:param params: Any additional query params
84+
:param raw: Return raw object instead of results object
85+
:return: Last trade
86+
"""
87+
url = f"/v1/last/crypto/{from_}/{to}"
88+
89+
return self._get(
90+
path=url,
91+
params=self._get_params(self.get_last_trade_crypto, locals()),
92+
deserializer=LastTradeCrypto.from_dict,
93+
raw=raw,
94+
)

rest-example.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,3 @@
88
print(aggs)
99
aggs = client.get_aggs("AAPL", 1, "day", date(2005, 4, 4), datetime(2005, 4, 4))
1010
print(aggs)
11-
12-
trades = []
13-
for t in client.list_trades("AAA", timestamp="2022-04-20", limit=5, sort=Sort.ASC):
14-
trades.append(t)
15-
print(trades)
16-
17-
print(client.list_trades("AAA", timestamp="2022-04-20", limit=5, raw=True))

0 commit comments

Comments
 (0)