diff --git a/freqtrade/exchange/binance.py b/freqtrade/exchange/binance.py index 178fa49da..4315585b6 100644 --- a/freqtrade/exchange/binance.py +++ b/freqtrade/exchange/binance.py @@ -153,17 +153,20 @@ class Binance(Exchange): return max_lev @retrier - def _set_leverage(self, leverage: float, pair: Optional[str]): + def _set_leverage( + self, + leverage: float, + pair: Optional[str], + trading_mode: Optional[TradingMode] + ): """ Set's the leverage before making a trade, in order to not have the same leverage on every trade """ - if not self.exchange_has("setLeverage"): - # Some exchanges only support one collateral type - return + trading_mode = trading_mode or self.trading_mode try: - if self.trading_mode == TradingMode.FUTURES: + if trading_mode == TradingMode.FUTURES: self._api.set_leverage(symbol=pair, leverage=leverage) except ccxt.DDoSProtection as e: raise DDosProtection(e) from e diff --git a/freqtrade/exchange/exchange.py b/freqtrade/exchange/exchange.py index bef8f5e57..dd3304921 100644 --- a/freqtrade/exchange/exchange.py +++ b/freqtrade/exchange/exchange.py @@ -778,7 +778,7 @@ class Exchange: return dry_order if self.trading_mode != TradingMode.SPOT: - self._set_leverage(pair, leverage) + self._set_leverage(leverage, pair) params = self._params.copy() if time_in_force != 'gtc' and ordertype != 'market': param = self._ft_has.get('time_in_force_parameter', '') @@ -1602,7 +1602,12 @@ class Exchange: return 1.0 @retrier - def _set_leverage(self, leverage: float, pair: Optional[str]): + def _set_leverage( + self, + leverage: float, + pair: Optional[str], + trading_mode: Optional[TradingMode] + ): """ Set's the leverage before making a trade, in order to not have the same leverage on every trade diff --git a/freqtrade/exchange/kraken.py b/freqtrade/exchange/kraken.py index 6981204a4..46f1ab934 100644 --- a/freqtrade/exchange/kraken.py +++ b/freqtrade/exchange/kraken.py @@ -172,9 +172,17 @@ class Kraken(Exchange): """ return float(max(self._leverage_brackets[pair])) - def _set_leverage(self, pair, leverage): + def _set_leverage( + self, + leverage: float, + pair: Optional[str], + trading_mode: Optional[TradingMode] + ): """ Kraken set's the leverage as an option in the order object, so we need to add it to params """ - self._params['leverage'] = leverage + if leverage > 1.0: + self._params['leverage'] = leverage + else: + del self._params['leverage'] diff --git a/tests/exchange/test_exchange.py b/tests/exchange/test_exchange.py index 939e45d63..3231d9811 100644 --- a/tests/exchange/test_exchange.py +++ b/tests/exchange/test_exchange.py @@ -2987,12 +2987,12 @@ def test_apply_leverage_to_stake_amount( assert exchange._apply_leverage_to_stake_amount(stake_amount, leverage) == min_stake_with_lev -@pytest.mark.parametrize("collateral", [ - (Collateral.CROSS), - (Collateral.ISOLATED) +@pytest.mark.parametrize("exchange_name,trading_mode", [ + ("binance", TradingMode.FUTURES), + ("ftx", TradingMode.MARGIN), + ("ftx", TradingMode.FUTURES) ]) -@pytest.mark.parametrize("exchange_name", [("ftx"), ("binance")]) -def test__set_leverage(mocker, default_conf, exchange_name, collateral): +def test__set_leverage(mocker, default_conf, exchange_name, trading_mode): api_mock = MagicMock() api_mock.set_leverage = MagicMock() @@ -3006,7 +3006,8 @@ def test__set_leverage(mocker, default_conf, exchange_name, collateral): "_set_leverage", "set_leverage", pair="XRP/USDT", - leverage=5.0 + leverage=5.0, + trading_mode=trading_mode ) @@ -3014,8 +3015,7 @@ def test__set_leverage(mocker, default_conf, exchange_name, collateral): (Collateral.CROSS), (Collateral.ISOLATED) ]) -@pytest.mark.parametrize("exchange_name", [("ftx"), ("binance")]) -def test_set_margin_mode(mocker, default_conf, exchange_name, collateral): +def test_set_margin_mode(mocker, default_conf, collateral): api_mock = MagicMock() api_mock.set_margin_mode = MagicMock() @@ -3025,7 +3025,7 @@ def test_set_margin_mode(mocker, default_conf, exchange_name, collateral): mocker, default_conf, api_mock, - exchange_name, + "binance", "set_margin_mode", "set_margin_mode", pair="XRP/USDT", diff --git a/tests/exchange/test_kraken.py b/tests/exchange/test_kraken.py index 74a06c96c..1a712fd3f 100644 --- a/tests/exchange/test_kraken.py +++ b/tests/exchange/test_kraken.py @@ -305,3 +305,9 @@ def test_fill_leverage_brackets_kraken(default_conf, mocker): 'XLTCUSDT': [1], 'LTC/ETH': [1] } + + +def test_kraken__set_leverage(default_conf, mocker): + exchange = get_patched_exchange(mocker, default_conf, id="kraken") + exchange._set_leverage(3) + assert exchange.params['leverage'] == 3