Overview StableStockPredictor is a robust, production-ready deep learning model designed for predicting S&P 500 stock prices with a focus on numerical stability and high performance. Built using TensorFlow and Keras, the model leverages Long Short-Term Memory (LSTM) networks to capture temporal patterns in financial time series data. The implementation incorporates advanced techniques such as gradient clipping, robust scaling, and stable feature engineering to ensure reliable predictions even in volatile market conditions. This project is ideal for financial institutions, algorithmic trading firms, or data science teams looking for a scalable and stable solution for stock price forecasting. The codebase is modular, well-documented, and adheres to best practices for machine learning engineering, making it suitable for deployment in production environments. Features
Robust Data Preprocessing: Uses RobustScaler for feature scaling to handle outliers effectively, ensuring numerical stability during training and inference. Stable Feature Engineering: Includes carefully designed financial features like RSI, moving averages, and volatility, with safeguards against numerical issues such as division by zero or infinite values. LSTM-based Architecture: Employs a two-layer LSTM model with dropout for regularization and gradient clipping to prevent exploding gradients, ensuring stable training. Dynamic Learning: Integrates EarlyStopping and ReduceLROnPlateau callbacks to optimize training and prevent overfitting. Comprehensive Evaluation: Provides key performance metrics (MSE, RMSE, MAE, MAPE) and visualizations to assess model accuracy and performance. Production-Ready Code: Modular StableStockPredictor class with clean, reusable code following PEP 8 standards, suitable for integration into larger systems.
Installation Prerequisites
Python 3.8+ pip package manager Jupyter Notebook (optional, for running the .ipynb file)
Dependencies Install the required Python packages using the following command: pip install yfinance numpy pandas tensorflow scikit-learn matplotlib
Setup
Clone the repository: git clone https://github.com/your-username/stable-stock-predictor.git cd stable-stock-predictor
Install dependencies: pip install -r requirements.txt
Run the Jupyter Notebook or Python script: jupyter notebook DeepTradeNet-2.ipynb
Alternatively, convert the notebook to a Python script and run it: jupyter nbconvert --to script DeepTradeNet-2.ipynb python DeepTradeNet-2.py
Usage
Data Acquisition: The model automatically downloads 5 years of S&P 500 (^GSPC) data using the yfinance library. Feature Engineering: The calculate_stable_features method computes robust financial indicators (e.g., returns, log volume, moving averages, RSI, volatility) with checks for numerical stability. Data Preparation: The prepare_data method scales features and creates sequences for LSTM input, ensuring no NaN or infinite values. Model Training: The train method builds and trains the LSTM model with early stopping and learning rate reduction for optimal convergence. Prediction and Evaluation: After training, the model generates predictions, inverse-transforms them to the original price scale, and calculates performance metrics (MSE, RMSE, MAE, MAPE). Results are visualized using Matplotlib.
To run the full pipeline, execute the main() function in the notebook or script. The model will download data, train, and display predictions along with performance metrics and a plot of actual vs. predicted prices. Code Structure
StableStockPredictor Class: init: Initializes the model with a configurable sequence length (default: 60 days). calculate_stable_features: Computes robust financial features with safety checks. prepare_data: Prepares and scales data for LSTM input. build_model: Constructs the LSTM architecture with gradient clipping and stable initializers. train: Trains the model with early stopping and learning rate scheduling.
Main Function: Orchestrates data downloading, preprocessing, training, prediction, and evaluation.
Model Architecture The model uses a sequential LSTM architecture:
Input Layer: Accepts sequences of shape (sequence_length, n_features). LSTM Layer 1: 64 units, return_sequences=True, with Glorot normal initialization and dropout (0.2). LSTM Layer 2: 32 units with orthogonal recurrent initialization and dropout (0.2). Output Layer: Dense layer with linear activation for price prediction. Optimizer: Adam with gradient clipping (clipnorm=1.0) and Huber loss for robust regression.
Performance Metrics The model outputs the following metrics:
Mean Squared Error (MSE): Measures the average squared difference between predicted and actual prices. Root Mean Squared Error (RMSE): Provides the error in the same units as the stock price. Mean Absolute Error (MAE): Quantifies the average absolute difference between predictions and actuals. Mean Absolute Percentage Error (MAPE): Expresses the error as a percentage of actual prices.
A plot of actual vs. predicted prices is generated for visual inspection. Why This Project Stands Out
Numerical Stability: Features like gradient clipping, robust scaling, and safe feature calculations ensure the model performs reliably even with noisy financial data. Scalability: The modular design allows easy integration into larger trading systems or pipelines. Extensibility: The codebase can be extended to include additional features, alternative models (e.g., GRU, Transformer), or other financial datasets. Production Readiness: Adheres to best practices for code organization, error handling, and documentation, making it suitable for Tier 1 firm environments.
Future Improvements
Incorporate additional features like macroeconomic indicators or sentiment analysis from X posts. Experiment with advanced architectures like Transformer-based models for improved sequence modeling. Implement backtesting for trading strategy evaluation. Add support for real-time data streaming and prediction.
Contributing Contributions are welcome! Please follow these steps:
Fork the repository. Create a feature branch (git checkout -b feature/your-feature). Commit your changes (git commit -m 'Add your feature'). Push to the branch (`git push origin feature/your-feature'). Open a pull request.
License This project is licensed under the MIT License. See the LICENSE file for details. Contact For questions or collaboration opportunities, please reach out via GitHub Issues or contact the maintainer at [niharmaheshjani@gmail.com ].