Pass order object to handle_cancel* methods
This commit is contained in:
+15
-14
@@ -374,8 +374,7 @@ class FreqtradeBot(LoggingMixin):
|
|||||||
fo = order.to_ccxt_object()
|
fo = order.to_ccxt_object()
|
||||||
fo['status'] = 'canceled'
|
fo['status'] = 'canceled'
|
||||||
self.handle_cancel_order(
|
self.handle_cancel_order(
|
||||||
fo, order.order_id, order.trade,
|
fo, order, order.trade, constants.CANCEL_REASON['TIMEOUT']
|
||||||
constants.CANCEL_REASON['TIMEOUT']
|
|
||||||
)
|
)
|
||||||
|
|
||||||
except ExchangeError as e:
|
except ExchangeError as e:
|
||||||
@@ -1357,22 +1356,23 @@ class FreqtradeBot(LoggingMixin):
|
|||||||
)
|
)
|
||||||
):
|
):
|
||||||
self.handle_cancel_order(
|
self.handle_cancel_order(
|
||||||
order, open_order.order_id, trade, constants.CANCEL_REASON['TIMEOUT']
|
order, open_order, trade, constants.CANCEL_REASON['TIMEOUT']
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
self.replace_order(order, open_order, trade)
|
self.replace_order(order, open_order, trade)
|
||||||
|
|
||||||
def handle_cancel_order(self, order: Dict, order_id: str, trade: Trade, reason: str) -> None:
|
def handle_cancel_order(self, order: Dict, order_obj: Order, trade: Trade, reason: str) -> None:
|
||||||
"""
|
"""
|
||||||
Check if current analyzed order timed out and cancel if necessary.
|
Check if current analyzed order timed out and cancel if necessary.
|
||||||
:param order: Order dict grabbed with exchange.fetch_order()
|
:param order: Order dict grabbed with exchange.fetch_order()
|
||||||
|
:param order_obj: Order object from the database.
|
||||||
:param trade: Trade object.
|
:param trade: Trade object.
|
||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
if order['side'] == trade.entry_side:
|
if order['side'] == trade.entry_side:
|
||||||
self.handle_cancel_enter(trade, order, order_id, reason)
|
self.handle_cancel_enter(trade, order, order_obj, reason)
|
||||||
else:
|
else:
|
||||||
canceled = self.handle_cancel_exit(trade, order, order_id, reason)
|
canceled = self.handle_cancel_exit(trade, order, order_obj, reason)
|
||||||
canceled_count = trade.get_canceled_exit_order_count()
|
canceled_count = trade.get_canceled_exit_order_count()
|
||||||
max_timeouts = self.config.get('unfilledtimeout', {}).get('exit_timeout_count', 0)
|
max_timeouts = self.config.get('unfilledtimeout', {}).get('exit_timeout_count', 0)
|
||||||
if (canceled and max_timeouts > 0 and canceled_count >= max_timeouts):
|
if (canceled and max_timeouts > 0 and canceled_count >= max_timeouts):
|
||||||
@@ -1446,7 +1446,7 @@ class FreqtradeBot(LoggingMixin):
|
|||||||
cancel_reason = constants.CANCEL_REASON['USER_CANCEL']
|
cancel_reason = constants.CANCEL_REASON['USER_CANCEL']
|
||||||
if order_obj.price != adjusted_entry_price:
|
if order_obj.price != adjusted_entry_price:
|
||||||
# cancel existing order if new price is supplied or None
|
# cancel existing order if new price is supplied or None
|
||||||
res = self.handle_cancel_enter(trade, order, order_obj.order_id, cancel_reason,
|
res = self.handle_cancel_enter(trade, order, order_obj, cancel_reason,
|
||||||
replacing=replacing)
|
replacing=replacing)
|
||||||
if not res:
|
if not res:
|
||||||
self.replace_order_failed(
|
self.replace_order_failed(
|
||||||
@@ -1487,25 +1487,27 @@ class FreqtradeBot(LoggingMixin):
|
|||||||
|
|
||||||
if order['side'] == trade.entry_side:
|
if order['side'] == trade.entry_side:
|
||||||
self.handle_cancel_enter(
|
self.handle_cancel_enter(
|
||||||
trade, order, open_order.order_id, constants.CANCEL_REASON['ALL_CANCELLED']
|
trade, order, open_order, constants.CANCEL_REASON['ALL_CANCELLED']
|
||||||
)
|
)
|
||||||
|
|
||||||
elif order['side'] == trade.exit_side:
|
elif order['side'] == trade.exit_side:
|
||||||
self.handle_cancel_exit(
|
self.handle_cancel_exit(
|
||||||
trade, order, open_order.order_id, constants.CANCEL_REASON['ALL_CANCELLED']
|
trade, order, open_order, constants.CANCEL_REASON['ALL_CANCELLED']
|
||||||
)
|
)
|
||||||
Trade.commit()
|
Trade.commit()
|
||||||
|
|
||||||
def handle_cancel_enter(
|
def handle_cancel_enter(
|
||||||
self, trade: Trade, order: Dict, order_id: str,
|
self, trade: Trade, order: Dict, order_obj: Order,
|
||||||
reason: str, replacing: Optional[bool] = False
|
reason: str, replacing: Optional[bool] = False
|
||||||
) -> bool:
|
) -> bool:
|
||||||
"""
|
"""
|
||||||
entry cancel - cancel order
|
entry cancel - cancel order
|
||||||
|
:param order_obj: Order object from the database.
|
||||||
:param replacing: Replacing order - prevent trade deletion.
|
:param replacing: Replacing order - prevent trade deletion.
|
||||||
:return: True if trade was fully cancelled
|
:return: True if trade was fully cancelled
|
||||||
"""
|
"""
|
||||||
was_trade_fully_canceled = False
|
was_trade_fully_canceled = False
|
||||||
|
order_id = order_obj.order_id
|
||||||
side = trade.entry_side.capitalize()
|
side = trade.entry_side.capitalize()
|
||||||
|
|
||||||
if order['status'] not in constants.NON_OPEN_EXCHANGE_STATES:
|
if order['status'] not in constants.NON_OPEN_EXCHANGE_STATES:
|
||||||
@@ -1519,8 +1521,7 @@ class FreqtradeBot(LoggingMixin):
|
|||||||
f"Order {order_id} for {trade.pair} not cancelled, "
|
f"Order {order_id} for {trade.pair} not cancelled, "
|
||||||
f"as the filled amount of {filled_val} would result in an unexitable trade.")
|
f"as the filled amount of {filled_val} would result in an unexitable trade.")
|
||||||
return False
|
return False
|
||||||
corder = self.exchange.cancel_order_with_result(order_id, trade.pair,
|
corder = self.exchange.cancel_order_with_result(order_id, trade.pair, trade.amount)
|
||||||
trade.amount)
|
|
||||||
# if replacing, retry fetching the order 3 times if the status is not what we need
|
# if replacing, retry fetching the order 3 times if the status is not what we need
|
||||||
if replacing:
|
if replacing:
|
||||||
retry_count = 0
|
retry_count = 0
|
||||||
@@ -1574,13 +1575,13 @@ class FreqtradeBot(LoggingMixin):
|
|||||||
return was_trade_fully_canceled
|
return was_trade_fully_canceled
|
||||||
|
|
||||||
def handle_cancel_exit(
|
def handle_cancel_exit(
|
||||||
self, trade: Trade, order: Dict, order_id: str,
|
self, trade: Trade, order: Dict, order_obj: Order, reason: str
|
||||||
reason: str
|
|
||||||
) -> bool:
|
) -> bool:
|
||||||
"""
|
"""
|
||||||
exit order cancel - cancel order and update trade
|
exit order cancel - cancel order and update trade
|
||||||
:return: True if exit order was cancelled, false otherwise
|
:return: True if exit order was cancelled, false otherwise
|
||||||
"""
|
"""
|
||||||
|
order_id = order_obj.order_id
|
||||||
cancelled = False
|
cancelled = False
|
||||||
# Cancelled orders may have the status of 'canceled' or 'closed'
|
# Cancelled orders may have the status of 'canceled' or 'closed'
|
||||||
if order['status'] not in constants.NON_OPEN_EXCHANGE_STATES:
|
if order['status'] not in constants.NON_OPEN_EXCHANGE_STATES:
|
||||||
|
|||||||
@@ -795,14 +795,14 @@ class RPC:
|
|||||||
|
|
||||||
if order['side'] == trade.entry_side:
|
if order['side'] == trade.entry_side:
|
||||||
fully_canceled = self._freqtrade.handle_cancel_enter(
|
fully_canceled = self._freqtrade.handle_cancel_enter(
|
||||||
trade, order, oo.order_id, CANCEL_REASON['FORCE_EXIT'])
|
trade, order, oo, CANCEL_REASON['FORCE_EXIT'])
|
||||||
trade_entry_cancelation_res['cancel_state'] = fully_canceled
|
trade_entry_cancelation_res['cancel_state'] = fully_canceled
|
||||||
trade_entry_cancelation_registry.append(trade_entry_cancelation_res)
|
trade_entry_cancelation_registry.append(trade_entry_cancelation_res)
|
||||||
|
|
||||||
if order['side'] == trade.exit_side:
|
if order['side'] == trade.exit_side:
|
||||||
# Cancel order - so it is placed anew with a fresh price.
|
# Cancel order - so it is placed anew with a fresh price.
|
||||||
self._freqtrade.handle_cancel_exit(
|
self._freqtrade.handle_cancel_exit(
|
||||||
trade, order, oo.order_id, CANCEL_REASON['FORCE_EXIT'])
|
trade, order, oo, CANCEL_REASON['FORCE_EXIT'])
|
||||||
|
|
||||||
if all(tocr['cancel_state'] is False for tocr in trade_entry_cancelation_registry):
|
if all(tocr['cancel_state'] is False for tocr in trade_entry_cancelation_registry):
|
||||||
if trade.has_open_orders:
|
if trade.has_open_orders:
|
||||||
@@ -955,7 +955,7 @@ class RPC:
|
|||||||
logger.info(f"Cannot query order for {trade} due to {e}.", exc_info=True)
|
logger.info(f"Cannot query order for {trade} due to {e}.", exc_info=True)
|
||||||
raise RPCException("Order not found.")
|
raise RPCException("Order not found.")
|
||||||
self._freqtrade.handle_cancel_order(
|
self._freqtrade.handle_cancel_order(
|
||||||
order, open_order.order_id, trade, CANCEL_REASON['USER_CANCEL'])
|
order, open_order, trade, CANCEL_REASON['USER_CANCEL'])
|
||||||
Trade.commit()
|
Trade.commit()
|
||||||
|
|
||||||
def _rpc_delete(self, trade_id: int) -> Dict[str, Union[str, int]]:
|
def _rpc_delete(self, trade_id: int) -> Dict[str, Union[str, int]]:
|
||||||
|
|||||||
Reference in New Issue
Block a user