From 08e4a4a6dd1527d416f75ac70416cf6d8f131455 Mon Sep 17 00:00:00 2001 From: Sam Germain Date: Sat, 29 Jan 2022 03:45:01 -0600 Subject: [PATCH 1/3] binance.get_max_leverage_fix --- freqtrade/exchange/binance.py | 8 ++-- tests/exchange/test_binance.py | 68 ++++++++++++++++++---------------- 2 files changed, 42 insertions(+), 34 deletions(-) diff --git a/freqtrade/exchange/binance.py b/freqtrade/exchange/binance.py index 6fcead08c..715c9fb04 100644 --- a/freqtrade/exchange/binance.py +++ b/freqtrade/exchange/binance.py @@ -155,19 +155,21 @@ class Binance(Exchange): except ccxt.BaseError as e: raise OperationalException(e) from e - def get_max_leverage(self, pair: Optional[str], nominal_value: Optional[float]) -> float: + def get_max_leverage(self, pair: Optional[str], margin: Optional[float]) -> float: """ Returns the maximum leverage that a pair can be traded at :param pair: The base/quote currency pair being traded - :nominal_value: The total value of the trade in quote currency (collateral + debt) + :margin: The total value of the traders collateral in quote currency """ if pair not in self._leverage_brackets: return 1.0 pair_brackets = self._leverage_brackets[pair] max_lev = 1.0 for [min_amount, margin_req] in pair_brackets: + lev = 1/margin_req + nominal_value = margin * lev if nominal_value >= min_amount: - max_lev = 1/margin_req + max_lev = lev return max_lev @retrier diff --git a/tests/exchange/test_binance.py b/tests/exchange/test_binance.py index ac7647e73..1c377c70a 100644 --- a/tests/exchange/test_binance.py +++ b/tests/exchange/test_binance.py @@ -162,43 +162,49 @@ def test_stoploss_adjust_binance(mocker, default_conf, sl1, sl2, sl3, side): assert not exchange.stoploss_adjust(sl3, order, side=side) -@pytest.mark.parametrize('pair,nominal_value,max_lev', [ +@pytest.mark.parametrize('pair,stake_amount,max_lev', [ ("BNB/BUSD", 0.0, 40.0), - ("BNB/USDT", 100.0, 153.84615384615384), + ("BNB/USDT", 100.0, 100.0), ("BTC/USDT", 170.30, 250.0), - ("BNB/BUSD", 999999.9, 10.0), - ("BNB/USDT", 5000000.0, 6.666666666666667), - ("BTC/USDT", 300000000.1, 2.0), + ("BNB/BUSD", 99999.9, 10.0), + ("BNB/USDT", 750000, 6.666666666666667), + ("BTC/USDT", 150000000.1, 2.0), ]) -def test_get_max_leverage_binance(default_conf, mocker, pair, nominal_value, max_lev): +def test_get_max_leverage_binance(default_conf, mocker, pair, stake_amount, max_lev): exchange = get_patched_exchange(mocker, default_conf, id="binance") exchange._leverage_brackets = { - 'BNB/BUSD': [[0.0, 0.025], - [100000.0, 0.05], - [500000.0, 0.1], - [1000000.0, 0.15], - [2000000.0, 0.25], - [5000000.0, 0.5]], - 'BNB/USDT': [[0.0, 0.0065], - [10000.0, 0.01], - [50000.0, 0.02], - [250000.0, 0.05], - [1000000.0, 0.1], - [2000000.0, 0.125], - [5000000.0, 0.15], - [10000000.0, 0.25]], - 'BTC/USDT': [[0.0, 0.004], - [50000.0, 0.005], - [250000.0, 0.01], - [1000000.0, 0.025], - [5000000.0, 0.05], - [20000000.0, 0.1], - [50000000.0, 0.125], - [100000000.0, 0.15], - [200000000.0, 0.25], - [300000000.0, 0.5]], + 'BNB/BUSD': [ + [0.0, 0.025], # lev = 40.0 + [100000.0, 0.05], # lev = 20.0 + [500000.0, 0.1], # lev = 10.0 + [1000000.0, 0.15], # lev = 6.666666666666667 + [2000000.0, 0.25], # lev = 4.0 + [5000000.0, 0.5], # lev = 2.0 + ], + 'BNB/USDT': [ + [0.0, 0.0065], # lev = 153.84615384615384 + [10000.0, 0.01], # lev = 100.0 + [50000.0, 0.02], # lev = 50.0 + [250000.0, 0.05], # lev = 20.0 + [1000000.0, 0.1], # lev = 10.0 + [2000000.0, 0.125], # lev = 8.0 + [5000000.0, 0.15], # lev = 6.666666666666667 + [10000000.0, 0.25], # lev = 4.0 + ], + 'BTC/USDT': [ + [0.0, 0.004], # lev = 250.0 + [50000.0, 0.005], # lev = 200.0 + [250000.0, 0.01], # lev = 100.0 + [1000000.0, 0.025], # lev = 40.0 + [5000000.0, 0.05], # lev = 20.0 + [20000000.0, 0.1], # lev = 10.0 + [50000000.0, 0.125], # lev = 8.0 + [100000000.0, 0.15], # lev = 6.666666666666667 + [200000000.0, 0.25], # lev = 4.0 + [300000000.0, 0.5], # lev = 2.0 + ], } - assert exchange.get_max_leverage(pair, nominal_value) == max_lev + assert exchange.get_max_leverage(pair, stake_amount) == max_lev def test_fill_leverage_brackets_binance(default_conf, mocker): From a368f8b322a1f4ec12da19ca3631651893755625 Mon Sep 17 00:00:00 2001 From: Sam Germain Date: Mon, 31 Jan 2022 04:47:52 -0600 Subject: [PATCH 2/3] exchange.get_max_leverage changed variable names, made more effecient --- freqtrade/exchange/binance.py | 8 +++++--- freqtrade/exchange/exchange.py | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/freqtrade/exchange/binance.py b/freqtrade/exchange/binance.py index 715c9fb04..d9eef4170 100644 --- a/freqtrade/exchange/binance.py +++ b/freqtrade/exchange/binance.py @@ -155,11 +155,11 @@ class Binance(Exchange): except ccxt.BaseError as e: raise OperationalException(e) from e - def get_max_leverage(self, pair: Optional[str], margin: Optional[float]) -> float: + def get_max_leverage(self, pair: str, stake_amount: Optional[float]) -> float: """ Returns the maximum leverage that a pair can be traded at :param pair: The base/quote currency pair being traded - :margin: The total value of the traders collateral in quote currency + :stake_amount: The total value of the traders collateral in quote currency """ if pair not in self._leverage_brackets: return 1.0 @@ -167,9 +167,11 @@ class Binance(Exchange): max_lev = 1.0 for [min_amount, margin_req] in pair_brackets: lev = 1/margin_req - nominal_value = margin * lev + nominal_value = stake_amount * lev if nominal_value >= min_amount: max_lev = lev + else: + break return max_lev @retrier diff --git a/freqtrade/exchange/exchange.py b/freqtrade/exchange/exchange.py index 58321a2db..e737d2b2a 100644 --- a/freqtrade/exchange/exchange.py +++ b/freqtrade/exchange/exchange.py @@ -1807,7 +1807,7 @@ class Exchange: """ return - def get_max_leverage(self, pair: Optional[str], nominal_value: Optional[float]) -> float: + def get_max_leverage(self, pair: str, stake_amount: Optional[float]) -> float: """ Returns the maximum leverage that a pair can be traded at :param pair: The base/quote currency pair being traded From 8190b0d83b93a95145bb3486b2045da5f5aec245 Mon Sep 17 00:00:00 2001 From: Sam Germain Date: Mon, 31 Jan 2022 12:49:18 -0600 Subject: [PATCH 3/3] binance.get_max_leverage adjustment --- freqtrade/exchange/binance.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/freqtrade/exchange/binance.py b/freqtrade/exchange/binance.py index d9eef4170..a922d9c0d 100644 --- a/freqtrade/exchange/binance.py +++ b/freqtrade/exchange/binance.py @@ -164,15 +164,20 @@ class Binance(Exchange): if pair not in self._leverage_brackets: return 1.0 pair_brackets = self._leverage_brackets[pair] - max_lev = 1.0 - for [min_amount, margin_req] in pair_brackets: + num_brackets = len(pair_brackets) + min_amount = 0 + for bracket_num in range(num_brackets): + [_, margin_req] = pair_brackets[bracket_num] lev = 1/margin_req - nominal_value = stake_amount * lev - if nominal_value >= min_amount: - max_lev = lev + if bracket_num+1 != num_brackets: # If not on last bracket + [min_amount, _] = pair_brackets[bracket_num+1] # Get min_amount of next bracket else: - break - return max_lev + return lev + nominal_value = stake_amount * lev + # Bracket is good if the leveraged trade value doesnt exceed min_amount of next bracket + if nominal_value < min_amount: + return lev + return 1.0 # default leverage @retrier def _set_leverage(