Options Module
Contains the classes and functions for scraping a yahoo finance option page.
ContractExpiration
Contract Expiration.
Attributes:
symbolstr - Ticker symbol.
timestampstr - Timestamp of expiration date.
expiration_dateDateTime - Datetime of expiration date.Notes:
This class inherits from the pydantic BaseModel which allows for the use of .json() and .dict() for serialization to json strings and dictionaries.
.json()- Serialize to a JSON object.
.dict()- Serialize to a dictionary.
convert_to_datetime
 | def convert_to_datetime(cls, value: str) -> DateTime
Convert expiration timestamp to datetime.
__lt__
 | def __lt__(other: "ContractExpiration") -> Optional["ContractExpiration"]
Compare expiration_dates for sorting.
ContractExpirationList
Contains Multiple Expirations.
Attributes:
expiration_listList[ContractExpiration] - multiple expirations.Notes:
This class inherits from the pydantic BaseModel which allows for the use of .json() and .dict() for serialization to json strings and dictionaries.
.json()- Serialize to a JSON object.
.dict()- Serialize to a dictionary.
sort_dates
 | def sort_dates(cls, values: List[ContractExpiration]) -> List[ContractExpiration]
Sort expiration_list by date.
filter_expirations_after
 | def filter_expirations_after(after: DateTime) -> None
Filter out any expiration dates prior to the after date.
Arguments:
afterDateTime - datetime to filter.Example:
Input Args Output [01JAN19, 01FEB19, 01MAR19] after: 15JAN19 [01FEB19, 01MAR19] 
filter_expirations_before
 | def filter_expirations_before(before: DateTime) -> None
Filter out any expiration dates post the before date.
Arguments:
beforeDateTime - datetime to filter.Example:
Input Args Output [01JAN19, 01FEB19, 01MAR19] before: 15JAN19 [01JAN19] 
filter_expirations_between
 | def filter_expirations_between(after: DateTime, before: DateTime) -> None
Filter dates outside of a after and before range.
Arguments:
afterDateTime - datetime to filter.
beforeDateTime - datetime to filter.Example:
Input Args Output [01JAN19, 01FEB19, 01MAR19] after: 15JAN19,before: 15JAN19 [01FEB19, 01MAR19] 
filter_expirations_after_days
 | def filter_expirations_after_days(days: int) -> None
Filter expirations only allowing expirations after n days.
Arguments:
daysint - Number of days to start filtering from. All expirations which expire prior to the days will be filtered out.
filter_expirations_before_days
 | def filter_expirations_before_days(days: int) -> None
Filter expiration only allowing expirations before n days.
Arguments:
daysint - Number of days to start filtering from. All expirations which expire post days will be filtered out.
filter_expirations_between_days
 | def filter_expirations_between_days(after_days: Optional[int] = None, before_days: Optional[int] = None) -> None
Filter expiration only allowing expirations between a range of days.
Arguments:
after_daysint - Number of days to start filtering from. All expirations which expire prior to the days will be filtered out.
before_daysint - Number of days to start filtering from. All expirations which expire post days will be filtered out.
__len__
 | def __len__() -> int
Length of the expiration_list.
__iter__
 | def __iter__() -> Iterable
Iterate over the expirations_list.
__add__
 | def __add__(other: "ContractExpirationList") -> Optional["ContractExpirationList"]
Combine two ContractExpirationLists using the + operator.
OptionContractType
Enum for option contract types.
OptionContract
Represents an Option Contract.
Attributes:
symbolstr - Ticker symbol.
contract_typeOptionContractType - Call or Put type.
timestampstr - Raw timestamp scraped from yahoo finance. This string is left untouched to make sure there is no issues when building a URL.
expiration_dateDateTime - Converted from the timestamp. This allows allows sorting and filtering.
in_the_moneybool - True if strike price is ITM else False.
contract_namestr - Contract Name.
last_trade_dateDateTime - Date of last trade.
strikefloat - Contracts strike price.
last_pricefloat - Last price of a transaction between a contract buyer and a seller.
bidfloat - Last bid price.
askfloat - Last ask price.
changefloat - Price change in dollars.
percent_changefloat - Price change in percentage.
volumeint - Volume.
open_interestint - Number of contracts opened.
implied_volatilityfloat - Contract IV.Notes:
This class inherits from the pydantic BaseModel which allows for the use of .json() and .dict() for serialization to json strings and dictionaries.
.json()- Serialize to a JSON object.
.dict()- Serialize to a dictionary.
OptionsChain
Chain of option contracts with the same expiration date.
Attributes:
symbolstr - Company symbol.
expiration_dateDateTime - Contracts expiration date.
chainList[OptionContract] - List of OptionContracts.Notes:
This class inherits from the pydantic BaseModel which allows for the use of .json() and .dict() for serialization to json strings and dictionaries.
.json()- Serialize to a JSON object.
.dict()- Serialize to a dictionary.
dataframe
 | def dataframe() -> DataFrame
Return a dataframe of the option chain.
calls
 | def calls() -> "OptionsChain"
Return a OptionChain with only call contracts.
puts
 | def puts() -> "OptionsChain"
Return a OptionChain with only put contracts.
__len__
 | def __len__() -> int
Return the number of OptionContracts in the OptionChain.
MultipleOptionChains
Multiple Option Chains with multiple expiration dates.
Attributes:
option_chain_listList[OptionsChain] - List of option chains.
contract_expiration_listContractExpirationList - List of expirations.Notes:
This class inherits from the pydantic BaseModel which allows for the use of .json() and .dict() for serialization to json strings and dictionaries.
.json()- Serialize to a JSON object.
.dict()- Serialize to a dictionary.
dataframe
 | def dataframe() -> DataFrame
Return a dataframe of multiple option chains.
calls
 | def calls() -> "MultipleOptionChains"
Return a MultipleOptionChains object with only call contracts.
puts
 | def puts() -> "MultipleOptionChains"
Return a MultipleOptionChains object with only put contracts.
__len__
 | def __len__() -> int
Return the number of option chains.
__iter__
 | def __iter__() -> Iterable
Iterate over option chain list.
__add__
 | def __add__(other: "MultipleOptionChains") -> Optional["MultipleOptionChains"]
Concatenate MultipleOptionChains.
get_table_elements
def get_table_elements(html: HTML) -> Tuple[Optional[HTML], Optional[HTML]]
Parse call and put HTML table elements.
Arguments:
htmlHTML - HTML element with call and put data.Returns:
Tuple of found call and put html elements.
parse_option_table
def parse_option_table(contract_expiration: ContractExpiration, contract_type: OptionContractType, options_table: HTML) -> List[OptionContract]
Parse and clean fields and rows of a options table HTML element.
Arguments:
contract_expirationContractExpiration - Used to pass ContractExpiration data to the returned OptionContract object.
contract_typeOptionContractType - Call or Put
options_tableHTML - HTML element with raw options table data.Returns:
A list of OptionContracts parsed from the html options_table.
get_option_expirations
def get_option_expirations(symbol: str, **kwargs) -> Optional[ContractExpirationList]
Get and parse option expiration data for the selected symbol.
Arguments:
symbolstr - Ticker symbol.
kwargs- Pass (session, proxies, and timeout) to the requestor function.Returns:
ContractExpirationList
OptionPageNotFound
Raised when options page data is not found.
get_options_page
def get_options_page(symbol: str, after_days: int = None, before_days: int = None, first_chain: bool = False, use_fuzzy_search: bool = True, page_not_found_ok: bool = False, **kwargs, ,) -> Optional[Union[OptionsChain, MultipleOptionChains]]
Get options data from yahoo finance options page.
Arguments:
symbolstr - Ticker symbol.
after_daysint - Number of days to start filtering from. All expirations which expire prior to the days will be filtered out.
before_daysint - Number of days to start filtering from. All expirations which expire post days will be filtered out.
first_chainbool - If True returns first chain. Else returns all found chains within search range.
use_fuzzy_searchbool - If True, does a symbol lookup validation prior to requesting options page data.
page_not_found_okbool - If True, returns None when page is not found.
**kwargs- Pass (session, proxies, and timeout) to the requestor function.Returns:
OptionsChain- If first_chain is set to True the first found OptionsChain within the after_days and before_days range is returned. This is all option contracts from a single expiration and symbol.
MultipleOptionChains- If first_chain is set to False all OptionsChains within the after_days and before_days range are returned. This can have multiple expirations. Even if one expiration date is found the MultipleOptionChains object is returned.
None- If no contracts are found and page_not_found_ok is True.Raises:
OptionPageNotFound- If page_not_found_ok is False and the Options page is not found.