
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
What is the best way to get stock data using Python?
In this article, we will learn the best way to get stock data using Python.
The yfinance Python library will be used to retrieve current and historical stock market price data from Yahoo Finance.
Installation of Yahoo Finance(yfinance)
One of the best platforms for acquiring Stock market data is Yahoo Finance. Just download the dataset from the Yahoo Finance website and access it using yfinance library and Python programming.
You can install yfinance with the help of pip, all you have to do is open up command prompt and type the following command show in syntax:
Syntax
pip install yfinance
The best part about yfinance library is, its free to use and no API key is required for it
How to get current data of Stock Prices
We need to find a ticker of the stock Which we can use for data extraction. we will show the current market price and the previous close price for GOOGL in the following example.
Example
The following program returns the market price value,previous close price value,ticker value using yfinance module −
import yfinance as yf ticker = yf.Ticker('GOOGL').info marketPrice = ticker['regularMarketPrice'] previousClosePrice = ticker['regularMarketPreviousClose'] print('Ticker Value: GOOGL') print('Market Price Value:', marketPrice) print('Previous Close Price Value:', previousClosePrice)
Output
On executing, the above program will generate the following output −
Ticker Value: GOOGL Market Price Value: 92.83 Previous Close Price Value: 93.71
How to get Historical data of Stock Prices
By giving the start date, end date, and ticker, we can obtain full historical price data.
Example
The following program returns the stock price data between the start and end dates −
# importing the yfinance package import yfinance as yf # giving the start and end dates startDate = '2015-03-01' endDate = '2017-03-01' # setting the ticker value ticker = 'GOOGL' # downloading the data of the ticker value between # the start and end dates resultData = yf.download(ticker, startDate, endDate) # printing the last 5 rows of the data print(resultData.tail())
Output
On executing, the above program will generate the following output −
[*********************100%***********************] 1 of 1 completed Open High Low Close Adj Close Volume Date 2017-02-22 42.400002 42.689499 42.335499 42.568001 42.568001 24488000 2017-02-23 42.554001 42.631001 42.125000 42.549999 42.549999 27734000 2017-02-24 42.382500 42.417999 42.147999 42.390499 42.390499 26924000 2017-02-27 42.247501 42.533501 42.150501 42.483501 42.483501 20206000 2017-02-28 42.367500 42.441502 42.071999 42.246498 42.246498 27662000
The above example will retrieve data of stock price dated from 2015-03-01 to 2017-03-01.
If you want to pull data from several tickers at the same time, provide the tickers as a space-separated string.
Transforming Data for Analysis
Date is the dataset's index rather than a column in the example above dataset. You must convert this index into a column before performing any data analysis on it. Here's how to do it −
Example
The following program adds the column names to the stock data between the start and end date −
import yfinance as yf # giving the start and end dates startDate = '2015-03-01' endDate = '2017-03-01' # setting the ticker value ticker = 'GOOGL' # downloading the data of the ticker value between # the start and end dates resultData = yf.download(ticker, startDate, endDate) # Setting date as index resultData["Date"] = resultData.index # Giving column names resultData = resultData[["Date", "Open", "High","Low", "Close", "Adj Close", "Volume"]] # Resetting the index values resultData.reset_index(drop=True, inplace=True) # getting the first 5 rows of the data print(resultData.head())
Output
On executing, the above program will generate the following output −
[*********************100%***********************] 1 of 1 completed Date Open High Low Close Adj Close Volume 0 2015-03-02 28.350000 28.799500 28.157499 28.750999 28.750999 50406000 1 2015-03-03 28.817499 29.042500 28.525000 28.939501 28.939501 50526000 2 2015-03-04 28.848499 29.081499 28.625999 28.916500 28.916500 37964000 3 2015-03-05 28.981001 29.160000 28.911501 29.071501 29.071501 35918000 4 2015-03-06 29.100000 29.139000 28.603001 28.645000 28.645000 37592000
The above converted data and data we acquired from Yahoo Finance are identical
Storing the Obtained Data in a CSV File
The to_csv() method can be used to export a DataFrame object to a CSV file.The following code will help you export the data in a CSV file as the above-converted data is already in the pandas dataframe.
# importing yfinance module with an alias name import yfinance as yf # giving the start and end dates startDate = '2015-03-01' endDate = '2017-03-01' # setting the ticker value ticker = 'GOOGL' # downloading the data of the ticker value between # the start and end dates resultData = yf.download(ticker, startDate, endDate) # printing the last 5 rows of the data print(resultData.tail()) # exporting/converting the above data to a CSV file resultData.to_csv("outputGOOGL.csv")
Output
On executing, the above program will generate the following output −
[*********************100%***********************] 1 of 1 completed Open High Low Close Adj Close Volume Date 2017-02-22 42.400002 42.689499 42.335499 42.568001 42.568001 24488000 2017-02-23 42.554001 42.631001 42.125000 42.549999 42.549999 27734000 2017-02-24 42.382500 42.417999 42.147999 42.390499 42.390499 26924000 2017-02-27 42.247501 42.533501 42.150501 42.483501 42.483501 20206000 2017-02-28 42.367500 42.441502 42.071999 42.246498 42.246498 27662000
Visualizing the Data
The yfinance Python module is one of the easiest to set up, collect data from, and perform data analysis activities with. Using packages such as Matplotlib, Seaborn, or Bokeh, you may visualize the results and capture insights.
You can even use PyScript to display these visualizations directly on a webpage.
Conclusion
In this article, we learned how to use the Python yfinance module to obtain the best stock data. Additionally, we learned how to obtain all stock data for the specified periods, how to do data analysis by adding custom indexes and columns, and how to convert this data to a CSV file.
- Related Articles
- What is the Best Way to Develop Desktop Applications Using Python?
- What is the best way to log a Python exception?
- What is the best way to learn Python and Django?
- What is the best way to show data in a table in Tkinter?
- What is the best way to handle list empty exception in Python?
- What is the best site to invest money in stock market?
- What is the simplest way to SSH using Python?
- What is the best way to remove an item from a Python dictionary?
- What is the Best Way to Install Python on a Windows 10 Computer?
- What is the best way to run all Python files in a directory?
- What is the best way to handle a Javascript popup using Selenium Webdriver?
- What is the best way to stop Brain Drain?
- What is the best way to earn money online?
- What is the best way to learn Vanilla JavaScript?
- What is best way to check if a list is empty in Python?
