How To Use Python To Get Stock Data

Sharing is caring!

Last Updated on May 28, 2022 by Jay

This tutorial will show you how to use Python to get historical stock price data from the Internet for free.

We are going to use a library called yfinance to download stock data from Yahoo Finance.

The Yahoo! finance API is for personal use only. Please refer to Yahoo!’s terms of use (herehere, and here) for details on your rights to use the data downloaded.

Install The Library

pip install yfinance

The Ticker Object

We need to first create a Ticker object for each stock we want to get data for. Once a Ticker is created, we can use it to get a lot of useful financial data provided by Yahoo Finance.

import yfinance as yf

tsla = yf.Ticker('tsla')

Company Information

To get general information about a company, we can call the Ticker.info property. We can find things like company description, sector, financial metrics, etc. from here.

tsla.info
Python Get Stock Data Using Yfinance
Python Get Stock Data Using Yfinance

Note that the result is a dictionary containing various keys. To access information stored in the info, we just need to pass a key into the dictionary. For example to get the forward PE value:

tsla.info['forwardPE']
47.745445

Historical Prices

The Ticker.history() method takes one argument period to specify how many days of data we want to get. The below example gets 2 days of data.

  • To get 3 months, simply pass “3m”.
  • To get 2 years of data, pass “2y” into the method.
  • period = “max” returns the maximum days of data available
tsla.history(period='2d')
                  Open        High         Low       Close    Volume  \
Date                                                                   
2022-05-26  661.419983  718.669983  653.659973  707.729980  35334400   
2022-05-27  723.250000  759.799988  720.530029  759.630005  29630300   

            Dividends  Stock Splits  
Date                                 
2022-05-26          0             0  
2022-05-27          0             0  

Dividends

We’ll use Apple stock to show the dividends since Tesla doesn’t pay dividends thus no data for it. The result is a dataframe with historical dividends payout per share of stock, post-split price.

aapl = yf.Ticker("aapl")
aapl.dividends
Date
1987-05-11    0.000536
1987-08-10    0.000536
1987-11-17    0.000714
1988-02-12    0.000714
1988-05-16    0.000714
                ...   
2021-05-07    0.220000
2021-08-06    0.220000
2021-11-05    0.220000
2022-02-04    0.220000
2022-05-06    0.230000
Name: Dividends, Length: 75, dtype: float64

Stock Splits

Companies may choose to “split stock” meaning increase the number of shares available without affecting the company value. As a result, the stock after the split will drop in price per new share.

For example, a $100 stock can split 1:5. If you own 1 share at $100 pre-split, then you’ll own 5 shares at $20 post-split.

TSLA actually did a split 1:5 previously. However, Yahoo Finance data doesn’t show that… Let’s use AAPL for the demonstration again. The result shows the date of the stock split and the split size.

aapl.splits
Date
1987-06-16    2.0
2000-06-21    2.0
2005-02-28    2.0
2014-06-09    7.0
2020-08-31    4.0
Name: Stock Splits, dtype: float64

Option Chain

We can also get options data using the library. The options property returns a list of expiration dates. Then we can pass the expiration date into option_chain() method to get the actual option chain.

tsla.options
('2022-06-17
 '2022-07-15',
 .....
 '2023-09-15',
 '2024-06-21')

tsla.option_chain('2022-06-17')

Leave a Reply

Your email address will not be published. Required fields are marked *