moved liquidation_buffer to exchange class, add check for valid liquidation_buffer values

This commit is contained in:
Sam Germain
2022-02-10 21:14:07 -06:00
committed by Matthias
parent 305d3738d9
commit 3c3675ea1a
3 changed files with 20 additions and 11 deletions

View File

@@ -135,13 +135,18 @@ class Exchange:
self._trades_pagination = self._ft_has['trades_pagination']
self._trades_pagination_arg = self._ft_has['trades_pagination_arg']
# Leverage properties
self.trading_mode = TradingMode(config.get('trading_mode', 'spot'))
self.margin_mode: Optional[MarginMode] = (
MarginMode(config.get('margin_mode'))
if config.get('margin_mode')
else None
)
self.liquidation_buffer = config.get('liquidation_buffer', 0.05)
if self.liquidation_buffer < 0.0:
raise OperationalException('Cannot have a negative liquidation_buffer')
if self.liquidation_buffer > 0.99:
raise OperationalException('Liquidation_buffer must be below 0.99')
# Initialize ccxt objects
ccxt_config = self._ccxt_config
@@ -2062,7 +2067,7 @@ class Exchange:
if self._config['dry_run'] or not self.exchange_has("fetchPositions"):
return self.dry_run_liquidation_price(
isolated_liq = self.dry_run_liquidation_price(
pair=pair,
open_rate=open_rate,
is_short=is_short,
@@ -2076,7 +2081,7 @@ class Exchange:
positions = self._api.fetch_positions([pair])
if len(positions) > 0:
pos = positions[0]
return pos['liquidationPrice']
isolated_liq = pos['liquidationPrice']
else:
return None
except ccxt.DDoSProtection as e:
@@ -2087,6 +2092,17 @@ class Exchange:
except ccxt.BaseError as e:
raise OperationalException(e) from e
if isolated_liq:
buffer_amount = abs(open_rate - isolated_liq) * self.liquidation_buffer
isolated_liq = (
isolated_liq - buffer_amount
if is_short else
isolated_liq + buffer_amount
)
return isolated_liq
else:
return None
def get_maintenance_ratio_and_amt(
self,
pair: str,

View File

@@ -758,14 +758,6 @@ class FreqtradeBot(LoggingMixin):
funding_fees = self.exchange.get_funding_fees(
pair=pair, amount=amount, is_short=is_short, open_date=open_date)
# This is a new trade
if isolated_liq:
liquidation_buffer = abs(enter_limit_filled_price -
isolated_liq) * self.liquidation_buffer
isolated_liq = (
isolated_liq - liquidation_buffer
if is_short else
isolated_liq + liquidation_buffer
)
if trade is None:
trade = Trade(
pair=pair,