Options Module
Contains the classes and functions for scraping a yahoo finance option page.
ContractExpiration
Contract Expiration.
Attributes:
symbol
str - Ticker symbol.timestamp
str - Timestamp of expiration date.expiration_date
DateTime - 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_list
List[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:
after
DateTime - 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:
before
DateTime - 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:
after
DateTime - datetime to filter.before
DateTime - 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:
days
int - 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:
days
int - 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_days
int - Number of days to start filtering from. All expirations which expire prior to the days will be filtered out.before_days
int - 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:
symbol
str - Ticker symbol.
contract_type
OptionContractType - Call or Put type.
timestamp
str - Raw timestamp scraped from yahoo finance. This string is left untouched to make sure there is no issues when building a URL.expiration_date
DateTime - Converted from the timestamp. This allows allows sorting and filtering.
in_the_money
bool - True if strike price is ITM else False.
contract_name
str - Contract Name.last_trade_date
DateTime - Date of last trade.strike
float - Contracts strike price.
last_price
float - Last price of a transaction between a contract buyer and a seller.
bid
float - Last bid price.
ask
float - Last ask price.
change
float - Price change in dollars.percent_change
float - Price change in percentage.volume
int - Volume.open_interest
int - Number of contracts opened.implied_volatility
float - 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:
symbol
str - Company symbol.expiration_date
DateTime - Contracts expiration date.chain
List[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_list
List[OptionsChain] - List of option chains.contract_expiration_list
ContractExpirationList - 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:
html
HTML - 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_expiration
ContractExpiration - Used to pass ContractExpiration data to the returned OptionContract object.contract_type
OptionContractType - Call or Putoptions_table
HTML - 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:
symbol
str - 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:
symbol
str - Ticker symbol.after_days
int - Number of days to start filtering from. All expirations which expire prior to the days will be filtered out.before_days
int - Number of days to start filtering from. All expirations which expire post days will be filtered out.first_chain
bool - If True returns first chain. Else returns all found chains within search range.use_fuzzy_search
bool - If True, does a symbol lookup validation prior to requesting options page data.page_not_found_ok
bool - 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.