Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions exchanges/paradex.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,14 +492,16 @@ async def get_order_info(self, order_id: str) -> Optional[OrderInfo]:
try:
# Get order by ID using official SDK
order_data = self.paradex.api_client.fetch_order(order_id)
size = Decimal(order_data.get('size', 0)).quantize(self.order_size_increment, rounding=ROUND_HALF_UP)
remaining_size = size - Decimal(order_data.get('filled_size', 0))
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里你的逻辑是正确的,但是代码可能不小心写错了。

paradex 返回内容没有 filled_size,只有 remaining_size, 所以这里应该:
remaining_size = Decimal(order_data.get('remaining_size', 0))

这里修复了之后也可以避免 partial fill 时无法正确地下止盈单的问题。

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

嗯嗯已修复

return OrderInfo(
order_id=order_data.get('id', ''),
side=order_data.get('side', '').lower(),
size=Decimal(order_data.get('size', 0)).quantize(self.order_size_increment, rounding=ROUND_HALF_UP),
size=size,
price=Decimal(order_data.get('price', 0)),
status=order_data.get('status', ''),
filled_size=Decimal(order_data.get('filled_size', 0)),
remaining_size=Decimal(order_data.get('remaining_size', 0)),
filled_size=size - remaining_size,
remaining_size=remaining_size,
Comment thread
daquexian marked this conversation as resolved.
cancel_reason=order_data.get('cancel_reason', '')
)

Expand Down
2 changes: 0 additions & 2 deletions trading_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,13 +250,11 @@ async def _handle_order_result(self, order_result) -> bool:
try:
cancel_result = await self.exchange_client.cancel_order(order_id)
if not cancel_result.success:
self.order_canceled_event.set()
self.logger.log(f"[CLOSE] Failed to cancel order {order_id}: {cancel_result.error_message}", "ERROR")
else:
self.current_order_status = "CANCELED"

except Exception as e:
self.order_canceled_event.set()
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果 set event,下面会无法获取最新的 order_filled_amount。并且我目前理解出错时不 set event 是更自然的:因为无法知道 cancel 是否成功,所以不在这里直接 set event 而是等待 order_update_handler 来 set(如果理解有误请指正)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这段代码因为需要兼顾三个交易所,所以确实有点绕。我们试图取消 paradex 的订单后的逻辑是这样的:

  1. 如果 paradex 的订单成功取消了,在 paradex.py 的 cancel_order() 中,会回复 cancel_result.success=True,这时的订单状态有两种情况:A. 订单完全没有fill,所以取消就可以了。 B. 订单partial fill,意味着我们需要下一个止盈单。
    在这种情况下,因为 cancel_result.success=True,我们没有在 trading_bot.py 中设定 self.order_canceled_event.set()。是由 websocket 负责设定和获取已成交的订单量,最终下单。
  2. 如果 paradex 的订单没有成功取消了,在排除网络因素的情况下,唯一的可能性是由于整个订单的状态已经是 FILLED。所以我们可以直接跳过 trading_bot.py 中订单取消的5秒等待,直接下一个止盈单。self.order_filled_amount 也是由websocket负责(websocket应该会收到订单成交的信息, 并设定。)

这里的问题是,在第二种情况下,会有race condition。也就是有可能在websocket还没来得及更新,设定self.order_filled_amount 为 order size之前,代码已经到了下止盈单的部分。

我想这也是为什么当延迟比较低时,这个问题不会发生。

Copy link
Copy Markdown
Author

@daquexian daquexian Sep 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

嗯嗯,那按照这个 PR 的改法去掉 event.set() 会有逻辑问题吗

这处改动如果群主觉得不太好也可以只合并另一处 filled_size 的改动,毕竟不止有这一处有网络延迟问题,要解决所有的网络延迟问题就很麻烦了,省事儿的做法还是建议大家都去用 aws 东京(

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

开源项目群策群力,不断发展,赞

self.logger.log(f"[CLOSE] Error canceling order {order_id}: {e}", "ERROR")

if self.config.exchange == "backpack":
Expand Down