This project uses LSTM (Long Short-Term Memory) neural networks to predict stock prices based on historical data and forecast future trends.
- Fetches historical stock data using yfinance
- Preprocesses data using MinMaxScaler
- Implements LSTM neural network for time series prediction
- Provides performance metrics (RMSE and R² score)
- Visualizes predictions vs actual prices
- Future price predictions with confidence intervals
- Docker support for easy deployment
- Live code editing through volume mounts
Example prediction showing historical data, current predictions, and future forecast with confidence intervals:
- Clone this repository
- Install dependencies:
pip install -r requirements.txt- Build and start the container:
docker-compose up -d --build- View logs:
docker-compose logs -f- Stop the container:
docker-compose down- Build the image:
docker build -t stock-predictor .- Run the container:
docker run -d \
-v $(pwd)/data:/app/data \
-v $(pwd)/models:/app/models \
-v $(pwd)/app.py:/app/app.py \
-p 8501:8501 \
--name stock-predictor \
stock-predictorThe stock prediction model:
- Uses 60-day sequences for prediction
- LSTM architecture with two layers (50 units each)
- Dropout layers (0.2) for regularization
- Training/Testing split: 80%/20%
- Customizable training epochs (default: 50)
- Future prediction window: 30 days
- Confidence intervals based on prediction variance
- Trained models are saved in the
models/directory - Historical data is cached in the
data/directory - Both directories are mounted as Docker volumes for persistence
- app.py is mounted for live development
- Default stock: TSMC (2330.TW)
- Default start date: 2025-01-01
- End date: Current date
- Timezone: Asia/Taipei
- Stock symbol input (Taiwan/US markets)
- Adjustable date range
- Configurable sequence length and epochs
- Real-time visualization updates
- Future trend predictions with uncertainty ranges
The application displays:
- Training and Testing RMSE (Root Mean Square Error)
- R² scores for both training and testing sets
- Interactive plot with:
- Historical prices (blue line)
- Model predictions (red dashed line)
- Future predictions (with confidence intervals)
- Training loss history visualization
