Skip to content

KomalGoel18/Stock-price-prediction

Repository files navigation

Stock Price Prediction

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.

Project structure

  • 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 (.h5 files) are saved.

Data requirements

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.

Dependencies

Install Python dependencies (Python 3.8+ recommended):

pip install numpy pandas matplotlib scikit-learn tensorflow

You may optionally create and activate a virtual environment before installing dependencies.

Configuration

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.

How to run

  1. Place your stock price CSV file in the project folder (or note its path).
  2. Open stock_price_prediction.py and update CSV_PATH to point to your CSV (for example, "nse_data.csv").
  3. Install the required Python packages (see Dependencies section).
  4. From a terminal in the project directory, run:
python stock_price_prediction.py

Training can take time depending on:

  • Number of data points
  • Model complexity
  • Whether you have GPU acceleration available for TensorFlow

Outputs

After a successful run, you should see:

  • Model weights saved as .h5 files in saved_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_Close
    • Pred_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.

Notes and tips

  • For small datasets, consider:
    • Reducing model complexity (fewer units/layers).
    • Decreasing the number of epochs.
  • You can experiment with:
    • Different LOOKBACK window sizes.
    • Alternative train/test/validation splits.
    • Adding or removing engineered features.

This setup provides a strong baseline for comparing multiple deep‑learning architectures on stock price prediction tasks.

About

Stock Price Prediction using Machine Learning — includes data preprocessing, feature engineering, model training, evaluation, and visualizations for forecasting market trends.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages