Get Stock Price Data using Python

As machine learning practitioners, we need to collect stock price data for regression analysis and time series analysis. We can easily download it from Yahoo Finance. But imagine if we want to create an application where we can analyze the real-time stock prices, we need to collect the latest dataset instead of using the downloaded dataset. So if you want to learn how to get the stock price data between any time interval by using the Python programming language, this article is for you. In this article, I will take you through how to get stock price data using Python.

Get Stock Price Data using Python

Get Stock Price Data using Python

Yahoo Finance is one of the most popular websites to collect stock price data. You need to visit the website, enter the company’s name or stock symbol, and you can easily download the dataset. But if you want to get the latest dataset every time you are running your code, you need to use the yfinance API. yfinance is an API provided by Yahoo Finance to collect the latest stock price data.

To use this API, you need to install it by using the pip command in your terminal or command prompt as mentioned below:

  • pip install yfinance

I hope you have easily installed this API. Now below is how you can get the latest stock price data using Python:

import pandas as pd
import yfinance as yf
import datetime
from datetime import date, timedelta
today = date.today()

d1 = today.strftime("%Y-%m-%d")
end_date = d1
d2 = date.today() - timedelta(days=360)
d2 = d2.strftime("%Y-%m-%d")
start_date = d2

data = yf.download('AAPL', 
                      start=start_date, 
                      end=end_date, 
                      progress=False)
print(data.head())
                  Open        High  ...   Adj Close     Volume
Date                                ...                       
2020-12-28  133.990005  137.339996  ...  135.852509  124486200
2020-12-29  138.050003  138.789993  ...  134.043640  121047300
2020-12-30  135.580002  135.990005  ...  132.900681   96452100
2020-12-31  134.080002  134.740005  ...  131.876999   99116600
2021-01-04  133.520004  133.610001  ...  128.617111  143301900

[5 rows x 6 columns]

The above code will collect the stock price data from today to the last 360 days. In this dataset, Date is not a column, it’s the index of this dataset. To use this data for any data science task, we need to convert this index into a column. Below is how you can do that:

data["Date"] = data.index
data = data[["Date", "Open", "High", 
             "Low", "Close", "Adj Close", "Volume"]]
data.reset_index(drop=True, inplace=True)
print(data.head())
        Date        Open        High  ...       Close   Adj Close     Volume
0 2020-12-28  133.990005  137.339996  ...  136.690002  135.852524  124486200
1 2020-12-29  138.050003  138.789993  ...  134.869995  134.043640  121047300
2 2020-12-30  135.580002  135.990005  ...  133.720001  132.900696   96452100
3 2020-12-31  134.080002  134.740005  ...  132.690002  131.876999   99116600
4 2021-01-04  133.520004  133.610001  ...  129.410004  128.617096  143301900

[5 rows x 7 columns]

So as you can see, the final dataset is just like the dataset that we download from Yahoo Finance. This is how we can get stock price data using Python.

Summary

So this is how you can collect the latest stock price dataset between any time interval, using the Python programming language. If you want to get the latest dataset every time you are running your code, you need to use the yfinance API. yfinance is an API provided by Yahoo Finance to collect the latest stock price data. I hope you liked this article on how to get stock price dataset using Python. Feel free to ask your valuable questions in the comments section below.

Aman Kharwal
Aman Kharwal

Coder with the ♥️ of a Writer || Data Scientist | Solopreneur | Founder

Articles: 1335

Leave a Reply