AmirhosseinHonardoust
Stock-LSTM-Forecasting
Python

Predict stock prices using LSTM networks in PyTorch. This project covers data preprocessing, sliding window creation, model training with early stopping, and evaluation with RMSE/MAE/MAPE. Includes visualizations of training loss, predicted vs actual prices, and short-horizon forecasts.

Last updated Jun 20, 2026
41
Stars
0
Forks
0
Issues
0
Stars/day
Attention Score
59
Language breakdown
No language data available.
Files click to expand
README

Stock Price Prediction with LSTM

Stock Price Prediction with LSTM is a hands-on deep learning project that demonstrates how sequential models can be applied to real-world financial data. Using historical OHLCV (Open, High, Low, Close, Volume) data, the project builds and trains an LSTM network to capture time-dependent patterns in stock movements.

The pipeline handles everything from preprocessing and sliding-window dataset creation to model training with early stopping and evaluation. The results are presented with intuitive visualizations — training and validation loss curves, predicted vs. actual stock prices, and short-horizon forecasts into the future. Metrics such as RMSE, MAE, and MAPE provide quantitative insight into performance.

This project serves as both a learning tool and a portfolio-ready showcase of time-series forecasting, deep learning, and financial modeling with PyTorch.


Features

  • Load stock data from CSV or fetch with Yahoo Finance (via yfinance)
  • Preprocessing: scaling & sliding window dataset creation
  • LSTM model with dropout and Adam optimizer
  • Metrics: RMSE, MAE, MAPE
  • Plots:
- Training & validation curves - Predicted vs actual prices - Short-horizon future forecast
  • Saved artifacts: best_lstm.pt, scaler.pkl, metrics.json

Project Structure

stock-lstm-forecasting/
├─ README.md
├─ LICENSE
├─ requirements.txt
├─ data/
│  ├─ fetch_yfinance.py      # Fetch data from Yahoo Finance
│  └─ aapl.csv               # Stock dataset (real or synthetic)
├─ src/
│  ├─ trainlstmstock.py    # Training script
│  ├─ evaluate.py            # Evaluation script
│  └─ utils.py               # Helpers (scaling, metrics, windowing)
└─ outputs/
   ├─ best_lstm.pt
   ├─ scaler.pkl
   ├─ metrics.json
   ├─ training_curves.png
   ├─ predictedvsactual.png
   └─ future_forecast.png

Setup

python -m venv .venv

Windows

.venv\Scripts\activate

Linux/macOS

source .venv/bin/activate

pip install -r requirements.txt


Fetch Data (optional)

# downloads daily OHLCV for AAPL (Jan 2015 → today)
python data/fetch_yfinance.py --ticker AAPL --start 2015-01-01 --out data/aapl.csv

Or use the included synthetic dataset (data/aapl.csv).


Train the Model

python src/trainlstmstock.py --input data/aapl.csv --column close     --lookback 60 --epochs 25 --batch-size 64 --outdir outputs --horizon 1 --seed 42

Evaluate the Model

python src/evaluate.py --input data/aapl.csv --model outputs/best_lstm.pt     --column close --lookback 60 --horizon 1 --outdir outputs

Results

Training & Validation Loss

training_curves


Predicted vs Actual

predicted<em>vs</em>actual


Short-Horizon Forecast

future_forecast


Metrics (metrics.json):

{   "rmse": 3.59,   "mae": 3.59,   "mape": 1.97 }


Recommendations

  • Train longer (50–100 epochs) for improved stability
  • Try multi-step forecasts (--horizon 5 or --horizon 30)
  • Experiment with other assets (e.g., MSFT, GOOGL, TSLA)
  • Add more features (Volume, technical indicators)
🔗 More in this category

© 2026 GitRepoTrend · AmirhosseinHonardoust/Stock-LSTM-Forecasting · Updated daily from GitHub