This project is a simple demonstration of using a linear regression model to predict the general trend of a stock's price over time. It fetches daily stock data from Alpha Vantage, trains a model on the historical prices, and plots the resulting trendline against the actual prices.
This is an educational tool to visualize how a simple statistical model can capture the long-term trend in a time series dataset.
- Data Fetching: The script connects to the Alpha Vantage API to retrieve daily historical stock data for a specific ticker (default is 'AAPL').
- Data Processing: It cleans and formats the data, keeping only the 'Close' price and ensuring the data is in chronological order.
- Feature Engineering: A simple 'TimeIndex' is created, which is a sequence of integers (0, 1, 2, ...). This index serves as the single feature (
X) to predict the stock price (y). The model essentially learns the relationshipPrice = m * Day + c. - Model Training: A
LinearRegressionmodel fromscikit-learnis trained on theTimeIndexandCloseprice data. - Visualization: The script uses
matplotlibto plot both the actual closing prices and the predicted trendline from the linear regression model, providing a clear visual comparison.
- Python 3.x
- An Alpha Vantage API Key