52 KiB
This code downloads historical stock data for a specified list of symbols using the yfinance library. It computes daily returns for each stock and then aggregates these returns to form a portfolio. The Calmar Ratio, a performance metric used to evaluate the risk-adjusted returns of the portfolio, is calculated using the empyrical library. Finally, it prints the Calmar Ratio and plots the portfolio's daily returns over the specified period.
import empyrical as ep import yfinance as yf import warnings
warnings.filterwarnings("ignore")
# Define a list of stock symbols to download historical data for symbols = [ "AAPL", "AMZN", "GOOG", "META", "MSFT", "NVDA", "TSLA", ]
# Download adjusted closing prices for the specified symbols from Yahoo Finance data = yf.download(symbols, start="2020-01-01")["Adj Close"]
[*********************100%%**********************] 7 of 7 completed
# Calculate daily percentage returns for each stock and remove any missing values returns = data.pct_change().dropna()
# Aggregate the daily returns of all stocks to form a portfolio return series portfolio_returns = returns.sum(axis=1)
# Compute the Calmar Ratio for the portfolio using the empyrical library calmar_ratio = ep.calmar_ratio(portfolio_returns) print(f"Calmar Ratio: {calmar_ratio}")
Calmar Ratio: -0.3244445113808806
# Plot the portfolio's daily returns over the specified period portfolio_returns.plot()
<Axes: xlabel='Date'>
PyQuant News is where finance practitioners level up with Python for quant finance, algorithmic trading, and market data analysis. Looking to get started? Check out the fastest growing, top-selling course to get started with Python for quant finance. For educational purposes. Not investment advise. Use at your own risk.
