| 文件 | 类型 | 说明 |
|---|---|---|
| GateWebSocketClientManager | @Component |
Spring 启动入口,组装组件 + 生命周期 |
| GateConfig | 配置 | Builder 模式:API 密钥、合约、策略参数、环境切换 |
| GateKlineWebSocketClient | WS 连接管理 | 连接/心跳/重连/消息路由 |
| GateGridTradeService | 交易服务 | 网格策略状态机 + 盈亏管理 |
| GateTradeExecutor | 异步执行器 | 独立线程池执行 REST 下单,不阻塞 WS 回调线程 |
| GateWebSocketClientMain | main 入口 | 独立测试启动 |
| Example.java | 示例 | Gate SDK 用法参考 |
| 文件 | 类型 | 说明 |
|---|---|---|
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/PositionClosesChannelHandler.java |
私有频道 | 平仓推送 → onPositionClose() |
┌──────────────────────────────────────────────────────────────────┐
│ GateWebSocketClientManager │
│ (Spring @Component) │
│ │
│ GateConfig.builder() → GateGridTradeService + WS Client │
│ │ │ │ │
│ │ REST BasePath │ GateTradeExecutor │ WS URL │
│ ▼ ▼ ▼ │
│ Gate API (REST) 独立线程池 (async) Gate WebSocket │
│ ┌──────────────┐ │
│ │Candlestick H │ │
│ │Positions H │ │
│ │PosCloses H │ │
│ └──────────────┘ │
└──────────────────────────────────────────────────────────────────┘
WebSocket → GateKlineWebSocketClient.handleMessage → 路由 dispatch
│
├─ futures.pong → cancelPongTimeout
├─ subscribe / unsubscribe / error → log
│
├─ futures.candlesticks (公开)
│ └─ CandlestickChannelHandler
│ └─ gridTradeService.onKline(closePx)
│ ├─ state=WAITING_KLINE → 异步双开 + 止盈单
│ └─ 后续 → 仅缓存 lastKlinePrice
│
├─ futures.positions (私有, HMAC-SHA512)
│ └─ PositionsChannelHandler
│ └─ gridTradeService.onPositionUpdate(mode, size, entryPrice)
│ ├─ size=0 && longActive → tryReopenLong()
│ └─ size=0 && shortActive → tryReopenShort()
│
├─ futures.position_closes (私有, HMAC-SHA512)
│ └─ PositionClosesChannelHandler
│ └─ gridTradeService.onPositionClose(side, pnl)
│ └─ cumulativePnl += pnl → checkStopConditions()
│
└─ 所有下单操作
└─ GateTradeExecutor (单线程 + 64队列 + CallerRunsPolicy)
├─ openLong/openShort → 市价单 (REST)
└─ placeTakeProfit → 条件单 (REST)
GateChannelHandler (接口)
├── CandlestickChannelHandler (公开频道)
└── AbstractPrivateChannelHandler (私有频道基类: HMAC-SHA512)
├── PositionsChannelHandler
└── PositionClosesChannelHandler
WAITING_KLINE ──onKline──→ OPENING ──双开成功──→ ACTIVE
│ │ │
│ 双开失败 ├─ size=0 → REOPENING_L/S → ACTIVE
│ ├─ 补仓失败 retry → 仍失败 → STOPPED
│ └─ cumulativePnl≥TP 或 ≤-maxLoss → STOPPED
▼
STOPPED ←─────────────────────────────────────────┘
| 状态 | 含义 |
|---|---|
WAITING_KLINE |
等待首次K线价格 |
OPENING |
正在异步双开(开多+开空已提交到GateTradeExecutor) |
ACTIVE |
网格运行中,等待止盈触发 |
REOPENING_LONG |
正在补开多头 |
REOPENING_SHORT |
正在补开空头 |
STOPPED |
停止(盈利达标 / 亏损超限 / 异常退出) |
Spring @PostConstruct
→ GateConfig.builder()...build()
→ GateGridTradeService(config)
→ init(): 查ID → 查账户切持仓 → 清旧条件单 → 平已有仓位 → 设杠杆
→ GateKlineWebSocketClient(config.getWsUrl())
→ addChannelHandler x3 → init() → connect()
→ onOpen: handlers依次subscribe → sendPing
→ gridTradeService.startGrid() → state=WAITING_KLINE
K线推送 → onKline(closePrice) → state=OPENING
→ GateTradeExecutor.openLong → 市价开多 → onSuccess: longActive=true, 下TP单
→ GateTradeExecutor.openShort → 市价开空 → onSuccess: shortActive=true, 下TP单
→ 双开均完成 → state=ACTIVE
仓位推送: dual_long, size=0 → longActive且无仓位 → tryReopenLong
→ GateTradeExecutor.openLong → 市价补多 → onSuccess: 下新TP单
仓位推送: dual_short, size=0 → shortActive且无仓位 → tryReopenShort
→ GateTradeExecutor.openShort → 市价补空 → onSuccess: 下新TP单
止盈由 Gate 服务端条件单自动执行。只补被平掉的单方向,另一方不受影响。
平仓推送: pnl=+0.6 → cumulativePnl=0.6 ≥ overallTp → state=STOPPED
平仓推送: pnl=-8.0 → cumulativePnl=-8.0 ≤ -maxLoss → state=STOPPED
角色: 统一配置中心。Builder模式管理所有参数,提供 REST/WS URL 环境自动切换。
核心方法:
- getRestBasePath(): isProduction ? 生产网 : 测试网
- getWsUrl(): 同上
配置项(含默认值):
| 参数 | 默认值 | 说明 |
|---|---|---|
| contract | BTC_USDT | 合约 |
| leverage | 10 | 倍数 |
| marginMode | cross | 全仓 |
| positionMode | dual | 双向持仓 |
| gridRate | 0.0035 | 网格间距 0.35% |
| overallTp | 0.5 USDT | 整体止盈 |
| maxLoss | 7.5 USDT | 最大亏损 |
| quantity | 1 | 下单张数 |
| reopenMaxRetries | 3 | 补仓重试次数 |
角色: 独立线程池执行 REST API 下单,解决 WebSocket 回调线程阻塞问题。
线程模型:
- ThreadPoolExecutor(1, 1, 60s, LinkedBlockingQueue(64), CallerRunsPolicy)
- 单线程保序 + 有界队列防堆积 + CallerRuns背压
| 方法 | 说明 |
|---|---|
openLong(qty, onSuccess) |
异步 IOC 市价开多,成功回调 |
openShort(qty, onSuccess) |
异步 IOC 市价开空 |
placeTakeProfit(trigger, rule, type, auto) |
异步条件单。已存在则清除旧单重试 |
cancelAllPriceTriggeredOrders() |
清除所有条件单 |
shutdown() |
等待10秒,超时强制关闭 |
角色: 策略核心,使用 Gate SDK 管理状态和执行下单。
状态: StrategyState enum + longActive/shortActive boolean
回调方法:
- onKline(closePrice): 缓存价格,WAITING_KLINE状态下首次触发双开
- onPositionUpdate(mode, size, entryPrice): size=0且方向活跃 → 补仓
- onPositionClose(side, pnl): 累加盈亏,检查停止条件
止盈计算:
| 方向 | 公式 | order_type | auto_size |
|---|---|---|---|
| 多头 TP | entry × (1+gridRate) | close-long-position |
close_long |
| 空头 TP | entry × (1-gridRate) | close-short-position |
close_short |
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() |
初始化顺序 (init()): 1. 获取用户 ID 2. 查账户 → 如需要切持仓模式 3. 清除旧的止盈止损条件单 4. 市价平掉当前合约所有已有仓位(确保从零持仓开始) 5. 设杠杆 6. 打印账户余额
独立 main() 方法入口,通过 Spring XML 上下文启动,运行后手动关闭。
Gate SDK 使用示例,展示 FuturesApi 的基本用法。仅作参考。