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
2 changes: 1 addition & 1 deletion polygon/rest/aggs.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def get_grouped_daily_aggs(
adjusted: Optional[bool] = None,
params: Optional[Dict[str, Any]] = None,
raw: bool = False,
) -> Union[List[GroupedDailyAgg], HTTPResponse]:
) -> Union[GroupedDailyAgg, HTTPResponse]:
"""
Get the daily open, high, low, and close (OHLC) for the entire market.

Expand Down
10 changes: 4 additions & 6 deletions polygon/rest/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,11 @@ def _get(
if result_key:
obj = obj[result_key]

# 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:
obj = [deserializer(o) for o in obj]
if type(obj) == list:
obj = [deserializer(o) for o in obj]
else:
obj = deserializer(obj)

return obj

Expand Down
17 changes: 0 additions & 17 deletions polygon/rest/models/trades.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,3 @@ class Last:
@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),
)
7 changes: 4 additions & 3 deletions 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, LastTrade, LastTradeCrypto, Sort, Order
from .models import Trade, LastTrade, Last, Sort, Order
from urllib3 import HTTPResponse
from datetime import datetime, date

Expand Down Expand Up @@ -75,7 +75,7 @@ def get_last_trade_crypto(
to: str,
params: Optional[Dict[str, Any]] = None,
raw: bool = False,
) -> Union[LastTrade, HTTPResponse]:
) -> Union[Last, HTTPResponse]:
"""
Get the most recent trade for a ticker.

Expand All @@ -89,6 +89,7 @@ def get_last_trade_crypto(
return self._get(
path=url,
params=self._get_params(self.get_last_trade_crypto, locals()),
deserializer=LastTradeCrypto.from_dict,
result_key="last",
deserializer=Last.from_dict,
raw=raw,
)
27 changes: 13 additions & 14 deletions tests/test_aggs.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,19 @@ def test_get_grouped_daily_aggs(self):

def test_get_daily_open_close_agg(self):
aggs = self.c.get_daily_open_close_agg("AAPL", "2005-04-01", True)
expected = [
DailyOpenCloseAgg(
after_hours=123,
close=123,
from_="2021-04-01",
high=124.18,
low=122.49,
open=123.66,
pre_market=123.45,
status="OK",
symbol="AAPL",
volume=75089134,
)
]
expected = DailyOpenCloseAgg(
after_hours=123,
close=123,
from_="2021-04-01",
high=124.18,
low=122.49,
open=123.66,
pre_market=123.45,
status="OK",
symbol="AAPL",
volume=75089134,
)

self.assertEqual(aggs, expected)

def test_get_previous_close_agg(self):
Expand Down
26 changes: 12 additions & 14 deletions tests/test_markets.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,18 +123,16 @@ def test_get_market_holidays(self):

def test_get_market_status(self):
status = self.c.get_market_status()
expected = [
MarketStatus(
after_hours=True,
currencies={"fx": "open", "crypto": "open"},
early_hours=False,
exchanges={
"nyse": "extended-hours",
"nasdaq": "extended-hours",
"otc": "extended-hours",
},
market="extended-hours",
server_time="2022-04-28T16:48:08-04:00",
)
]
expected = MarketStatus(
after_hours=True,
currencies={"fx": "open", "crypto": "open"},
early_hours=False,
exchanges={
"nyse": "extended-hours",
"nasdaq": "extended-hours",
"otc": "extended-hours",
},
market="extended-hours",
server_time="2022-04-28T16:48:08-04:00",
)
self.assertEqual(status, expected)
216 changes: 105 additions & 111 deletions tests/test_snapshots.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,123 +155,117 @@ def test_get_snapshot_direction(self):

def test_get_snapshot_ticker(self):
snapshots = self.c.get_snapshot_ticker("AAPL")
expected = [
Snapshot(
day={
"c": 160.315,
"h": 166.2,
"l": 159.8,
"o": 161.84,
"v": 68840127,
"vw": 162.7124,
},
last_quote={
"P": 159.99,
"S": 5,
"p": 159.98,
"s": 3,
"t": 1651251948407646487,
},
last_trade={
"c": None,
"i": "121351",
"p": 159.99,
"s": 200,
"t": 1651251948294080343,
"x": 12,
},
min={
"av": 68834255,
"c": 160.3,
"h": 160.71,
"l": 160.3,
"o": 160.71,
"v": 197226,
"vw": 160.5259,
},
prev_day={
"c": 163.64,
"h": 164.515,
"l": 158.93,
"o": 159.25,
"v": 130149192,
"vw": 161.8622,
},
ticker="AAPL",
todays_change=-3.65,
todays_change_percent=None,
updated=1651251948294080343,
)
]
expected = Snapshot(
day={
"c": 160.315,
"h": 166.2,
"l": 159.8,
"o": 161.84,
"v": 68840127,
"vw": 162.7124,
},
last_quote={
"P": 159.99,
"S": 5,
"p": 159.98,
"s": 3,
"t": 1651251948407646487,
},
last_trade={
"c": None,
"i": "121351",
"p": 159.99,
"s": 200,
"t": 1651251948294080343,
"x": 12,
},
min={
"av": 68834255,
"c": 160.3,
"h": 160.71,
"l": 160.3,
"o": 160.71,
"v": 197226,
"vw": 160.5259,
},
prev_day={
"c": 163.64,
"h": 164.515,
"l": 158.93,
"o": 159.25,
"v": 130149192,
"vw": 161.8622,
},
ticker="AAPL",
todays_change=-3.65,
todays_change_percent=None,
updated=1651251948294080343,
)
self.assertEqual(snapshots, expected)

def test_get_snapshot_option(self):
snapshots = self.c.get_snapshot_option("AAPL", "O:AAPL230616C00150000")
expected = [
OptionContractSnapshot(
break_even_price=179.075,
day={
"change": -2.3999999999999986,
"change_percent": -7.643312101910824,
"close": 29,
"high": 32.25,
"last_updated": 1651204800000000000,
"low": 29,
"open": 29.99,
"previous_close": 31.4,
"volume": 8,
"vwap": 30.7738,
},
details={
"contract_type": "call",
"exercise_style": "american",
"expiration_date": "2023-06-16",
"shares_per_contract": 100,
"strike_price": 150,
"ticker": "O:AAPL230616C00150000",
},
greeks={
"delta": 0.6436614934293701,
"gamma": 0.0061735291012820675,
"theta": -0.028227189324641973,
"vega": 0.6381159723175714,
},
implied_volatility=0.3570277203465058,
last_quote={
"ask": 29.25,
"ask_size": 209,
"bid": 28.9,
"bid_size": 294,
"last_updated": 1651254260800059648,
"midpoint": 29.075,
"timeframe": "REAL-TIME",
},
open_interest=8133,
underlying_asset={
"change_to_break_even": 19.11439999999999,
"last_updated": 1651254263172073152,
"price": 159.9606,
"ticker": "AAPL",
"timeframe": "REAL-TIME",
},
)
]
expected = OptionContractSnapshot(
break_even_price=179.075,
day={
"change": -2.3999999999999986,
"change_percent": -7.643312101910824,
"close": 29,
"high": 32.25,
"last_updated": 1651204800000000000,
"low": 29,
"open": 29.99,
"previous_close": 31.4,
"volume": 8,
"vwap": 30.7738,
},
details={
"contract_type": "call",
"exercise_style": "american",
"expiration_date": "2023-06-16",
"shares_per_contract": 100,
"strike_price": 150,
"ticker": "O:AAPL230616C00150000",
},
greeks={
"delta": 0.6436614934293701,
"gamma": 0.0061735291012820675,
"theta": -0.028227189324641973,
"vega": 0.6381159723175714,
},
implied_volatility=0.3570277203465058,
last_quote={
"ask": 29.25,
"ask_size": 209,
"bid": 28.9,
"bid_size": 294,
"last_updated": 1651254260800059648,
"midpoint": 29.075,
"timeframe": "REAL-TIME",
},
open_interest=8133,
underlying_asset={
"change_to_break_even": 19.11439999999999,
"last_updated": 1651254263172073152,
"price": 159.9606,
"ticker": "AAPL",
"timeframe": "REAL-TIME",
},
)
self.assertEqual(snapshots, expected)

def test_get_snapshot_crypto_book(self):
snapshots = self.c.get_snapshot_crypto_book("X:BTCUSD")
expected = [
SnapshotTickerFullBook(
ticker="X:BTCUSD",
bids=[
{"p": 16303.17, "x": {"1": 2}},
{"p": 16302.94, "x": {"1": 0.02859424, "6": 0.023455}},
],
asks=[{"p": 11454, "x": {"2": 1}}, {"p": 11455, "x": {"2": 1}}],
bid_count=694.951789670001,
ask_count=593.1412981600005,
spread=-4849.17,
updated=1605295074162,
)
]
expected = SnapshotTickerFullBook(
ticker="X:BTCUSD",
bids=[
{"p": 16303.17, "x": {"1": 2}},
{"p": 16302.94, "x": {"1": 0.02859424, "6": 0.023455}},
],
asks=[{"p": 11454, "x": {"2": 1}}, {"p": 11455, "x": {"2": 1}}],
bid_count=694.951789670001,
ask_count=593.1412981600005,
spread=-4849.17,
updated=1605295074162,
)
self.assertEqual(snapshots, expected)
Loading