Analyzing Historical Stock Data and Revenue Comparison
The Problem
Imagine you’re interested in comparing the historical stock prices and revenue of two companies, Tesla and GameStop. You want to visualize the data to understand the trends and patterns over time. However, manually collecting the data and creating a comprehensive analysis can be time-consuming and complex.
To tackle this problem, I developed a Python code that automates the process of retrieving historical stock data and revenue data for Tesla and GameStop, and creates an interactive graph for easy visualization.
This Python code allows you to retrieve historical stock data and revenue data for two companies, Tesla and GameStop, and visualize the data by creating a graph that compares their historical share prices and revenues.
Dependencies
Make sure you have the following dependencies installed before running the code:
- yfinance
- pandas
- requests
- bs4 (BeautifulSoup)
- plotly.graph_objects
- plotly.subplots
Code Overview
The code begins by importing the required libraries and modules:
import yfinance as yf
import pandas as pd
import requests
from bs4 import BeautifulSoup
import plotly.graph_objects as go
from plotly.subplots import make_subplots
- `yfinance` is imported as `yf` for retrieving historical stock data.
- `pandas` is imported as `pd` for data manipulation and analysis.
- `requests` is imported for making HTTP requests to retrieve revenue data.
- `BeautifulSoup` is imported from `bs4` to parse HTML data.
- `plotly.graph_objects` is imported as `go` for creating interactive plots.
- `plotly.subplots` is imported as `make_subplots` for creating subplots within the graph.
Next, there is a function named `make_graph` that takes in three parameters: `stock_data`, `revenue_data`, and `stock`. This function is responsible for creating the graph that compares the historical share price and revenue.
def make_graph(stock_data, revenue_data, stock):
fig = make_subplots(rows=2, cols=1, shared_xaxes=True, subplot_titles=("Historical Share Price", "Historical Revenue"), vertical_spacing = .3)
stock_data_specific = stock_data[stock_data.Date <= '2021--06-14']
revenue_data_specific = revenue_data[revenue_data.Date <= '2021-04-30']
fig.add_trace(go.Scatter(x=pd.to_datetime(stock_data_specific.Date, infer_datetime_format=True), y=stock_data_specific.Close.astype("float"), name="Share Price"), row=1, col=1)
fig.add_trace(go.Scatter(x=pd.to_datetime(revenue_data_specific.Date, infer_datetime_format=True), y=revenue_data_specific.Revenue.astype("float"), name="Revenue"), row=2, col=1)
fig.update_xaxes(title_text="Date", row=1, col=1)
fig.update_xaxes(title_text="Date", row=2, col=1)
fig.update_yaxes(title_text="Price ($US)", row=1, col=1)
fig.update_yaxes(title_text="Revenue ($US Millions)", row=2, col=1)
fig.update_layout(showlegend=False,
height=900,
title=stock,
xaxis_rangeslider_visible=True)
fig.show()
The code then proceeds to retrieve the historical stock data for Tesla using the `yf.Ticker` function and the `history` method. The data is stored in a pandas DataFrame named `tsla_data`. Similarly, the revenue data for Tesla is retrieved by making an HTTP request to the specified URL using the `requests` library, and then parsed using BeautifulSoup. The revenue data is stored in a pandas DataFrame named `tsla_revenue`.
# Create a ticker object for GamesTop
tsla = yf.Ticker("TSLA")
# Use the history() function to get historical stock data
historical_data = tsla.history(period="max")
# Print the historical data
print(historical_data)
tsla_data = pd.DataFrame(historical_data)
tsla_data.reset_index(inplace=True)
print(tsla_data.head())
tsla_url = "https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0220EN-SkillsNetwork/labs/project/revenue.htm"
data = requests.get(tsla_url).text
soup = BeautifulSoup(data, 'xml')
tsla_dict = {}
tsla_dict["Date"]=[]
tsla_dict["Revenue"]=[]
table = soup.select("table")[1]
rows = table.select("tbody > tr")
for row in rows:
col = row.find_all("td")
if len(col) >= 2:
tsla_date = col[0].text
tsla_revenue_ = col[1].text
tsla_dict["Date"].append(tsla_date)
tsla_dict["Revenue"].append(tsla_revenue_)
#print(tsla_dict)
tsla_revenue = pd.DataFrame(tsla_dict)
print(tsla_revenue.head())
tsla_revenue["Revenue"] = tsla_revenue['Revenue'].str.replace(',|\$',"")
tsla_revenue.dropna(inplace=True)
tsla_revenue = tsla_revenue[tsla_revenue['Revenue'] != ""]
print(tsla_revenue.head())
The same steps are repeated for GameStop, retrieving the historical stock data and revenue data, and storing them in `gme_data` and `gme_revenue` DataFrames, respectively.
gamestop = yf.Ticker("GME")
# Use the history() function to get historical stock data
historical_data_g = gamestop.history(period="max")
# Print the historical data
print(historical_data)
gme_data = pd.DataFrame(historical_data)
gme_data.reset_index(inplace=True)
print(gme_data.head())
gme_url = "https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0220EN-SkillsNetwork/labs/project/stock.html"
data_1 = requests.get(gme_url).text
soup_1 = BeautifulSoup(data_1, "html.parser")
gme_dict = {}
gme_dict["Date"] = []
gme_dict["Revenue"] = []
table_1 = soup_1.select("table")[1]
rows_1 = table_1.select("tbody > tr")
for row_1 in rows_1:
col_1 = row_1.find_all("td")
gme_date = col_1[0].text
gme_revenue_ = col_1[1].text
gme_dict["Date"].append(gme_date)
gme_dict["Revenue"].append(gme_revenue_)
print(gme_dict)
gme_revenue = pd.DataFrame(gme_dict)
print(gme_revenue.head())
gme_revenue["Revenue"] = gme_revenue['Revenue'].str.replace(',|\$',"")
gme_revenue.dropna(inplace=True)
gme_revenue = gme_revenue[gme_revenue['Revenue'] != ""]
print(gme_revenue.head())
Finally, the `make_graph` function is called twice: once for Tesla data and once for GameStop data. This function creates a graph with two subplots, one for the historical share price and one for the historical revenue. The resulting graph is displayed.
Running the Code
To run the code, make sure you have installed all the required dependencies. Simply execute the code, and it will retrieve the data and generate the graph automatically.
By running this code, you can effortlessly compare the historical stock prices and revenue of Tesla and GameStop in a visually appealing and informative manner. The generated graph allows you to identify trends, patterns, and potential correlations between stock prices and revenue over time.
With this automated approach, you can save valuable time and effort in collecting and analyzing the data manually. The code streamlines the process and enables you to focus on gaining insights and making informed decisions based on the visualized information.
Conclusion
The Python code presented here provides an efficient solution to analyze and compare historical stock data and revenue for Tesla and GameStop. By automating the retrieval and visualization process, it empowers you to explore the data effectively and derive meaningful insights.
Feel free to customize the code according to your needs, add additional functionalities, or even extend it to analyze data from other companies. This code serves as a foundation for conducting comprehensive analyses and gaining valuable insights into the financial performance of different companies.
Here is a link to the code: https://gist.github.com/temidayo67/947a9dc4332d9d5c10098ee0eadcdb17