A real-time portfolio optimization application built with React and Python, featuring Modern Portfolio Theory (MPT) allocators with WebSocket-based communication.
Portfolio Optimizer allows users to create, configure, and backtest different portfolio allocation strategies. It supports manual allocations as well as automated optimization using Max Sharpe Ratio and Minimum Volatility strategies from Modern Portfolio Theory.
-
Multiple Allocator Types
- Manual: Define fixed allocations across instruments
- Max Sharpe: Optimize for maximum risk-adjusted returns using the Sharpe ratio
- Min Volatility: Minimize portfolio variance with optional target return constraints
-
Real-Time Computation: WebSocket-based communication for live progress updates during backtests
-
Dynamic Rebalancing: Configure automatic portfolio rebalancing at custom intervals
-
Performance Analytics: Comprehensive metrics including total return, annualized return, volatility, Sharpe ratio, and max drawdown
-
Interactive Charts: Zoomable, pannable performance charts with selection tooltips
-
Dark/Light Theme: Full theme support with system preference detection
- React 19 with TypeScript
- Vite for build tooling
- Tailwind CSS for styling
- Recharts for data visualization
- WebSocket for real-time communication
- Python 3.11+ with FastAPI
- PyPortfolioOpt for MPT optimization
- Pandas for data manipulation
- Alpha Vantage API for price data
- SQLite for caching (optional)
- Node.js 18+
- Python 3.11+
- Alpha Vantage API key (free tier available)
cd backend
# Create virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt
# Set environment variable
export ALPHA_VANTAGE_API_KEY=your_api_key_here
# Start server
python main.pyThe backend runs on ws://localhost:8000/ws
cd frontend
# Install dependencies
npm install
# Start development server
npm run devThe frontend runs on http://localhost:5173
portfolio_optimizer_react/
├── backend/
│ ├── allocators/ # Allocation strategy implementations
│ │ ├── base.py # Abstract base classes
│ │ ├── manual.py # Manual allocator
│ │ ├── max_sharpe.py # Max Sharpe optimizer
│ │ └── min_volatility.py # Min volatility optimizer
│ ├── services/
│ │ ├── portfolio.py # Performance calculation
│ │ └── price_fetcher.py # Price data fetching
│ ├── main.py # FastAPI WebSocket server
│ ├── message_handlers.py # WebSocket message routing
│ ├── schemas.py # Pydantic message models
│ └── connection_state.py # Per-connection state
├── frontend/
│ ├── src/
│ │ ├── components/ # React components
│ │ ├── hooks/ # Custom React hooks
│ │ ├── services/ # WebSocket service
│ │ └── types/ # TypeScript definitions
│ └── ...
└── README.md
- Create an Allocator: Click "+ Add" and choose allocator type
- Configure: Set instruments, weights, and options
- Set Date Range: Define fit period (training) and test period (backtest)
- Compute: Click "Compute" to run the backtest
- Analyze: View performance metrics and charts
- Name and fixed percentage allocations per ticker
- Instrument list (tickers)
- Allow shorting toggle
- Use adjusted close (dividend-adjusted) prices
- Update interval for dynamic rebalancing
- Target return (Min Volatility only)
Client → Server:
create_allocator: Create new allocatorupdate_allocator: Update allocator configdelete_allocator: Remove allocatorcompute: Run portfolio computation
Server → Client:
allocator_created: Confirmation with IDprogress: Computation progress updatesresult: Final computation results with performance dataerror: Error messages
| Metric | Description |
|---|---|
| Total Return | Cumulative percentage return over test period |
| Annualized Return | CAGR (Compound Annual Growth Rate) |
| Volatility | Annualized standard deviation of daily returns |
| Sharpe Ratio | Risk-adjusted return (assumes 4% risk-free rate) |
| Max Drawdown | Largest peak-to-trough decline |
- Alpha Vantage free tier: 25 requests/day, 5 requests/minute
- Price data cached locally to minimize API calls
- Optimization assumes normally distributed returns
- No transaction costs modeled
# Run frontend in development mode
cd frontend && npm run dev
# Run backend with auto-reload
cd backend && uvicorn main:app --reload
# Type check frontend
cd frontend && npx tsc --noEmit
# Build for production
cd frontend && npm run buildMIT License - see LICENSE file for details.
Contributions welcome! Please read the architecture documentation before submitting PRs.