abhishekh14/stock-market-forecasting
๐ Stock Market Forecasting System
    
A production-ready Stock Market Forecasting System that fetches historical market data, performs Exploratory Data Analysis (EDA), trains multiple Machine Learning and Deep Learning models, compares their performance, and makes recursive price predictions for various horizons (7, 30, or 60 days).
๐ Key Features
- Real-Time Data Access: Fetches historical stock data dynamically from Yahoo Finance (
yfinance). - Technical Feature Engineering: Constructs Simple Moving Averages (20, 50, 100 days), Daily Returns, rolling Volatility, and multi-day price lags.
- Multiple Forecasting Engines:
- Linear Regression: Fast baseline regression.
- Random Forest Regressor: Handles complex non-linear relations.
- XGBoost Regressor: High-performance gradient boosted decision trees.
- LSTM Neural Network (TensorFlow/Keras): Captures deep sequential relationships.
- Auto-Selection: Compares models side-by-side using RMSE, MAE, and $R^2$, and auto-selects the best model.
- Interactive Dashboards:
- Plotly Candlestick charts with overlayed moving averages.
- Dynamic correlation heatmaps of indicators.
- Comparison of actual vs. predicted validation sets.
- Future price forecast charts with recursive day-by-day calculations.
- Data Export: Export future forecast predictions to CSV instantly.
๐ ๏ธ Tech Stack
- Frontend/Dashboard: Streamlit
- Data Preprocessing & Manipulation: Pandas, NumPy
- Visualizations: Plotly, Matplotlib, Seaborn
- Machine Learning: Scikit-Learn, XGBoost
- Deep Learning: TensorFlow / Keras (LSTM)
- Data Source: yfinance API
๐ Project Structure
Stock-Market-Forecasting/
โ
โโโ app.py # Main Streamlit Dashboard UI
โโโ requirements.txt # Package Dependencies
โโโ README.md # Project Documentation & HF metadata
โโโ .gitignore # Python Git Ignore Rules
โ
โโโ models/
โ โโโ best_model.pkl # Serialized best traditional ML model
โ โโโ best_model_info.pkl # Performance metadata and selected model name
โ โโโ scaler_X.pkl # Feature Min-Max Scaler
โ โโโ scaler_y.pkl # Target Min-Max Scaler
โ โโโ linear_regression.pkl # Serialized Linear Regression model
โ โโโ random_forest.pkl # Serialized Random Forest model
โ โโโ xgboost.pkl # Serialized XGBoost model
โ โโโ lstm.keras # Saved LSTM Keras neural network
โ
โโโ data/
โ โโโ stock_data.csv # Cached downloaded stock historical dataset
โ โโโ test_stock_data.csv # Cache for test verification run
โ
โโโ src/
โ โโโ data_loader.py # Yahoo Finance downloader
โ โโโ preprocessing.py # Scaling, splitting, and sequencing inputs
โ โโโ feature_engineering.py# Tech indicator builder and lagged feature generator
โ โโโ train_model.py # Pipelines for LR, RF, XGB, LSTM, & auto-selection
โ โโโ evaluate.py # Evaluation metrics: RMSE, MAE, Rยฒ
โ โโโ predict.py # Recursive day-by-day future forecasting
โ
โโโ notebooks/
โ โโโ 01_EDA.ipynb # Jupyter Notebook for exploratory analysis
โ โโโ 02_Model_Building.ipynb # Jupyter Notebook for model training & evaluation
โ
โโโ images/
โ โโโ dashboard.png # Screenshot mockup of the UI dashboard
โ
โโโ utils/
โโโ helper.py # UI helpers, Plotly charting, and API caches๐ Installation & Local Run
Follow these steps to run the application locally:
1. Clone the repository
git clone https://github.com/<username>/Stock-Market-Forecasting.git
cd Stock-Market-Forecasting2. Set up a virtual environment (Recommended)
python -m venv venv
# On Windows:
venv\Scripts\activate
# On macOS/Linux:
source venv/bin/activate3. Install dependencies
pip install -r requirements.txt4. Run the Streamlit Dashboard
streamlit run app.pyOpen your browser and navigate to http://localhost:8501 to view the system.
๐ Evaluation Metrics
All models are evaluated on a 20% out-of-sample chronological test set using the following metrics:
- Root Mean Squared Error (RMSE): Measures the average magnitude of the prediction error.
- Mean Absolute Error (MAE): Measures average absolute prediction error.
- Coefficient of Determination ($R^2$ Score): Measures how well unseen samples are likely to be predicted by the model.
๐ฎ Future Improvements
- Additional Features: Integrate sentiment analysis from Twitter/Reddit/Financial news APIs.
- Alternative Architectures: Add Transformer (Attention) models and Prophet for comparison.
- Hyperparameter Tuning: Introduce Bayesian Optimization for optimizing model hyperparameters.
- Order Simulation: Add a simple paper-trading simulator to test forecast strategies.
๐ License
This project is licensed under the MIT License - see the LICENSE file or standard MIT terms for details.
