Stock Prediction Web Application Using Hidden Markov Model (HMM)
This project now features a Python React web application that automates the entire process of generating stock price predictions with a single click. Users can input a stock ticker and interval, and the application will automatically fetch data, preprocess, clean, train, test, and generate predictions using the newly integrated hmm_model.py
script. The UI has been significantly enhanced to display data through beautifully colored charts and simple, elegant components, including a chart for historic data and prediction models.
Financial forecasting is a critical task in investment and trading. Traders, investors, and financial analysts constantly seek effective tools and models to predict stock prices. This web application simplifies the process by automating data fetching, preprocessing, cleaning, training, testing, and predicting, making it accessible to users with varying levels of expertise. The addition of visually appealing charts and components enhances the user experience by providing a more intuitive and engaging way to view predictions and historical data.
The Hidden Markov Model (HMM)
Hidden Markov Models are a class of statistical models used to describe a system where the states are not directly observable but can be inferred from the observed data. In the context of stock price forecasting, HMMs can be employed to model the hidden states of the market, which can include various market conditions (e.g., bull, bear, or sideways markets).
Our project highlights include:
-
Automated Predictions: Users can generate all possible predictions with a single click, including data fetching, preprocessing, cleaning, training, testing, and predicting for given interval and stock ticker inputs.
-
Python React Web Application: A new web application developed using Python and React, allowing for a user-friendly interface and seamless interaction.
-
Integration of
hmm_model.py
: The application now utilizes thehmm_model.py
script, which is derived from theHMM_Nifty50.ipynb
notebook. This integration allows for a more robust and efficient prediction process. -
Empirical Evidence: We provide empirical evidence to demonstrate the effectiveness of the HMM in stock price forecasting, supported by the automated process.
-
Theoretical Foundations: We explore the theoretical foundations of Markov models, with a specific focus on Hidden Markov Models, to gain a deeper understanding of their application in financial forecasting.
-
Enhanced UI with Charts: The UI now displays data through beautifully colored charts and simple, elegant components. This includes a chart for historic data and different UI components to show predictions from various models elegantly.
The hmm_model.py
script has been significantly enhanced to include all possible HMM models and predictions from HMM_Nifty50.ipynb
, allowing for multiple predictions from a single function call. This new functionality enables the application to generate predictions using multiple HMM configurations, including different numbers of hidden states and covariance types.
To utilize the new capabilities of hmm_model.py
, a function named generate_all_predictions
has been added. This function iterates through multiple HMM configurations and generates predictions for each, providing a comprehensive analysis of potential market movements. The predictions are structured in a JSON response, making it easy to integrate with the app.py
make_prediction
route.
To set up and run the web application, follow these steps:
-
Install Dependencies:
Ensure you have the necessary Python libraries and Node.js installed. Run the following commands in your terminal:
pip install -r requirements.txt cd client npm install
-
Start the Backend Server:
Navigate back to the project root directory and run the Flask backend server:
python app.py
-
Launch the Web Application:
Open a new terminal window, navigate to the
client
directory, and start the React application:npm start
The web application will be accessible at
http://localhost:3000
.
To generate stock price predictions:
- Enter the stock ticker and interval in the provided input fields.
- Click the "Predict" button.
- The application will automatically fetch the data, process it using the
hmm_model.py
script, and display the predictions on the screen. This streamlined process ensures that all operations, including data fetching, preprocessing, and training, are executed with a single click. The predictions are now displayed through a line chart for historical data and bar charts for model predictions, providing a visually appealing and intuitive way to understand the data.
The hmm_model.py
script has been enhanced to include data fetching functionality, allowing for a more streamlined process from data fetching to prediction. This integration simplifies the application's operation, making it more efficient and user-friendly. Users can now enjoy a seamless experience, from data fetching to generating predictions, with just a single click.
The HMM_Nifty50.ipynb
notebook has been converted into a Python script, hmm_model.py
, to facilitate its integration into the web application. This conversion process involved extracting the logic from the notebook and implementing it in a structured script format, allowing for easier maintenance and scalability. The hmm_model.py
script now serves as the backbone for the prediction process in the application.
If you encounter any issues while setting up or using the web application, please ensure that all dependencies are correctly installed and that the Flask backend server is running. For further assistance, feel free to contact the author of this repository.
Author: Nikhil Kumar Patel