Downloading option chain and fundamental using Python
The recently updated yfinance added a lot more capabilities to this already popular library. You can now download fundamental data, including company financials, balance sheet and cashflow, as well as option chain data. Here's how...
First, import yfinance
and create a ticker object:
import yfinance as yf
aapl = yf.Ticker("AAPL")
Next, let's get information about the stock
aapl.info
Output:
{'language': 'en-US',
'region': 'US',
'quoteType': 'EQUITY',
'quoteSourceName': 'Nasdaq Real Time Price',
'currency': 'USD',
'sharesOutstanding': 4601079808,
...
...
'marketCap': 814805221376,
'marketState': 'REGULAR',
'priceHint': 2,
'exchange': 'NMS',
'exchangeDataDelayedBy': 0,
'symbol': 'AAPL'}
Getting historical market data
hist = aapl.history(period="max")
hist.head()
Output:
Open High Low Close Volume Dividends Stock Splits
Date
1980-12-12 0.02 0.02 0.02 0.02 117258400 0.0 0.0
1980-12-15 0.02 0.02 0.02 0.02 43971200 0.0 0.0
1980-12-16 0.02 0.02 0.02 0.02 26432000 0.0 0.0
1980-12-17 0.02 0.02 0.02 0.02 21610400 0.0 0.0
1980-12-18 0.02 0.02 0.02 0.02 18362400 0.0 0.0
Show only actions (dividends + splits):
aapl.actions
Output:
Dividends Stock Splits
Date
1987-05-11 0.12 0.0
1987-06-16 0.00 2.0
...
2019-02-08 0.73 0.0
2019-05-10 0.77 0.0
Show only dividents:
aapl.dividends
Output:
1987-05-11 0.12
1987-08-10 0.06
...
2019-02-08 0.73
2019-05-10 0.77
...or only splits:
aapl.splits
Output:
1987-06-16 2.0
2000-06-21 2.0
2005-02-28 2.0
2014-06-09 7.0
Dowloading fundamental data
Let's download Apple's financials:
aapl.financials
Output:
9/29/2018 9/30/2017 9/24/2016 9/26/2015
Total Revenue 265595000 229234000 215639000 233715000
...
Gross Profit 101839000 88186000 84263000 93626000
Downloading the balance sheet is as easy as...
aapl.balance_sheet
Output:
9/29/2018 9/30/2017 9/24/2016 9/26/2015
Cash And Cash Equivalents 25913000 20289000 20484000 21120000
...
Net Tangible Assets 107147000 134047000 119629000 110346000
Same goes for the cashflow:
aapl.cashflow
9/29/2018 9/30/2017 9/24/2016 9/26/2015
Net Income 59531000 48351000 45687000 53394000
...
Change In Cash / Cash Equivalents 5624000 -195000 -636000 7276000
Dowloading options data
To download option chains, let's first get the list of expirations:
aapl.options
('2019-05-31',
'2019-06-07',
...
'2021-01-15',
'2021-06-18')
Now, we can download the option chain for a specific expiration date:
opt = aapl.option_chain('2019-06-07')
Let's look at the call options:
opt.calls
Output:
contractSymbol lastTradeDate strike lastPrice ... currency
0 AAPL190607C00150000 1559318324 150.0 27.05 ... USD
1 AAPL190607C00152500 1558710976 152.5 27.80 ... USD
2 AAPL190607C00155000 1559321089 155.0 22.06 ... USD
...
34 AAPL190607C00242500 1557513401 242.5 0.04 ... USD
35 AAPL190607C00245000 1557846185 245.0 0.01 ... USD
Same goes for put options, of course:
opt.puts
Output:
contractSymbol lastTradeDate strike lastPrice ... currency
0 AAPL190607P00145000 1559320134 145.0 0.05 ... USD
1 AAPL190607P00146000 1559243891 146.0 0.06 ... USD
2 AAPL190607P00150000 1559322559 150.0 0.09 ... USD
...
30 AAPL190607P00235000 1557415866 235.0 38.40 ... USD
31 AAPL190607P00240000 1556642527 240.0 40.30 ... USD
To install/upgrade yfinance using pip, run:
$ pip install yfinance --upgrade --no-cache-dir
The Github repository has more information and issue tracking.
Enjoy!
Updated on 31 May 2019.
For the latest version, please see:
https://aroussi.com/print/download-options-data