Stock Price Analysis and Prediction
Overview
This project presents a comprehensive approach to Stock Price Analysis and Prediction, encompassing:
- Data Retrieval:
- Fetching historical stock data using the Financial Modeling Prep (FMP) API.
- Technical Indicator Computation:
- Calculating various technical indicators to analyze stock trends and volatility.
- Machine Learning Models:
- Implementing models such as Linear Regression, Random Forest, and Long Short-Term Memory (LSTM) networks for stock price prediction.
Features
Data Retrieval:
- Financial Modeling Prep (FMP) API Integration:
- Utilizes FMP’s free API to obtain real-time and historical stock data, supporting over 25,000 stocks across multiple exchanges.
Technical Indicator Computation:
- Moving Averages:
- Calculates Simple Moving Averages (SMA) and Exponential Moving Averages (EMA) over specified windows to identify price trends.
- Relative Strength Index (RSI):
- Computes RSI to assess the magnitude of recent price changes and evaluate overbought or oversold conditions.
- Moving Average Convergence Divergence (MACD):
- Measures the relationship between two EMAs to identify bullish or bearish momentum.
- Bollinger Bands:
- Calculates Bollinger Bands to analyze price volatility and potential overbought or oversold signals.
- Volatility Metrics:
- Determines historical volatility and Average True Range (ATR) to gauge market fluctuations.
- On-Balance Volume (OBV):
- Computes OBV to relate price changes to trading volume, aiding in trend confirmation.
Machine Learning Models:
- Linear Regression:
- Implements linear regression to model the relationship between lagged closing prices and predict future prices.
- Random Forest Regressor:
- Utilizes ensemble learning to enhance prediction accuracy and manage overfitting.
- Long Short-Term Memory (LSTM) Networks:
- Applies LSTM networks to capture temporal dependencies in sequential stock price data for improved forecasting.
How It Works
- Data Retrieval:
- The
stock_historyfunction fetches historical stock data for a specified ticker, year, and month using the FMP API.
- The
- Technical Indicator Computation:
- Functions like
calculate_moving_averages,calculate_rsi, andcalculate_macdcompute respective technical indicators using thetalibrary.
- Functions like
- Machine Learning Modeling:
- The
prepare_datafunction structures the data with lag features for regression models. - The
train_predict_lr_rffunction trains and evaluates Linear Regression and Random Forest models. - The
prepare_lstm_dataandtrain_predict_lstmfunctions handle data preparation and training for LSTM networks.
- The
- Prediction and Evaluation:
- Models are evaluated using metrics such as Root Mean Squared Error (RMSE) to assess prediction accuracy.
Technologies
- requests
- BeautifulSoup
- ta (Technical Analysis Library)
- scikit-learn
- TensorFlow