| | |
| | | // String quantity = String.valueOf(config.getBaseQuantity()); |
| | | |
| | | |
| | | String quantity = calculateEntryQuantity(true); |
| | | String quantity = calculateEntryQuantity(true, false); |
| | | // 向下检查是否已有多单挂在更低价格网格,有则跳过(防止价格回升后重复挂单) |
| | | boolean hasLongOrderBelow = false; |
| | | GridElement checkDownCursor = GridElement.findById(newEntryGrid.getDownId()); |
| | |
| | | // String quantity = String.valueOf(Math.max(queryPositionSize(Position.ModeEnum.DUAL_LONG), longPositionSize.intValue()) + Integer.parseInt(config.getQuantity())); |
| | | // String quantity = String.valueOf(config.getBaseQuantity()); |
| | | |
| | | String quantity = calculateEntryQuantity(false); |
| | | String quantity = calculateEntryQuantity(false, false); |
| | | // 向上检查是否已有空单挂在更高价格网格,有则跳过(防止价格回落后重复挂单) |
| | | boolean hasShortOrderAbove = false; |
| | | GridElement checkUpCursor = GridElement.findById(newEntryGrid.getUpId()); |
| | |
| | | } |
| | | |
| | | /** |
| | | * 根据加仓配置计算止损追单时的实际下单量。 |
| | | * 根据加仓配置计算入场/追单时的实际下单量。 |
| | | * <p>公式:</p> |
| | | * <pre> |
| | | * divisor = addPositionInterval + 1 |
| | | * addMultiplier = floor(effectiveStopLossCount / divisor) |
| | | * addQty = addMultiplier × addPositionQuantity |
| | | * finalQty = min(baseQuantity + addQty, maxPositionPerSide > 0 ? maxPositionPerSide : ∞) |
| | | * finalQty = min(baseQuantity + addQty, upperLimit) |
| | | * </pre> |
| | | * <p>上限 upperLimit:</p> |
| | | * <ul> |
| | | * <li>{@code respectCurrentPosition=false}:{@code upperLimit = maxPositionPerSide} |
| | | * (仓位归零重新入场,仅限制单次下单量)</li> |
| | | * <li>{@code respectCurrentPosition=true}:{@code upperLimit = maxPositionPerSide - 当前持仓} |
| | | * (止损追单,保证单边总持仓 ≤ maxPositionPerSide)</li> |
| | | * </ul> |
| | | * |
| | | * @param isLong {@code true}=多仓方向,{@code false}=空仓方向 |
| | | * @param isLong {@code true}=多仓方向,{@code false}=空仓方向 |
| | | * @param respectCurrentPosition {@code true}=按「单边总持仓 ≤ maxPositionPerSide」扣减当前持仓;{@code false}=仅限制单次下单量 |
| | | * @return 实际下单张数(字符串) |
| | | */ |
| | | private String calculateEntryQuantity(boolean isLong) { |
| | | private String calculateEntryQuantity(boolean isLong, boolean respectCurrentPosition) { |
| | | int baseQty = Integer.parseInt(config.getBaseQuantity()); |
| | | int interval = config.getAddPositionInterval(); |
| | | int addQtyPerUnit = config.getAddPositionQuantity(); |
| | |
| | | int addQty = addMultiplier * addQtyPerUnit; |
| | | int totalQty = baseQty + addQty; |
| | | |
| | | if (maxPerSide > 0 && totalQty > maxPerSide) { |
| | | totalQty = maxPerSide; |
| | | if (maxPerSide > 0) { |
| | | int upperLimit = maxPerSide; |
| | | if (respectCurrentPosition) { |
| | | // 止损追单:单边总持仓不得超过 maxPerSide,追单量 = maxPerSide - 当前持仓 |
| | | int currentPos = isLong |
| | | ? Math.max(queryPositionSize(Position.ModeEnum.DUAL_LONG), longPositionSize.intValue()) |
| | | : Math.max(queryPositionSize(Position.ModeEnum.DUAL_SHORT), shortPositionSize.intValue()); |
| | | upperLimit = maxPerSide - currentPos; |
| | | } |
| | | if (upperLimit <= 0) { |
| | | totalQty = 0; |
| | | } else if (totalQty > upperLimit) { |
| | | totalQty = upperLimit; |
| | | } |
| | | } |
| | | |
| | | return String.valueOf(totalQty); |
| | |
| | | |
| | | // 止损追单:同一网格可有多笔挂单,不判断 isHasLongOrder,直接挂单 |
| | | BigDecimal triggerPrice = newEntryGrid.getGridPrice(); |
| | | String size = calculateEntryQuantity(true); |
| | | String size = calculateEntryQuantity(true, true); |
| | | log.info("[Gate] 多仓止损追单 有效次数:{}, 基础:{}张 → 实际:{}张, 模式:{}", |
| | | getEffectiveStopLossCount(true), config.getBaseQuantity(), size, config.getStopLossCountMode()); |
| | | newEntryGrid.getLongTraderParam().setQuantity(size); |
| | | placeEntryOrderWithPreFlag(newEntryGrid, true, triggerPrice, |
| | | FuturesPriceTrigger.RuleEnum.NUMBER_1, size); |
| | | if (Integer.parseInt(size) <= 0) { |
| | | log.warn("[Gate] 多仓止损追单 当前持仓已达单边上限({}), 跳过挂单", config.getMaxPositionPerSide()); |
| | | } else { |
| | | newEntryGrid.getLongTraderParam().setQuantity(size); |
| | | placeEntryOrderWithPreFlag(newEntryGrid, true, triggerPrice, |
| | | FuturesPriceTrigger.RuleEnum.NUMBER_1, size); |
| | | } |
| | | |
| | | // 取消 gridId+2 位置的所有多单 |
| | | int cancelGridId = gridId + 2; |
| | |
| | | |
| | | // 止损追单:同一网格可有多笔挂单,不判断 isHasShortOrder,直接挂单 |
| | | BigDecimal triggerPrice = newEntryGrid.getGridPrice(); |
| | | String size = calculateEntryQuantity(false); |
| | | String size = calculateEntryQuantity(false, true); |
| | | log.info("[Gate] 空仓止损追单 有效次数:{}, 基础:{}张 → 实际:{}张, 模式:{}", |
| | | getEffectiveStopLossCount(false), config.getBaseQuantity(), size, config.getStopLossCountMode()); |
| | | newEntryGrid.getShortTraderParam().setQuantity(size); |
| | | placeEntryOrderWithPreFlag(newEntryGrid, false, triggerPrice, |
| | | FuturesPriceTrigger.RuleEnum.NUMBER_2, negate(size)); |
| | | if (Integer.parseInt(size) <= 0) { |
| | | log.warn("[Gate] 空仓止损追单 当前持仓已达单边上限({}), 跳过挂单", config.getMaxPositionPerSide()); |
| | | } else { |
| | | newEntryGrid.getShortTraderParam().setQuantity(size); |
| | | placeEntryOrderWithPreFlag(newEntryGrid, false, triggerPrice, |
| | | FuturesPriceTrigger.RuleEnum.NUMBER_2, negate(size)); |
| | | } |
| | | |
| | | // 取消 gridId-2 位置的所有空单 |
| | | int cancelGridId = gridId - 2; |
| | |
| | | } |
| | | |
| | | // [Gate] 止损只针对初始开仓仓位(baseQuantity),加仓部分只做止盈 |
| | | int totalOrders = baseQty / qty; |
| | | int totalOrders = filledQty / qty; |
| | | int[] orders = new int[gridCount]; |
| | | |
| | | if (gridCount == 1) { |
| | |
| | | } |
| | | |
| | | // [Gate] 止损只针对初始开仓仓位(baseQuantity),加仓部分只做止盈 |
| | | int totalOrders = baseQty / qty; |
| | | int totalOrders = filledQty / qty; |
| | | int[] orders = new int[gridCount]; |
| | | |
| | | if (gridCount == 1) { |