Skip to content

Commit 015764b

Browse files
authored
Options contracts (#183)
1 parent c63b6e3 commit 015764b

File tree

10 files changed

+749
-59
lines changed

10 files changed

+749
-59
lines changed

docs/source/Contracts.rst

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
.. _contracts_header:
2+
3+
Contracts
4+
=================================
5+
6+
=================================
7+
Get option contract
8+
=================================
9+
10+
- `Options Contract`_
11+
12+
.. automethod:: polygon.RESTClient.get_options_contract
13+
14+
=================================
15+
List Options Contracts
16+
=================================
17+
18+
- `Options Contracts`_
19+
20+
.. automethod:: polygon.RESTClient.list_options_contracts
21+
22+
23+
.. _Options Contract: https://polygon.io/docs/options/get_v3_reference_options_contracts__options_ticker
24+
.. _Options Contracts: https://polygon.io/docs/options/get_v3_reference_options_contracts

docs/source/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ This documentation is for the Python client only. For details about the response
1919
Enums
2020
WebSocket-Enums
2121
Exceptions
22+
Contracts
2223

2324
Indices and tables
2425
==================

polygon/rest/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
DividendsClient,
1010
ConditionsClient,
1111
ExchangesClient,
12+
ContractsClient,
1213
)
1314
from .vX import VXClient
1415
from typing import Optional
@@ -30,6 +31,7 @@ class RESTClient(
3031
DividendsClient,
3132
ConditionsClient,
3233
ExchangesClient,
34+
ContractsClient,
3335
):
3436
def __init__(
3537
self,

polygon/rest/models/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@
1010
from .exchanges import *
1111
from .snapshot import *
1212
from .financials import *
13+
from .contracts import *

polygon/rest/models/contracts.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from typing import Optional, List
2+
from ...modelclass import modelclass
3+
4+
5+
@modelclass
6+
class Underlying:
7+
"Underlying contains data for an underlying or deliverable associated with an option contract."
8+
amount: Optional[float] = None
9+
type: Optional[str] = None
10+
underlying: Optional[str] = None
11+
12+
@staticmethod
13+
def from_dict(d):
14+
return Underlying(**d)
15+
16+
17+
@modelclass
18+
class OptionsContract:
19+
"OptionsContract contains data for a specified ticker symbol."
20+
additional_underlyings: Optional[List[Underlying]] = None
21+
cfi: Optional[str] = None
22+
correction: Optional[str] = None
23+
exercise_style: Optional[str] = None
24+
expiration_date: Optional[str] = None
25+
primary_exchange: Optional[str] = None
26+
shares_per_contract: Optional[float] = None
27+
strike_price: Optional[float] = None
28+
ticker: Optional[str] = None
29+
underlying_ticker: Optional[str] = None
30+
31+
@staticmethod
32+
def from_dict(d):
33+
return OptionsContract(
34+
additional_underlyings=None
35+
if "additional_underlyings" not in d
36+
else Underlying.from_dict(d["additional_underlyings"]),
37+
cfi=d.get("cfi", None),
38+
correction=d.get("correction", None),
39+
exercise_style=d.get("exercise_style", None),
40+
expiration_date=d.get("expiration_date", None),
41+
primary_exchange=d.get("primary_exchange", None),
42+
shares_per_contract=d.get("shares_per_contract", None),
43+
strike_price=d.get("strike_price", None),
44+
size=d.get("size", None),
45+
ticker=d.get("ticker", None),
46+
underlying_ticker=d.get("underlying_ticker", None),
47+
)

polygon/rest/reference.py

+139-59
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
DataType,
2020
SIP,
2121
Exchange,
22+
OptionsContract,
2223
)
2324
from urllib3 import HTTPResponse
2425
from datetime import date
@@ -31,9 +32,9 @@ def get_market_holidays(
3132
"""
3233
Get upcoming market holidays and their open/close times.
3334
34-
:param params: Any additional query params
35-
:param raw: Return HTTPResponse object instead of results object
36-
:return: List of market holidays
35+
:param params: Any additional query params.
36+
:param raw: Return HTTPResponse object instead of results object.
37+
:return: List of market holidays.
3738
"""
3839
url = "/v1/marketstatus/upcoming"
3940

@@ -51,9 +52,9 @@ def get_market_status(
5152
"""
5253
Get the current trading status of the exchanges and overall financial markets.
5354
54-
:param params: Any additional query params
55-
:param raw: Return HTTPResponse object instead of results object
56-
:return: Market status
55+
:param params: Any additional query params.
56+
:param raw: Return HTTPResponse object instead of results object.
57+
:return: Market status.
5758
"""
5859
url = "/v1/marketstatus/now"
5960

@@ -88,10 +89,10 @@ def list_tickers(
8889
Query all ticker symbols which are supported by Polygon.io. This API currently includes Stocks/Equities, Crypto, and Forex.
8990
9091
:param ticker: Specify a ticker symbol. Defaults to empty string which queries all tickers.
91-
:param ticker_lt: Ticker less than
92-
:param ticker_lte: Ticker less than or equal to
93-
:param ticker_gt: Ticker greater than
94-
:param ticker_gte: Ticker greater than or equal to
92+
:param ticker_lt: Ticker less than.
93+
:param ticker_lte: Ticker less than or equal to.
94+
:param ticker_gt: Ticker greater than.
95+
:param ticker_gte: Ticker greater than or equal to.
9596
:param type: Specify the type of the tickers. Find the types that we support via our Ticker Types API. Defaults to empty string which queries all types.
9697
:param market: Filter by market type. By default all markets are included.
9798
:param exchange: Specify the primary exchange of the asset in the ISO code format. Find more information about the ISO codes at the ISO org website. Defaults to empty string which queries all exchanges.
@@ -103,9 +104,9 @@ def list_tickers(
103104
:param limit: Limit the size of the response, default is 100 and max is 1000.
104105
:param sort: The field to sort the results on. Default is ticker. If the search query parameter is present, sort is ignored and results are ordered by relevance.
105106
:param order: The order to sort the results on. Default is asc (ascending).
106-
:param params: Any additional query params
107-
:param raw: Return raw object instead of results object
108-
:return: List of tickers
107+
:param params: Any additional query params.
108+
:param raw: Return raw object instead of results object.
109+
:return: List of tickers.
109110
"""
110111
url = "/v3/reference/tickers"
111112

@@ -168,9 +169,9 @@ def list_ticker_news(
168169
:param limit: Limit the number of results returned, default is 10 and max is 1000.
169170
:param sort: Sort field used for ordering.
170171
:param order: Order results based on the sort field.
171-
:param params: Any additional query params
172-
:param raw: Return raw object instead of results object
173-
:return: Ticker News
172+
:param params: Any additional query params.
173+
:param raw: Return raw object instead of results object.
174+
:return: Ticker News.
174175
"""
175176
url = "/v2/reference/news"
176177

@@ -193,9 +194,9 @@ def get_ticker_types(
193194
194195
:param asset_class: Filter by asset class.
195196
:param locale: Filter by locale.
196-
:param params: Any additional query params
197-
:param raw: Return raw object instead of results object
198-
:return: Ticker Types
197+
:param params: Any additional query params.
198+
:param raw: Return raw object instead of results object.
199+
:return: Ticker Types.
199200
"""
200201
url = "/v3/reference/tickers/types"
201202

@@ -232,22 +233,22 @@ def list_splits(
232233
Get a list of historical stock splits, including the ticker symbol, the execution date, and the factors of the split ratio.
233234
234235
:param ticker: Return the stock splits that contain this ticker.
235-
:param ticker_lt: Ticker less than
236-
:param ticker_lte: Ticker less than or equal to
237-
:param ticker_gt: Ticker greater than
238-
:param ticker_gte: Ticker greater than or equal to
236+
:param ticker_lt: Ticker less than.
237+
:param ticker_lte: Ticker less than or equal to.
238+
:param ticker_gt: Ticker greater than.
239+
:param ticker_gte: Ticker greater than or equal to.
239240
:param execution_date: Query by execution date with the format YYYY-MM-DD.
240-
:param execution_date_lt: Execution date less than
241-
:param execution_date_lte: Execution date less than or equal to
242-
:param execution_date_gt: Execution date greater than
243-
:param execution_date_gte: Execution date greater than or equal to
241+
:param execution_date_lt: Execution date less than.
242+
:param execution_date_lte: Execution date less than or equal to.
243+
:param execution_date_gt: Execution date greater than.
244+
:param execution_date_gte: Execution date greater than or equal to.
244245
:param reverse_split: Query for reverse stock splits. A split ratio where split_from is greater than split_to represents a reverse split. By default this filter is not used.
245246
:param limit: Limit the number of results returned, default is 10 and max is 1000.
246247
:param sort: Sort field used for ordering.
247248
:param order: Order results based on the sort field.
248-
:param params: Any additional query params
249-
:param raw: Return raw object instead of results object
250-
:return: List of splits
249+
:param params: Any additional query params.
250+
:param raw: Return raw object instead of results object.
251+
:return: List of splits.
251252
"""
252253
url = "/v3/reference/splits"
253254

@@ -304,39 +305,39 @@ def list_dividends(
304305
Get a list of historical cash dividends, including the ticker symbol, declaration date, ex-dividend date, record date, pay date, frequency, and amount.
305306
306307
:param ticker: Return the dividends that contain this ticker.
307-
:param ticker_lt: Ticker less than
308-
:param ticker_lte: Ticker less than or equal to
309-
:param ticker_gt: Ticker greater than
310-
:param ticker_gte: Ticker greater than or equal to
308+
:param ticker_lt: Ticker less than.
309+
:param ticker_lte: Ticker less than or equal to.
310+
:param ticker_gt: Ticker greater than.
311+
:param ticker_gte: Ticker greater than or equal to.
311312
:param ex_dividend_date: Query by ex-dividend date with the format YYYY-MM-DD.
312-
:param ex_dividend_date_lt: Ex-dividend date less than
313-
:param ex_dividend_date_lte: Ex-dividend date less than or equal to
314-
:param ex_dividend_date_gt: Ex-dividend date greater than
315-
:param ex_dividend_date_gte: Ex-dividend date greater than or equal to
313+
:param ex_dividend_date_lt: Ex-dividend date less than.
314+
:param ex_dividend_date_lte: Ex-dividend date less than or equal to.
315+
:param ex_dividend_date_gt: Ex-dividend date greater than.
316+
:param ex_dividend_date_gte: Ex-dividend date greater than or equal to.
316317
:param record_date: Query by record date with the format YYYY-MM-DD.
317-
:param record_date_lt: Record date less than
318-
:param record_date_lte: Record date less than or equal to
319-
:param record_date_gt: Record date greater than
320-
:param record_date_gte: Record date greater than or equal to
318+
:param record_date_lt: Record date less than.
319+
:param record_date_lte: Record date less than or equal to.
320+
:param record_date_gt: Record date greater than.
321+
:param record_date_gte: Record date greater than or equal to.
321322
:param declaration_date: Query by declaration date with the format YYYY-MM-DD.
322-
:param declaration_date_lt: Declaration date less than
323-
:param declaration_date_lte: Declaration date less than or equal to
324-
:param declaration_date_gt: Declaration date greater than
325-
:param declaration_date_gte: Declaration date greater than or equal to
323+
:param declaration_date_lt: Declaration date less than.
324+
:param declaration_date_lte: Declaration date less than or equal to.
325+
:param declaration_date_gt: Declaration date greater than.
326+
:param declaration_date_gte: Declaration date greater than or equal to.
326327
:param pay_date: Query by pay date with the format YYYY-MM-DD.
327-
:param pay_date_lt: Pay date less than
328-
:param pay_date_lte: Pay date less than or equal to
329-
:param pay_date_gt: Pay date greater than
330-
:param pay_date_gte: Pay date greater than or equal to
328+
:param pay_date_lt: Pay date less than.
329+
:param pay_date_lte: Pay date less than or equal to.
330+
:param pay_date_gt: Pay date greater than.
331+
:param pay_date_gte: Pay date greater than or equal to.
331332
:param frequency: Query by the number of times per year the dividend is paid out. Possible values are 0 (one-time), 1 (annually), 2 (bi-annually), 4 (quarterly), and 12 (monthly).
332333
:param cash_amount: Query by the cash amount of the dividend.
333334
:param dividend_type: Query by the type of dividend. Dividends that have been paid and/or are expected to be paid on consistent schedules are denoted as CD. Special Cash dividends that have been paid that are infrequent or unusual, and/or can not be expected to occur in the future are denoted as SC.
334335
:param limit: Limit the number of results returned, default is 10 and max is 1000.
335336
:param sort: Sort field used for ordering.
336337
:param order: Order results based on the sort field.
337-
:param params: Any additional query params
338-
:param raw: Return raw object instead of results object
339-
:return: List of dividends
338+
:param params: Any additional query params.
339+
:param raw: Return raw object instead of results object.
340+
:return: List of dividends.
340341
"""
341342
url = "/v3/reference/dividends"
342343

@@ -371,9 +372,9 @@ def list_conditions(
371372
:param limit: Limit the number of results returned, default is 10 and max is 1000.
372373
:param sort: Sort field used for ordering.
373374
:param order: Order results based on the sort field.
374-
:param params: Any additional query params
375-
:param raw: Return raw object instead of results object
376-
:return: List of conditions
375+
:param params: Any additional query params.
376+
:param raw: Return raw object instead of results object.
377+
:return: List of conditions.
377378
"""
378379
url = "/v3/reference/conditions"
379380

@@ -398,9 +399,9 @@ def get_exchanges(
398399
399400
:param asset_class: Filter by asset class.
400401
:param locale: Filter by locale.
401-
:param params: Any additional query params
402-
:param raw: Return HTTPResponse object instead of results object
403-
:return: List of exchanges
402+
:param params: Any additional query params.
403+
:param raw: Return HTTPResponse object instead of results object.
404+
:return: List of exchanges.
404405
"""
405406
url = "/v3/reference/exchanges"
406407

@@ -411,3 +412,82 @@ def get_exchanges(
411412
raw=raw,
412413
result_key="results",
413414
)
415+
416+
417+
class ContractsClient(BaseClient):
418+
def get_options_contract(
419+
self,
420+
ticker: str,
421+
as_of: Union[str, date] = None,
422+
params: Optional[Dict[str, Any]] = None,
423+
raw: bool = False,
424+
) -> Union[OptionsContract, HTTPResponse]:
425+
"""
426+
Get the most recent trade for a ticker.
427+
428+
:param ticker: The ticker symbol of the asset
429+
:param as_of: Specify a point in time for the contract as of this date with format YYYY-MM-DD.
430+
:param params: Any additional query params.
431+
:param raw: Return raw object instead of results object.
432+
:return: Last trade.
433+
"""
434+
url = f"/v3/reference/options/contracts/{ticker}"
435+
436+
return self._get(
437+
path=url,
438+
params=self._get_params(self.get_options_contract, locals()),
439+
result_key="results",
440+
deserializer=OptionsContract.from_dict,
441+
raw=raw,
442+
)
443+
444+
def list_options_contracts(
445+
self,
446+
underlying_ticker: Optional[str] = None,
447+
underlying_ticker_lt: Optional[str] = None,
448+
underlying_ticker_lte: Optional[str] = None,
449+
underlying_ticker_gt: Optional[str] = None,
450+
underlying_ticker_gte: Optional[str] = None,
451+
contract_type: Optional[str] = None,
452+
expiration_date: Optional[Union[str, date]] = None,
453+
expiration_date_lt: Optional[Union[str, date]] = None,
454+
expiration_date_lte: Optional[Union[str, date]] = None,
455+
expiration_date_gt: Optional[Union[str, date]] = None,
456+
expiration_date_gte: Optional[Union[str, date]] = None,
457+
as_of: Optional[Union[str, date]] = None,
458+
strike_price: Optional[float] = None,
459+
strike_price_lt: Optional[float] = None,
460+
strike_price_lte: Optional[float] = None,
461+
strike_price_gt: Optional[float] = None,
462+
strike_price_gte: Optional[float] = None,
463+
expired: Optional[bool] = None,
464+
limit: Optional[int] = None,
465+
sort: Optional[Union[str, Sort]] = None,
466+
order: Optional[Union[str, Order]] = None,
467+
params: Optional[Dict[str, Any]] = None,
468+
raw: bool = False,
469+
) -> Union[Iterator[OptionsContract], HTTPResponse]:
470+
"""
471+
List historical options contracts.
472+
473+
:param underlying_ticker: Query for contracts relating to an underlying stock ticker.
474+
:param contract_type: Query by the type of contract.
475+
:param expiration_date: Query by contract expiration with date format YYYY-MM-DD.
476+
:param as_of: Specify a point in time for contracts as of this date with format YYYY-MM-DD.
477+
:param strike_price: Query by strike price of a contract.
478+
:param expired: Query for expired contracts.
479+
:param limit: Limit the number of results returned, default is 10 and max is 1000.
480+
:param sort: Sort field used for ordering.
481+
:param order: Order results based on the sort field.
482+
:param params: Any additional query params.
483+
:param raw: Return raw object instead of results object
484+
:return: List of options contracts.
485+
"""
486+
url = "/v3/reference/options/contracts"
487+
488+
return self._paginate(
489+
path=url,
490+
params=self._get_params(self.list_options_contracts, locals()),
491+
raw=raw,
492+
deserializer=OptionsContract.from_dict,
493+
)

0 commit comments

Comments
 (0)