Algorithmic Trading & Indicators
Beginner
Calculate Maximum Drawdown, Sharpe Ratio & Calmar Ratio in Python
Evaluate trading strategy performance in Python by computing Peak-to-Trough Maximum Drawdown, Annualized Sharpe Ratio, and Calmar Ratio from daily returns.
#pandas#numpy
Problem Statement
Evaluating a strategy solely by net profit hides catastrophic drawdowns. This script takes a series of daily returns or equity curve values and computes institutional risk-adjusted performance metrics.
Formula / Core Logic
Drawdown_t = (Equity_t - Running_Peak_t) / Running_Peak_t
Sharpe Ratio = (Mean_Return / Std_Return) * sqrt(252)
Installation
pip install pandas numpy
calculate-max-drawdown-portfolio-python.py
import pandas as pd
import numpy as np
def portfolio_risk_metrics(returns_series, risk_free_rate=0.05):
equity = (1 + returns_series).cumprod()
running_peak = equity.cummax()
drawdown = (equity - running_peak) / running_peak
max_dd = drawdown.min()
excess_ret = returns_series - (risk_free_rate / 252)
sharpe = np.sqrt(252) * (excess_ret.mean() / returns_series.std())
cagr = (equity.iloc[-1] ** (252 / len(returns_series))) - 1
calmar = cagr / abs(max_dd) if max_dd != 0 else np.nan
return {'CAGR_Pct': round(cagr * 100, 2), 'Max_Drawdown_Pct': round(max_dd * 100, 2), 'Sharpe_Ratio': round(sharpe, 2), 'Calmar_Ratio': round(calmar, 2)}
Get the Complete 100+ Python Quant & Automation Bundle
Instant lifetime access to all production-ready scripts, valuation models, and trading bots ($19).
Buy Full Bundle on Gumroad ($19) →Frequently Asked Questions
Q: What is considered a good Calmar Ratio?
A Calmar Ratio above 1.5 indicates strong annualized returns relative to the worst historical drawdown.