7.1 KiB
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.
from openbb_terminal.sdk import openbb
import numpy as np
Fetch historical futures data for specified indices from OpenBB
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
futs = data['Adj Close'].pct_change().dropna()
Create first portfolio with specified weights for each index
port_1 = futs.ES * 0.60 + futs.YM * 0.10 + futs.NQ * 0.10
Create second portfolio with different weights for each index
port_2 = futs.ES * 0.90 + futs.YM * 0.15 + futs.NQ * 0.15
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
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
ret = ann_return(port_1) print(ret)
Calculate Calmar ratio for the first portfolio and print it
p1 = calmar_ratio(port_1) print(p1)
Calculate annualized return for the second portfolio and print it
ret = ann_return(port_2) print(ret)
Calculate Calmar ratio for the second portfolio and print it
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.
