Skip to content

Latest commit

Β 

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 

Repository files navigation

Deep Learning Β· Finance Β· Time Series

🧠 Stock Price Prediction
with LSTM

Forecasting weekly closing prices of Barclays PLC (BARC.L) using a stacked LSTM neural network trained on 2.5 years of historical market data.

🐍 Python 3.7+ πŸ”΄ Keras / TensorFlow πŸ“Š Pandas Β· NumPy πŸ”¬ scikit-learn
Overview

πŸ“Œ Project Summary

This project builds a Long Short-Term Memory (LSTM) neural network to forecast stock prices β€” one of the most challenging problems in financial machine learning. Unlike traditional models, LSTMs learn long-range temporal dependencies, making them well-suited for financial time series where past behaviour influences future price movements.


The model is trained on weekly OHLCV data for Barclays PLC (LON: BARC) from January 2020 to August 2022, covering the COVID-19 crash, the recovery rally, and the 2022 macro downturn β€” a highly varied test of the model's generalisation ability.


Data

πŸ“ˆ Closing Price History

Full BARC.L weekly close price history. The vertical dashed line marks the 80/20 train/test boundary.

Close Price History
Fig 1 β€” BARC.L weekly closing prices (Jan 2020 – Aug 2022), 137 data points total.

Model

πŸ—οΈ LSTM Architecture

A stacked two-layer LSTM followed by fully connected Dense layers for regression output.

LSTM Architecture
Fig 2 β€” Model data flow: 60-step input window β†’ LSTMΓ—2 β†’ Dense layers β†’ single price prediction.
LayerTypeUnitsNotes
Inputβ€”60 Γ— 160-week lookback window
LSTM 1LSTM50return_sequences=True
LSTM 2LSTM50return_sequences=False
Dense 1Dense12Hidden layer
Dense 2Dense1Price output

Preprocessing

πŸ“ Data Preparation

Raw close prices are MinMax scaled to [0, 1] before training to stabilise gradient updates. The dataset is split into 80% training and 20% testing with no data leakage. Each training sample uses a 60-timestep sliding window to predict the next week's price.

Train/Test Split
Fig 3 β€” Scaled price data with 80% training (blue) and 20% test (orange) regions highlighted.
from sklearn.preprocessing import MinMaxScaler

scaler = MinMaxScaler(feature_range=(0, 1)) scaled_data = scaler.fit_transform(dataset)

training_data_len = math.ceil(len(dataset) * .8) # β†’ 110 weeks

# Build sliding windows: [t-60 ... t-1] β†’ predict t for i in range(60, len(train_data)): x_train.append(train_data[i-60:i, 0]) y_train.append(train_data[i, 0])


Training

πŸ” Model Training

Compiled with the Adam optimiser and Mean Squared Error loss. Trained for 2 epochs with a batch size of 2.

Training Loss
Fig 4 β€” Training vs validation MSE loss over epochs. Loss drops sharply from 0.1082 β†’ 0.0053.
EpochTraining Loss (MSE)
10.1082
20.0053

Results

🎯 Predictions vs Actual

Predicted prices (red dashed) overlaid against actual prices (green) across the held-out test set. The shaded region shows the prediction error band.

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages