AAPL Stock Price Prediction
Recently, I completed a small machine learning project to predict the closing price of Apple's (AAPL) stock. Using historical data from Yahoo Finance and Pytorch, I built an LSTM (Long Short-Term Memory) model to forecast prices based on past market trends. This project provided an opportunity to explore time series forecasting, feature engineering, and data visualisation.
Project Overview:
- Goal: Predict Apple's daily closing stock prices using historical data.
- Model: A three-layer LSTM with 100 hidden units, trained over 100 epochs.
- Tools: Python, PyTorch, Pandas, Numpy, Plotly, YFinance
Data and Features:
I worked with daily stock data spanning from January 2018 to June 2025. The model used the following features to understand market behaviour:
- Open, High, Low, Volume: These capture the stock's daily price range and trading activity.
- RSI (Relative Strength Index): I feature-engineered this by calculating it from price changes. It is a momentum indicator calculated over 14 days, helping the model detect potential trend shifts.
The model predicts the next day's closing price by analysing the past 60 days of data, a sequence length that balances the historical context with efficiency.
Training and Results:
- Training Setup: I trained the model with the Adam optimiser (learning rate: 0.001) and Mean Squared Error (MSE) as the loss function.
Performance Metrics- Mean Absolute Error (MAE): 1.8379
- Root Mean Squared Error (RMSE): 2.5498
With Apple's average closing price around $131 during the test period, an MAE of 1.8379 means an average prediction error of ~1.40%, indicating a strong result given the unpredictability of stock markets.
The Graph: Actual vs Predicted Prices I used Plotly to create an interactive chart comparing actual and predicted closing prices over time.
- X-Axis (Date): Trading dates from the test period (e.g. 2024-01-01, 2025-06-01).
- Y-Axis (Close Price in USD): The stock's closing price, with actual prices in blue and predicted prices in orange.
The closer the orange line tracks the blue line, the more accurate the predictions. The graph shows that the model is able to follow the market trends well.
Challenges and Solutions:
- Data integrity: I avoided data leakage by ensuring the target (Close) wasn't part of the input features and used proper sequence indexing for time series data.
- Debugging: During development, an 'IndexError' occurred from mismatched tensor dimensions. However, this was solved by simply double-checking input data shapes and model outputs.
What I Learned:
This project helped improve my skills in:
- Time series forecasting with LSTMs.
- Feature engineering to boost model accuracy.
- - Deep learning with Pytorch.
- Data visualisation with Plotly.
Available here and graph below.