Merge pull request #9153 from freqtrade/rpc/drawdown

Add better drawdown metrics to /profit outputs
This commit is contained in:
Matthias
2023-09-07 06:33:15 +02:00
committed by GitHub
4 changed files with 27 additions and 3 deletions

View File

@@ -141,6 +141,10 @@ class Profit(BaseModel):
expectancy_ratio: float
max_drawdown: float
max_drawdown_abs: float
max_drawdown_start: str
max_drawdown_start_timestamp: int
max_drawdown_end: str
max_drawdown_end_timestamp: int
trading_volume: Optional[float] = None
bot_start_timestamp: int
bot_start_date: str

View File

@@ -532,6 +532,7 @@ class RPC:
winrate = (winning_trades / closed_trade_count) if closed_trade_count > 0 else 0
trades_df = DataFrame([{'close_date': format_date(trade.close_date),
'close_date_dt': trade.close_date,
'profit_abs': trade.close_profit_abs}
for trade in trades if not trade.is_open and trade.close_date])
@@ -539,10 +540,15 @@ class RPC:
max_drawdown_abs = 0.0
max_drawdown = 0.0
drawdown_start: Optional[datetime] = None
drawdown_end: Optional[datetime] = None
dd_high_val = dd_low_val = 0.0
if len(trades_df) > 0:
try:
(max_drawdown_abs, _, _, _, _, max_drawdown) = calculate_max_drawdown(
trades_df, value_col='profit_abs', starting_balance=starting_balance)
(max_drawdown_abs, drawdown_start, drawdown_end, dd_high_val, dd_low_val,
max_drawdown) = calculate_max_drawdown(
trades_df, value_col='profit_abs', date_col='close_date_dt',
starting_balance=starting_balance)
except ValueError:
# ValueError if no losing trade.
pass
@@ -594,6 +600,12 @@ class RPC:
'expectancy_ratio': expectancy_ratio,
'max_drawdown': max_drawdown,
'max_drawdown_abs': max_drawdown_abs,
'max_drawdown_start': format_date(drawdown_start),
'max_drawdown_start_timestamp': dt_ts_def(drawdown_start),
'max_drawdown_end': format_date(drawdown_end),
'max_drawdown_end_timestamp': dt_ts_def(drawdown_end),
'drawdown_high': dd_high_val,
'drawdown_low': dd_low_val,
'trading_volume': trading_volume,
'bot_start_timestamp': dt_ts_def(bot_start, 0),
'bot_start_date': format_date(bot_start),

View File

@@ -889,7 +889,11 @@ class Telegram(RPCHandler):
f"*Trading volume:* `{round_coin_value(stats['trading_volume'], stake_cur)}`\n"
f"*Profit factor:* `{stats['profit_factor']:.2f}`\n"
f"*Max Drawdown:* `{stats['max_drawdown']:.2%} "
f"({round_coin_value(stats['max_drawdown_abs'], stake_cur)})`"
f"({round_coin_value(stats['max_drawdown_abs'], stake_cur)})`\n"
f" from `{stats['max_drawdown_start']} "
f"({round_coin_value(stats['drawdown_high'], stake_cur)})`\n"
f" to `{stats['max_drawdown_end']} "
f"({round_coin_value(stats['drawdown_low'], stake_cur)})`\n"
)
await self._send_msg(markdown_msg, reload_able=True, callback_path="update_profit",
query=update.callback_query)

View File

@@ -977,6 +977,10 @@ def test_api_profit(botclient, mocker, ticker, fee, markets, is_short, expected)
'expectancy_ratio': expected['expectancy_ratio'],
'max_drawdown': ANY,
'max_drawdown_abs': ANY,
'max_drawdown_start': ANY,
'max_drawdown_start_timestamp': ANY,
'max_drawdown_end': ANY,
'max_drawdown_end_timestamp': ANY,
'trading_volume': expected['trading_volume'],
'bot_start_timestamp': 0,
'bot_start_date': '',