Angel Ortiz
Angel Ortiz

Reputation: 138

How to get all At-The-Money options using yahoo_fin

I am trying to create a list of all At-The-Money (ATM) option contracts using yahoo_fin options module.

Yahoo_fin offers 2 methods for getting all call and put contracts:

from yahoo_fin import options as ops

# ops.get_call(Ticker, expiration_date=None)
# ops.get_pull(Ticker, expiration_date=None)
# If no expiration_date is passed, the nearest expiration date is used

ops.get_calls("aapl")
ops.get_puts("aapl")

These two methods return the following dataframes, respectively:enter image description here

enter image description here

I have done some research at possibly using the strike price and comparing it with the underlying stock price. This is probably the most basic way, but the underlying stock may hay a price that is not exactly the same as an option's strike price. Another alternative I have read is to use delta. Can anybody provide insight into how I could find the ATM options using the data provided by yahoo_fin? Is it possible?

Upvotes: 0

Views: 1244

Answers (1)

KarelZe
KarelZe

Reputation: 1731

For ATM options the strike price is equal to the underlying asset’s current market price, as explained here.

However, there is no option for every possible market price, as options are oganized in grids. You could get the price of the option for which the strike price is closest to the underlying's market price. You can implement it as:

from yahoo_fin import options, stock_info

symbol = "AAPL"

last_adj_close = stock_info.get_data(symbol)["adjclose"][-1]

calls = options.get_calls("aapl")
puts = options.get_puts("aapl")

atm_call = calls.iloc[(calls["Strike"] - last_adj_close).abs().argsort()[:1]]

Output:

Contract Name   Last Trade Date Strike  Last Price  Bid Ask Change  % Change    Volume  Open Interest   Implied Volatility
43  AAPL221118C00149000 2022-11-16 3:59PM EST   149.0   1.58    1.5 1.66    -1.12   -41.48% 22594   14120   40.09%

for the AAPL stock:

    open    high    low close   adjclose    volume  ticker
2022-11-14  148.970001  150.279999  147.429993  148.279999  148.279999  73374100    AAPL
2022-11-15  152.220001  153.589996  148.559998  150.039993  150.039993  89868300    AAPL

You can also obtain the two closest options by adjusting the parameter in .argsort()[:2].

Upvotes: 1

Related Questions