Skip to content

Keshav-gehlot/AI-stock-prediction-

Repository files navigation

Stock Price Prediction Model using LSTM

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.

Features

  • 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

Project Structure

├── 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

Installation

  1. Clone the repository:
git clone <repository-url>
cd AI-Stock-Prediction
  1. Install the required dependencies:
pip install -r requirements.txt
  1. Create a .env file with your Alpha Vantage API key:
cp .env.example .env

Then 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.

Usage

Training a Model

To train a model for a specific stock symbol:

python -m src.main --symbol AAPL --train

This will:

  1. Fetch historical data for Apple (AAPL)
  2. Preprocess the data
  3. Train an LSTM model
  4. Save the model to the models/ directory
  5. Generate visualizations in the visualizations/ directory

Predicting Stock Prices

To predict the next day's stock price:

python -m src.main --symbol AAPL --predict

If no model exists for the specified symbol, it will first train a model and then make a prediction.

Using the Prediction Function

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']}%")

Customization

You can customize the model parameters in src/config.py:

  • SEQUENCE_LENGTH: Number of previous days to use for prediction
  • TRAIN_TEST_SPLIT: Percentage of data to use for training
  • BATCH_SIZE: Batch size for training
  • EPOCHS: Number of training epochs
  • LEARNING_RATE: Learning rate for the optimizer

Limitations

  • 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

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages