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

7.1 KiB

No description has been provided for this image

This code fetches historical futures data for specific indices, calculates their percentage changes, and creates two different portfolios. It then computes the annualized return and Calmar ratio for each portfolio. The annualized return provides a measure of the portfolio's performance over time. The Calmar ratio assesses the risk-adjusted return by comparing the annualized return to the maximum drawdown. This analysis helps in understanding and comparing the performance and risk of different investment portfolios.

In [ ]:
from openbb_terminal.sdk import openbb
In [ ]:
import numpy as np

Fetch historical futures data for specified indices from OpenBB

In [ ]:
data = openbb.futures.historical(
    ["ES", "YM", "NQ"], 
    start_date="2020-01-01", 
    end_date="2022-12-31"
)

Calculate percentage changes of adjusted closing prices and drop NaN values

In [ ]:
futs = data['Adj Close'].pct_change().dropna()

Create first portfolio with specified weights for each index

In [ ]:
port_1 = futs.ES * 0.60 + futs.YM * 0.10 + futs.NQ * 0.10

Create second portfolio with different weights for each index

In [ ]:
port_2 = futs.ES * 0.90 + futs.YM * 0.15 + futs.NQ * 0.15
In [ ]:
def ann_return(returns):
    """Calculate annualized return of a portfolio
    
    Parameters
    ----------
    returns : pd.Series
        Daily returns of the portfolio
    
    Returns
    -------
    ann_return : float
        Annualized return of the portfolio
    
    Notes
    -----
    This method computes the annualized return 
    using the geometric mean of daily returns.
    """
    
    # Compute the ending value of the investment
    ending_value = (returns + 1).prod()
    
    # Calculate the number of years based on daily returns
    num_years = len(returns) / 252
    
    # Calculate annualized return
    ann_return = ending_value ** (1/num_years) - 1
    
    return ann_return
In [ ]:
def calmar_ratio(returns):
    """Calculate Calmar ratio of a portfolio
    
    Parameters
    ----------
    returns : pd.Series
        Daily returns of the portfolio
    
    Returns
    -------
    calmar_ratio : float
        Calmar ratio of the portfolio
    
    Notes
    -----
    This method computes the Calmar ratio by 
    dividing the annualized return by the 
    absolute value of maximum drawdown.
    """
    
    # Compute cumulative returns
    cumulative_returns = (returns + 1).cumprod() * 100
    
    # Compute maximum drawdown
    max_return = np.fmax.accumulate(cumulative_returns)
    max_dd = ((cumulative_returns - max_return) / max_return).min()
    
    # Calculate annualized return
    ann_ret = ann_return(returns)
    
    return ann_ret / abs(max_dd)

Calculate annualized return for the first portfolio and print it

In [ ]:
ret = ann_return(port_1)
print(ret)

Calculate Calmar ratio for the first portfolio and print it

In [ ]:
p1 = calmar_ratio(port_1)
print(p1)

Calculate annualized return for the second portfolio and print it

In [ ]:
ret = ann_return(port_2)
print(ret)

Calculate Calmar ratio for the second portfolio and print it

In [ ]:
p2 = calmar_ratio(port_2)
print(p2)

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.