A simplified two-file system for training LSTM neural networks to predict stock prices with epoch-based training and accuracy visualization.
# Install dependencies
pip install -r requirements.txtStep 1: Train a Model
python train_model.py- Downloads 5 years of stock data
- Engineers 20+ technical indicators
- Trains LSTM neural network for 50 epochs
- Shows training curves and accuracy plots
- Saves trained model and scaler
Step 2: Make Real-Time Predictions
python predict_realtime.py- Loads your trained model
- Downloads recent data
- Makes 5-day predictions
- Provides buy/sell/hold recommendations
- Shows prediction chart
- LSTM Neural Network: 3-layer LSTM with dropout
- Sequence Learning: Uses 60 days to predict next day
- Epoch Training: Shows training progress over 50 epochs
- Validation: 80/20 train-test split with time series validation
- Moving Averages: SMA and EMA (5, 10, 20, 50 days)
- Momentum: RSI, MACD, MACD Signal
- Volatility: Bollinger Bands, Price volatility
- Volume: Volume ratios and trends
- Price Action: Returns, high-low percentages
- Training Curves: Loss over epochs
- Actual vs Predicted: Full time series comparison
- Recent Performance: Last 100 days zoom-in
- Error Distribution: Prediction accuracy analysis
- Future Predictions: 5-day forecast chart
- Buy/Sell/Hold: Based on predicted price movement
- Confidence Scoring: Multi-factor confidence calculation
- Technical Analysis: RSI, Bollinger Bands, Volume confirmation
- Risk Assessment: Accuracy metrics and error bounds
{SYMBOL}_lstm_model.h5- Trained LSTM model{SYMBOL}_scaler.pkl- Feature scaler for predictionsmodel_analysis.png- Training and accuracy plots
{SYMBOL}_prediction.png- Future price prediction chart- Console output with recommendations and confidence
- Data Collection: Downloads 5 years of stock data directly from Yahoo Finance
- Feature Engineering: Creates 20+ technical indicators
- Sequence Preparation: Creates 60-day sequences for LSTM training
- Model Training: Trains 3-layer LSTM for 50 epochs
- Evaluation: Tests on unseen data and creates accuracy plots
- Model Saving: Saves trained model and scaler for predictions
- Model Loading: Loads trained LSTM model and scaler
- Recent Data: Downloads last 6 months of data
- Feature Engineering: Applies same features as training
- Prediction: Forecasts next 5 days of prices
- Analysis: Generates buy/sell recommendations
- Visualization: Creates prediction chart
Input: 60 days × 20 features
↓
LSTM Layer (100 units) + Dropout
↓
LSTM Layer (100 units) + Dropout
↓
LSTM Layer (50 units) + Dropout
↓
Dense Layer (25 units)
↓
Output: Next day price
The system provides multiple accuracy measures:
- MSE/MAE/RMSE: Standard regression metrics
- Percentage Accuracy: Within 1% and 5% thresholds
- Directional Accuracy: Predicting up/down movement
- Confidence Scoring: Multi-factor confidence assessment
🎯 RECOMMENDATION FOR AAPL
==================================================
🚀 STRONG BUY
💰 Current Price: $185.25
🎯 Target Price: $195.80
📊 Expected Return: +5.69%
🎲 Confidence: 78.5%
📊 Technical Indicators:
RSI: 45.2 (Neutral)
BB Position: 0.65 (Middle Range)
Volume Ratio: 1.34x (Normal)
- Python 3.8+
- TensorFlow 2.13+
- Internet connection for data download
- Sufficient disk space for model files
- Yahoo Finance API: Direct HTTP requests (no API key needed)
- 5-Year History: Comprehensive training data
- Daily Resolution: End-of-day prices and volumes
- Price Features: Returns, volatility, price changes
- Technical Indicators: RSI, MACD, Bollinger Bands
- Moving Averages: Multiple timeframes
- Volume Analysis: Volume ratios and trends
- Normalization: MinMax scaling for neural network
- Sequence Length: 60 days
- Batch Size: 32
- Epochs: 50
- Optimizer: Adam
- Loss Function: Mean Squared Error
- Validation: 20% holdout
-
TensorFlow Installation
pip install tensorflow>=2.13.0 -
Data Download Errors
- Check internet connection
- Verify stock symbol is valid
- Try again later if rate limited
-
Model Loading Errors
- Ensure you've trained a model first
- Check file permissions
- Verify model files exist
-
Memory Issues
- Reduce sequence length if needed
- Use smaller batch size
- Close other applications
This system demonstrates:
- Time Series Prediction: Using historical sequences
- Deep Learning: LSTM neural networks
- Feature Engineering: Technical indicator creation
- Model Evaluation: Comprehensive accuracy testing
- Financial Analysis: Stock market prediction techniques
This software is for educational purposes only. Stock predictions are inherently uncertain and should not be used for actual trading without proper risk management. Always consult financial advisors before making investment decisions.