Skip to content

Arjun840/portfolio-optimization

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

20 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

PortfolioMax πŸ“ˆ

AI-Powered Portfolio Optimization for the Modern Investor

Python React FastAPI License

PortfolioMax is a sophisticated portfolio optimization platform that combines cutting-edge AI with institutional-grade portfolio theory to deliver optimal investment allocations. Built with modern technologies and featuring advanced machine learning algorithms, it provides real-time portfolio optimization, risk management, and performance analytics.

PortfolioMax Demo

πŸš€ Features

🧠 AI-Enhanced Portfolio Optimization

  • Machine Learning Predictions: Advanced ML models for enhanced return forecasting
  • Multiple Optimization Strategies: Maximum Sharpe ratio, minimum variance, and custom targets
  • Real-time Optimization: Instant portfolio optimization with live market data
  • Expected Performance: Achieving 2.69 average Sharpe ratio with 40.2% expected returns

πŸ›‘οΈ Advanced Risk Management

  • Multi-layered Constraints: Sophisticated constraint systems for risk control
  • Risk Tolerance Profiles: Conservative, Moderate, and Aggressive strategies
  • Drawdown Analysis: Maximum potential loss tracking (24.14% historical worst-case)
  • Cluster-based Diversification: 4-cluster asset allocation across 45+ instruments

🎯 Custom Portfolio Builder

  • Interactive Portfolio Construction: Build portfolios with real-time weight validation
  • Optimization Types: Improve, rebalance, or risk-adjust existing portfolios
  • Template Portfolios: Quick-start with Balanced, Growth, or Conservative templates
  • Core Holdings Preservation: Maintain key positions while optimizing around them

πŸ“Š Real-Time Analytics

  • Performance Tracking: Live portfolio performance vs benchmarks (+7.8% outperformance)
  • Efficient Frontier Visualization: Interactive risk-return analysis
  • Comprehensive Metrics: Sharpe ratio, volatility, max drawdown, and more
  • Portfolio Comparison: Side-by-side analysis of multiple strategies

πŸ’Ύ Portfolio Management

  • Save & Manage Portfolios: Persistent storage with names and descriptions
  • Portfolio History: Track creation and modification dates
  • Export Capabilities: Download optimization results and analytics
  • User Authentication: Secure JWT-based user sessions

πŸ—οΈ Tech Stack

Backend

Frontend

Development & Deployment

  • Uvicorn - ASGI server for FastAPI
  • ESLint - JavaScript/React code linting
  • PostCSS - CSS processing and optimization
  • Docker - Containerization (optional)

πŸ“ Project Structure

portfolio-optimization/
β”œβ”€β”€ πŸ“‚ backend/                          # FastAPI Backend
β”‚   β”œβ”€β”€ πŸ“‚ api/                          # API Layer
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   β”œβ”€β”€ auth.py                      # JWT Authentication
β”‚   β”‚   β”œβ”€β”€ data_utils.py                # Data Processing Utilities
β”‚   β”‚   β”œβ”€β”€ endpoints.py                 # API Route Handlers
β”‚   β”‚   └── models.py                    # Pydantic Data Models
β”‚   β”œβ”€β”€ πŸ“‚ data/                         # Data Storage
β”‚   β”‚   β”œβ”€β”€ πŸ“‚ individual_assets/        # Stock Price Data (45 assets)
β”‚   β”‚   β”œβ”€β”€ πŸ“‚ analytics/                # Analysis Results
β”‚   β”‚   β”œβ”€β”€ cleaned_prices.csv           # Processed Price Data
β”‚   β”‚   β”œβ”€β”€ returns_matrix_latest.csv    # Return Calculations
β”‚   β”‚   β”œβ”€β”€ asset_features.csv           # ML Feature Engineering
β”‚   β”‚   └── optimization_results_summary.csv # Portfolio Results
β”‚   β”œβ”€β”€ πŸ“‚ scripts/                      # Data Processing Scripts
β”‚   β”‚   β”œβ”€β”€ enhanced_portfolio_optimizer.py # Core Optimization Engine
β”‚   β”‚   β”œβ”€β”€ enhanced_ml_training.py      # ML Model Training
β”‚   β”‚   β”œβ”€β”€ data_analysis_and_cleaning.py # Data Preprocessing
β”‚   β”‚   └── feature_engineering.py      # Feature Creation
β”‚   β”œβ”€β”€ πŸ“‚ analysis_plots/               # Generated Visualizations
β”‚   β”œβ”€β”€ api_server.py                    # FastAPI Application Entry
β”‚   β”œβ”€β”€ requirements_api.txt             # API Dependencies
β”‚   β”œβ”€β”€ requirements.txt                 # Core Dependencies
β”‚   └── venv/                           # Virtual Environment
β”œβ”€β”€ πŸ“‚ frontend/frontend/                # React Frontend
β”‚   β”œβ”€β”€ πŸ“‚ src/
β”‚   β”‚   β”œβ”€β”€ πŸ“‚ components/               # Reusable UI Components
β”‚   β”‚   β”‚   β”œβ”€β”€ AllocationChart.jsx      # Portfolio Pie Charts
β”‚   β”‚   β”‚   β”œβ”€β”€ CustomPortfolioBuilder.jsx # Portfolio Construction
β”‚   β”‚   β”‚   β”œβ”€β”€ EfficientFrontierChart.jsx # Risk-Return Visualization
β”‚   β”‚   β”‚   β”œβ”€β”€ OptimizationResults.jsx  # Results Display
β”‚   β”‚   β”‚   β”œβ”€β”€ SavedPortfolios.jsx      # Portfolio Management
β”‚   β”‚   β”‚   └── StockClusterView.jsx     # Asset Clustering Display
β”‚   β”‚   β”œβ”€β”€ πŸ“‚ pages/                    # Application Pages
β”‚   β”‚   β”‚   β”œβ”€β”€ Dashboard.jsx            # Main Application Interface
β”‚   β”‚   β”‚   β”œβ”€β”€ LandingPage.jsx          # Marketing Landing Page
β”‚   β”‚   β”‚   β”œβ”€β”€ Login.jsx                # User Authentication
β”‚   β”‚   β”‚   └── Signup.jsx               # User Registration
β”‚   β”‚   β”œβ”€β”€ πŸ“‚ contexts/                 # React Context
β”‚   β”‚   β”‚   └── AuthContext.jsx          # Authentication State
β”‚   β”‚   β”œβ”€β”€ πŸ“‚ services/                 # API Integration
β”‚   β”‚   β”‚   └── authService.js           # API Client Functions
β”‚   β”‚   β”œβ”€β”€ App.jsx                      # Main App Component
β”‚   β”‚   β”œβ”€β”€ main.jsx                     # React Entry Point
β”‚   β”‚   └── index.css                    # Global Styles
β”‚   β”œβ”€β”€ package.json                     # Node.js Dependencies
β”‚   β”œβ”€β”€ vite.config.js                   # Vite Configuration
β”‚   └── tailwind.config.js               # TailwindCSS Configuration
β”œβ”€β”€ README.md                            # This file
└── .gitignore                           # Git ignore rules

πŸš€ Quick Start

Prerequisites

  • Python 3.12+ with pip
  • Node.js 18+ with npm
  • Git for version control

1. Clone the Repository

git clone https://github.com/yourusername/portfolio-optimization.git
cd portfolio-optimization

2. Backend Setup

# Navigate to backend directory
cd backend

# Create and activate virtual environment
python3 -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install dependencies
pip install -r requirements_api.txt
pip install -r requirements.txt

# Start the FastAPI server
python3 -m uvicorn api_server:app --reload --host 0.0.0.0 --port 8000

The backend API will be available at http://localhost:8000

3. Frontend Setup

# Navigate to frontend directory (in a new terminal)
cd frontend/frontend

# Install Node.js dependencies
npm install

# Start the development server
npm run dev

The frontend will be available at http://localhost:5173 (or next available port)

4. Access the Application

  1. Open your browser to the frontend URL
  2. Use demo credentials:
    • Email: demo@portfoliomax.com
    • Password: demo123
  3. Start optimizing portfolios!

πŸ“Š Data Sources & Assets

Asset Universe (45 Securities)

  • US Equities: AAPL, MSFT, GOOGL, AMZN, NVDA, TSLA, META, etc.
  • ETFs: SPY, QQQ, VTI, IWM, EFA, EEM, VNQ
  • Bonds: TLT (Long-term Treasuries)
  • Commodities: GLD (Gold), NEM (Mining)
  • Sectors: Technology, Healthcare, Finance, Consumer, Energy, Utilities

Data Processing Pipeline

  1. Data Collection: Real-time price data via yfinance API
  2. Feature Engineering: 20+ technical and fundamental features
  3. ML Enhancement: Return predictions using ensemble models
  4. Risk Modeling: Historical covariance with 2,511+ observations
  5. Clustering: K-means segmentation into 4 asset clusters

🎯 API Documentation

Authentication Endpoints

POST /auth/login          # User login with JWT token
POST /auth/register       # New user registration
GET  /auth/profile        # Get user profile information

Data Endpoints

GET  /data/stocks         # Available stocks with metrics
GET  /data/clusters       # Asset clustering information
GET  /data/efficient-frontier # Risk-return efficient frontier

Optimization Endpoints

POST /optimize            # Standard portfolio optimization
POST /optimize/custom     # Custom portfolio optimization
GET  /optimize/strategies # Available optimization strategies

Portfolio Management

POST /portfolios/save     # Save optimized portfolio
GET  /portfolios          # Get user's saved portfolios
GET  /portfolios/{id}     # Get specific portfolio details
PUT  /portfolios/{id}     # Update portfolio metadata
DELETE /portfolios/{id}   # Delete saved portfolio
POST /portfolios/compare  # Compare multiple portfolios

πŸ”§ Development

Running Tests

# Backend tests (if available)
cd backend
python -m pytest

# Frontend linting
cd frontend/frontend
npm run lint

Building for Production

# Build frontend
cd frontend/frontend
npm run build

# Frontend build output will be in frontend/frontend/dist/

Environment Variables

Create a .env file in the backend directory:

# JWT Configuration
SECRET_KEY=your-secret-key-here
ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES=30

# API Configuration
API_HOST=0.0.0.0
API_PORT=8000

# Data Configuration
DATA_UPDATE_INTERVAL=3600  # 1 hour

🎨 Customization

Adding New Assets

  1. Update backend/data/individual_assets/ with new CSV files
  2. Modify asset list in backend/scripts/config.py
  3. Re-run feature engineering: python scripts/feature_engineering.py
  4. Retrain ML models: python scripts/enhanced_ml_training.py

Custom Optimization Strategies

  1. Extend backend/scripts/enhanced_portfolio_optimizer.py
  2. Add new strategy to backend/api/endpoints.py
  3. Update frontend UI in frontend/src/components/OptimizationControls.jsx

UI Theming

  • Colors: Edit frontend/frontend/tailwind.config.js
  • Components: Modify frontend/frontend/src/index.css
  • Layouts: Update individual component files

πŸš€ Deployment

Docker Deployment (Recommended)

# Build and run with Docker Compose
docker-compose up -d

Manual Deployment

  1. Backend: Deploy FastAPI with Gunicorn/Uvicorn on cloud platforms
  2. Frontend: Build and deploy static files to CDN/hosting service
  3. Database: Set up PostgreSQL for production data storage

Cloud Platforms

  • AWS: EC2 + RDS + S3 for comprehensive deployment
  • Heroku: Easy deployment with buildpacks
  • Vercel: Frontend hosting with serverless backend
  • DigitalOcean: App Platform for full-stack deployment

πŸ“ˆ Performance

Optimization Results

  • Processing Speed: < 2 seconds for standard optimization
  • Accuracy: 2.69 average Sharpe ratio across strategies
  • Scalability: Handles 45+ assets with 2,500+ data points
  • Success Rate: 99.9% optimization convergence

System Requirements

  • Memory: 4GB RAM minimum (8GB recommended)
  • Storage: 2GB for data and models
  • CPU: Multi-core processor for ML computations
  • Network: Stable internet for real-time data

🀝 Contributing

We welcome contributions! Please follow these steps:

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/amazing-feature
  3. Make your changes with proper testing
  4. Commit: git commit -m 'Add amazing feature'
  5. Push: git push origin feature/amazing-feature
  6. Open a Pull Request

Development Guidelines

  • Follow PEP 8 for Python code
  • Use ESLint configuration for JavaScript/React
  • Add tests for new features
  • Update documentation for API changes
  • Ensure responsive design for UI changes

πŸ“ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ”— Links

πŸ™ Acknowledgments

  • Modern Portfolio Theory: Harry Markowitz
  • Financial Data: Yahoo Finance API
  • ML Libraries: scikit-learn community
  • UI Inspiration: Modern fintech applications
  • Icons: Lucide React icon library

Built with ❀️ for smarter investing

PortfolioMax - Where AI meets portfolio optimization

About

Resources

Stars

Watchers

Forks

Packages