From 6e4d4f1ef5720b171b49a282db85f92f920e214b Mon Sep 17 00:00:00 2001 From: WrBug Date: Tue, 9 Dec 2025 04:23:23 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E4=BB=93=E4=BD=8D?= =?UTF-8?q?=E8=BD=AE=E8=AF=A2=E6=A3=80=E6=9F=A5=E9=80=BB=E8=BE=91=EF=BC=8C?= =?UTF-8?q?=E9=81=BF=E5=85=8D=E5=88=9A=E5=88=9B=E5=BB=BA=E7=9A=84=E8=AE=A2?= =?UTF-8?q?=E5=8D=95=E8=A2=AB=E8=AF=AF=E5=88=A4=E4=B8=BA=E5=B7=B2=E5=8D=96?= =?UTF-8?q?=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 当仓位不存在时,只有当订单创建时间超过30秒才认为仓位被出售 - 避免因API延迟导致刚创建的订单被误判为已卖出状态 --- .../service/accounts/PositionCheckService.kt | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/backend/src/main/kotlin/com/wrbug/polymarketbot/service/accounts/PositionCheckService.kt b/backend/src/main/kotlin/com/wrbug/polymarketbot/service/accounts/PositionCheckService.kt index 189c44e..95ee012 100644 --- a/backend/src/main/kotlin/com/wrbug/polymarketbot/service/accounts/PositionCheckService.kt +++ b/backend/src/main/kotlin/com/wrbug/polymarketbot/service/accounts/PositionCheckService.kt @@ -372,9 +372,24 @@ class PositionCheckService( val position = positionsByAccountAndMarket[positionKey]?.firstOrNull() if (position == null) { - // 仓位不存在,更新所有订单状态为已卖出 - val currentPrice = getCurrentMarketPrice(marketId, outcomeIndex) - updateOrdersAsSold(orders, currentPrice, copyTrading.id, marketId, outcomeIndex) + // 仓位不存在,检查订单创建时间 + // 只有当订单创建时间超过30秒时,才认为仓位被出售了 + // 这样可以避免刚创建的订单因为API延迟而被误判为已卖出 + val now = System.currentTimeMillis() + val ordersToMarkAsSold = orders.filter { order -> + val orderAge = now - order.createdAt + orderAge > 30000 // 30秒 = 30000毫秒 + } + + if (ordersToMarkAsSold.isNotEmpty()) { + // 有订单创建时间超过30秒,认为仓位已被出售 + val currentPrice = getCurrentMarketPrice(marketId, outcomeIndex) + updateOrdersAsSold(ordersToMarkAsSold, currentPrice, copyTrading.id, marketId, outcomeIndex) + logger.debug("仓位不存在且订单创建时间超过30秒,标记为已卖出: marketId=$marketId, outcomeIndex=$outcomeIndex, orderCount=${ordersToMarkAsSold.size}") + } else { + // 订单创建时间不足30秒,可能是刚创建的订单,暂时不处理 + logger.debug("仓位不存在但订单创建时间不足30秒,暂不标记为已卖出: marketId=$marketId, outcomeIndex=$outcomeIndex, orderCount=${orders.size}, oldestOrderAge=${orders.minOfOrNull { now - it.createdAt }?.let { "${it}ms" } ?: "N/A"}") + } } else { // 有仓位,按订单下单顺序(FIFO)更新状态 // 如果仓位数量 >= 订单数量总和,所有订单完全成交