From 4a7140c05df522e4d234e4c2e2f32e6de1108e05 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 8 Mar 2025 17:29:55 +0100 Subject: [PATCH] chore: Update bybit dry-liquidation calculation the result remains the same - but the calculation now matches the bybit documentation better. --- freqtrade/exchange/bybit.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/freqtrade/exchange/bybit.py b/freqtrade/exchange/bybit.py index ae15ad521..1f5c19b2a 100644 --- a/freqtrade/exchange/bybit.py +++ b/freqtrade/exchange/bybit.py @@ -166,15 +166,16 @@ class Bybit(Exchange): PERPETUAL: bybit: https://www.bybithelp.com/HelpCenterKnowledge/bybitHC_Article?language=en_US&id=000001067 + https://www.bybit.com/en/help-center/article/Liquidation-Price-Calculation-under-Isolated-Mode-Unified-Trading-Account#b Long: Liquidation Price = ( - Entry Price * (1 - Initial Margin Rate + Maintenance Margin Rate) - - Extra Margin Added/ Contract) + Entry Price - [(Initial Margin - Maintenance Margin)/Contract Quantity] + - (Extra Margin Added/Contract Quantity)) Short: Liquidation Price = ( - Entry Price * (1 + Initial Margin Rate - Maintenance Margin Rate) - + Extra Margin Added/ Contract) + Entry Price + [(Initial Margin - Maintenance Margin)/Contract Quantity] + + (Extra Margin Added/Contract Quantity)) Implementation Note: Extra margin is currently not used. @@ -196,13 +197,16 @@ class Bybit(Exchange): if self.trading_mode == TradingMode.FUTURES and self.margin_mode == MarginMode.ISOLATED: if market["inverse"]: raise OperationalException("Freqtrade does not yet support inverse contracts") - initial_margin_rate = 1 / leverage + position_value = amount * open_rate + initial_margin = position_value / leverage + maintenance_margin = position_value * mm_ratio + margin_diff_per_contract = (initial_margin - maintenance_margin) / amount # See docstring - ignores extra margin! if is_short: - return open_rate * (1 + initial_margin_rate - mm_ratio) + return open_rate + margin_diff_per_contract else: - return open_rate * (1 - initial_margin_rate + mm_ratio) + return open_rate - margin_diff_per_contract else: raise OperationalException(