| | |
| | | ORDER_TYPE_CLOSE_SHORT, |
| | | String.valueOf(qty), |
| | | profitId -> { |
| | | slElem.addShortStopLossOrderId(profitId); |
| | | slElem.addShortStopLossOrderId(profitId, qty); |
| | | GridElement.refreshIndices(); |
| | | log.info("[Gate] 对向加仓空仓止损已挂, 开仓gridId:{}, 止损gridId:{}, 数量:{}张, stopLossId:{}", |
| | | oppositeGridId, stopLossGridId, qty, profitId); |
| | |
| | | ORDER_TYPE_CLOSE_LONG, |
| | | negate(String.valueOf(qty)), |
| | | profitId -> { |
| | | slElem.addLongStopLossOrderId(profitId); |
| | | slElem.addLongStopLossOrderId(profitId, qty); |
| | | GridElement.refreshIndices(); |
| | | log.info("[Gate] 对向加仓多仓止损已挂, 开仓gridId:{}, 止损gridId:{}, 数量:{}张, stopLossId:{}", |
| | | oppositeGridId, stopLossGridId, qty, profitId); |
| | |
| | | ORDER_TYPE_CLOSE_LONG, |
| | | negate(baseQty), |
| | | profitId -> { |
| | | longElem.addLongStopLossOrderId(profitId); |
| | | longElem.addLongStopLossOrderId(profitId, Integer.parseInt(baseQty)); |
| | | GridElement.refreshIndices(); |
| | | log.info("[Gate] 自定义多仓止损已挂, 目标价:{}, 匹配gridId:{}, 触发价:{}, stopLossId:{}", |
| | | longPrice, longElem.getId(), triggerPrice, profitId); |
| | |
| | | ORDER_TYPE_CLOSE_SHORT, |
| | | baseQty, |
| | | profitId -> { |
| | | shortElem.addShortStopLossOrderId(profitId); |
| | | shortElem.addShortStopLossOrderId(profitId, Integer.parseInt(baseQty)); |
| | | GridElement.refreshIndices(); |
| | | log.info("[Gate] 自定义空仓止损已挂, 目标价:{}, 匹配gridId:{}, 触发价:{}, stopLossId:{}", |
| | | shortPrice, shortElem.getId(), triggerPrice, profitId); |
| | |
| | | ORDER_TYPE_CLOSE_SHORT, |
| | | size, |
| | | profitId -> { |
| | | elem.addShortStopLossOrderId(profitId); |
| | | elem.addShortStopLossOrderId(profitId, Integer.parseInt(size)); |
| | | GridElement.refreshIndices(); |
| | | log.info("[Gate] 空仓止损已挂, gridId:{}, 触发价:{}, stopLossId:{}", finalId, triggerPrice, profitId); |
| | | } |
| | |
| | | ORDER_TYPE_CLOSE_LONG, |
| | | negate(size), |
| | | profitId -> { |
| | | elem.addLongStopLossOrderId(profitId); |
| | | elem.addLongStopLossOrderId(profitId, Integer.parseInt(size)); |
| | | GridElement.refreshIndices(); |
| | | log.info("[Gate] 多仓止损已挂, gridId:{}, 触发价:{}, stopLossId:{}", finalId, triggerPrice, profitId); |
| | | } |
| | |
| | | * |
| | | * @param isLong {@code true}=多仓方向,{@code false}=空仓方向 |
| | | * @param respectCurrentPosition {@code true}=按「单边总持仓 ≤ maxPositionPerSide」扣减当前持仓;{@code false}=仅限制单次下单量 |
| | | * @param stopLossSize 本次止损单张数(仅止损追单场景传入,用于修正 REST 时序竞态;其余场景传 0) |
| | | * @param stopLossSize 本次触发网格的全部止损单张数(仅止损追单场景传入,用于修正 REST 持仓时序竞态;其余场景传 0) |
| | | * @return 实际下单张数(字符串) |
| | | */ |
| | | private String calculateEntryQuantity(boolean isLong, boolean respectCurrentPosition, int stopLossSize) { |
| | |
| | | if (respectCurrentPosition) { |
| | | // 止损追单:单边总持仓不得超过 maxPerSide,追单量 = maxPerSide - 当前持仓。 |
| | | // 止损单刚成交、REST 查询可能仍返回止损前持仓(时序竞态), |
| | | // 减去本次止损单张数得到止损后的真实持仓,避免追单量被错误扣减。 |
| | | // 减去本网格全部止损张数得到止损后的真实持仓,避免追单量被错误扣减。 |
| | | int currentPos = isLong |
| | | ? queryPositionSize(Position.ModeEnum.DUAL_LONG) |
| | | : queryPositionSize(Position.ModeEnum.DUAL_SHORT); |
| | |
| | | } |
| | | |
| | | private void handleLongStopLossTriggered(GridElement gridElement, String orderId) { |
| | | // 本网格全部止损单覆盖张数(含刚触发的这一单),用于追单时修正 REST 持仓的时序竞态 |
| | | int gridStopLossSize = gridElement.getLongStopLossTotalSize(); |
| | | gridElement.removeLongStopLossOrderId(orderId); |
| | | |
| | | int gridId = gridElement.getId(); |
| | |
| | | |
| | | // 止损追单:同一网格可有多笔挂单,不判断 isHasLongOrder,直接挂单 |
| | | BigDecimal triggerPrice = newEntryGrid.getGridPrice(); |
| | | String size = calculateEntryQuantity(true, true, Integer.parseInt(config.getQuantity())); |
| | | String size = calculateEntryQuantity(true, true, gridStopLossSize); |
| | | log.info("[Gate] 多仓止损追单 有效次数:{}, 加仓执行:{}, 停止阈值:{}, 基础:{}张 → 实际:{}张, 模式:{}", |
| | | getEffectiveStopLossCount(true), getEffectiveAddCount(true), config.getAddPositionStopThreshold(), |
| | | config.getBaseQuantity(), size, config.getStopLossCountMode()); |
| | |
| | | } |
| | | |
| | | private void handleShortStopLossTriggered(GridElement gridElement, String orderId) { |
| | | // 本网格全部止损单覆盖张数(含刚触发的这一单),用于追单时修正 REST 持仓的时序竞态 |
| | | int gridStopLossSize = gridElement.getShortStopLossTotalSize(); |
| | | gridElement.removeShortStopLossOrderId(orderId); |
| | | |
| | | int gridId = gridElement.getId(); |
| | |
| | | |
| | | // 止损追单:同一网格可有多笔挂单,不判断 isHasShortOrder,直接挂单 |
| | | BigDecimal triggerPrice = newEntryGrid.getGridPrice(); |
| | | String size = calculateEntryQuantity(false, true, Integer.parseInt(config.getQuantity())); |
| | | String size = calculateEntryQuantity(false, true, gridStopLossSize); |
| | | log.info("[Gate] 空仓止损追单 有效次数:{}, 加仓执行:{}, 停止阈值:{}, 基础:{}张 → 实际:{}张, 模式:{}", |
| | | getEffectiveStopLossCount(false), getEffectiveAddCount(false), config.getAddPositionStopThreshold(), |
| | | config.getBaseQuantity(), size, config.getStopLossCountMode()); |
| | |
| | | // ========== 止损追单(新逻辑:3 个固定位置,每个位置挂多个 1 张止损单) ========== |
| | | |
| | | /** |
| | | * 多仓止损追单 — 网格数量 = baseQuantity/quantity,从近到远分配。 |
| | | * aCount<0跳过, aCount=0→主=2, aCount=1→主=2,辅0=1, aCount>=2→主=aCount,辅依次填1 |
| | | * 多仓止损追单 — 把当前多头持仓<b>全量</b>拆分为多笔止损单,挂在靠近入场的连续网格上。 |
| | | * 网格数 gridCount = baseQuantity / quantity;最近网格挂大部分张数、其余网格各挂 1 笔 quantity 张, |
| | | * 不足一格的余数单独在更远一网格挂一笔。所有异步挂单完成后通过计数器重置防重入标记; |
| | | * 期间若有入场单成交则打 pendingStopLossReExtend 待重挂。 |
| | | */ |
| | | private void extendLongStopLoss(int filledQty, int gridId) { |
| | | int qty = Integer.parseInt(config.getQuantity()); |
| | |
| | | return; |
| | | } |
| | | |
| | | // [Gate] 止损只针对初始开仓仓位(baseQuantity),加仓部分只做止盈; |
| | | // 持仓不足一个网格量时也按剩余张数挂一张止损(手动重建场景,避免裸奔) |
| | | int slSize = Math.min(filledQty, baseQty); |
| | | // [Gate] 止损覆盖全持仓:加仓部分同样受止损保护,不再只保初始仓位。 |
| | | // 取 max 是为了在 REST 时序竞态导致 filledQty 偏小时,至少保住 baseQuantity 的基础仓位 |
| | | int slSize = Math.max(filledQty, baseQty); |
| | | int totalOrders = slSize / qty; |
| | | int remainder = slSize % qty; |
| | | int[] orders = new int[gridCount]; |
| | |
| | | } |
| | | |
| | | /** |
| | | * 空仓止损追单 — 网格数量 = baseQuantity/quantity,从近到远分配。 |
| | | * aCount<0跳过, aCount=0→主=2, aCount=1→主=2,辅0=1, aCount>=2→主=aCount,辅依次填1 |
| | | * 空仓止损追单 — 把当前空头持仓<b>全量</b>拆分为多笔止损单,挂在靠近入场的连续网格上。 |
| | | * 网格数 gridCount = baseQuantity / quantity;最近网格挂大部分张数、其余网格各挂 1 笔 quantity 张, |
| | | * 不足一格的余数单独在更远一网格挂一笔。所有异步挂单完成后通过计数器重置防重入标记; |
| | | * 期间若有入场单成交则打 pendingStopLossReExtend 待重挂。 |
| | | */ |
| | | private void extendShortStopLoss(int filledQty, int gridId) { |
| | | int qty = Integer.parseInt(config.getQuantity()); |
| | |
| | | return; |
| | | } |
| | | |
| | | // [Gate] 止损只针对初始开仓仓位(baseQuantity),加仓部分只做止盈; |
| | | // 持仓不足一个网格量时也按剩余张数挂一张止损(手动重建场景,避免裸奔) |
| | | int slSize = Math.min(filledQty, baseQty); |
| | | // [Gate] 止损覆盖全持仓:加仓部分同样受止损保护,不再只保初始仓位。 |
| | | // 取 max 是为了在 REST 时序竞态导致 filledQty 偏小时,至少保住 baseQuantity 的基础仓位 |
| | | int slSize = Math.max(filledQty, baseQty); |
| | | int totalOrders = slSize / qty; |
| | | int remainder = slSize % qty; |
| | | int[] orders = new int[gridCount]; |
| | |
| | | triggerPrice, rule, orderType, size, |
| | | profitId -> { |
| | | if (isLong) { |
| | | elem.addLongStopLossOrderId(profitId); |
| | | elem.addLongStopLossOrderId(profitId, Integer.parseInt(qtyStr)); |
| | | } else { |
| | | elem.addShortStopLossOrderId(profitId); |
| | | elem.addShortStopLossOrderId(profitId, Integer.parseInt(qtyStr)); |
| | | } |
| | | GridElement.refreshIndices(); |
| | | log.info("[Gate] {}止损追加, gridId:{}, 触发价:{}, 第{}单, stopLossId:{}", |