Administrator
2026-06-05 1b55621d4dcf3b4ee6b9c4beb81ad69e5b7a5856
src/main/java/com/xcong/excoin/modules/okxNewPrice/OkxGridTradeService.java
@@ -215,6 +215,7 @@
        longPriceQueue.clear();
        currentLongOrderIds.clear();
        currentShortOrderIds.clear();
        stopLossManager.resetAllEntryQuantities();
        // 每次重启重新获取当前本金,确保盈亏对比基准正确
        refreshInitialPrincipal();
@@ -269,11 +270,9 @@
        updateUnrealizedPnl();
        if (state == StrategyState.STOPPED) {
            executor.cancelAllAlgoOrders();
            closeExistingPositions();
            // stopGrid() 已做清理,仅打印日志不重复操作
            BigDecimal totalPnl = cumulativePnl.add(unrealizedPnl);
            log.info("[OKX] 已实现:{}, 未实现:{}, 合计:{}", cumulativePnl, unrealizedPnl, totalPnl);
            startGrid();
            return;
        }
@@ -363,7 +362,6 @@
            }
            } else {
                if (longActive && state == StrategyState.ACTIVE) {
                    log.info("[OKX] 多仓持仓归零,重置策略");
                    handlePositionZeroAndReset("多仓");
                }
                longActive = false;
@@ -385,7 +383,6 @@
            }
            } else {
                if (shortActive && state == StrategyState.ACTIVE) {
                    log.info("[OKX] 空仓持仓归零,重置策略");
                    handlePositionZeroAndReset("空仓");
                }
                shortActive = false;
@@ -436,6 +433,7 @@
        if (shortGridElement != null && shortGridElement.isHasShortOrder()) {
            int filledQty = Integer.parseInt(shortGridElement.getShortTraderParam().getQuantity());
            stopLossManager.clearShortEntryState(shortGridElement);
            stopLossManager.resetShortEntryQty();
            stopLossManager.extendShortStopLoss(filledQty);
            log.info("[OKX] 空单成交 gridId:{}, qty:{}, 追挂止损", shortGridElement.getId(), filledQty);
            return;
@@ -444,6 +442,7 @@
        if (longGridElement != null && longGridElement.isHasLongOrder()) {
            int filledQty = Integer.parseInt(longGridElement.getLongTraderParam().getQuantity());
            stopLossManager.clearLongEntryState(longGridElement);
            stopLossManager.resetLongEntryQty();
            stopLossManager.extendLongStopLoss(filledQty);
            log.info("[OKX] 多单成交 gridId:{}, qty:{}, 追挂止损", longGridElement.getId(), filledQty);
            return;
@@ -555,7 +554,11 @@
                state = StrategyState.STOPPED;
                closeExistingPositions();
                executor.cancelAllAlgoOrders();
                startGrid();
                // 提交到 executor 末尾:单线程FIFO保证前面所有平仓/取消任务完成后才重置
                executor.submitTask(() -> {
                    try { Thread.sleep(3000); } catch (InterruptedException e) { Thread.currentThread().interrupt(); }
                    startGrid();
                });
            }
        } catch (Exception e) {
            log.warn("[OKX] 盈亏检查失败", e);
@@ -563,10 +566,15 @@
    }
    private void handlePositionZeroAndReset(String direction) {
        log.info("[OKX] {}持仓归零,重置策略", direction);
        state = StrategyState.STOPPED;
        executor.cancelAllAlgoOrders();
        closeExistingPositions();
        startGrid();
        // 提交到 executor 末尾:FIFO保证平仓完成后再重置
        executor.submitTask(() -> {
            try { Thread.sleep(3000); } catch (InterruptedException e) { Thread.currentThread().interrupt(); }
            startGrid();
        });
    }
    // ---- getters ----