Skip to content

teckgeekz/lstm-prediction

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

10 Commits
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿง  Stocks / Cryprocurrency Price Prediction โ€” Production LSTM Model

Python TensorFlow Keras scikit-learn License Status

Built by Teckgeekz ยท Advanced AI & Machine Learning Engineering

End-to-end deep learning pipeline for cryptocurrency or Stocks price forecasting using stacked LSTM networks

Inspired by Stock-Prediction-using-Transformer-NN

Google Colab link


๐Ÿ“Œ Overview

This repository contains a production-grade LSTM model developed by Teckgeekz for forecasting SOL-USD (Solana) prices across multiple time horizons. The pipeline spans data ingestion, feature engineering, model training, evaluation, and future forecasting โ€” built with software engineering best practices throughout.

Forecast horizons: 1-day ยท 3-day ยท 7-day ahead predictions with 95% confidence intervals


โœจ Key Features

Capability Details
๐Ÿ“ฅ Resilient data ingestion yfinance with 3-attempt retry + exponential backoff
๐Ÿ”ง Feature engineering Returns, MA-7, MA-14, 14-day volatility, RSI(14)
๐Ÿ—๏ธ Stacked LSTM architecture 128 โ†’ 64 units with BatchNorm + Dropout regularisation
๐Ÿ›‘ Early stopping restore_best_weights=True, patience = 20
๐Ÿ“‰ LR scheduling ReduceLROnPlateau (factor 0.5, patience 10)
๐Ÿ“Š Multi-metric evaluation MAE ยท RMSE ยท MAPE ยท Rยฒ
๐Ÿ”ฎ Multi-horizon forecasting 1, 3, 7 days ahead with 95% CI
๐Ÿ“ˆ Rich visualisations Training curves, actual vs predicted, error fills

๐Ÿ›๏ธ Model Architecture

Input: (batch, 14 timesteps, 1 feature)
โ”‚
โ”œโ”€ LSTM(128)  โ†’  BatchNorm  โ†’  Dropout(0.2)
โ”œโ”€ LSTM(64)   โ†’  BatchNorm  โ†’  Dropout(0.2)
โ”œโ”€ Dense(32, relu)          โ†’  Dropout(0.1)
โ”œโ”€ Dense(16, relu)
โ””โ”€ Dense(1)  โ† price output

Optimiser: Adam (lr = 0.001) ยท Loss: MSE ยท Epochs: up to 200 (early stopped) ยท Batch size: 16


๐Ÿ”ฌ ML Pipeline

Raw OHLCV data (yfinance)
        โ”‚
        โ–ผ
  Feature Engineering
  (Returns ยท MA-7 ยท MA-14 ยท Volatility ยท RSI)
        โ”‚
        โ–ผ
  MinMaxScaler  [0, 1]
        โ”‚
        โ–ผ
  Train / Test split  (80 / 20)
        โ”‚
        โ–ผ
  Sliding window sequences  (lookback = 14)
        โ”‚
        โ–ผ
  Stacked LSTM training
  + EarlyStopping + ReduceLROnPlateau
        โ”‚
        โ–ผ
  Evaluation  (MAE ยท RMSE ยท MAPE ยท Rยฒ)
        โ”‚
        โ–ผ
  Future forecasts  (1d ยท 3d ยท 7d)
  + 95% Confidence Intervals

๐Ÿ“‚ Repository Structure

sol-lstm-prediction/
โ”‚
โ”œโ”€โ”€ Stock_Prediction_Production.ipynb   # Main training notebook
โ”œโ”€โ”€ README.md                           # This file
โ”œโ”€โ”€ requirements.txt                    # Python dependencies
โ””โ”€โ”€ outputs/
    โ”œโ”€โ”€ training_curves.png             # Loss & MAE plots
    โ””โ”€โ”€ predictions_vs_actual.png       # Test set overlay

โš™๏ธ Configuration

All hyperparameters are centralised at the top of the notebook for easy experimentation:

# Asset & data
STOCK          = 'SOL-USD'
LOOKBACK_DAYS  = 1095       # 3 years of history

# Sequence
N_STEPS        = 14         # 2-week lookback window
LOOKUP_STEPS   = [1, 3, 7]  # Forecast horizons

# Training
TRAIN_TEST_SPLIT  = 0.80
BATCH_SIZE        = 16
EPOCHS            = 200
VALIDATION_SPLIT  = 0.15

# Architecture
LSTM_UNITS_1   = 128
LSTM_UNITS_2   = 64
DENSE_UNITS    = 32
DROPOUT_RATE   = 0.2
LEARNING_RATE  = 0.001

๐Ÿš€ Getting Started

Prerequisites

  • Python 3.9+
  • pip or conda

Installation

# Clone the repository
git clone https://github.com/teckgeekz/lstm-prediction.git
cd lstm-prediction

# Install dependencies
pip install -r requirements.txt

Run

# Launch Jupyter
jupyter notebook Stock_Prediction_Production.ipynb

Or run all cells end-to-end in one shot:

jupyter nbconvert --to notebook --execute Stock_Prediction_Production.ipynb \
  --output executed_output.ipynb

Requirements

yfinance>=0.2.0
pandas>=2.0
numpy>=1.24
scikit-learn>=1.3
tensorflow>=2.13
matplotlib>=3.7
seaborn>=0.12
scipy>=1.11

๐Ÿ“Š Performance Evaluation

The notebook reports four metrics on the held-out test set:

Metric Formula Interpretation
MAE Mean |actual โˆ’ predicted| Dollar error on average
RMSE โˆšMean(actual โˆ’ predicted)ยฒ Penalises large errors more heavily
MAPE Mean |actual โˆ’ predicted| / actual ร— 100 Scale-independent % error
Rยฒ 1 โˆ’ SS_res / SS_tot Variance explained (1.0 = perfect)

Quality thresholds used by the model:

MAPE < 5%   โ†’ โœ… Excellent  โ€” production ready
MAPE < 10%  โ†’ โœ… Good       โ€” use with regular monitoring
MAPE < 20%  โ†’ โš ๏ธ  Acceptable โ€” consider architecture changes
MAPE โ‰ฅ 20%  โ†’ โŒ Poor       โ€” retrain or redesign

๐Ÿ”ฎ Sample Output

======================================================================
FUTURE PRICE PREDICTIONS FOR SOL-USD
======================================================================

Day  1 (2025-05-04):
  Predicted: $148.32
  Range (95% CI): $141.18 - $155.46

Day  3 (2025-05-06):
  Predicted: $151.07
  Range (95% CI): $143.93 - $158.21

Day  7 (2025-05-10):
  Predicted: $155.44
  Range (95% CI): $148.30 - $162.58

======================================================================
Current Price: $146.88
======================================================================

๐Ÿงฉ Technical Highlights

Stacked LSTM with Regularisation

Two-layer stacked LSTM (128 โ†’ 64 units) with BatchNormalisation and Dropout between each block prevents overfitting and stabilises training on the high-variance SOL-USD series.

Smart Training Callbacks

EarlyStopping with restore_best_weights=True automatically rolls back to the lowest-validation-loss checkpoint, eliminating the need for manual epoch tuning. ReduceLROnPlateau halves the learning rate when progress stalls, enabling fine convergence.

Uncertainty Quantification

Future predictions include 95% confidence intervals derived from the standard deviation of test-set residuals, giving a principled uncertainty band rather than a single point estimate.

Production-Grade Data Ingestion

Exponential backoff retry logic handles transient yfinance API failures, and explicit MultiIndex column handling ensures compatibility with the new yfinance โ‰ฅ 0.2.x API structure.


๐Ÿ—บ๏ธ Roadmap

  • Wire engineered features (RSI, volatility, MAs) into LSTM input as multi-channel time series
  • Implement iterative multi-step forecasting for 3- and 7-day horizons
  • Add walk-forward cross-validation for more robust metric estimates
  • Persist model with model.save() and scaler with joblib.dump()
  • Experiment with additional features: volume, on-chain metrics, BTC correlation
  • Containerise with Docker for reproducible deployment
  • REST API endpoint for real-time inference (FastAPI)

โš ๏ธ Disclaimer

This project is built for research and educational purposes only. Price predictions from any machine learning model carry inherent uncertainty and should not be used as the sole basis for financial decisions. Always apply risk management when trading.


๐Ÿค Contributing

Contributions, issues, and feature requests are welcome.

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/my-feature)
  3. Commit your changes (git commit -m 'Add my feature')
  4. Push to the branch (git push origin feature/my-feature)
  5. Open a Pull Request

๐Ÿ“„ License

Distributed under the MIT License. See LICENSE for details.


Made with precision by Teckgeekz

Building production-grade AI systems ยท Deep Learning ยท Time Series ยท Quantitative Finance

โญ Star this repo if you found it useful

About

End-to-end deep learning pipeline for cryptocurrency or Stocks price forecasting using stacked LSTM networks

Topics

Resources

Stars

5 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors