diff --git a/freqtrade/exchange/binance.py b/freqtrade/exchange/binance.py index d7fb0a353..637bfafef 100644 --- a/freqtrade/exchange/binance.py +++ b/freqtrade/exchange/binance.py @@ -153,8 +153,7 @@ class Binance(Exchange): stake_amount: float, leverage: float, wallet_balance: float, # Or margin balance - mm_ex_1: float = 0.0, # (Binance) Cross only - upnl_ex_1: float = 0.0, # (Binance) Cross only + other_trades: list, ) -> Optional[float]: """ Important: Must be fetching data from cached values as this is used by backtesting! @@ -172,6 +171,7 @@ class Binance(Exchange): :param wallet_balance: Amount of margin_mode in the wallet being used to trade Cross-Margin Mode: crossWalletBalance Isolated-Margin Mode: isolatedWalletBalance + :param other_trades: List of other open trades in the same wallet # * Only required for Cross :param mm_ex_1: (TMM) @@ -180,15 +180,31 @@ class Binance(Exchange): :param upnl_ex_1: (UPNL) Cross-Margin Mode: Unrealized PNL of all other contracts, excluding Contract 1. Isolated-Margin Mode: 0 + :param other """ - - side_1 = -1 if is_short else 1 - cross_vars = upnl_ex_1 - mm_ex_1 if self.margin_mode == MarginMode.CROSS else 0.0 + cross_vars: float = 0.0 # mm_ratio: Binance's formula specifies maintenance margin rate which is mm_ratio * 100% # maintenance_amt: (CUM) Maintenance Amount of position mm_ratio, maintenance_amt = self.get_maintenance_ratio_and_amt(pair, stake_amount) + if self.margin_mode == MarginMode.CROSS: + mm_ex_1: float = 0.0 + upnl_ex_1: float = 0.0 + for trade in other_trades: + mm_ratio1, maint_amnt1 = self.get_maintenance_ratio_and_amt( + trade["pair"], trade["stake_amount"] + ) + maint_margin = trade["amount"] * trade["mark_price"] * mm_ratio1 - maint_amnt1 + mm_ex_1 += maint_margin + + upnl_ex_1 += ( + trade["amount"] * trade["mark_price"] - trade["amount"] * trade["open_rate"] + ) + cross_vars = upnl_ex_1 - mm_ex_1 + + side_1 = -1 if is_short else 1 + if maintenance_amt is None: raise OperationalException( "Parameter maintenance_amt is required by Binance.liquidation_price" diff --git a/freqtrade/exchange/bybit.py b/freqtrade/exchange/bybit.py index 719c64dc3..c14273e53 100644 --- a/freqtrade/exchange/bybit.py +++ b/freqtrade/exchange/bybit.py @@ -147,8 +147,7 @@ class Bybit(Exchange): stake_amount: float, leverage: float, wallet_balance: float, # Or margin balance - mm_ex_1: float = 0.0, # (Binance) Cross only - upnl_ex_1: float = 0.0, # (Binance) Cross only + other_trades: list, ) -> Optional[float]: """ Important: Must be fetching data from cached values as this is used by backtesting! @@ -178,6 +177,7 @@ class Bybit(Exchange): :param wallet_balance: Amount of margin_mode in the wallet being used to trade Cross-Margin Mode: crossWalletBalance Isolated-Margin Mode: isolatedWalletBalance + :param other_trades: List of other open trades in the same wallet """ market = self.markets[pair] diff --git a/freqtrade/exchange/exchange.py b/freqtrade/exchange/exchange.py index 9d84f59e4..83aff2def 100644 --- a/freqtrade/exchange/exchange.py +++ b/freqtrade/exchange/exchange.py @@ -3532,8 +3532,7 @@ class Exchange: stake_amount: float, leverage: float, wallet_balance: float, - mm_ex_1: float = 0.0, # (Binance) Cross only - upnl_ex_1: float = 0.0, # (Binance) Cross only + other_trades: list, ) -> Optional[float]: """ Set's the margin mode on the exchange to cross or isolated for a specific pair @@ -3555,8 +3554,7 @@ class Exchange: leverage=leverage, stake_amount=stake_amount, wallet_balance=wallet_balance, - mm_ex_1=mm_ex_1, - upnl_ex_1=upnl_ex_1, + other_trades=other_trades, ) else: positions = self.fetch_positions(pair) @@ -3582,8 +3580,7 @@ class Exchange: stake_amount: float, leverage: float, wallet_balance: float, # Or margin balance - mm_ex_1: float = 0.0, # (Binance) Cross only - upnl_ex_1: float = 0.0, # (Binance) Cross only + other_trades: list, ) -> Optional[float]: """ Important: Must be fetching data from cached values as this is used by backtesting! @@ -3608,10 +3605,7 @@ class Exchange: :param wallet_balance: Amount of margin_mode in the wallet being used to trade Cross-Margin Mode: crossWalletBalance Isolated-Margin Mode: isolatedWalletBalance - - # * Not required by Gate or OKX - :param mm_ex_1: - :param upnl_ex_1: + :param other_trades: List of other open trades in the same wallet """ market = self.markets[pair]