Advanced Stock Price Prediction App
This is a sophisticated Python-based web application that pulls stock data using yfinance and performs time series analysis using various advanced models (ARIMA, SARIMA, GARCH, and LSTM) to predict future stock prices. The backend is built with FastAPI, and the frontend is developed using HTML, CSS, and JavaScript with Chart.js for visualization.
Table of Contents
- Project Setup
- Running the Application
- Using the Application
- Project Structure
- Technologies Used
- Time Series Models
- Key Features
- Recent Improvements
- Future Enhancements
Project Setup
-
Clone the repository:
git clone https://github.com/yourusername/advanced-stock-prediction-app.git
cd advanced-stock-prediction-app
-
Create and activate a virtual environment:
python -m venv venv
source venv/bin/activate # On Windows, use: venv\Scripts\activate
-
Install the required dependencies:
pip install -r requirements.txt
Note: Make sure to use the latest requirements.txt file, as it has been updated to include all necessary dependencies and resolve potential conflicts.
Running the Application
-
Start the FastAPI backend:
uvicorn main:app --reload
The API will be available at http://127.0.0.1:8000.
-
Open the frontend:
Navigate to the frontend directory and open index.html in your web browser.
Using the Application
- Enter a stock symbol (e.g., AAPL for Apple Inc.) in the input field.
- Select a start date and end date for historical data.
- Choose a prediction model (ARIMA, SARIMA, GARCH, or LSTM).
- Click the “Predict” button to fetch historical data and generate predictions.
- The chart will display historical stock prices and predictions for the next 30 days.
- Below the chart, you’ll find information about the selected model and prediction results.
Project Structure
main.py: FastAPI backend application
requirements.txt: Python dependencies
frontend/
index.html: Main HTML file for the frontend
styles.css: CSS styles for the frontend
script.js: JavaScript for handling user interactions and API calls
Technologies Used
- Backend:
- Python
- FastAPI
- yfinance
- pandas
- numpy
- statsmodels (ARIMA, SARIMA)
- arch (GARCH)
- scikit-learn
- TensorFlow/Keras (LSTM)
- Frontend:
- HTML5
- CSS3
- JavaScript
- Chart.js
Time Series Models
- ARIMA (AutoRegressive Integrated Moving Average):
A statistical method for time series forecasting that combines autoregression, differencing, and moving average components.
- SARIMA (Seasonal ARIMA):
An extension of ARIMA that supports seasonal time series data, capturing both trend and seasonal components.
- GARCH (Generalized AutoRegressive Conditional Heteroskedasticity):
Used to model time series where the variance of the error term is not constant, making it particularly useful for financial time series with volatility clustering.
- LSTM (Long Short-Term Memory):
A type of recurrent neural network capable of learning long-term dependencies, making it well-suited for time series prediction tasks.
Key Features
- Multiple Prediction Models: The application supports four different time series models (ARIMA, SARIMA, GARCH, and LSTM) for stock price prediction.
- Interactive User Interface: The frontend provides an intuitive interface for users to input stock symbols, select date ranges, and choose prediction models.
- Data Visualization: Historical and predicted stock prices are visualized using Chart.js, providing a clear representation of the data and predictions.
- Caching Mechanism: The backend implements a simple in-memory cache to improve performance for frequently requested stocks. Cached results are valid for one hour.
- Comprehensive Error Handling: Both frontend and backend incorporate error handling to provide informative feedback to users in case of issues.
- Responsive Design: The frontend is designed to be responsive, ensuring a good user experience across different device sizes.
- Well-Commented Codebase: All major components of the application (backend, frontend HTML, JavaScript, and CSS) are thoroughly commented, making it easier for developers to understand and maintain the code.
Recent Improvements
- Enhanced GARCH Model: Improved error handling and prediction generation for the GARCH model to ensure more reliable results.
- LSTM Model Upgrade: Updated the LSTM model to generate a full 30-day forecast, providing more comprehensive predictions.
-
ARIMA and SARIMA Model Enhancements:
- Implemented more robust error handling for ARIMA and SARIMA models.
- Added warning suppression to handle potential warnings during model fitting.
- Improved exception handling to provide more informative error messages for these models.
- Dependency Management: Updated the requirements.txt file to resolve potential conflicts and ensure compatibility across all dependencies.
- Error Handling: Implemented more specific error messages for different types of exceptions, improving debugging and user feedback.
- Code Documentation: Added comprehensive comments throughout the codebase to enhance readability and maintainability.
Future Enhancements
- Implement model parameter optimization to improve prediction accuracy.
- Add more advanced charting options and technical indicators for better data analysis.
- Implement user authentication and the ability to save favorite stocks.
- Provide model comparison and ensemble methods to leverage the strengths of different prediction models.
- Integrate real-time data updates for live stock price tracking.
- Implement a more robust caching system, potentially using Redis or a similar technology.
- Add unit tests and integration tests to ensure code reliability and ease of maintenance.
-
Implement a backend database to store historical predictions and user preferences
[… Keep the existing content up to the “Deploying to Heroku” section …]
Deploying to Heroku
To deploy this application to Heroku, follow these steps:
- Make sure you have a Heroku account and the Heroku CLI installed.
-
Log in to Heroku CLI:
-
Create a new Heroku app:
heroku create your-app-name
-
Set the Python buildpack:
heroku buildpacks:set heroku/python
-
Choose one of the following deployment methods:
Option A: Deploy from the main branch (recommended for production)
Option B: Deploy directly from the dev branch
-
Scale the web dyno:
-
Open the deployed application in your browser:
Note: Make sure all the necessary files (Procfile, runtime.txt, requirements.txt) are present and committed in your current branch before deploying to Heroku.
Feel free to contribute to this project by submitting pull requests or opening issues for any bugs or feature requests.