write a script that run in google colab, its for finding the best sharpe ratio/lowest…
- User
write a script that run in google colab, its for finding the best sharpe ratio/lowest maximum drawdown/maximum annulized return stock portfolio with allocation, it will loop through spy500 stock only, optimized date can be chosen, it will find the portfolio with 8 different stocks only while each stock allocation is around 10%-20%
- Assistant
Certainly! Below is a comprehensive Python script designed to run in Google Colab. This script helps you identify the optimal 8-stock portfolio from the S&P 500 (represented by
SPYin Yahoo Finance) based on the best Sharpe Ratio, lowest Maximum Drawdown, and highest Annualized Return. The portfolio allocations for each stock are constrained between 10% and 20%.Overview of the Script:
-
Setup and Imports:
- Install and import necessary libraries.
-
Define Parameters:
- Set the date range for historical data analysis.
- Define the number of portfolios to sample.
-
Fetch S&P 500 Tickers:
- Scrape the list of S&P 500 companies from Wikipedia.
-
Download Historical Data:
- Use
yfinanceto fetch adjusted closing prices for the tickers.
- Use
-
Calculate Performance Metrics:
- Compute daily returns, annualized returns, annualized volatility, Sharpe Ratio, and Maximum Drawdown for each portfolio.
-
Random Portfolio Generation:
- Randomly generate portfolios adhering to the constraints (8 stocks, allocations between 10%-20%).
-
Optimization and Selection:
- Evaluate all sampled portfolios based on the defined metrics.
- Select the best portfolio with the highest Sharpe Ratio, lowest Maximum Drawdown, and highest Annualized Return.
-
Display Results:
- Present the optimal portfolio allocations and its performance metrics.
Script Implementation:
# Install required packages !pip install yfinance !pip install pandas !pip install numpy !pip install beautifulsoup4 !pip install requests # Import necessary libraries import yfinance as yf import pandas as pd import numpy as np import itertools import random from datetime import datetime from bs4 import BeautifulSoup import requests # Set random seed for reproducibility random.seed(42) np.random.seed(42) # Function to fetch S&P 500 tickers from Wikipedia def get_sp500_tickers(): url = 'https://en.wikipedia.org/wiki/List_of_S%26P_500_companies' response = requests.get(url) soup = BeautifulSoup(response.text, "lxml") table = soup.find('table', {'id': 'constituents'}) tickers = [] for row in table.findAll('tr')[1:]: ticker = row.findAll('td')[0].text.strip() # Replace '.' with '-' for tickers like BRK.B ticker = ticker.replace('.', '-') tickers.append(ticker) return tickers # Function to calculate Maximum Drawdown def max_drawdown(series): roll_max = series.cummax() drawdown = (series - roll_max) / roll_max return drawdown.min() # Function to generate random portfolio weights def generate_weights(n, lower=0.10, upper=0.20): weights = [] for _ in range(n): w = random.uniform(lower, upper) weights.append(w) # Normalize weights to sum to 1 total = sum(weights) weights = [w / total for w in weights] return weights # Parameters start_date = '2018-01-01' end_date = '2023-10-01' num_portfolios = 10000 # Number of random portfolios to generate num_assets = 8 # Number of stocks in the portfolio lower_weight = 0.10 upper_weight = 0.20 # Step 1: Get S&P 500 tickers print("Fetching S&P 500 tickers...") tickers = get_sp500_tickers() print(f"Number of tickers fetched: {len(tickers)}") # Step 2: Download historical data print("Downloading historical data...") data = yf.download(tickers, start=start_date, end=end_date)['Adj Close'] data.dropna(axis=1, inplace=True) # Drop tickers with missing data print(f"Data shape after dropping tickers with missing data: {data.shape}") # Step 3: Calculate daily returns returns = data.pct_change().dropna() # Step 4: Calculate annualized metrics annual_return = returns.mean() * 252 annual_volatility = returns.std() * np.sqrt(252) risk_free_rate = 0.017 # Approximate risk-free rate (e.g., 10-year US Treasury yield) # Step 5: Generate random portfolios print(f"Generating and evaluating {num_portfolios} random portfolios...") best_sharpe = -np.inf best_return = -np.inf best_drawdown = np.inf best_portfolio = None for i in range(num_portfolios): # Select 8 random stocks selected = random.sample(list(returns.columns), num_assets) # Generate random weights between 10% and 20% weights = generate_weights(num_assets, lower=lower_weight, upper=upper_weight) # Calculate portfolio return and volatility portfolio_return = np.dot(weights, annual_return[selected]) portfolio_volatility = np.sqrt(np.dot(weights, np.dot(returns[selected].cov() * 252, weights))) # Calculate Sharpe Ratio sharpe_ratio = (portfolio_return - risk_free_rate) / portfolio_volatility # Calculate portfolio cumulative returns for Max Drawdown portfolio_cum_returns = (1 + returns[selected].dot(weights)).cumprod() portfolio_max_drawdown = max_drawdown(portfolio_cum_returns) # Check if this portfolio is better based on Sharpe Ratio if sharpe_ratio > best_sharpe and portfolio_max_drawdown < best_drawdown: best_sharpe = sharpe_ratio best_return = portfolio_return best_drawdown = portfolio_max_drawdown best_portfolio = { 'Stocks': selected, 'Weights': weights, 'Annual Return': portfolio_return, 'Annual Volatility': portfolio_volatility, 'Sharpe Ratio': sharpe_ratio, 'Max Drawdown': portfolio_max_drawdown } # Step 6: Display the best portfolio if best_portfolio: print("\nOptimal Portfolio Found:") portfolio_df = pd.DataFrame({ 'Ticker': best_portfolio['Stocks'], 'Allocation': [f"{w*100:.2f}%" for w in best_portfolio['Weights']] }) print(portfolio_df) print(f"\nAnnualized Return: {best_portfolio['Annual Return']:.2f}") print(f"Annualized Volatility: {best_portfolio['Annual Volatility']:.2f}") print(f"Sharpe Ratio: {best_portfolio['Sharpe Ratio']:.2f}") print(f"Maximum Drawdown: {best_portfolio['Max Drawdown']:.2%}") else: print("No optimal portfolio found based on the given constraints.")Detailed Explanation:
-
Installing and Importing Libraries:
yfinance: For fetching historical stock data.pandas&numpy: For data manipulation and numerical computations.BeautifulSoup&requests: For web scraping S&P 500 tickers from Wikipedia.
-
Fetching S&P 500 Tickers:
- The script scrapes the S&P 500 tickers from Wikipedia's List of S&P 500 companies page.
- It ensures that tickers like "BRK.B" are correctly formatted as "BRK-B" to be compatible with Yahoo Finance.
-
Downloading Historical Data:
- Adjusted closing prices are fetched for the selected date range.
- Tickers with incomplete data (e.g., newly listed stocks) are removed to ensure consistency.
-
Calculating Returns and Metrics:
- Daily Returns: Percentage change in adjusted closing prices.
- Annualized Return: Mean daily return multiplied by 252 (trading days in a year).
- Annualized Volatility: Standard deviation of daily returns multiplied by the square root of 252.
- Sharpe Ratio: Measures risk-adjusted return. Here, a risk-free rate of 1.7% is assumed.
- Maximum Drawdown: Largest peak-to-trough decline during the investment period.
-
Generating Random Portfolios:
- For each of the 10,000 iterations:
- Stock Selection: Randomly pick 8 unique stocks.
- Weight Allocation: Assign random weights between 10% and 20% ensuring that the total allocation sums to 100%.
- Performance Calculation: Compute the portfolio's annual return, volatility, Sharpe Ratio, and Maximum Drawdown.
- Optimization: Track the portfolio that offers the highest Sharpe Ratio while maintaining the lowest Maximum Drawdown.
- For each of the 10,000 iterations:
-
Displaying the Optimal Portfolio:
- Once all portfolios are evaluated, the script outputs the best-performing portfolio, detailing each stock's allocation and the portfolio's overall performance metrics.
Notes and Recommendations:
-
Computation Time: Generating and evaluating 10,000 portfolios can be time-consuming, especially with larger datasets. You can adjust
num_portfoliosbased on your computational resources and desired accuracy. -
Optimization Approach: This script uses a brute-force random sampling method. For more efficient optimization, especially with larger portfolios or stricter constraints, consider using advanced optimization techniques or libraries like
PyPortfolioOpt. -
Constraints Handling: The current approach ensures that each stock's allocation is between 10% and 20%. However, it doesn't strictly limit the maximum number of stocks beyond randomly selecting 8 per portfolio. If you need stricter control over the number of assets, more sophisticated optimization techniques might be necessary.
-
Risk-Free Rate: The Sharpe Ratio calculation assumes a risk-free rate of 1.7%. Adjust this value based on current economic conditions or specific requirements.
-
Data Update Frequency: Ensure that the script is run periodically to fetch the latest stock data, as market conditions and stock performances change over time.
Conclusion:
This script provides a foundational framework for portfolio optimization based on specific financial metrics. By adjusting parameters and incorporating more advanced optimization techniques, you can further refine the portfolio selection to meet diverse investment strategies and risk appetites.
-