This project provides tools for fetching stock data, preprocessing it, and training a model to predict future stock prices using LSTM neural networks.
- Fetch historical stock data using the
yfinancelibrary. - Store stock data in a PostgreSQL database.
- Preprocess the data and scale it for training.
- Build and train an LSTM model to predict stock prices.
- Python 3.x
yfinancepsycopg2numpypandasscikit-learntensorflow- PostgreSQL
-
Clone the repository:
git clone https://github.com/KunshArora912/Stock-Prediction.git cd stock-price-prediction-tool -
Install the required packages:
pip install yfinance psycopg2 numpy pandas scikit-learn tensorflow
-
Set up your PostgreSQL database and update the
db_configdictionary indata_fetch.pywith your database credentials. Make sure to change the PostgreSQL password.
- Run the
data_fetch.pyscript to fetch and store stock data in the PostgreSQL database:python data_fetch.py
- Preprocess the stock data using the functions provided in
data_preprocess.py. These functions scale the data and create datasets suitable for training the LSTM model.
- Run the
model_train.pyscript to train the LSTM model and predict stock prices:python model_train.py
fetch_stock_data(ticker, start, end): Fetches historical stock data for the given ticker between the specified dates.store_stock_data(ticker, data): Stores the fetched stock data in the PostgreSQL database.fetch_and_store_stock_data(ticker, start, end): Combines the fetch and store operations.
preprocess_data(data): Scales the 'Close' prices of the stock data.create_dataset(data, time_step=60): Creates datasets for training the LSTM model, based on a specified time step.
build_model(input_shape): Builds and compiles the LSTM model.process_stock(ticker, start, end): Fetches, preprocesses, and trains the model on the stock data. Predicts the stock price for the given ticker.
- Create a PostgreSQL database named
stock_data. - Create a table named
stock_priceswith the following structure:CREATE TABLE stock_prices ( ticker VARCHAR(10), date DATE, open FLOAT, high FLOAT, low FLOAT, close FLOAT, volume BIGINT, PRIMARY KEY (ticker, date) );
Contributions are welcome! Please open an issue or submit a pull request for any improvements or bug fixes.