| | |
| | | private final Map<String, Integer> tpReplenishLongOrderMap = new ConcurrentHashMap<>(); |
| | | /** 仓位止盈补仓:空仓止盈→空仓开仓单ID → 张数(成交后按原空仓止损位重挂全持仓止损) */ |
| | | private final Map<String, Integer> tpReplenishShortOrderMap = new ConcurrentHashMap<>(); |
| | | /** 原始仓位减仓:本轮多头是否已发生首次止损(startGrid 重置) */ |
| | | private volatile boolean longHasStopped = false; |
| | | /** 原始仓位减仓:本轮空头是否已发生首次止损(startGrid 重置) */ |
| | | private volatile boolean shortHasStopped = false; |
| | | /** 原始仓位减仓:当前多头递减追单数量(价格驱动移动时保持) */ |
| | | private volatile int currentLongChaseQty = 0; |
| | | /** 原始仓位减仓:当前空头递减追单数量(价格驱动移动时保持) */ |
| | |
| | | private final Map<String, Integer> reduceReplenishLongOrderMap = new ConcurrentHashMap<>(); |
| | | /** 原始仓位减仓:空头补仓单 orderId → 张数(补仓单挂在多头第一止损位) */ |
| | | private final Map<String, Integer> reduceReplenishShortOrderMap = new ConcurrentHashMap<>(); |
| | | /** 多头止损事件待处理标记:仓位已归零、autoorders finished 尚未到达时置位,阻止价格驱动抢先挂单 */ |
| | | private volatile boolean longStopLossPending = false; |
| | | /** 空头止损事件待处理标记:同上 */ |
| | | private volatile boolean shortStopLossPending = false; |
| | | /** 多头止损待处理置位时间戳(0=未置位),用于兜底超时复位 */ |
| | | private volatile long longStopLossPendingSince = 0L; |
| | | /** 空头止损待处理置位时间戳(0=未置位),用于兜底超时复位 */ |
| | | private volatile long shortStopLossPendingSince = 0L; |
| | | |
| | | private volatile BigDecimal lastKlinePrice; |
| | | private volatile BigDecimal markPrice = BigDecimal.ZERO; |
| | |
| | | dualStopLossModeActive.set(false); |
| | | tpReplenishLongOrderMap.clear(); |
| | | tpReplenishShortOrderMap.clear(); |
| | | longHasStopped = false; |
| | | shortHasStopped = false; |
| | | currentLongChaseQty = 0; |
| | | currentShortChaseQty = 0; |
| | | longStopLossPending = false; |
| | | shortStopLossPending = false; |
| | | longStopLossPendingSince = 0L; |
| | | shortStopLossPendingSince = 0L; |
| | | reduceReplenishLongOrderMap.clear(); |
| | | reduceReplenishShortOrderMap.clear(); |
| | | shortPriceQueue.clear(); |
| | |
| | | } |
| | | |
| | | // 异步检查盈亏,避免 REST 调用阻塞 WS 心跳导致 Socket closed |
| | | executor.submitTask(this::checkProfitAndReset); |
| | | executor.submitMaintenanceTask(this::checkProfitAndReset); |
| | | |
| | | // 持仓主动对账(节流):WS 持仓推送可能因断线盲区/推送延迟而失真, |
| | | // 导致本地持仓状态与交易所不一致、价格驱动重入迟迟不触发。 |
| | |
| | | if ((state == StrategyState.ACTIVE || state == StrategyState.OPENING) |
| | | && System.currentTimeMillis() - lastPositionSyncTime >= POSITION_SYNC_INTERVAL_MS) { |
| | | lastPositionSyncTime = System.currentTimeMillis(); |
| | | executor.submitTask(this::syncPositionsFromExchange); |
| | | executor.submitMaintenanceTask(this::syncPositionsFromExchange); |
| | | } |
| | | |
| | | // 止损事件兜底:置位超时仍未收到 finished(事件丢失且对账补触发未生效),解除拦截让价格驱动接管 |
| | | releaseExpiredStopLossPending(); |
| | | |
| | | if (state == StrategyState.ACTIVE && |
| | | config.isPriceDriveEnabled() && |
| | | longActive == false && |
| | | longPositionSize.compareTo(BigDecimal.ZERO) == 0){ |
| | | longPositionSize.compareTo(BigDecimal.ZERO) == 0 && |
| | | !longStopLossPending){ |
| | | processLongGrid(closePrice); |
| | | } |
| | | |
| | |
| | | if (state == StrategyState.ACTIVE && |
| | | config.isPriceDriveEnabled() && |
| | | shortActive == false && |
| | | shortPositionSize.compareTo(BigDecimal.ZERO) == 0){ |
| | | shortPositionSize.compareTo(BigDecimal.ZERO) == 0 && |
| | | !shortStopLossPending){ |
| | | processShortGrid(closePrice); |
| | | } |
| | | } |
| | | |
| | | /** 止损事件兜底超时:置位超过该时长仍未收到 autoorders finished,自动解除价格驱动拦截 */ |
| | | private static final long STOP_LOSS_PENDING_TIMEOUT_MS = 30_000L; |
| | | |
| | | /** |
| | | * 止损事件兜底:pending 置位超过 {@link #STOP_LOSS_PENDING_TIMEOUT_MS} 仍无 autoorders finished, |
| | | * 说明止损事件已丢失且对账补触发未生效(如无 trade_id 的幽灵单),主动解除拦截, |
| | | * 让价格驱动接管重挂,避免永久停摆。每次 onKline 进入价格驱动判定前调用。 |
| | | */ |
| | | private void releaseExpiredStopLossPending() { |
| | | long now = System.currentTimeMillis(); |
| | | if (longStopLossPending && now - longStopLossPendingSince >= STOP_LOSS_PENDING_TIMEOUT_MS) { |
| | | longStopLossPending = false; |
| | | longStopLossPendingSince = 0L; |
| | | log.warn("[Gate] 多头止损事件{}秒未达, 兜底解除价格驱动拦截", STOP_LOSS_PENDING_TIMEOUT_MS / 1000); |
| | | } |
| | | if (shortStopLossPending && now - shortStopLossPendingSince >= STOP_LOSS_PENDING_TIMEOUT_MS) { |
| | | shortStopLossPending = false; |
| | | shortStopLossPendingSince = 0L; |
| | | log.warn("[Gate] 空头止损事件{}秒未达, 兜底解除价格驱动拦截", STOP_LOSS_PENDING_TIMEOUT_MS / 1000); |
| | | } |
| | | } |
| | | |
| | |
| | | longActive = true; |
| | | longPositionSize = size; |
| | | longEntryPrice = entryPrice; |
| | | longStopLossPending = false; |
| | | longStopLossPendingSince = 0L; |
| | | } else { |
| | | |
| | | log.info("[Gate-0]多仓: {}", longBaseEntryPrice); |
| | | longActive = false; |
| | | longPositionSize = BigDecimal.ZERO; |
| | | longEntryPrice = BigDecimal.ZERO; |
| | | longStopLossPendingSince = System.currentTimeMillis(); |
| | | longStopLossPending = true; |
| | | } |
| | | } else if (Position.ModeEnum.DUAL_SHORT == mode) { |
| | | if (hasPosition) { |
| | | shortActive = true; |
| | | shortPositionSize = size.abs(); |
| | | shortEntryPrice = entryPrice; |
| | | shortStopLossPending = false; |
| | | shortStopLossPendingSince = 0L; |
| | | } else { |
| | | |
| | | log.info("[Gate-0]空仓: {}", shortBaseEntryPrice); |
| | | shortActive = false; |
| | | shortPositionSize = BigDecimal.ZERO; |
| | | shortEntryPrice = BigDecimal.ZERO; |
| | | shortStopLossPendingSince = System.currentTimeMillis(); |
| | | shortStopLossPending = true; |
| | | } |
| | | } |
| | | } |
| | |
| | | |
| | | /** |
| | | * 计算原始仓位减仓模式下的递减追单数量。 |
| | | * 本轮首次止损 → 追单 = 原始仓位;之后 → 追单 = 本次被平数量 - 步长; |
| | | * 每次止损(含首次)→ 追单 = 本次被平数量 - 步长; |
| | | * 若结果 < 步长 → 重置为原始仓位(最大仓位),形成循环。 |
| | | */ |
| | | private int computeReducedChaseQty(boolean isLong, int stoppedQty) { |
| | | private int computeReducedChaseQty(int stoppedQty) { |
| | | int baseQty = Integer.parseInt(config.getBaseQuantity()); |
| | | int reduceStep = config.getOriginalPositionReduce(); |
| | | if (isLong) { |
| | | if (!longHasStopped) { |
| | | longHasStopped = true; |
| | | return baseQty; |
| | | } |
| | | } else { |
| | | if (!shortHasStopped) { |
| | | shortHasStopped = true; |
| | | return baseQty; |
| | | } |
| | | } |
| | | int chase = stoppedQty - reduceStep; |
| | | if (chase < reduceStep) { |
| | | chase = baseQty; |
| | |
| | | } |
| | | |
| | | lastLongStopLossGridId = gridId; |
| | | longStopLossPending = false; |
| | | longStopLossPendingSince = 0L; |
| | | |
| | | accumulatedLongLossCount++; |
| | | log.info("[Gate] 多仓止损触发 gridId:{}, 止损次数:{}{}, 开始追单", |
| | |
| | | String size; |
| | | boolean hasAdd; |
| | | if (config.getOriginalPositionReduce() > 0) { |
| | | // 原始仓位减仓:递减追单(首次=原始仓位,之后=本次被平数量-步长,≤步长时重置为原始仓位) |
| | | int chaseQty = computeReducedChaseQty(true, gridStopLossSize); |
| | | // 原始仓位减仓:每次止损(含首次)按 本次被平数量-步长 递减,<步长时重置为原始仓位 |
| | | int chaseQty = computeReducedChaseQty(gridStopLossSize); |
| | | currentLongChaseQty = chaseQty; |
| | | size = String.valueOf(chaseQty); |
| | | hasAdd = false; |
| | |
| | | return; |
| | | } |
| | | lastShortStopLossGridId = gridId; |
| | | shortStopLossPending = false; |
| | | shortStopLossPendingSince = 0L; |
| | | accumulatedShortLossCount++; |
| | | log.info("[Gate] 空仓止损触发 gridId:{}, 止损次数:{}{}, 开始追单", |
| | | gridId, accumulatedShortLossCount, sameGrid ? "(同网格)" : ""); |
| | |
| | | String size; |
| | | boolean hasAdd; |
| | | if (config.getOriginalPositionReduce() > 0) { |
| | | int chaseQty = computeReducedChaseQty(false, gridStopLossSize); |
| | | int chaseQty = computeReducedChaseQty(gridStopLossSize); |
| | | currentShortChaseQty = chaseQty; |
| | | size = String.valueOf(chaseQty); |
| | | hasAdd = false; |
| | |
| | | } |
| | | |
| | | // 单位置全量止损:当前方向全部持仓挂一张止损单(入场格下方第2格,价格≤触发价平多)。 |
| | | // 取 max 是为了在 REST 时序竞态导致 filledQty 偏小时,至少保住 baseQuantity 的基础仓位 |
| | | int slSize = Math.max(filledQty, Integer.parseInt(config.getBaseQuantity())); |
| | | // 原始仓位减仓模式:止损必须严格等于实际持仓,否则下次止损的“被平数量”会被撑回 baseQuantity, |
| | | // 导致递减卡死(base-step 后不再继续递减);非减仓模式才用 max(filledQty, baseQuantity) 兜底。 |
| | | int slSize = config.getOriginalPositionReduce() > 0 |
| | | ? filledQty |
| | | : Math.max(filledQty, Integer.parseInt(config.getBaseQuantity())); |
| | | AtomicInteger remainCount = new AtomicInteger(1); |
| | | int sgId = gridId - 2; |
| | | placeStopLossOrders(sgId, 1, String.valueOf(slSize), FuturesPriceTrigger.RuleEnum.NUMBER_2, |
| | |
| | | } |
| | | |
| | | // 单位置全量止损:当前方向全部持仓挂一张止损单(入场格上方第2格,价格≥触发价平空)。 |
| | | // 取 max 是为了在 REST 时序竞态导致 filledQty 偏小时,至少保住 baseQuantity 的基础仓位 |
| | | int slSize = Math.max(filledQty, Integer.parseInt(config.getBaseQuantity())); |
| | | // 原始仓位减仓模式:止损必须严格等于实际持仓,避免递减卡死;非减仓模式才用 max 兜底。 |
| | | int slSize = config.getOriginalPositionReduce() > 0 |
| | | ? filledQty |
| | | : Math.max(filledQty, Integer.parseInt(config.getBaseQuantity())); |
| | | AtomicInteger remainCount = new AtomicInteger(1); |
| | | int sgId = gridId + 2; |
| | | placeStopLossOrders(sgId, 1, String.valueOf(slSize), FuturesPriceTrigger.RuleEnum.NUMBER_1, |
| | |
| | | if (wsClient != null) { |
| | | wsClient.setOnAllSubscribedCallback(() -> { |
| | | if (state == StrategyState.ACTIVE || state == StrategyState.OPENING) { |
| | | executor.submitTask(this::syncPositionsFromExchange); |
| | | executor.submitMaintenanceTask(this::syncPositionsFromExchange); |
| | | } |
| | | }); |
| | | } |