Skip to content

Commit 46dff7f

Browse files
committed
Add realized maximum drawdown metric
1 parent 5461304 commit 46dff7f

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

jesse/services/metrics.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,17 @@ def max_drawdown(returns):
157157
# Always convert to pandas Series
158158
return pd.Series([result])
159159

160+
def realized_max_drawdown(starting_balance, df_pnl):
161+
"""
162+
Calculates the realized maximum drawdown as a percentage
163+
"""
164+
cumulative_balance = starting_balance + df_pnl.cumsum()
165+
peak_balance = cumulative_balance.cummax()
166+
drawdown = peak_balance - cumulative_balance
167+
drawdown_pct = (drawdown / peak_balance) * 100
168+
result = -drawdown_pct.max()
169+
170+
return pd.Series([result])
160171

161172
def cagr(returns, rf=0.0, compounded=True, periods=365):
162173
"""
@@ -311,6 +322,7 @@ def trades(trades_list: List[ClosedTrade], daily_balance: list, final: bool = Tr
311322
average_losing_holding_period = losing_trades['holding_period'].mean()
312323
gross_profit = winning_trades['PNL'].sum()
313324
gross_loss = losing_trades['PNL'].sum()
325+
realized_max_dd = realized_max_drawdown(starting_balance, df['PNL']).iloc[0]
314326

315327
start_date = datetime.fromtimestamp(store.app.starting_time / 1000)
316328
date_index = pd.date_range(start=start_date, periods=len(daily_balance))
@@ -366,6 +378,7 @@ def safe_convert(value, convert_type=float):
366378
'gross_profit': safe_convert(gross_profit),
367379
'gross_loss': safe_convert(gross_loss),
368380
'max_drawdown': safe_convert(max_dd),
381+
'realized_max_drawdown': safe_convert(realized_max_dd),
369382
'annual_return': safe_convert(annual_return),
370383
'sharpe_ratio': safe_convert(sharpe),
371384
'calmar_ratio': safe_convert(calmar),

0 commit comments

Comments
 (0)