Simplify fetch_positions by using already existing method

This commit is contained in:
Matthias 2022-07-30 17:49:06 +02:00
parent fc31c890e3
commit bad15f077c
2 changed files with 16 additions and 31 deletions

View File

@ -1332,11 +1332,19 @@ class Exchange:
raise OperationalException(e) from e raise OperationalException(e) from e
@retrier @retrier
def fetch_positions(self) -> List[Dict]: def fetch_positions(self, pair: str = None) -> List[Dict]:
"""
Fetch positions from the exchange.
If no pair is given, all positions are returned.
:param pair: Pair for the query
"""
if self._config['dry_run'] or self.trading_mode != TradingMode.FUTURES: if self._config['dry_run'] or self.trading_mode != TradingMode.FUTURES:
return [] return []
try: try:
positions: List[Dict] = self._api.fetch_positions() symbols = []
if pair:
symbols.append(pair)
positions: List[Dict] = self._api.fetch_positions(symbols)
self._log_exchange_response('fetch_positions', positions) self._log_exchange_response('fetch_positions', positions)
return positions return positions
except ccxt.DDoSProtection as e: except ccxt.DDoSProtection as e:
@ -2539,7 +2547,6 @@ class Exchange:
else: else:
return 0.0 return 0.0
@retrier
def get_or_calculate_liquidation_price( def get_or_calculate_liquidation_price(
self, self,
pair: str, pair: str,
@ -2573,20 +2580,12 @@ class Exchange:
upnl_ex_1=upnl_ex_1 upnl_ex_1=upnl_ex_1
) )
else: else:
try: positions = self.fetch_positions(pair)
positions = self._api.fetch_positions([pair]) if len(positions) > 0:
if len(positions) > 0: pos = positions[0]
pos = positions[0] isolated_liq = pos['liquidationPrice']
isolated_liq = pos['liquidationPrice'] else:
else: return None
return None
except ccxt.DDoSProtection as e:
raise DDosProtection(e) from e
except (ccxt.NetworkError, ccxt.ExchangeError) as e:
raise TemporaryError(
f'Could not set margin mode due to {e.__class__.__name__}. Message: {e}') from e
except ccxt.BaseError as e:
raise OperationalException(e) from e
if isolated_liq: if isolated_liq:
buffer_amount = abs(open_rate - isolated_liq) * self.liquidation_buffer buffer_amount = abs(open_rate - isolated_liq) * self.liquidation_buffer

View File

@ -4099,20 +4099,6 @@ def test_get_or_calculate_liquidation_price(mocker, default_conf):
) )
assert liq_price == 17.540699999999998 assert liq_price == 17.540699999999998
ccxt_exceptionhandlers(
mocker,
default_conf,
api_mock,
"binance",
"get_or_calculate_liquidation_price",
"fetch_positions",
pair="XRP/USDT",
open_rate=0.0,
is_short=False,
position=0.0,
wallet_balance=0.0,
)
@pytest.mark.parametrize('exchange,rate_start,rate_end,d1,d2,amount,expected_fees', [ @pytest.mark.parametrize('exchange,rate_start,rate_end,d1,d2,amount,expected_fees', [
('binance', 0, 2, "2021-09-01 01:00:00", "2021-09-01 04:00:00", 30.0, 0.0), ('binance', 0, 2, "2021-09-01 01:00:00", "2021-09-01 04:00:00", 30.0, 0.0),