This project implements a complete deep‑learning pipeline for stock closing price prediction using multiple neural network architectures (MLP, GRU, LSTM, Bi‑LSTM, 1D CNN, and CNN‑LSTM).
The main script (stock_price_prediction.py) loads historical OHLCV data from a CSV file, engineers features, scales and splits the data, trains all models, compares their performance, and saves plots and prediction results.
stock_price_prediction.py: End‑to‑end training and evaluation pipeline.nse_data.csv: Example input data file with historical stock prices (OHLCV).LSTM_test_predictions.csv: Example predictions output (from a previous run).saved_models/(created at runtime): Directory where trained models (.h5files) are saved.
Your input CSV must contain at least the following columns (case‑insensitive, partial matches allowed):
- Date
- Open
- High
- Low
- Close
- Volume
The script will:
- Sort data chronologically by
Date. - Drop rows with missing values.
- Engineer two additional features:
- HiLo = High − Low
- OpSe = Open − Close
- Use the following features for prediction:
High,Low,Open,Volume,HiLo,OpSe. - Predict the Close price.
Install Python dependencies (Python 3.8+ recommended):
pip install numpy pandas matplotlib scikit-learn tensorflowYou may optionally create and activate a virtual environment before installing dependencies.
Key user‑configurable parameters are defined near the top of stock_price_prediction.py:
CSV_PATH: Path to your input CSV file.- Example for running locally in this folder:
CSV_PATH = "nse_data.csv"
LOOKBACK: Number of past days used to build each input sequence (default: 60).TRAIN_RATIO,TEST_RATIO,VAL_RATIO: Proportions for splitting the dataset.EPOCHS,BATCH_SIZE,LEARNING_RATE: Training hyperparameters.MODEL_DIR: Output directory for saving trained models (default:saved_models).
Adjust these values as needed for your data size and hardware.
- Place your stock price CSV file in the project folder (or note its path).
- Open
stock_price_prediction.pyand updateCSV_PATHto point to your CSV (for example,"nse_data.csv"). - Install the required Python packages (see Dependencies section).
- From a terminal in the project directory, run:
python stock_price_prediction.pyTraining can take time depending on:
- Number of data points
- Model complexity
- Whether you have GPU acceleration available for TensorFlow
After a successful run, you should see:
- Model weights saved as
.h5files insaved_models/(one file per model type). model_mape_comparison.png: Bar chart comparing each model’s MAPE (on original price scale).<BEST_MODEL_NAME>_pred_vs_true.png: Plot of true vs. predicted closing prices on the test set.<BEST_MODEL_NAME>_test_predictions.csv: CSV containing:True_ClosePred_Close
The script also prints a summary of each model’s performance (MSE and MAPE in both scaled and original units) and reports which model performed best.
- For small datasets, consider:
- Reducing model complexity (fewer units/layers).
- Decreasing the number of epochs.
- You can experiment with:
- Different
LOOKBACKwindow sizes. - Alternative train/test/validation splits.
- Adding or removing engineered features.
- Different
This setup provides a strong baseline for comparing multiple deep‑learning architectures on stock price prediction tasks.