This project implements a stock price prediction model using Long Short-Term Memory (LSTM) neural networks. It fetches historical stock data from the Alpha Vantage API, preprocesses it, trains an LSTM model, and predicts future stock prices with confidence intervals.
- Fetch historical stock data using Alpha Vantage API
- Preprocess data with MinMaxScaler
- Train LSTM neural network model using TensorFlow
- Visualize actual vs predicted prices
- Save and load trained models
- Predict next day's closing price with confidence level
├── src/
│ ├── config.py # Configuration settings
│ ├── data_fetcher.py # Alpha Vantage API data fetching
│ ├── data_processor.py # Data preprocessing and scaling
│ ├── model.py # LSTM model implementation
│ ├── visualizer.py # Visualization utilities
│ └── main.py # Main script for training and prediction
├── models/ # Directory for saved models
├── visualizations/ # Directory for saved visualizations
├── .env.example # Example environment variables file
└── requirements.txt # Project dependencies
- Clone the repository:
git clone <repository-url>
cd AI-Stock-Prediction- Install the required dependencies:
pip install -r requirements.txt- Create a
.envfile with your Alpha Vantage API key:
cp .env.example .envThen edit the .env file and add your Alpha Vantage API key:
ALPHA_VANTAGE_API_KEY=your_api_key_here
You can get a free API key from Alpha Vantage.
To train a model for a specific stock symbol:
python -m src.main --symbol AAPL --trainThis will:
- Fetch historical data for Apple (AAPL)
- Preprocess the data
- Train an LSTM model
- Save the model to the
models/directory - Generate visualizations in the
visualizations/directory
To predict the next day's stock price:
python -m src.main --symbol AAPL --predictIf no model exists for the specified symbol, it will first train a model and then make a prediction.
You can also use the prediction function in your own code:
from src.main import predict_stock_price
# Predict next day's price for a stock
result = predict_stock_price('AAPL')
print(f"Predicted Price: ${result['prediction']}")
print(f"Confidence Level: {result['confidence_level']}%")You can customize the model parameters in src/config.py:
SEQUENCE_LENGTH: Number of previous days to use for predictionTRAIN_TEST_SPLIT: Percentage of data to use for trainingBATCH_SIZE: Batch size for trainingEPOCHS: Number of training epochsLEARNING_RATE: Learning rate for the optimizer
- The model's accuracy depends on the quality and quantity of historical data
- Stock prices are influenced by many external factors that may not be captured in historical data
- The Alpha Vantage API has rate limits for free tier users
This project is licensed under the MIT License - see the LICENSE file for details.