A modern Django web application for analyzing and comparing stock performance using machine learning predictions.
- Interactive Stock Analysis: Compare two stocks with real-time data from Yahoo Finance
- Machine Learning Predictions: K-Nearest Neighbors (KNN) algorithm for price predictions
- Beautiful Visualizations: Interactive charts using Plotly
- Statistical Analysis: Comprehensive statistics and metrics
- Modern UI: Responsive design with Bootstrap 5
- Model Performance Metrics: MAE, MSE, RMSE, and MAPE calculations
- Backend: Django 4.2.7
- Data Analysis: Pandas, NumPy, Scikit-learn
- Stock Data: yfinance
- Visualizations: Plotly
- Frontend: Bootstrap 5, Font Awesome
- Machine Learning: K-Nearest Neighbors Regressor
-
Clone the repository:
git clone <repository-url> cd stock-analyzer
-
Create a virtual environment:
python -m venv venv
-
Activate the virtual environment:
- Windows:
venv\Scripts\activate
- macOS/Linux:
source venv/bin/activate
- Windows:
-
Install dependencies:
pip install -r requirements.txt
-
Run database migrations:
python manage.py makemigrations python manage.py migrate
-
Create a superuser (optional):
python manage.py createsuperuser
-
Run the development server:
python manage.py runserver
-
Open your browser and navigate to
http://127.0.0.1:8000/
- Enter Stock Tickers: Input two stock ticker symbols (e.g., AAPL, GOOGL, MSFT)
- Click Analyze: The application will fetch real-time data and perform analysis
- View Results:
- Current and predicted prices
- Statistical analysis
- Interactive charts
- Model performance metrics
- Fetches 90 days of historical data from Yahoo Finance
- Handles missing data with imputation
- Performs comprehensive statistical analysis
- Uses K-Nearest Neighbors (KNN) algorithm
- Standardizes data for better predictions
- Calculates multiple performance metrics
- Line Chart: Stock price comparison over time
- Scatter Plot: Price distribution analysis
- Histogram: Price frequency distribution
- Correlation Heatmap: Relationship between stocks
- MAE (Mean Absolute Error): Average absolute difference between predicted and actual values
- MSE (Mean Squared Error): Average squared difference
- RMSE (Root Mean Squared Error): Square root of MSE
- MAPE (Mean Absolute Percentage Error): Average percentage error
stock_analyzer/
├── manage.py
├── requirements.txt
├── README.md
├── stock_analyzer/ # Main Django project
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ ├── wsgi.py
│ └── asgi.py
├── stock_analysis/ # Django app
│ ├── __init__.py
│ ├── admin.py
│ ├── apps.py
│ ├── models.py
│ ├── urls.py
│ └── views.py
├── templates/
│ └── stock_analysis/
│ └── home.html
└── static/ # Static files (CSS, JS)
GET /
: Home page with analysis formPOST /analyze/
: Analyze stocks and return JSON results
Access the Django admin interface at http://127.0.0.1:8000/admin/
to:
- View analysis history
- Monitor model performance
- Manage stock analysis records
- New Analysis Methods: Add functions in
views.py
- Additional Charts: Extend the
create_charts()
function - Different ML Models: Modify
train_models_and_predict()
- Modify CSS in
templates/stock_analysis/home.html
- Add custom Bootstrap classes
- Update color scheme in CSS variables
-
Import Errors: Ensure all dependencies are installed
pip install -r requirements.txt
-
Database Errors: Run migrations
python manage.py makemigrations python manage.py migrate
-
Stock Data Issues: Check internet connection and ticker symbols
-
Chart Display Issues: Ensure JavaScript is enabled in browser
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
This project is licensed under the MIT License.
- Yahoo Finance for stock data
- Django community for the excellent framework
- Plotly for interactive visualizations
- Bootstrap for responsive design