This repository contains a Python project for downloading, analyzing, and visualizing historical stock price data for Apple Inc. (AAPL) from Yahoo Finance. The project leverages libraries such as yfinance, pandas, and plotly to handle date ranges, fetch financial data, and prepare it for further analysis and visualization.
This project aims to:
- Download historical stock price data for Apple Inc. (AAPL) over the past 5000 days.
- Organize and clean the data for analysis.
- Provide a framework for visualizing stock price data using candlestick charts and other plots.
- Clone the repository:
git clone https://github.com/your-username/AAPL-Stock-Price-Analysis.git cd AAPL-Stock-Price-Analysis
- Create and activate a virtual environment (optional but recommended):
python3 -m venv venv source venv/bin/activate # On Windows use `venv\Scripts\activate`
- Install the required packages:
pip install -r requirements.txt
- Ensure you have an internet connection to download data from Yahoo Finance.
- Update the
data.csv
path in the notebook or script if necessary. - Run the Jupyter Notebook or Python script to download, clean, and visualize the data.
import pandas as pd
import yfinance as yf
import datetime
from datetime import date, timedelta
# Get today's date
today = date.today()
# Calculate the start and end dates for the past 5000 days
end_date = today.strftime("%Y-%m-%d")
start_date = (today - timedelta(days=5000)).strftime("%Y-%m-%d")
# Download stock data
data = yf.download('AAPL', start=start_date, end=end_date, progress=False)
# Prepare the data
data["Date"] = data.index
data = data[["Date", "Open", "High", "Low", "Close", "Adj Close", "Volume"]]
data.reset_index(drop=True, inplace=True)
# Display the first few rows of the data
print(data.head())