Files
strategy-lab/to_explore/pyquantnews/105_CalmarRatio.ipynb
David Brazda e3da60c647 daily update
2024-10-21 20:57:56 +02:00

52 KiB

No description has been provided for this image

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.

In [1]:
import empyrical as ep
import yfinance as yf
import warnings
In [2]:
warnings.filterwarnings("ignore")
In [3]:
# Define a list of stock symbols to download historical data for
symbols = [
    "AAPL",
    "AMZN",
    "GOOG",
    "META",
    "MSFT",
    "NVDA",
    "TSLA",
]
In [4]:
# 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
In [5]:
# Calculate daily percentage returns for each stock and remove any missing values
returns = data.pct_change().dropna()
In [6]:
# Aggregate the daily returns of all stocks to form a portfolio return series
portfolio_returns = returns.sum(axis=1)
In [7]:
# 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
In [8]:
# Plot the portfolio's daily returns over the specified period
portfolio_returns.plot()
Out[8]:
<Axes: xlabel='Date'>
No description has been provided for this image

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.