Administrator
4 days ago 01da1f754426a1285fc20b9cf1b80672b16d8814
refactor(gateApi): 重构网格交易服务添加补仓重试机制
6 files modified
271 ■■■■■ changed files
src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java 59 ●●●● patch | view | raw | blame | history
src/main/java/com/xcong/excoin/modules/gateApi/GateTradeExecutor.java 49 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/excoin/modules/gateApi/gateApi-logic.md 125 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/excoin/modules/gateApi/wsHandler/handler/CandlestickChannelHandler.java 8 ●●●● patch | view | raw | blame | history
src/main/java/com/xcong/excoin/modules/gateApi/wsHandler/handler/PositionClosesChannelHandler.java 12 ●●●● patch | view | raw | blame | history
src/main/java/com/xcong/excoin/modules/gateApi/wsHandler/handler/PositionsChannelHandler.java 18 ●●●● patch | view | raw | blame | history
src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
@@ -41,6 +41,11 @@
        WAITING_KLINE, OPENING, ACTIVE, REOPENING_LONG, REOPENING_SHORT, STOPPED
    }
    private static final String AUTO_SIZE_LONG = "close_long";
    private static final String AUTO_SIZE_SHORT = "close_short";
    private static final String ORDER_TYPE_CLOSE_LONG = "close-long-position";
    private static final String ORDER_TYPE_CLOSE_SHORT = "close-short-position";
    private final GateConfig config;
    private final GateTradeExecutor executor;
    private final FuturesApi futuresApi;
@@ -59,7 +64,9 @@
    private volatile BigDecimal cumulativePnl = BigDecimal.ZERO;
    private Long userId;
    /** 多头补仓连续失败次数 */
    private int longReopenFails = 0;
    /** 空头补仓连续失败次数 */
    private int shortReopenFails = 0;
    public GateGridTradeService(GateConfig config) {
@@ -130,12 +137,15 @@
                FuturesOrder closeOrder = new FuturesOrder();
                closeOrder.setContract(config.getContract());
                closeOrder.setSize(closeSize);
                closeOrder.setPrice("0");
                closeOrder.setTif(FuturesOrder.TifEnum.IOC);
                closeOrder.setReduceOnly(true);
                if (mode != null && mode.getValue() != null && mode.getValue().contains("dual")) {
                    closeOrder.setSize("0");
                    closeOrder.setClose(false);
                    closeOrder.setAutoSize(isLong ? FuturesOrder.AutoSizeEnum.LONG : FuturesOrder.AutoSizeEnum.SHORT);
                } else {
                    closeOrder.setSize(closeSize);
                }
                closeOrder.setText("t-grid-init-close");
                futuresApi.createFuturesOrder(SETTLE, closeOrder, null);
@@ -187,47 +197,48 @@
                longActive = true;
            }
            executor.placeTakeProfit(longTpPrice(), FuturesPriceTrigger.RuleEnum.NUMBER_1,
                    "close-long-position", "close_long");
        });
                    ORDER_TYPE_CLOSE_LONG, AUTO_SIZE_LONG);
        }, null);
        executor.openShort(negate(config.getQuantity()), () -> {
            synchronized (this) {
                shortEntryPrice = lastKlinePrice;
                shortActive = true;
            }
            executor.placeTakeProfit(shortTpPrice(), FuturesPriceTrigger.RuleEnum.NUMBER_2,
                    "close-short-position", "close_short");
                    ORDER_TYPE_CLOSE_SHORT, AUTO_SIZE_SHORT);
            if (longActive && shortActive && state != StrategyState.STOPPED) {
                state = StrategyState.ACTIVE;
                log.info("[Gate] 已激活, 多头入场:{}, 空头入场:{}, 多头止盈:{}, 空头止盈:{}",
                        longEntryPrice, shortEntryPrice, longTpPrice(), shortTpPrice());
            }
        });
        }, null);
    }
    /**
     * 仓位推送回调。检测 size=0 触发补仓。
     */
    public void onPositionUpdate(String contract, String mode, BigDecimal size, BigDecimal entryPrice) {
    public void onPositionUpdate(String contract, Position.ModeEnum mode, BigDecimal size,
                                  BigDecimal entryPrice) {
        if (state == StrategyState.STOPPED || state == StrategyState.WAITING_KLINE) {
            return;
        }
        boolean hasPosition = size.abs().compareTo(BigDecimal.ZERO) > 0;
        if ("dual_long".equals(mode)) {
        if (Position.ModeEnum.DUAL_LONG == mode) {
            if (longActive && !hasPosition) {
                log.info("[Gate] 多头已平仓");
                longActive = false;
                tryReopenLong(0);
                tryReopenLong();
            } else if (hasPosition) {
                longActive = true;
                longEntryPrice = entryPrice;
            }
        } else if ("dual_short".equals(mode)) {
        } else if (Position.ModeEnum.DUAL_SHORT == mode) {
            if (shortActive && !hasPosition) {
                log.info("[Gate] 空头已平仓");
                shortActive = false;
                tryReopenShort(0);
                tryReopenShort();
            } else if (hasPosition) {
                shortActive = true;
                shortEntryPrice = entryPrice;
@@ -254,13 +265,20 @@
        }
    }
    // ---- 补仓(含重试) ----
    // ---- 补仓(含失败重试) ----
    private void tryReopenLong(int retry) {
    private void tryReopenLong() {
        if (state == StrategyState.STOPPED) {
            return;
        }
        if (longActive) {
            return;
        }
        longReopenFails++;
        if (longReopenFails > config.getReopenMaxRetries()) {
            log.warn("[Gate] 多头补仓连续失败{}次,停止策略", longReopenFails);
            state = StrategyState.STOPPED;
            return;
        }
@@ -271,20 +289,27 @@
                longActive = true;
            }
            executor.placeTakeProfit(longTpPrice(), FuturesPriceTrigger.RuleEnum.NUMBER_1,
                    "close-long-position", "close_long");
                    ORDER_TYPE_CLOSE_LONG, AUTO_SIZE_LONG);
            longReopenFails = 0;
            if (state != StrategyState.STOPPED) {
                state = StrategyState.ACTIVE;
            }
            log.info("[Gate] 多头已补开, 价格:{}", longEntryPrice);
        });
        }, this::tryReopenLong);
    }
    private void tryReopenShort(int retry) {
    private void tryReopenShort() {
        if (state == StrategyState.STOPPED) {
            return;
        }
        if (shortActive) {
            return;
        }
        shortReopenFails++;
        if (shortReopenFails > config.getReopenMaxRetries()) {
            log.warn("[Gate] 空头补仓连续失败{}次,停止策略", shortReopenFails);
            state = StrategyState.STOPPED;
            return;
        }
@@ -295,13 +320,13 @@
                shortActive = true;
            }
            executor.placeTakeProfit(shortTpPrice(), FuturesPriceTrigger.RuleEnum.NUMBER_2,
                    "close-short-position", "close_short");
                    ORDER_TYPE_CLOSE_SHORT, AUTO_SIZE_SHORT);
            shortReopenFails = 0;
            if (state != StrategyState.STOPPED) {
                state = StrategyState.ACTIVE;
            }
            log.info("[Gate] 空头已补开, 价格:{}", shortEntryPrice);
        });
        }, this::tryReopenShort);
    }
    // ---- 止盈价格计算 ----
src/main/java/com/xcong/excoin/modules/gateApi/GateTradeExecutor.java
@@ -84,55 +84,38 @@
    }
    /**
     * 异步市价开多。
     * <p>创建 IOC 市价单(price=0),数量为正数。成功后调用 onSuccess 回调。
     *
     * @param quantity  数量(正数,如 "10")
     * @param onSuccess 成功后回调,在交易线程中执行
     * 异步市价开多。quantity 为正数(如 "10")。
     */
    public void openLong(String quantity, Runnable onSuccess) {
        executor.execute(() -> {
            try {
                FuturesOrder order = new FuturesOrder();
                order.setContract(contract);
                order.setSize(quantity);
                order.setPrice("0");
                order.setTif(FuturesOrder.TifEnum.IOC);
                order.setText("t-grid-long");
                FuturesOrder result = futuresApi.createFuturesOrder(SETTLE, order, null);
                log.info("[TradeExec] 开多成功, 价格:{}, id:{}", result.getFillPrice(), result.getId());
                if (onSuccess != null) {
                    onSuccess.run();
                }
            } catch (Exception e) {
                log.error("[TradeExec] 开多失败", e);
            }
        });
    public void openLong(String quantity, Runnable onSuccess, Runnable onFailure) {
        openPosition(quantity, "t-grid-long", "开多", onSuccess, onFailure);
    }
    /**
     * 异步市价开空。
     * <p>创建 IOC 市价单(price=0),size 需为负数。
     *
     * @param negQuantity 负数数量(如 "-10")
     * @param onSuccess   成功后回调
     * 异步市价开空。quantity 为负数(如 "-10")。
     */
    public void openShort(String negQuantity, Runnable onSuccess) {
    public void openShort(String quantity, Runnable onSuccess, Runnable onFailure) {
        openPosition(quantity, "t-grid-short", "开空", onSuccess, onFailure);
    }
    private void openPosition(String size, String text, String label, Runnable onSuccess, Runnable onFailure) {
        executor.execute(() -> {
            try {
                FuturesOrder order = new FuturesOrder();
                order.setContract(contract);
                order.setSize(negQuantity);
                order.setSize(size);
                order.setPrice("0");
                order.setTif(FuturesOrder.TifEnum.IOC);
                order.setText("t-grid-short");
                order.setText(text);
                FuturesOrder result = futuresApi.createFuturesOrder(SETTLE, order, null);
                log.info("[TradeExec] 开空成功, 价格:{}, id:{}", result.getFillPrice(), result.getId());
                log.info("[TradeExec] {}成功, 价格:{}, id:{}", label, result.getFillPrice(), result.getId());
                if (onSuccess != null) {
                    onSuccess.run();
                }
            } catch (Exception e) {
                log.error("[TradeExec] 开空失败", e);
                log.error("[TradeExec] {}失败", label, e);
                if (onFailure != null) {
                    onFailure.run();
                }
            }
        });
    }
src/main/java/com/xcong/excoin/modules/gateApi/gateApi-logic.md
@@ -7,8 +7,8 @@
| [GateWebSocketClientManager](#gatewebsocketclientmanager) | `@Component` | Spring 启动入口,组装组件 + 生命周期 |
| [GateConfig](#gateconfig) | 配置 | Builder 模式:API 密钥、合约、策略参数、环境切换 |
| [GateKlineWebSocketClient](#gateklinewebsocketclient) | WS 连接管理 | 连接/心跳/重连/消息路由 |
| [GateGridTradeService](#gategridtradeservice) | 交易服务 | 网格策略状态机 + 盈亏管理 |
| [GateTradeExecutor](#gatetradeexecutor) | 异步执行器 | 独立线程池执行 REST 下单,不阻塞 WS 回调线程 |
| [GateGridTradeService](#gategridtradeservice) | 交易服务 | 网格策略状态机 + 盈亏管理 + 补仓重试 |
| [GateTradeExecutor](#gatetradeexecutor) | 异步执行器 | 独立线程池执行 REST 下单,成功/失败双回调 |
| [GateWebSocketClientMain](#gatewebsocketclientmain) | main 入口 | 独立测试启动 |
| [Example.java](#examplejava) | 示例 | Gate SDK 用法参考 |
@@ -19,7 +19,7 @@
| `wsHandler/GateChannelHandler.java` | **接口** | subscribe / unsubscribe / handleMessage / getChannelName |
| `wsHandler/AbstractPrivateChannelHandler.java` | **抽象类** | 私有频道基类:HMAC-SHA512 签名 + 认证请求 |
| `wsHandler/handler/CandlestickChannelHandler.java` | 公开频道 | K 线解析 → `onKline()` |
| `wsHandler/handler/PositionsChannelHandler.java` | 私有频道 | 仓位推送 → `onPositionUpdate()` |
| `wsHandler/handler/PositionsChannelHandler.java` | 私有频道 | 仓位推送 → `onPositionUpdate()`(传 `Position.ModeEnum`) |
| `wsHandler/handler/PositionClosesChannelHandler.java` | 私有频道 | 平仓推送 → `onPositionClose()` |
---
@@ -36,7 +36,7 @@
│     │ REST BasePath           │ GateTradeExecutor    │ WS URL    │
│     ▼                         ▼                      ▼           │
│  Gate API (REST)        独立线程池 (async)     Gate WebSocket     │
│                                               ┌──────────────┐  │
│                      onSuccess/onFailure双回调  ┌──────────────┐  │
│                                               │Candlestick H  │  │
│                                               │Positions H    │  │
│                                               │PosCloses H    │  │
@@ -62,9 +62,10 @@
├─ futures.positions (私有, HMAC-SHA512)
│   └─ PositionsChannelHandler
│       ├─ 解析 mode → Position.ModeEnum(DUAL_LONG / DUAL_SHORT)
│       └─ gridTradeService.onPositionUpdate(mode, size, entryPrice)
│           ├─ size=0 && longActive → tryReopenLong()
│           └─ size=0 && shortActive → tryReopenShort()
│           ├─ DUAL_LONG, size=0 && longActive → tryReopenLong()
│           └─ DUAL_SHORT, size=0 && shortActive → tryReopenShort()
├─ futures.position_closes (私有, HMAC-SHA512)
│   └─ PositionClosesChannelHandler
@@ -73,7 +74,8 @@
└─ 所有下单操作
    └─ GateTradeExecutor (单线程 + 64队列 + CallerRunsPolicy)
        ├─ openLong/openShort → 市价单 (REST)
        ├─ openLong/Short(qty, onSuccess, onFailure)
        │   └─ 失败回调 → tryReopenXxx() 递归重试
        └─ placeTakeProfit → 条件单 (REST)
```
@@ -85,7 +87,7 @@
GateChannelHandler (接口)
  ├── CandlestickChannelHandler          (公开频道)
  └── AbstractPrivateChannelHandler      (私有频道基类: HMAC-SHA512)
        ├── PositionsChannelHandler
        ├── PositionsChannelHandler       (解析 mode → Position.ModeEnum)
        └── PositionClosesChannelHandler
```
@@ -93,6 +95,7 @@
- **unsubscribe**: 发送取消订阅请求(私有频道也带签名认证)
- **handleMessage**: 解析推送数据并回调GateGridTradeService,返回true表示已处理
- 消息路由: update/all事件 → 遍历channelHandlers → handler内部二次匹配channel名 → 匹配成功回调并停止遍历
- **PositionsChannelHandler 特殊处理**: 推送的 mode 字符串("dual_long")通过 `Position.ModeEnum.fromValue()` 转为枚举,避免调用方用字符串匹配
---
@@ -101,9 +104,11 @@
```
WAITING_KLINE ──onKline──→ OPENING ──双开成功──→ ACTIVE
     │                        │                     │
     │                    双开失败             ├─ size=0 → REOPENING_L/S → ACTIVE
     │                                         ├─ 补仓失败 retry → 仍失败 → STOPPED
     │                                         └─ cumulativePnl≥TP 或 ≤-maxLoss → STOPPED
     │                    双开失败             ├─ size=0 → REOPENING_L/S
     │                                         │   ├─ 补仓成功 → ACTIVE
     │                                         │   └─ 补仓失败 → 递归重试
     │                                         │       └─ 超 reopenMaxRetries → STOPPED
     │                                         └─ cumPnl≥TP 或 ≤-maxLoss → STOPPED
     ▼
   STOPPED ←─────────────────────────────────────────┘
```
@@ -115,19 +120,26 @@
| `ACTIVE` | 网格运行中,等待止盈触发 |
| `REOPENING_LONG` | 正在补开多头 |
| `REOPENING_SHORT` | 正在补开空头 |
| `STOPPED` | 停止(盈利达标 / 亏损超限 / 异常退出) |
| `STOPPED` | 停止(盈利达标 / 亏损超限 / 补仓重试耗尽 / 异常退出) |
---
## 策略时序
### 阶段 1:启动
### 阶段 1:启动与初始化
```
Spring @PostConstruct
  → GateConfig.builder()...build()
  → GateGridTradeService(config)
    → init(): 查ID → 查账户切持仓 → 清旧条件单 → 平已有仓位 → 设杠杆
    → init():
      1. 查用户ID
      2. 查账户 → 如需要切持仓模式
      3. 清除旧止盈止损条件单
      4. 查当前合约所有仓位 → 逐个市价平仓(reduce_only, IOC)
         - 单向持仓: size=相反数平仓
         - 双向持仓: size=0, close=false, autoSize=LONG/SHORT
      5. 设杠杆
  → GateKlineWebSocketClient(config.getWsUrl())
    → addChannelHandler x3 → init() → connect()
      → onOpen: handlers依次subscribe → sendPing
@@ -138,28 +150,35 @@
```
K线推送 → onKline(closePrice) → state=OPENING
  → GateTradeExecutor.openLong → 市价开多 → onSuccess: longActive=true, 下TP单
  → GateTradeExecutor.openShort → 市价开空 → onSuccess: shortActive=true, 下TP单
  → GateTradeExecutor.openLong(qty, onSuccess, null)
    → 市价开多 → onSuccess: longActive=true, placeTakeProfit(多头TP)
  → GateTradeExecutor.openShort(-qty, onSuccess, null)
    → 市价开空 → onSuccess: shortActive=true, placeTakeProfit(空头TP)
  → 双开均完成 → state=ACTIVE
```
### 阶段 3:止盈触发 → 补仓
### 阶段 3:止盈触发 → 补仓(含重试)
```
仓位推送: dual_long, size=0 → longActive且无仓位 → tryReopenLong
  → GateTradeExecutor.openLong → 市价补多 → onSuccess: 下新TP单
仓位推送: mode=DUAL_LONG, size=0 → longActive且无仓位 → tryReopenLong()
  ┌─ longReopenFails++  → 超 reopenMaxRetries → STOPPED
  └─ GateTradeExecutor.openLong(qty,
        onSuccess: failCount=0, ACTIVE, 下新TP单,
        onFailure: → 递归调用 tryReopenLong() 再试
     )
仓位推送: dual_short, size=0 → shortActive且无仓位 → tryReopenShort
  → GateTradeExecutor.openShort → 市价补空 → onSuccess: 下新TP单
仓位推送: mode=DUAL_SHORT, size=0 → 同理 tryReopenShort()
```
> 止盈由 Gate 服务端条件单自动执行。只补被平掉的单方向,另一方不受影响。
> 补仓失败通过 onFailure 回调递归重试,连续失败超过 `reopenMaxRetries`(默认3次)则停止策略。
### 阶段 4:停止
```
平仓推送: pnl=+0.6 → cumulativePnl=0.6 ≥ overallTp → state=STOPPED
平仓推送: pnl=-8.0 → cumulativePnl=-8.0 ≤ -maxLoss → state=STOPPED
补仓连续失败 4 次 → 超过 reopenMaxRetries=3 → state=STOPPED
```
---
@@ -184,22 +203,27 @@
| overallTp | 0.5 USDT | 整体止盈 |
| maxLoss | 7.5 USDT | 最大亏损 |
| quantity | 1 | 下单张数 |
| reopenMaxRetries | 3 | 补仓重试次数 |
| reopenMaxRetries | 3 | 补仓最大重试次数 |
---
## GateTradeExecutor
**角色**: 独立线程池执行 REST API 下单,解决 WebSocket 回调线程阻塞问题。
**角色**: 独立线程池执行 REST API 下单。采用成功/失败双回调模式支持补仓重试。
**线程模型**:
- `ThreadPoolExecutor(1, 1, 60s, LinkedBlockingQueue(64), CallerRunsPolicy)`
- 单线程保序 + 有界队列防堆积 + CallerRuns背压
**回调设计**:
- 每个下单方法接受 `onSuccess` 和 `onFailure` 两个 `Runnable`
- REST 调用成功 → 执行 `onSuccess`(更新状态、下止盈单、重置失败计数)
- REST 调用失败 → 执行 `onFailure`(递归重试入口)
| 方法 | 说明 |
|------|------|
| `openLong(qty, onSuccess)` | 异步 IOC 市价开多,成功回调 |
| `openShort(qty, onSuccess)` | 异步 IOC 市价开空 |
| `openLong(qty, onSuccess, onFailure)` | 异步 IOC 市价开多,双回调 |
| `openShort(qty, onSuccess, onFailure)` | 异步 IOC 市价开空,双回调 |
| `placeTakeProfit(trigger, rule, type, auto)` | 异步条件单。已存在则清除旧单重试 |
| `cancelAllPriceTriggeredOrders()` | 清除所有条件单 |
| `shutdown()` | 等待10秒,超时强制关闭 |
@@ -210,12 +234,31 @@
**角色**: 策略核心,使用 Gate SDK 管理状态和执行下单。
**状态**: StrategyState enum + longActive/shortActive boolean
**状态**: `StrategyState` enum + `longActive`/`shortActive` boolean
**关键常量**(替代字面字符串):
```java
private static final String AUTO_SIZE_LONG = "close_long";
private static final String AUTO_SIZE_SHORT = "close_short";
private static final String ORDER_TYPE_CLOSE_LONG = "close-long-position";
private static final String ORDER_TYPE_CLOSE_SHORT = "close-short-position";
```
**回调方法**:
- `onKline(closePrice)`: 缓存价格,WAITING_KLINE状态下首次触发双开
- `onPositionUpdate(mode, size, entryPrice)`: size=0且方向活跃 → 补仓
- `onPositionUpdate(Position.ModeEnum mode, size, entryPrice)`: `== DUAL_LONG/DUAL_SHORT` 枚举比较,size=0且方向活跃 → 补仓重试
- `onPositionClose(side, pnl)`: 累加盈亏,检查停止条件
**补仓重试逻辑**:
```
tryReopenLong():
  1. longReopenFails++(失败计数递增)
  2. 超过 reopenMaxRetries → STOPPED
  3. openLong(qty,
       onSuccess → failCount=0, ACTIVE, 下新TP单,
       onFailure → 递归 tryReopenLong() 重试
     )
```
**止盈计算**:
@@ -226,30 +269,34 @@
**REST API 调用**:
| 操作 | API | 方法 |
|------|-----|------|
| 获取用户ID | `GET /account/detail` | `AccountApi.getAccountDetail()` |
| 切持仓模式 | `POST /futures/usdt/set_position_mode` | `FuturesApi.setPositionMode()` |
| 查仓位 | `GET /futures/usdt/positions` | `FuturesApi.listPositions()` |
| 市价平仓 | `POST /futures/usdt/orders` (reduce_only, IOC) | `FuturesApi.createFuturesOrder()` |
| 设杠杆 | `POST /futures/usdt/positions/{contract}/leverage` | `FuturesApi.updateContractPositionLeverageCall()` |
| 查账户 | `GET /futures/usdt/accounts` | `FuturesApi.listFuturesAccounts()` |
| 清除条件单 | `DELETE /futures/usdt/price_orders` | `FuturesApi.cancelPriceTriggeredOrderList()` |
| 市价单 | `POST /futures/usdt/orders` (price=0, IOC) | `FuturesApi.createFuturesOrder()` |
| 条件单 | `POST /futures/usdt/price_orders` | `FuturesApi.createPriceTriggeredOrder()` |
| 操作 | API | 方法 | 说明 |
|------|-----|------|------|
| 获取用户ID | `GET /account/detail` | `AccountApi.getAccountDetail()` | |
| 切持仓模式 | `POST /futures/usdt/set_position_mode` | `FuturesApi.setPositionMode()` | |
| 查仓位 | `GET /futures/usdt/positions` | `FuturesApi.listPositions()` | 遍历所有仓位,按合约过滤 |
| 市价平仓 | `POST /futures/usdt/orders` | `FuturesApi.createFuturesOrder()` | reduce_only, IOC。双向: size=0+close=false+autoSize |
| 设杠杆 | `POST /futures/usdt/positions/{contract}/leverage` | `FuturesApi.updateContractPositionLeverageCall()` | |
| 查账户 | `GET /futures/usdt/accounts` | `FuturesApi.listFuturesAccounts()` | |
| 清除条件单 | `DELETE /futures/usdt/price_orders` | `FuturesApi.cancelPriceTriggeredOrderList()` | |
| 市价单 | `POST /futures/usdt/orders` | `FuturesApi.createFuturesOrder()` | price=0, tif=IOC |
| 条件单 | `POST /futures/usdt/price_orders` | `FuturesApi.createPriceTriggeredOrder()` | strategy=0, price_type=0, expiration=0 |
**初始化顺序** (`init()`):
```
1. 获取用户 ID
2. 查账户 → 如需要切持仓模式
3. 清除旧的止盈止损条件单
4. 市价平掉当前合约所有已有仓位(确保从零持仓开始)
4. 查当前合约所有仓位 → 逐个市价平仓
   - 单向持仓(single): size=相反数, reduce_only=true
   - 双向持仓(dual): size=0, close=false, autoSize=LONG/SHORT, reduce_only=true
5. 设杠杆
6. 打印账户余额
```
---
## GateWebSocketClientMain
独立 `main()` 方法入口,通过 Spring XML 上下文启动,运行后手动关闭。
---
src/main/java/com/xcong/excoin/modules/gateApi/wsHandler/handler/CandlestickChannelHandler.java
@@ -76,7 +76,9 @@
    @Override
    public boolean handleMessage(JSONObject response) {
        if (!CHANNEL_NAME.equals(response.getString("channel"))) return false;
        if (!CHANNEL_NAME.equals(response.getString("channel"))) {
            return false;
        }
        try {
            JSONArray resultArray = response.getJSONArray("result");
            if (resultArray == null || resultArray.isEmpty()) { log.warn("[{}] 数据为空", CHANNEL_NAME); return true; }
@@ -91,7 +93,9 @@
                    data.getBooleanValue("w"));
            log.info("==================================");
            if (gridTradeService != null) gridTradeService.onKline(closePx);
            if (gridTradeService != null) {
                gridTradeService.onKline(closePx);
            }
        } catch (Exception e) { log.error("[{}] 处理数据失败", CHANNEL_NAME, e); }
        return true;
    }
src/main/java/com/xcong/excoin/modules/gateApi/wsHandler/handler/PositionClosesChannelHandler.java
@@ -36,13 +36,19 @@
    @Override
    public boolean handleMessage(JSONObject response) {
        if (!CHANNEL_NAME.equals(response.getString("channel"))) return false;
        if (!CHANNEL_NAME.equals(response.getString("channel"))) {
            return false;
        }
        try {
            JSONArray resultArray = response.getJSONArray("result");
            if (resultArray == null || resultArray.isEmpty()) return true;
            if (resultArray == null || resultArray.isEmpty()) {
                return true;
            }
            for (int i = 0; i < resultArray.size(); i++) {
                JSONObject item = resultArray.getJSONObject(i);
                if (!getContract().equals(item.getString("contract"))) continue;
                if (!getContract().equals(item.getString("contract"))) {
                    continue;
                }
                BigDecimal pnl = new BigDecimal(item.getString("pnl"));
                String side = item.getString("side");
                log.info("[{}] 平仓更新, 方向:{}, 盈亏:{}", CHANNEL_NAME, side, pnl);
src/main/java/com/xcong/excoin/modules/gateApi/wsHandler/handler/PositionsChannelHandler.java
@@ -4,6 +4,7 @@
import com.alibaba.fastjson.JSONObject;
import com.xcong.excoin.modules.gateApi.GateGridTradeService;
import com.xcong.excoin.modules.gateApi.wsHandler.AbstractPrivateChannelHandler;
import io.gate.gateapi.models.Position;
import lombok.extern.slf4j.Slf4j;
import java.math.BigDecimal;
@@ -37,17 +38,24 @@
    @Override
    public boolean handleMessage(JSONObject response) {
        if (!CHANNEL_NAME.equals(response.getString("channel"))) return false;
        if (!CHANNEL_NAME.equals(response.getString("channel"))) {
            return false;
        }
        try {
            JSONArray resultArray = response.getJSONArray("result");
            if (resultArray == null || resultArray.isEmpty()) return true;
            if (resultArray == null || resultArray.isEmpty()) {
                return true;
            }
            for (int i = 0; i < resultArray.size(); i++) {
                JSONObject pos = resultArray.getJSONObject(i);
                if (!getContract().equals(pos.getString("contract"))) continue;
                String mode = pos.getString("mode");
                if (!getContract().equals(pos.getString("contract"))) {
                    continue;
                }
                String modeStr = pos.getString("mode");
                Position.ModeEnum mode = Position.ModeEnum.fromValue(modeStr);
                BigDecimal size = new BigDecimal(pos.getString("size"));
                BigDecimal entryPrice = new BigDecimal(pos.getString("entry_price"));
                log.info("[{}] 持仓更新, 模式:{}, 数量:{}, 入场价:{}", CHANNEL_NAME, mode, size, entryPrice);
                log.info("[{}] 持仓更新, 模式:{}, 数量:{}, 入场价:{}", CHANNEL_NAME, modeStr, size, entryPrice);
                if (getGridTradeService() != null) {
                    getGridTradeService().onPositionUpdate(getContract(), mode, size, entryPrice);
                }