customized set_leverage for different exchanges

This commit is contained in:
Sam Germain 2021-09-12 02:42:13 -06:00
parent bc102d57c9
commit ad44048e29
5 changed files with 40 additions and 18 deletions

View File

@ -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

View File

@ -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

View File

@ -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']

View File

@ -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",

View File

@ -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