This project involves using Convolutional Neural Networks (CNNs) to forecast stock prices and predict price movements. The main objectives are to build a CNN model for stock price prediction, evaluate its performance, and visualize results through various metrics and plots.
-
analysis_notebook.ipynb
: Jupyter Notebook that walks through the entire workflow, including data fetching, model training, evaluation, and visualization. -
lib/
: A directory containing utility modules for metrics, visualization, data preparation, and model building.metrics.py
: Contains functions to compute evaluation metrics like mean percentage error, confusion matrix, F1 score, and accuracy score.visualization.py
: Provides functions for plotting predictions, training history, mean percentage error, and price movements.data_preparation.py
: Includes functions for fetching and preparing stock data for model input.model_building.py
: Contains functions for building and compiling the CNN model, as well as setting up callbacks for training.
Ensure you have the following Python packages installed:
numpy
pandas
yfinance
tensorflow
plotly
scikit-learn
You can install the required packages using pip:
pip install numpy pandas yfinance tensorflow plotly scikit-learn
The data_preparation.py
module provides the load_data()
function to download historical stock data for a given ticker symbol and date range. The prepare_data()
function prepares this data for model training by creating windows of stock prices and normalizing the input features.
Use the build_model()
and get_callbacks()
functions from the model_building.py
module to create a CNN model tailored for stock price prediction and define its callbacks for efficient learning. The model includes convolutional layers, pooling layers, and dense layers, as for the callbacks we use the Early Stopping and Learning Rate Reduction.
The analysis_notebook.ipynb
notebook demonstrates how to:
- Fetch and prepare stock data using the
data_preparation.py
functions. - Build and train the CNN model using the
model_building.py
functions. - Evaluate the model using the
metrics.py
functions:mean_percentage_error()
to compute the MPE between actual and predicted prices.binary_evaluation_metrics()
to compute the confusion matrix, F1 score, and accuracy score.
- Visualize results with the
visualization.py
functions.
The visualization.py
module provides several functions to visualize the model's performance:
plot_predictions()
: Shows actual vs. predicted stock prices.plot_price_moves()
: Displays actual and predicted price movements.plot_mean_percentage_error()
: Illustrates the mean percentage error between actual and predicted prices.plot_training_history()
: Plots training and validation loss over epochs.plot_monitoring_dashboard()
: Builds the performance monitoring dashboard of the model.
To see the project in action, open the analysis_notebook.ipynb
notebook and follow the steps outlined to train and evaluate the model.
Feel free to submit issues or pull requests to enhance the project.
This project is licensed under the MIT License - see the LICENSE file for details.
Thank you for visiting my GitHub profile! Feel free to reach out if you have any questions or opportunities to collaborate. Let's connect and explore new possibilities together!