| | |
| | | 每根K线 → onKline → updateUnrealizedPnl → processShortGrid + processLongGrid |
| | | |
| | | 仓位推送(每次开仓成交后自动触发): |
| | | → DUAL_LONG, size>0, 非基底 → 设多头止盈单 entryPrice × (1+gridRate) |
| | | → DUAL_SHORT, size<0, 非基底 → 设空头止盈单 entryPrice × (1-gridRate) |
| | | → DUAL_LONG, size>0, 非基底 → 设多头止盈单:止盈价=entryPrice×(1+gridRate),平仓张数=quantity(负=平多) |
| | | → DUAL_SHORT, size<0, 非基底 → 设空头止盈单:止盈价=entryPrice×(1−gridRate),平仓张数=quantity(正=平空) |
| | | ``` |
| | | |
| | | > 止盈由 Gate 服务端条件单自动执行。服务端监控价格,达到触发价后自动平仓。 |
| | | > 平仓后仓位变为0,盈亏通过 position_closes 频道推送到 cumulativePnl。 |
| | | > 每次网格触发开仓 quantity 张,只在该批张数上设止盈,与之前已设的止盈单互不影响。 |
| | | > 平仓后仓位减少 quantity 张,盈亏通过 position_closes 频道推送到 cumulativePnl。 |
| | | |
| | | ### 阶段 4:停止 |
| | | |
| | |
| | | |------|------| |
| | | | `openLong(qty, onSuccess, onFailure)` | 异步 IOC 市价开多,双回调 | |
| | | | `openShort(qty, onSuccess, onFailure)` | 异步 IOC 市价开空,双回调 | |
| | | | `placeTakeProfit(trigger, rule, type, auto)` | 异步条件单。已存在则清除旧单重试 | |
| | | | `placeTakeProfit(trigger, rule, type, size)` | 异步止盈条件单。size 为显式平仓张数(正=平空,负=平多),多次调用互不影响 | |
| | | | `cancelAllPriceTriggeredOrders()` | 清除所有条件单 | |
| | | | `shutdown()` | 等待10秒,超时强制关闭 | |
| | | |
| | |
| | | |
| | | **关键常量**: |
| | | ```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)`: 更新 lastKlinePrice → 计算 unrealizedPnl → WAITING_KLINE 时触发基底双开,ACTIVE 时驱动 processShortGrid+processLongGrid |
| | | - `onPositionUpdate(contract, mode, size, entryPrice)`: 记录当前入场价和持仓量 → 有仓位时标记活跃+设止盈,无仓位时清空持仓量并标记不活跃 |
| | | - `onPositionUpdate(contract, mode, size, entryPrice)`: 记录当前入场价和持仓量 → 基底:首次成交记录入场价、生成队列;非基底:按 quantity 张数创建独立止盈条件单。无仓位时清空持仓量并标记不活跃 |
| | | - `onPositionClose(contract, side, pnl)`: 累加已实现盈亏,检查停止条件 |
| | | |
| | | **未实现盈亏计算** (`updateUnrealizedPnl()`): |
| | |
| | | |
| | | **止盈计算**: |
| | | |
| | | | 方向 | 公式 | order_type | auto_size | rule | |
| | | | 方向 | 公式 | order_type | size(平仓张数) | rule | |
| | | |------|------|------------|-----------|------| |
| | | | 多头 TP | entry × (1+gridRate) | `close-long-position` | `close_long` | NUMBER_1(≥触发价) | |
| | | | 空头 TP | entry × (1-gridRate) | `close-short-position` | `close_short` | NUMBER_2(≤触发价) | |
| | | | 多头 TP | entry × (1+gridRate) | `close-long-position` | `-quantity`(负=平多) | NUMBER_1(≥触发价) | |
| | | | 空头 TP | entry × (1−gridRate) | `close-short-position` | `+quantity`(正=平空) | NUMBER_2(≤触发价) | |
| | | |
| | | > 止盈单使用显式张数而非 autoSize。每次网格触发开仓 quantity 张,只为该批张数创建独立的条件单,多个止盈单之间互不覆盖。 |
| | | |
| | | **REST API 调用**: |
| | | |
| | |
| | | | 查账户 | `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 | |
| | | | 条件单 | `POST /futures/usdt/price_orders` | `FuturesApi.createPriceTriggeredOrder()` | strategy=0, price_type=0, expiration=0, size=显式张数(非autoSize),多次调用互不冲突 | |
| | | |
| | | **初始化顺序** (`init()`): |
| | | ``` |