Recovery Factor in Python
The ratio of net profit to maximum drawdown, measuring how efficiently a strategy recovers from losses.
Definition
The Recovery Factor measures the relationship between a strategy's total net profit and its worst historical drawdown. It answers a crucial operational question: 'For every dollar lost in the worst drawdown, how many dollars did the strategy ultimately generate?' A high Recovery Factor indicates a strategy that experiences significant pain but ultimately delivers strong returns relative to that pain. It is a key metric for evaluating the resilience of a trading system under adverse conditions.
Quantitative Formula
Where is the total absolute profit generated by the strategy over the evaluation period (final equity minus initial equity), and is the absolute value of the Maximum Drawdown expressed in the same currency units (not as a percentage).
Why It Matters in Backtesting
A Recovery Factor below 1.0 means the strategy's worst drawdown exceeded its total profit — a deeply unfavorable risk profile. Most professional allocators require a Recovery Factor above 3.0 before committing capital. In backtesting, this metric helps distinguish between strategies that are merely lucky (high returns, high drawdown) versus systematically robust (moderate returns, minimal drawdown).
Python Implementation
import numpy as np
import pandas as pd
def calculate_recovery_factor(returns: pd.Series, initial_capital: float = 10000.0) -> float:
"""
Calculates the Recovery Factor from a daily returns series.
initial_capital: Starting portfolio value in currency units.
"""
equity_curve = initial_capital * (1 + returns).cumprod()
net_profit = equity_curve.iloc[-1] - initial_capital
peak = equity_curve.cummax()
drawdown_currency = (equity_curve - peak)
max_drawdown_currency = drawdown_currency.min()
return net_profit / abs(max_drawdown_currency) if max_drawdown_currency != 0 else 0.0Test this in a live environment
Stop running Jupyter notebooks locally. Paste this Recovery Factor code directly into Valetha's Strategy Lab and run a full historical backtest in seconds.
Open the Python Strategy Lab