binance.get_max_leverage_fix

This commit is contained in:
Sam Germain 2022-01-29 03:45:01 -06:00
parent f10ef7f2da
commit 08e4a4a6dd
2 changed files with 42 additions and 34 deletions

View File

@ -155,19 +155,21 @@ class Binance(Exchange):
except ccxt.BaseError as e: except ccxt.BaseError as e:
raise OperationalException(e) from 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 Returns the maximum leverage that a pair can be traded at
:param pair: The base/quote currency pair being traded :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: if pair not in self._leverage_brackets:
return 1.0 return 1.0
pair_brackets = self._leverage_brackets[pair] pair_brackets = self._leverage_brackets[pair]
max_lev = 1.0 max_lev = 1.0
for [min_amount, margin_req] in pair_brackets: for [min_amount, margin_req] in pair_brackets:
lev = 1/margin_req
nominal_value = margin * lev
if nominal_value >= min_amount: if nominal_value >= min_amount:
max_lev = 1/margin_req max_lev = lev
return max_lev return max_lev
@retrier @retrier

View File

@ -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) 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/BUSD", 0.0, 40.0),
("BNB/USDT", 100.0, 153.84615384615384), ("BNB/USDT", 100.0, 100.0),
("BTC/USDT", 170.30, 250.0), ("BTC/USDT", 170.30, 250.0),
("BNB/BUSD", 999999.9, 10.0), ("BNB/BUSD", 99999.9, 10.0),
("BNB/USDT", 5000000.0, 6.666666666666667), ("BNB/USDT", 750000, 6.666666666666667),
("BTC/USDT", 300000000.1, 2.0), ("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 = get_patched_exchange(mocker, default_conf, id="binance")
exchange._leverage_brackets = { exchange._leverage_brackets = {
'BNB/BUSD': [[0.0, 0.025], 'BNB/BUSD': [
[100000.0, 0.05], [0.0, 0.025], # lev = 40.0
[500000.0, 0.1], [100000.0, 0.05], # lev = 20.0
[1000000.0, 0.15], [500000.0, 0.1], # lev = 10.0
[2000000.0, 0.25], [1000000.0, 0.15], # lev = 6.666666666666667
[5000000.0, 0.5]], [2000000.0, 0.25], # lev = 4.0
'BNB/USDT': [[0.0, 0.0065], [5000000.0, 0.5], # lev = 2.0
[10000.0, 0.01], ],
[50000.0, 0.02], 'BNB/USDT': [
[250000.0, 0.05], [0.0, 0.0065], # lev = 153.84615384615384
[1000000.0, 0.1], [10000.0, 0.01], # lev = 100.0
[2000000.0, 0.125], [50000.0, 0.02], # lev = 50.0
[5000000.0, 0.15], [250000.0, 0.05], # lev = 20.0
[10000000.0, 0.25]], [1000000.0, 0.1], # lev = 10.0
'BTC/USDT': [[0.0, 0.004], [2000000.0, 0.125], # lev = 8.0
[50000.0, 0.005], [5000000.0, 0.15], # lev = 6.666666666666667
[250000.0, 0.01], [10000000.0, 0.25], # lev = 4.0
[1000000.0, 0.025], ],
[5000000.0, 0.05], 'BTC/USDT': [
[20000000.0, 0.1], [0.0, 0.004], # lev = 250.0
[50000000.0, 0.125], [50000.0, 0.005], # lev = 200.0
[100000000.0, 0.15], [250000.0, 0.01], # lev = 100.0
[200000000.0, 0.25], [1000000.0, 0.025], # lev = 40.0
[300000000.0, 0.5]], [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): def test_fill_leverage_brackets_binance(default_conf, mocker):