Add handle_insufficient exception

This commit is contained in:
Matthias 2020-08-22 15:48:00 +02:00
parent 3b4446339e
commit fd33282eb1
2 changed files with 29 additions and 9 deletions

View File

@ -269,6 +269,27 @@ class FreqtradeBot:
self.update_trade_state(trade, order.order_id, self.update_trade_state(trade, order.order_id,
order.ft_order_side == 'stoploss') order.ft_order_side == 'stoploss')
def handle_insufficient_funds(self, trade: Trade):
"""
"""
sell_order = trade.select_order('sell', None)
if sell_order:
self.refind_lost_order(trade)
else:
self.reupdate_buy_order_fees(trade)
# See if we ever opened a sell order for this
# If not, try update buy fees
def reupdate_buy_order_fees(self, trade: Trade):
"""
"""
logger.info(f"Trying to reupdate buy fees for {trade}")
order = trade.select_order('buy', 'closed')
if order:
logger.info(f"Updating buy-fee on trade {trade} for order {order.order_id}.")
self.update_trade_state(trade, order.order_id)
def refind_lost_order(self, trade): def refind_lost_order(self, trade):
""" """
Try refinding a lost trade. Try refinding a lost trade.
@ -864,9 +885,8 @@ class FreqtradeBot:
return True return True
except InsufficientFundsError as e: except InsufficientFundsError as e:
logger.warning(f"Unable to place stoploss order {e}.") logger.warning(f"Unable to place stoploss order {e}.")
# Try refinding stoploss order # Try to figure out what went wrong
# TODO: Currently disabled to allow testing without this first self.handle_insufficient_funds(trade)
# self.refind_lost_order(trade)
except InvalidOrderException as e: except InvalidOrderException as e:
trade.stoploss_order_id = None trade.stoploss_order_id = None
@ -1221,9 +1241,8 @@ class FreqtradeBot:
) )
except InsufficientFundsError as e: except InsufficientFundsError as e:
logger.warning(f"Unable to place order {e}.") logger.warning(f"Unable to place order {e}.")
# Try refinding "lost" orders # Try to figure out what went wrong
# TODO: Currently disabled to allow testing without this first self.handle_insufficient_funds(trade)
# self.refind_lost_order(trade)
return False return False
order_obj = Order.parse_from_ccxt_object(order, trade.pair, 'sell') order_obj = Order.parse_from_ccxt_object(order, trade.pair, 'sell')

View File

@ -510,12 +510,14 @@ class Trade(_DECL_BASE):
profit_ratio = (close_trade_price / self.open_trade_price) - 1 profit_ratio = (close_trade_price / self.open_trade_price) - 1
return float(f"{profit_ratio:.8f}") return float(f"{profit_ratio:.8f}")
def select_order(self, order_side: str, status: str): def select_order(self, order_side: str, status: Optional[str]):
""" """
Returns latest order for this orderside and status Returns latest order for this orderside and status
Returns None if nothing is found Returns None if nothing is found
""" """
orders = [o for o in self.orders if o.side == order_side and o.status == status] orders = [o for o in self.orders if o.side == order_side]
if status:
orders = [o for o in orders if o.status == status]
if len(orders) > 0: if len(orders) > 0:
return orders[-1] return orders[-1]
else: else:
@ -558,7 +560,6 @@ class Trade(_DECL_BASE):
Returns all closed trades which don't have fees set correctly Returns all closed trades which don't have fees set correctly
""" """
return Trade.get_trades([Trade.fee_close_currency.is_(None), return Trade.get_trades([Trade.fee_close_currency.is_(None),
Trade.id == 100,
Trade.orders.any(), Trade.orders.any(),
Trade.is_open.is_(False), Trade.is_open.is_(False),
]).all() ]).all()