This project demonstrates how to fetch, process, and visualize historical stock prices and revenues for companies like Tesla and GameStop using Python. 📈📉 The visualizations are created with Plotly, providing interactive and insightful graphs for analysis.
- Fetch Historical Stock Data: Uses Yahoo Finance to retrieve stock data 📅.
- Extract Revenue Data: Scrapes HTML tables to obtain revenue information from provided URLs 💻.
- Interactive Graphs: Combines stock price and revenue data into an interactive dual-subplot visualization 📊.
- Export as HTML: Saves the visualizations for easy sharing and offline viewing 🌐.
|-- main.py # The primary script containing all functionality
|-- requirements.txt # Dependencies for the project
|-- README.md # Project documentation
yfinancefor fetching stock datapandasfor data manipulationplotlyfor interactive visualizationsrequestsfor web scrapingbeautifulsoup4for parsing HTML content
- Purpose: Combines historical stock prices and revenues into a dual-plot interactive graph.
- Input:
stock_data- Stock price data (DataFrame)revenue_data- Revenue data (DataFrame)stock- Stock name (string)
- Output: Displays the graph and saves it as an HTML file (
<stock>_revenue_graph.html) 📂.
- Fetch Stock Data:
- Uses
yfinanceto fetch historical Tesla data. - Resets index and prepares the DataFrame for plotting.
- Uses
- Fetch Revenue Data:
- Scrapes Tesla revenue data from a provided URL using
requestsandBeautifulSoup. - Cleans and processes the revenue data for visualization.
- Scrapes Tesla revenue data from a provided URL using
- Similar process to Tesla:
- Stock data fetched with
yfinance. - Revenue data scraped from a provided URL, cleaned, and processed.
- Stock data fetched with
- Top Plot: Tesla's stock prices over time.
- Bottom Plot: Tesla's revenue over time.
- Interactive: Zoom, pan, and hover over data points for details!
- Same structure as Tesla but focused on GameStop.
- Fetch Data:
- Stock data:
yfinance.Ticker("TSLA").history(period="max") - Revenue data: Scraped from HTML tables.
- Stock data:
- Clean and Process:
- Remove unwanted characters (e.g.,
$,,) from revenue data. - Filter rows to focus on relevant data.
- Remove unwanted characters (e.g.,
- Visualize:
- Combine data using Plotly's
make_subplots. - Customize layouts, titles, and interactivity.
- Combine data using Plotly's
- Export:
- Save visualizations as standalone HTML files.
make_graph(tesla_data, tesla_revenue, 'Tesla')
make_graph(gamestop_data, gamestop_revenue, 'Gamestop')Tesla_revenue_graph.htmlGamestop_revenue_graph.html
- User-Friendly: Generates HTML graphs that can be opened in any browser.
- Customizable: Easily switch stock tickers or URLs for other companies.
- Scalable: Add more companies or metrics with minimal code changes.
- This project is for educational purposes only.
- Data is retrieved from public APIs and sources. Ensure compliance with data usage policies.