Extract amount checking to wallets, implement for futures

This commit is contained in:
Matthias 2023-04-25 17:46:58 +02:00
parent 974cf6c365
commit 24cab00479
2 changed files with 32 additions and 6 deletions

View File

@ -1063,14 +1063,11 @@ class FreqtradeBot(LoggingMixin):
"""
trades_closed = 0
for trade in trades:
# TODO: get_total currently fails for futures!
wallet_amount = self.wallets.get_total(trade.safe_base_currency)
if wallet_amount < trade.amount:
#
if not self.wallets.check_exit_amount(trade):
logger.warning(
f'Not enough {trade.safe_base_currency} in wallet to exit {trade.pair}. '
f'Amount needed: {trade.amount}, amount available: {wallet_amount}')
f'Not enough {trade.safe_base_currency} in wallet to exit {trade}. '
'Trying to recover.')
self.handle_onexchange_order(trade)
try:

View File

@ -181,6 +181,35 @@ class Wallets:
def get_all_positions(self) -> Dict[str, PositionWallet]:
return self._positions
def _check_exit_amount(self, trade: Trade) -> bool:
if trade.trading_mode != TradingMode.FUTURES:
# Slightly higher offset than in safe_exit_amount.
wallet_amount: float = self.get_total(trade.safe_base_currency) * 0.981
else:
# wallet_amount: float = self.wallets.get_free(trade.safe_base_currency)
position = self._positions.get(trade.pair)
if position is None:
# We don't own anything :O
return False
wallet_amount = position.position
if wallet_amount >= trade.amount:
return True
return False
def check_exit_amount(self, trade: Trade) -> bool:
"""
Checks if the exit amount is available in the wallet.
:param trade: Trade to check
:return: True if the exit amount is available, False otherwise
"""
if not self._check_exit_amount(trade):
# Update wallets just to make sure
self.update()
return self._check_exit_amount(trade)
return True
def get_starting_balance(self) -> float:
"""
Retrieves starting balance - based on either available capital,