Skip to content

Conversation

@daquexian
Copy link

No description provided.

Copy link
Author

@daquexian daquexian left a comment

Choose a reason for hiding this comment

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

mismatch 的原因(至少在我这里)是超时 cancel open 单时由于和服务器之间存在时延,实际 open order 已经成交,虽然 cancel 报错,但代码逻辑却认为 cancel 已完成而没有下 close 单

trading_bot.py Outdated
self.current_order_status = "CANCELED"

except Exception as e:
self.order_canceled_event.set()
Copy link
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
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
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 东京(

Choose a reason for hiding this comment

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

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

# 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
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
Author

Choose a reason for hiding this comment

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

嗯嗯已修复

Signed-off-by: daquexian <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants