How to Download historical stock prices in Python ? - GeeksforGeeks

05 Apr, 2021
Stock prices refer to the current price of the share of that stock. Stock prices are widely used in the field of Machine Learning for the demonstration of the regression problem. Stock prediction is an application of Machine learning where we predict the stocks of a particular firm by looking at its past data. Now to build something like this first step is to get our historical stock data.
We can get our historical stock data using API’s provided as library support in Python. A few of the API’s are mentioned below:
Yahoo Finance
Pandas DataReader
Quandl
Approach:
Each of the methods uses a different python module, but they have a similar procedural structure which includes the following steps:
1. Import required libraries
We are using datetime module to get the date of the starting and ending limit of the stock data required.
We are using matplotlib module to display the data extracted in a graphical format.
2. Initialize the start and end date for getting the stock data during that time period.
3. Get the data using the dedicated functions provided in each of the modules.
4.  Display the data using the matplotlib library. We use the plot() function to plot the data in a graphical format.
We can get stock using the yfinance.download() function provided in the yfinance module which is a module for Yahoo’s Finance API. We can download the module using the following command.
pip install yfinance
We need to supply 3 required parameters in the yfinance.download() function which are 
Stock Symbol
Start date
End date
Below is the implementation.
Python3
# import modulesfrom datetime import datetimeimport yfinance as yfimport matplotlib.pyplot as plt  # initialize parametersstart_date = datetime(2020, 1, 1)end_date = datetime(2021, 1, 1)  # get the datadata = yf.download('SPY', start = start_date,                   end = end_date)  # displayplt.figure(figsize = (20,10))plt.title('Opening Prices from {} to {}'.format(start_date,                                                end_date))plt.plot(data['Open'])plt.show()
Output:
Another way of getting the historical stock data is to use the pandas_datareader library. It also uses Yahoo’s Finance API to load in the data. We can download the module using the following command.
pip install pandas_datareader
It also requires the similar three fields to load in the data which are
Stock Symbol
Start date
End date
Below is the implementation:
Python3
# import modulesfrom pandas_datareader import data as pdrimport matplotlib.pyplot as plt  # initializing Parametersstart = "2020-01-01"end = "2021-01-01"symbols = ["AAPL"]  # Getting the datadata = pdr.get_data_yahoo(symbols, start, end)  # Displayplt.figure(figsize = (20,10))plt.title('Opening Prices from {} to {}'.format(start, end))plt.plot(data['Open'])plt.show()
Output:
Quandl has hundreds of free and paid data sources, across equities, fixed incomes, commodities, exchange rates, etc. In order to get the access, we need to create an account on Quandl and get an API Key to access the data for free. After that, we need to download the API support quandl library of python using the following command.
pip install quandl
 We will use quandl.get() function to get the data. It takes four fields to load in the data
Symbol
start_date
end_date
Authentication token
Below is the implementation:
Python3
# import modulesimport quandlfrom datetime import datetimeimport matplotlib.pyplot as plt  # initialize parametersstart = datetime(2015, 1, 1)end = datetime(2020, 1, 1)  # get the datadf = quandl.get('NSE/OIL', start_date = start,                end_date = end,                 authtoken = 'enter_your_api_key')  # displayplt.figure(figsize=(20,10))plt.title('Opening Prices from {} to {}'.format(start, end))plt.plot(df['Open'])plt.show()
Output:
Picked
python-utility
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Read a file line by line in Python
How to Install PIP on Windows ?
Enumerate() in Python
Different ways to create Pandas Dataframe
Iterate over a list in Python
*args and **kwargs in Python
Reading and Writing to text files in Python
Create a Pandas DataFrame from Lists
Check if element exists in list in Python