| | |
| | | import java.math.RoundingMode; |
| | | import java.util.ArrayList; |
| | | import java.util.Collections; |
| | | import java.util.HashMap; |
| | | import java.util.Iterator; |
| | | import java.util.LinkedHashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | |
| | | * <ul> |
| | | * <li><b>条件开仓单</b>:使用 Gate API {@code FuturesPriceTriggeredOrder},服务器监控价格, |
| | | * 达到触发价后以市价 IOC 开仓。相比限价单,条件单仅在触发价到达时才执行,避免提前成交。</li> |
| | | * <li><b>止盈队列</b>(longTakeProfitQueue / shortTakeProfitQueue):每次网格触发时, |
| | | * 将新队列首元素加减 step 作为止盈价加入止盈队列。仓位推送回调中检测到净增张数后, |
| | | * 从止盈队列头部取出止盈价,创建止盈条件单。</li> |
| | | * <li><b>条件单 ID 集合</b>(currentLongOrderIds / currentShortOrderIds): |
| | | * 用同步列表管理所有活跃的条件单 ID。每次网格触发时,取消对方方向的旧条件单(防止堆积), |
| | | * 再挂新的条件单。反向条件单的 ID 也存入对应方向集合统一管理。</li> |
| | | * <li><b>条件单 ID 映射</b>(currentLongOrderIds / currentShortOrderIds): |
| | | * 用同步 Map 管理所有活跃的条件单(订单ID → 止盈价格)。挂条件单时通过回调存入, |
| | | * 订单成交后通过 {@code futures.orders} 推送匹配止盈价并挂止盈单。</li> |
| | | * <li><b>订单订阅(futures.orders)</b>:订单成交(status=finished, finish_as=filled)时, |
| | | * 通过 {@link #onOrderUpdate(String, String, String)} 从 Map 中取出止盈价, |
| | | * 调用 {@code executor.placeTakeProfit} 创建止盈条件单。</li> |
| | | * <li><b>反向条件单</b>:当新网格首元素价格夹在多/空持仓均价之间, |
| | | * 且反向持仓张数不超过 3 张时,额外挂一张反向条件单并加入对方止盈队列。</li> |
| | | * 且反向持仓张数不超过 3 张时,额外挂一张反向市价单,通过订单订阅自动挂止盈。</li> |
| | | * </ul> |
| | | * |
| | | * <h3>状态机</h3> |
| | |
| | | * ├─ 每根K线 → 更新 unrealizedPnl → 方向判断 |
| | | * │ ├─ closePrice > longPriceQueue[0] → processLongGrid |
| | | * │ └─ closePrice < shortPriceQueue[0] → processShortGrid |
| | | * ├─ processShortGrid: 匹配空仓队列 → 队列转移 → 止盈入队 → |
| | | * │ 取消旧多仓条件单 → 挂新空仓+多仓条件单 → 条件满足挂反向多单 |
| | | * ├─ processLongGrid: 匹配多仓队列 → 队列转移 → 止盈入队 → |
| | | * │ 取消旧空仓条件单 → 挂新多仓+空仓条件单 → 条件满足挂反向空单 |
| | | * ├─ 仓位推送(净增张数) → 从止盈队列取止盈价 → 创建止盈条件单(plan-close-*-position) |
| | | * ├─ processShortGrid: 匹配空仓队列 → 队列转移→ 挂新空仓+多仓条件单 |
| | | * ├─ processLongGrid: 匹配多仓队列 → 队列转移→ 挂新多仓+空仓条件单 |
| | | * ├─ 订单推送(新) → onOrderUpdate → Map 匹配止盈价 → 挂止盈条件单 |
| | | * ├─ 仓位推送 → 更新均价/持仓量、处理反向单 |
| | | * ├─ 平仓推送 → 累加 cumulativePnl |
| | | * ├─ 保证金安全阀 → 超限跳过挂单,队列照常更新 |
| | | * └─ cumulativePnl ≥ overallTp 或 ≤ -maxLoss → STOPPED |
| | |
| | | * |
| | | * <h3>止盈机制</h3> |
| | | * <ul> |
| | | * <li>网格触发时,新队列首元素 ± step 作为止盈价加入止盈队列。</li> |
| | | * <li>仓位推送检测到净增张数时,从止盈队列头部取止盈价创建止盈条件单。</li> |
| | | * <li>网格触发时,挂条件单的回调中将订单 ID 和止盈价存入 currentLongOrderIds / currentShortOrderIds Map。</li> |
| | | * <li>条件单成交后,{@code futures.orders} 推送触发 {@link #onOrderUpdate}, |
| | | * 通过订单 ID 取出止盈价,创建止盈条件单(plan-close-*-position)。</li> |
| | | * <li>止盈条件单:以触发价监控(price_type=最新价,strategy_type=价格触发), |
| | | * 到达后以市价 IOC 平仓(reduce_only=true,price="0")。</li> |
| | | * <li>止盈队列为空时兜底:entryPrice ± step 作为止盈价。</li> |
| | | * </ul> |
| | | * |
| | | * <h3>反向条件单条件</h3> |
| | |
| | | * newFirstPrice > shortEntryPrice AND newFirstPrice < longEntryPrice |
| | | * AND 反向持仓张数 < 3 |
| | | * </pre> |
| | | * 满足条件时以 newFirstPrice 为触发价挂反向条件单,同时将 newFirstPrice ± step 加入对方止盈队列。 |
| | | * 满足条件时以 newFirstPrice ± step 为止盈价直接挂市价单,通过订单订阅自动挂止盈。 |
| | | * |
| | | * <h3>未实现盈亏公式(正向合约)</h3> |
| | | * <pre> |
| | |
| | | /** 多仓价格队列,升序排列(小→大),容量 gridQueueSize */ |
| | | private final List<BigDecimal> longPriceQueue = Collections.synchronizedList(new ArrayList<>()); |
| | | |
| | | /** 多仓止盈队列,升序排列(小→大),仓位推送时消费 */ |
| | | private final List<BigDecimal> longTakeProfitQueue = Collections.synchronizedList(new ArrayList<>()); |
| | | /** 空仓止盈队列,降序排列(大→小),仓位推送时消费 */ |
| | | private final List<BigDecimal> shortTakeProfitQueue = Collections.synchronizedList(new ArrayList<>()); |
| | | |
| | | /** 当前多仓条件单映射:订单ID → 止盈价格,订单成交后通过订单订阅推送匹配止盈 */ |
| | | private final Map<String, BigDecimal> currentLongOrderIds = Collections.synchronizedMap(new HashMap<>()); |
| | | private final Map<String, BigDecimal> currentLongOrderIds = Collections.synchronizedMap(new LinkedHashMap<>()); |
| | | /** 当前空仓条件单映射:订单ID → 止盈价格,订单成交后通过订单订阅推送匹配止盈 */ |
| | | private final Map<String, BigDecimal> currentShortOrderIds = Collections.synchronizedMap(new HashMap<>()); |
| | | private final Map<String, BigDecimal> currentShortOrderIds = Collections.synchronizedMap(new LinkedHashMap<>()); |
| | | |
| | | /** 基底空头入场价 */ |
| | | private BigDecimal shortBaseEntryPrice; |
| | |
| | | shortActive = false; |
| | | shortPriceQueue.clear(); |
| | | longPriceQueue.clear(); |
| | | longTakeProfitQueue.clear(); |
| | | shortTakeProfitQueue.clear(); |
| | | currentLongOrderIds.clear(); |
| | | currentShortOrderIds.clear(); |
| | | log.info("[Gate] 网格策略已启动"); |
| | |
| | | longActive = false; |
| | | longPositionSize = BigDecimal.ZERO; |
| | | } |
| | | synchronized (currentLongOrderIds) { |
| | | if (currentLongOrderIds.size() > 5) { |
| | | Iterator<String> it = currentLongOrderIds.keySet().iterator(); |
| | | for (int i = 0, remove = currentLongOrderIds.size() - 5; i < remove; i++) { |
| | | it.next(); |
| | | it.remove(); |
| | | } |
| | | } |
| | | } |
| | | } else if (Position.ModeEnum.DUAL_SHORT == mode) { |
| | | if (hasPosition) { |
| | | shortActive = true; |
| | |
| | | shortActive = false; |
| | | shortPositionSize = BigDecimal.ZERO; |
| | | } |
| | | synchronized (currentShortOrderIds) { |
| | | if (currentShortOrderIds.size() > 5) { |
| | | Iterator<String> it = currentShortOrderIds.keySet().iterator(); |
| | | for (int i = 0, remove = currentShortOrderIds.size() - 5; i < remove; i++) { |
| | | it.next(); |
| | | it.remove(); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |