How to Get Stock Price Data Using Python

Want to get stock market data with Python? You are in the right place. This article shows you how to use Python to get stock market data. You can further use the data to analyze, visualize and gain insights from it.


You use the finance Python library for retrieving Yahoo Finance current and historical stock price data.


Install required libraries

Yahoo Finance is one of the widely used platforms that provides stock market data. You can easily download the dataset from their website, but if you want to access it directly from a Python program, you can use the yfinance library. To install yfinance using pip, you need to run the following command from a command prompt:

pip install yfinance

The yfinance python library is free to use and does not require an API key.

The code used in this project is available in a GitHub repository and is free for you to use under the MIT license.

Get live stock price data

You need the ticker of the stock you want to extract the data for. In the example below we find the market price and the previous closing price for GOOGL.

import yfinance as yf
ticker = yf.Ticker('GOOGL').info
market_price = ticker['regularMarketPrice']
previous_close_price = ticker['regularMarketPreviousClose']
print('Ticker: GOOGL')
print('Market Price:', market_price)
print('Previous Close Price:', previous_close_price)

This produces the following output:

This example uses the regular market price and regularMarketPreviousClose Properties to get the required data. The yfinance library has many other properties for you to explore. This includes zip code, sector, full time employees, long business summary, city, phone, state and country. You can get the full list of available properties with this code:

import yfinance as yf
ticker = yf.Ticker('GOOGL').info
print(ticker.keys())

Get historical stock price data

You can get all historical price data by providing start date, end date and ticker.


import yfinance as yf


start_date = '2020-01-01'
end_date = '2022-01-01'


ticker = 'GOOGL'


data = yf.download(ticker, start_date, end_date)


print(data.tail())

This produces the following output:

The code above retrieves the stock price data from 2020-01-01 to 2022-01-01.

If you want to retrieve data from multiple tickers at once, you can do so by providing the tickers in the form of a space-delimited string.

import yfinance as yf
start_date = '2020-01-01'
end_date = '2022-01-01'


ticker = 'GOOGL MSFT TSLA'
data = yf.download(ticker, start_date, end_date)
print(data.tail())

Transformation of data for analysis

In the above data set date is the index of the record and not a column. To perform data analysis on this data, you must convert this index to a column. Below is how you can do that:

import yfinance as yf
start_date = '2020-01-01'
end_date = '2022-01-01'
ticker = 'GOOGL'
data = yf.download(ticker, start_date, end_date)
data["Date"] = data.index

data = data[["Date", "Open", "High",
"Low", "Close", "Adj Close", "Volume"]]

data.reset_index(drop=True, inplace=True)
print(data.head())

This produces the following output:

This transformed data is the same as the data you would have downloaded from Yahoo Finance.

Saving the received data in a CSV file

You can export a DataFrame object to a CSV file using to_csv() Method. Since the above data is already in the form of a Pandas DataFrame, you can export the data to a CSV file using the following code:

import yfinance as yf
start_date = '2020-01-01'
end_date = '2022-01-01'
ticker = 'GOOGL'
data = yf.download(ticker, start_date, end_date)
print(data.tail())
data.to_csv("GOOGL.csv")

Pandas is the widely used Python library for data analysis. If you are not very familiar with this library, you should start with the basic operations with pandas.

Visualize the data

The yfinance python library is one of the most convenient libraries for setting up, retrieving data and performing data analysis tasks. You can use this data to visualize results and capture insights using libraries like Matplotlib, Seaborn, or Bokeh.

You can even display these visualizations directly on a web page using PyScript.

Leave a Reply

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