S&P 500 Stock Selection and Portfolio Optimizer
This tool analyzes the S&P 500 stocks over a 15-year period to select the top 30 performing stocks based on multiple metrics including:
- Annual and total returns
- Sharpe ratio (risk-adjusted returns)
- Maximum drawdown
- Return stability
It then uses Modern Portfolio Theory (Markowitz Optimization) to determine optimal portfolio weights for the selected stocks.
- Analyzes S&P 500 stocks using 15 years of historical data
- Selects top 30 stocks based on:
- Risk-adjusted returns (Sharpe ratio)
- Historical performance
- Return stability
- Maximum drawdown
- Uses Modern Portfolio Theory (MPT)
- Optimizes for maximum Sharpe ratio
- Implements position size constraints
- Handles risk management through diversification
- Live market data monitoring
- Automated portfolio rebalancing
- Stop-loss implementation
- Performance tracking
- Tests strategy on historical data
- Optimizes trading parameters
- Calculates key performance metrics
- Visualizes results
- Python 3.6+
- Clone the repository
- Install dependencies:
pip install -r requirements- yfinance: Market data access
- pandas: Data analysis
- numpy: Numerical computations
- scipy: Portfolio optimization
- matplotlib: Data visualization
- alpha_vantage: Alternative data source
- websocket-client: Real-time data
- tqdm: Progress tracking
Print the optimal weights of selected stocks:
python S_and_P500_top_30.pyTest the strategy on historical data:
python backtesting.pyLaunch the live trading system:
python mpt_trading_bot.py- Maximum position size limits
- Automated stop-loss orders
- Regular portfolio rebalancing
- Correlation-based diversification
- Total Return
- Annual Return
- Sharpe Ratio
- Maximum Drawdown
- Portfolio Volatility
- Number of Trades