| | |
| | | /** 多头是否活跃(有仓位) */ |
| | | private volatile boolean longActive = false; |
| | | |
| | | /** 多头累计止损张数(加仓订单成交后归零) */ |
| | | /** 多头累计止损次数(始终递增,加仓订单成交后归零) */ |
| | | private volatile int accumulatedLongLossCount = 0; |
| | | /** 空头累计止损张数(加仓订单成交后归零) */ |
| | | /** 空头累计止损次数(始终递增,加仓订单成交后归零) */ |
| | | private volatile int accumulatedShortLossCount = 0; |
| | | /** 多头当前网格重复触发次数(换网格时归零),用于 targetAmount 扣除 */ |
| | | private volatile int longSameGridRepeatCount = 0; |
| | | /** 空头当前网格重复触发次数(换网格时归零),用于 targetAmount 扣除 */ |
| | | private volatile int shortSameGridRepeatCount = 0; |
| | | /** 最近触发止损的多头网格ID */ |
| | | private volatile int lastLongStopLossGridId = Integer.MAX_VALUE; |
| | | /** 最近触发止损的空头网格ID */ |
| | | private volatile int lastShortStopLossGridId = Integer.MAX_VALUE; |
| | | |
| | | private volatile BigDecimal lastKlinePrice; |
| | | private volatile BigDecimal markPrice = BigDecimal.ZERO; |
| | |
| | | shortActive = false; |
| | | accumulatedLongLossCount = 0; |
| | | accumulatedShortLossCount = 0; |
| | | longSameGridRepeatCount = 0; |
| | | shortSameGridRepeatCount = 0; |
| | | lastLongStopLossGridId = Integer.MAX_VALUE; |
| | | lastShortStopLossGridId = Integer.MAX_VALUE; |
| | | shortPriceQueue.clear(); |
| | | longPriceQueue.clear(); |
| | | totalShortPriceQueue.clear(); |
| | |
| | | if (StrUtil.isNotEmpty(tradeId) && !tradeId.equals("0") ){ |
| | | int filledQty = Integer.parseInt(shortGridElement.getShortTraderParam().getQuantity()); |
| | | shortEntryTraderIdParam(shortGridElement, orderId, false); |
| | | accumulatedShortLossCount = 0; |
| | | shortSameGridRepeatCount = 0; |
| | | lastShortStopLossGridId = Integer.MAX_VALUE; |
| | | // [Gate-需求2] 加仓后先撤空仓所有止盈+止损,再查交易所持仓后重挂 |
| | | cancelAllShortTakeProfitsAndStopLosses(); |
| | | // REST 查询可能因交易所延迟返回旧值,与 WS 本地缓存取最大值兜底 |
| | |
| | | // } |
| | | // ); |
| | | |
| | | // 空仓持仓超过baseQuantity时,先找多仓第一个止损位置,从该位置向下挂止盈(间隔=1) |
| | | BigDecimal shortBaseQty = new BigDecimal(config.getBaseQuantity()); |
| | | BigDecimal shortGridQty = new BigDecimal(config.getQuantity()); |
| | | if (BigDecimal.valueOf(posSize).compareTo(shortBaseQty) > 0) { |
| | | BigDecimal shortExcess = BigDecimal.valueOf(posSize).subtract(shortBaseQty); |
| | | int shortExcessCount = shortExcess.divide(shortGridQty, 0, RoundingMode.DOWN).intValue(); |
| | | // 空仓止盈:每1张持仓对应1个止盈位,从entry网格向下逐个挂 |
| | | int shortGridQty = Integer.parseInt(config.getQuantity()); |
| | | int shortTpCount = posSize / shortGridQty; |
| | | |
| | | // // 找多仓第一个(最近的)止损位置 |
| | | // int firstLongSlId = 0; |
| | | // for (GridElement e : config.getGridElements()) { |
| | | // if (e.hasLongStopLossOrders()) { |
| | | // if (firstLongSlId == 0 || e.getId() > firstLongSlId) { |
| | | // firstLongSlId = e.getId(); |
| | | // } |
| | | // } |
| | | // } |
| | | |
| | | for (int i = 0; i < shortExcessCount; i++) { |
| | | int tpGridId = shortGridElement.getId() - 2 * (i + 1); |
| | | GridElement tpElem = GridElement.findById(tpGridId); |
| | | if (tpElem == null || tpElem.getShortTakeProfitOrderId() != null) { |
| | | continue; |
| | | } |
| | | BigDecimal tpPrice = tpElem.getGridPrice(); |
| | | int finalTpGridId = tpGridId; |
| | | executor.placeTakeProfit( |
| | | tpPrice, |
| | | FuturesPriceTrigger.RuleEnum.NUMBER_2, |
| | | ORDER_TYPE_CLOSE_SHORT, |
| | | config.getQuantity(), |
| | | profitId -> { |
| | | shortTakeProfitTraderIdParam(tpElem, profitId, true); |
| | | log.info("[Gate] 空仓止盈挂单, gridId:{}, 触发价:{}, takeProfitId:{}", |
| | | finalTpGridId, tpPrice, profitId); |
| | | } |
| | | ); |
| | | for (int i = 0; i < shortTpCount; i++) { |
| | | int tpGridId = shortGridElement.getId() - 2 * (i + 1); |
| | | GridElement tpElem = GridElement.findById(tpGridId); |
| | | if (tpElem == null || tpElem.getShortTakeProfitOrderId() != null) { |
| | | continue; |
| | | } |
| | | BigDecimal tpPrice = tpElem.getGridPrice(); |
| | | int finalTpGridId = tpGridId; |
| | | executor.placeTakeProfit( |
| | | tpPrice, |
| | | FuturesPriceTrigger.RuleEnum.NUMBER_2, |
| | | ORDER_TYPE_CLOSE_SHORT, |
| | | config.getQuantity(), |
| | | profitId -> { |
| | | shortTakeProfitTraderIdParam(tpElem, profitId, true); |
| | | log.info("[Gate] 空仓止盈挂单, gridId:{}, 触发价:{}, takeProfitId:{}", |
| | | finalTpGridId, tpPrice, profitId); |
| | | } |
| | | ); |
| | | } |
| | | } |
| | | } |
| | |
| | | |
| | | int filledQty = Integer.parseInt(longGridElement.getLongTraderParam().getQuantity()); |
| | | longEntryTraderIdParam(longGridElement, orderId, false); |
| | | accumulatedLongLossCount = 0; |
| | | longSameGridRepeatCount = 0; |
| | | lastLongStopLossGridId = Integer.MAX_VALUE; |
| | | // [Gate-需求2] 加仓后先撤多仓所有止盈+止损,再查交易所持仓后重挂 |
| | | cancelAllLongTakeProfitsAndStopLosses(); |
| | | // REST 查询可能因交易所延迟返回旧值,与 WS 本地缓存取最大值兜底 |
| | |
| | | // } |
| | | // ); |
| | | |
| | | // 多仓持仓超过baseQuantity时,先找空仓第一个止损位置,从该位置向上挂止盈(间隔=1) |
| | | BigDecimal longBaseQty = new BigDecimal(config.getBaseQuantity()); |
| | | BigDecimal longGridQty = new BigDecimal(config.getQuantity()); |
| | | if (BigDecimal.valueOf(posSize).compareTo(longBaseQty) > 0) { |
| | | BigDecimal longExcess = BigDecimal.valueOf(posSize).subtract(longBaseQty); |
| | | int longExcessCount = longExcess.divide(longGridQty, 0, RoundingMode.DOWN).intValue(); |
| | | // 多仓止盈:每1张持仓对应1个止盈位,从entry网格向上逐个挂 |
| | | int longGridQty = Integer.parseInt(config.getQuantity()); |
| | | int longTpCount = posSize / longGridQty; |
| | | |
| | | // // 找空仓第一个(最近的)止损位置 |
| | | // int firstShortSlId = 0; |
| | | // for (GridElement e : config.getGridElements()) { |
| | | // if (e.hasShortStopLossOrders()) { |
| | | // if (firstShortSlId == 0 || e.getId() < firstShortSlId) { |
| | | // firstShortSlId = e.getId(); |
| | | // } |
| | | // } |
| | | // } |
| | | |
| | | for (int i = 0; i < longExcessCount; i++) { |
| | | int tpGridId = longGridElement.getId() + 2 * (i + 1); |
| | | GridElement tpElem = GridElement.findById(tpGridId); |
| | | if (tpElem == null || tpElem.getLongTakeProfitOrderId() != null) { |
| | | continue; |
| | | } |
| | | BigDecimal tpPrice = tpElem.getGridPrice(); |
| | | int finalTpGridId = tpGridId; |
| | | executor.placeTakeProfit( |
| | | tpPrice, |
| | | FuturesPriceTrigger.RuleEnum.NUMBER_1, |
| | | ORDER_TYPE_CLOSE_LONG, |
| | | negate(config.getQuantity()), |
| | | profitId -> { |
| | | longTakeProfitTraderIdParam(tpElem, profitId, true); |
| | | log.info("[Gate] 多仓止盈挂单, gridId:{}, 触发价:{}, takeProfitId:{}", |
| | | finalTpGridId, tpPrice, profitId); |
| | | } |
| | | ); |
| | | for (int i = 0; i < longTpCount; i++) { |
| | | int tpGridId = longGridElement.getId() + 2 * (i + 1); |
| | | GridElement tpElem = GridElement.findById(tpGridId); |
| | | if (tpElem == null || tpElem.getLongTakeProfitOrderId() != null) { |
| | | continue; |
| | | } |
| | | BigDecimal tpPrice = tpElem.getGridPrice(); |
| | | int finalTpGridId = tpGridId; |
| | | executor.placeTakeProfit( |
| | | tpPrice, |
| | | FuturesPriceTrigger.RuleEnum.NUMBER_1, |
| | | ORDER_TYPE_CLOSE_LONG, |
| | | negate(config.getQuantity()), |
| | | profitId -> { |
| | | longTakeProfitTraderIdParam(tpElem, profitId, true); |
| | | log.info("[Gate] 多仓止盈挂单, gridId:{}, 触发价:{}, takeProfitId:{}", |
| | | finalTpGridId, tpPrice, profitId); |
| | | } |
| | | ); |
| | | } |
| | | } |
| | | } |
| | |
| | | private void handleLongStopLossTriggered(GridElement gridElement, String orderId) { |
| | | gridElement.removeLongStopLossOrderId(orderId); |
| | | |
| | | accumulatedLongLossCount++; |
| | | int gridId = gridElement.getId(); |
| | | accumulatedLongLossCount++; |
| | | if (gridId == lastLongStopLossGridId) { |
| | | longSameGridRepeatCount++; |
| | | } else { |
| | | longSameGridRepeatCount = 0; |
| | | lastLongStopLossGridId = gridId; |
| | | } |
| | | log.info("[Gate] 多仓止损触发 gridId:{}, 止损次数:{}, 开始追单", gridId, accumulatedLongLossCount); |
| | | int newEntryGridId = gridId + 1; |
| | | |
| | |
| | | BigDecimal triggerPrice = newEntryGrid.getGridPrice(); |
| | | int posSize = Math.min(queryPositionSize(Position.ModeEnum.DUAL_LONG), longPositionSize.intValue()); |
| | | int maxPos = config.getMaxPositionSize(); |
| | | int targetAmount; |
| | | if (config.getStopLossCount() > 0 && accumulatedLongLossCount <= config.getStopLossCount()) { |
| | | targetAmount = Integer.parseInt(config.getQuantity()); |
| | | } else { |
| | | targetAmount = Integer.parseInt(config.getQuantity()) * 2; |
| | | } |
| | | int targetAmount = accumulatedLongLossCount - longSameGridRepeatCount + 1; |
| | | int addSize; |
| | | if (maxPos > 0) { |
| | | int remainingRoom = maxPos - posSize; |
| | |
| | | private void handleShortStopLossTriggered(GridElement gridElement, String orderId) { |
| | | gridElement.removeShortStopLossOrderId(orderId); |
| | | |
| | | accumulatedShortLossCount++; |
| | | int gridId = gridElement.getId(); |
| | | accumulatedShortLossCount++; |
| | | if (gridId == lastShortStopLossGridId) { |
| | | shortSameGridRepeatCount++; |
| | | } else { |
| | | shortSameGridRepeatCount = 0; |
| | | lastShortStopLossGridId = gridId; |
| | | } |
| | | log.info("[Gate] 空仓止损触发 gridId:{}, 止损次数:{}, 开始追单", gridId, accumulatedShortLossCount); |
| | | int newEntryGridId = gridId - 1; |
| | | |
| | |
| | | BigDecimal triggerPrice = newEntryGrid.getGridPrice(); |
| | | int posSize = Math.min(queryPositionSize(Position.ModeEnum.DUAL_SHORT), shortPositionSize.intValue()); |
| | | int maxPos = config.getMaxPositionSize(); |
| | | int targetAmount; |
| | | if (config.getStopLossCount() > 0 && accumulatedShortLossCount <= config.getStopLossCount()) { |
| | | targetAmount = Integer.parseInt(config.getQuantity()); |
| | | } else { |
| | | targetAmount = Integer.parseInt(config.getQuantity()) * 2; |
| | | } |
| | | int targetAmount = accumulatedShortLossCount - shortSameGridRepeatCount + 1; |
| | | int addSize; |
| | | if (maxPos > 0) { |
| | | int remainingRoom = maxPos - posSize; |