Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kucoin lost order error #6968

Closed
supervik opened this issue Apr 14, 2024 · 2 comments
Closed

Kucoin lost order error #6968

supervik opened this issue Apr 14, 2024 · 2 comments

Comments

@supervik
Copy link
Contributor

Describe the bug

When using Kucoin, the bot sometimes returns an error during order cancellation, and then this order is considered lost. It subsequently tries to cancel it multiple times and prints the 'Error fetching status update for the lost order' error.

The reason for this is that when an order is placed on Kucoin, the exchange sometimes returns an error, such as 'Insufficient balance' or similar, especially during high volatility market events. So, the order is not actually placed on Kucoin, but the bot changes its status to 'Open' and assumes it was successfully opened. In the database, such orders have a None value in the exchange_order_id field

Traceback (most recent call last):
File "/root/bots/hb_1/hummingbot/connector/exchange_py_base.py", line 520, in _execute_order_cancel
cancelled = await self._execute_order_cancel_and_process_update(order=order)
File "/root/bots/hb_1/hummingbot/connector/exchange_py_base.py", line 540, in _execute_order_cancel_and_process_update
cancelled = await self._place_cancel(order.client_order_id, order)
File "/root/bots/hb_1/hummingbot/connector/exchange/kucoin/kucoin_exchange.py", line 241, in _place_cancel
if tracked_order.exchange_order_id in cancel_result["data"].get(response_param, []):
KeyError: 'data'
2024-04-13 06:12:09,382 - 2241164 - hummingbot.connector.exchange.kucoin.kucoin_exchange.KucoinExchange - ERROR - Failed to cancel order SLCUC615ea99733b4513ff7d88742dda44d01b84
2024-04-13 06:12:09,555 - 2241164 - hummingbot.connector.exchange.kucoin.kucoin_exchange.KucoinExchange - WARNING - Error fetching status update for the lost order SLCUC615ea99733b4513ff7d88742dda44d01b84: 'data'.

Steps to reproduce

To reproduce you can simulate insufficient balance error:

  1. Use simple_pmm_example.py script
  2. Edit the line to place the order without adjusting to the budget by changing
    self.place_orders(proposal_adjusted) to self.place_orders(proposal)
  3. Change the exchange to Kucoin: exchange = "kucoin"
  4. Ensure you have 0 assets on the Kucoin account or less than needed for the order's creation
  5. Optionally, add the following line to kucoin_exchange.py to log the response from KuCoin after it placed the order:

After this section of code:

exchange_order_id = await self._api_post(
            path_url=self.orders_path_url,
            data=data,
            is_auth_required=True,
            limit_id=CONSTANTS.POST_ORDER_LIMIT_ID,
        )

add
self.logger().info(f"response = {exchange_order_id}")
7. Run the script and you should see that the order is considered opened and tracked.

The problem could be fixed by checking the response from Kucoin. If the reply doesn't contain the exchange order_id, then do not trigger order creation.

In this screenshot, I have two orders placed: the SELL order is ok, while the BUY order has not opened, but is considered as opened by Hummingbot
test

The database exchange_order_id is None
test2

Release version

1.27

Type of installation

Source

Attach required files

No response

@supervik supervik added the bug label Apr 14, 2024
@rapcmia rapcmia added the kucoin label Apr 15, 2024
@rapcmia
Copy link
Contributor

rapcmia commented Apr 15, 2024

Found this issue happened when testing xemm script using kucoin as maker exchange on #6946

2024-04-12 18:39:46,792 - 865872 - hummingbot.strategy.script_strategy_base - INFO - Creating LOOM-USDT sell order: price: 0.08161463698594319452330082552 amount: 20.
2024-04-12 18:39:46,793 - 865872 - hummingbot.smart_components.executors.xemm_executor.xemm_executor - INFO - Created maker order SLMUT615ea98967b73ff34e4fdb834401cc56895 at price 0.08161463698594319452330082552.
2024-04-12 18:39:46,793 - 865872 - hummingbot.smart_components.executors.xemm_executor.xemm_executor - INFO - Trade profitability 0.008472371962177922893017885208 is above target profitability. Cancelling order.
2024-04-12 18:39:46,794 - 865872 - hummingbot.strategy.script_strategy_base - INFO - (LOOM-USDT) Canceling the limit order BLMUT615ea985958d4ff34e4fdb834401cc56895. [clock=2024-04-12 18:39:46+00:00]
2024-04-12 18:39:46,899 - 865872 - hummingbot.core.event.event_reporter - EVENT_LOG - {"timestamp": 1712947186.0, "order_id": "BLMUT615ea985958d4ff34e4fdb834401cc56895", "exchange_order_id": "66197feec1455c00070e2a91", "event_name": "OrderCancelledEvent", "event_source": "kucoin"}
2024-04-12 18:39:46,900 - 865872 - hummingbot.connector.client_order_tracker - INFO - Successfully canceled order BLMUT615ea985958d4ff34e4fdb834401cc56895.
2024-04-12 18:39:46,968 - 865872 - hummingbot.connector.client_order_tracker - INFO - Created LIMIT SELL order SLMUT615ea98967b73ff34e4fdb834401cc56895 for 20.0000 LOOM-USDT.
2024-04-12 18:39:46,977 - 865872 - hummingbot.smart_components.executors.xemm_executor.xemm_executor - INFO - Maker order SLMUT615ea98967b73ff34e4fdb834401cc56895 created.
2024-04-12 18:39:46,978 - 865872 - hummingbot.core.event.event_reporter - EVENT_LOG - {"timestamp": 1712947186.0, "type": "OrderType.LIMIT", "trading_pair": "LOOM-USDT", "amount": "20.0000", "price": "0.08161", "order_id": "SLMUT615ea98967b73ff34e4fdb834401cc56895", "creation_timestamp": 1712947186.0, "exchange_order_id": "None", "leverage": 1, "position": "NIL", "event_name": "SellOrderCreatedEvent", "event_source": "kucoin"}
2024-04-12 18:39:47,794 - 865872 - hummingbot.smart_components.executors.xemm_executor.xemm_executor - INFO - Trade profitability 0.000689209789620495201559558024 is below minimum profitability. Cancelling order.
2024-04-12 18:39:47,795 - 865872 - hummingbot.strategy.script_strategy_base - INFO - (LOOM-USDT) Canceling the limit order SLMUT615ea98967b73ff34e4fdb834401cc56895. [clock=2024-04-12 18:39:47+00:00]
2024-04-12 18:39:47,799 - 865872 - hummingbot.strategy.script_strategy_base - INFO - Creating LOOM-USDT buy order: price: 0.08087664958279296760661480698 amount: 20.
2024-04-12 18:39:47,800 - 865872 - hummingbot.smart_components.executors.xemm_executor.xemm_executor - INFO - Created maker order BLMUT615ea98a5d8d5ff34e4fdb834401cc56895 at price 0.08087664958279296760661480698.
# trimmed timestamp

2024-04-12 18:39:47,991 - 865872 - hummingbot.connector.exchange.kucoin.kucoin_exchange.KucoinExchange - ERROR - Failed to cancel order SLMUT615ea98967b73ff34e4fdb834401cc56895
Traceback (most recent call last):
  File "/home/ralph/github/hummingbot/6946/hummingbot/connector/exchange_py_base.py", line 520, in _execute_order_cancel
    cancelled = await self._execute_order_cancel_and_process_update(order=order)
  File "/home/ralph/github/hummingbot/6946/hummingbot/connector/exchange_py_base.py", line 540, in _execute_order_cancel_and_process_update
    cancelled = await self._place_cancel(order.client_order_id, order)
  File "/home/ralph/github/hummingbot/6946/hummingbot/connector/exchange/kucoin/kucoin_exchange.py", line 231, in _place_cancel
    if tracked_order.exchange_order_id in cancel_result["data"].get("cancelledOrderIds", []):
KeyError: 'data'
# trimmed timestamp

2024-04-12 18:40:02,550 - 865872 - hummingbot.connector.exchange.kucoin.kucoin_exchange.KucoinExchange - WARNING - Error fetching status update for the active order SLMUT615ea98967b73ff34e4fdb834401cc56895: 'data'.
# trimmed timestamp

2024-04-12 18:46:01,825 - 865872 - hummingbot.connector.exchange.kucoin.kucoin_exchange.KucoinExchange - WARNING - Error fetching status update for the active order SLMUT615ea98967b73ff34e4fdb834401cc56895: 'data'.
2024-04-12 18:46:01,825 - 865872 - hummingbot.connector.client_order_tracker - WARNING - The order SLMUT615ea98967b73ff34e4fdb834401cc56895(None) will be considered lost. Please check its status in the exchange.
2024-04-12 18:46:01,842 - 865872 - hummingbot.core.event.event_reporter - EVENT_LOG - {"timestamp": 1712947560.0, "order_id": "SLMUT615ea98967b73ff34e4fdb834401cc56895", "order_type": "OrderType.LIMIT", "event_name": "MarketOrderFailureEvent", "event_source": "kucoin"}
2024-04-12 18:46:01,843 - 865872 - hummingbot.connector.client_order_tracker - INFO - Order SLMUT615ea98967b73ff34e4fdb834401cc56895 has failed. Order Update: OrderUpdate(trading_pair='LOOM-USDT', update_timestamp=1712947560.0, new_state=<OrderState.FAILED: 6>, client_order_id='SLMUT615ea98967b73ff34e4fdb834401cc56895', exchange_order_id=None, misc_updates=None)
  • 2024-04-12 18:39:46,792 - 865872 Creating order SLMUT615ea98967b73ff34e4fdb834401cc56895. Order created but no ..OrderCreatedEvent
  • 2024-04-12 18:39:46,968 - 865872 Order created twice, this time had a ..OrderCreatedEvent but does not have exchange_order_id
  • 2024-04-12 18:39:47,991 - 865872 Got same KeyError when failed to cancel order
  • 2024-04-12 18:40:02,550 - 865872 Got instances of Error fetching status update for the active order when there is no active order on exchange and only on client
  • 2024-04-12 18:46:01,825 - 865872 Order is now considered as lost order
  • Check sqlite and found record of the order_id and same no exchange_order_id
    image

0412024.zip

@rapcmia
Copy link
Contributor

rapcmia commented May 13, 2024

The fix has been deployed to development and will be part of version 1.28.0

@rapcmia rapcmia closed this as completed May 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants