Skip to content

Commit 425bdd4

Browse files
authored
add options chain endpoint (#352)
* add options chain endpoint * fixed response * changed to pagination * doc changes
1 parent dafd940 commit 425bdd4

File tree

3 files changed

+129
-1
lines changed

3 files changed

+129
-1
lines changed

polygon/rest/snapshot.py

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from .base import BaseClient
2-
from typing import Optional, Any, Dict, List, Union
2+
from typing import Optional, Any, Dict, List, Union, Iterator
33
from .models import (
44
TickerSnapshot,
55
Direction,
@@ -137,6 +137,29 @@ def get_snapshot_option(
137137
options=options,
138138
)
139139

140+
def list_snapshot_options_chain(
141+
self,
142+
underlying_asset: str,
143+
params: Optional[Dict[str, Any]] = None,
144+
raw: bool = False,
145+
options: Optional[RequestOptionBuilder] = None,
146+
) -> Union[Iterator[OptionContractSnapshot], HTTPResponse]:
147+
"""
148+
Get the snapshot of all options contracts for an underlying ticker.
149+
150+
:param underlying_asset: The underlying ticker symbol of the option contract.
151+
:return: List of Snapshots
152+
"""
153+
url = f"/v3/snapshot/options/{underlying_asset}"
154+
return self._paginate(
155+
path=url,
156+
params=self._get_params(self.list_snapshot_options_chain, locals()),
157+
result_key="results",
158+
deserializer=OptionContractSnapshot.from_dict,
159+
raw=raw,
160+
options=options,
161+
)
162+
140163
def get_snapshot_crypto_book(
141164
self,
142165
ticker: str,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
{
3+
"request_id": "104d9b901d0c9e81d284cb8b41c5cdd3",
4+
"results": [{
5+
"break_even_price": 179.075,
6+
"day": {
7+
"change": -2.3999999999999986,
8+
"change_percent": -7.643312101910824,
9+
"close": 29,
10+
"high": 32.25,
11+
"last_updated": 1651204800000000000,
12+
"low": 29,
13+
"open": 29.99,
14+
"previous_close": 31.4,
15+
"volume": 8,
16+
"vwap": 30.7738
17+
},
18+
"details": {
19+
"contract_type": "call",
20+
"exercise_style": "american",
21+
"expiration_date": "2023-06-16",
22+
"shares_per_contract": 100,
23+
"strike_price": 150,
24+
"ticker": "O:AAPL230616C00150000"
25+
},
26+
"greeks": {
27+
"delta": 0.6436614934293701,
28+
"gamma": 0.0061735291012820675,
29+
"theta": -0.028227189324641973,
30+
"vega": 0.6381159723175714
31+
},
32+
"implied_volatility": 0.3570277203465058,
33+
"last_quote": {
34+
"ask": 29.25,
35+
"ask_size": 209,
36+
"bid": 28.9,
37+
"bid_size": 294,
38+
"last_updated": 1651254260800059648,
39+
"midpoint": 29.075,
40+
"timeframe": "REAL-TIME"
41+
},
42+
"open_interest": 8133,
43+
"underlying_asset": {
44+
"change_to_break_even": 19.11439999999999,
45+
"last_updated": 1651254263172073152,
46+
"price": 159.9606,
47+
"ticker": "AAPL",
48+
"timeframe": "REAL-TIME"
49+
}
50+
}],
51+
"status": "OK"
52+
}

test_rest/test_snapshots.py

+53
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,59 @@ def test_get_snapshot_option(self):
210210
)
211211
self.assertEqual(snapshots, expected)
212212

213+
def test_list_snapshot_options_chain(self):
214+
snapshots = [s for s in self.c.list_snapshot_options_chain("AAPL")]
215+
expected = [
216+
OptionContractSnapshot(
217+
break_even_price=179.075,
218+
day=DayOptionContractSnapshot(
219+
change=-2.3999999999999986,
220+
change_percent=-7.643312101910824,
221+
close=29,
222+
high=32.25,
223+
last_updated=1651204800000000000,
224+
low=29,
225+
open=29.99,
226+
previous_close=31.4,
227+
volume=8,
228+
vwap=30.7738,
229+
),
230+
details=OptionDetails(
231+
contract_type="call",
232+
exercise_style="american",
233+
expiration_date="2023-06-16",
234+
shares_per_contract=100,
235+
strike_price=150,
236+
ticker="O:AAPL230616C00150000",
237+
),
238+
greeks=Greeks(
239+
delta=0.6436614934293701,
240+
gamma=0.0061735291012820675,
241+
theta=-0.028227189324641973,
242+
vega=0.6381159723175714,
243+
),
244+
implied_volatility=0.3570277203465058,
245+
last_quote=LastQuoteOptionContractSnapshot(
246+
ask=29.25,
247+
ask_size=209,
248+
bid=28.9,
249+
bid_size=294,
250+
last_updated=1651254260800059648,
251+
midpoint=29.075,
252+
timeframe="REAL-TIME",
253+
),
254+
open_interest=8133,
255+
underlying_asset=UnderlyingAsset(
256+
change_to_break_even=19.11439999999999,
257+
last_updated=1651254263172073152,
258+
price=159.9606,
259+
ticker="AAPL",
260+
timeframe="REAL-TIME",
261+
),
262+
)
263+
]
264+
self.assertEqual(snapshots, expected)
265+
213266
def test_get_snapshot_crypto_book(self):
214267
snapshots = self.c.get_snapshot_crypto_book("X:BTCUSD")
215268
expected = SnapshotTickerFullBook(

0 commit comments

Comments
 (0)