Administrator
2026-05-28 b3b9edaf5eb570a899ce90d05310f6a1aef11807
feat(gateApi): 添加保证金比例限制和网格队列容量配置选项

- 在 GateConfig 中新增 marginRatioLimit 配置项用于设置保证金占初始本金比例上限
- 在 GateConfig 中新增 gridQueueSize 配置项用于设置网格队列容量
- 启用被注释的 TPonUserTradeShortEntry 和 TPonUserTradeLongEntry 方法调用
- 优化网格交易逻辑,将开仓位置判断从当前网格改为相邻上下网格
- 修复网格遍历逻辑,确保正确的多空仓位开仓顺序
- 移除冗余的注释代码和无用变量声明
2 files modified
76 ■■■■ changed files
src/main/java/com/xcong/excoin/modules/gateApi/GateConfig.java 4 ●●●● patch | view | raw | blame | history
src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java 72 ●●●● patch | view | raw | blame | history
src/main/java/com/xcong/excoin/modules/gateApi/GateConfig.java
@@ -314,6 +314,10 @@
        public Builder contractMultiplier(BigDecimal contractMultiplier) { this.contractMultiplier = contractMultiplier; return this; }
        /** 设置价格精度(交易所价格的小数位数,如 1=0.1精度,2=0.01精度) */
        public Builder priceScale(int priceScale) { this.priceScale = priceScale; return this; }
        /** 设置保证金占初始本金比例上限 */
        public Builder marginRatioLimit(BigDecimal marginRatioLimit) { this.marginRatioLimit = marginRatioLimit; return this; }
        /** 设置网格队列容量 */
        public Builder gridQueueSize(int gridQueueSize) { this.gridQueueSize = gridQueueSize; return this; }
        /** 设置未实现盈亏计价模式 */
        public Builder unrealizedPnlPriceMode(PnLPriceMode mode) { this.unrealizedPnlPriceMode = mode; return this; }
src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
@@ -731,7 +731,7 @@
                    null,
                    false
            );
//            TPonUserTradeShortEntry(byShortTakeProfitOrderId);
            TPonUserTradeShortEntry(byShortTakeProfitOrderId);
        }
        GridElement byLongTakeProfitOrderId = GridElement.findByLongTakeProfitOrderId(orderId);
        if (byLongTakeProfitOrderId != null){
@@ -745,7 +745,7 @@
                    null,
                    false
            );
//            TPonUserTradeLongEntry(byLongTakeProfitOrderId);
            TPonUserTradeLongEntry(byLongTakeProfitOrderId);
        }
        /**
@@ -806,43 +806,20 @@
        if (!isMarginSafe()) {
            log.warn("[Gate] 保证金超限,跳过挂条件单");
        } else {
            /**
             * 下一个开仓位置
             *      获取队列第一个元素的价格对应的网格
             *      判断网格是否能开空仓,如果不能则跳过
             *      前进方向挂空仓条件单
             *      后置方向挂多空条件单
             */
            //下一个开仓位置
            BigDecimal gridPrice = gridElement.getGridPrice();
            // 判断网格是否能开空仓,如果不能则跳过
            if (gridElement != null) {
                TraderParam downShortTraderParam = gridElement.getShortTraderParam();
            // 判断网格是否能开多仓,如果不能则跳过
            GridElement upGridElement = GridElement.findById(gridElement.getUpId());
            if (upGridElement != null){
                BigDecimal upGridPrice = upGridElement.getGridPrice();
                TraderParam downLongTraderParam = upGridElement.getLongTraderParam();
                if (
                        !gridElement.isHasShortOrder() &&
                                gridPrice.compareTo(longEntryPrice) <= 0 &&
                                gridPrice.compareTo(shortEntryPrice) >= 0
                        !upGridElement.isHasLongOrder() &&
                                upGridPrice.compareTo(longEntryPrice) <= 0
                ){
                    placeEntryOrderWithPreFlag(gridElement, false,
                            downShortTraderParam.getEntryPrice(),
                            FuturesPriceTrigger.RuleEnum.NUMBER_1,
                            negate(downShortTraderParam.getQuantity()));
                }
                TraderParam downLongTraderParam = gridElement.getLongTraderParam();
                if (
                        !gridElement.isHasLongOrder() &&
                                gridPrice.compareTo(longEntryPrice) <= 0
                ){
                    placeEntryOrderWithPreFlag(gridElement, true,
                    placeEntryOrderWithPreFlag(upGridElement, true,
                            downLongTraderParam.getEntryPrice(),
                            FuturesPriceTrigger.RuleEnum.NUMBER_1,
                            downLongTraderParam.getQuantity());
                }
            }
        }
    }
@@ -851,31 +828,18 @@
        if (!isMarginSafe()) {
            log.warn("[Gate] 保证金超限,跳过挂条件单");
        } else {
            BigDecimal newLongFirst = gridElement.getGridPrice() ;
            // 判断网格是否能开空仓,如果不能则跳过
            GridElement downGridElement = GridElement.findById(gridElement.getDownId());
            if (downGridElement != null){
            // 判断网格是否能开多空仓,如果不能则跳过
            if (gridElement != null) {
                BigDecimal downGridPrice = downGridElement.getGridPrice();
//                TraderParam downLongTraderParam = gridElement.getLongTraderParam();
//                if (
//                        !gridElement.isHasLongOrder() &&
//                                newLongFirst.compareTo(shortEntryPrice) >= 0 &&
//                                newLongFirst.compareTo(longEntryPrice) <= 0
//                ){
//                    placeEntryOrderWithPreFlag(gridElement, true,
//                            downLongTraderParam.getEntryPrice(),
//                            FuturesPriceTrigger.RuleEnum.NUMBER_2,
//                            config.getQuantity());
//
//                }
                TraderParam shortTraderParam = gridElement.getShortTraderParam();
                TraderParam shortTraderParam = downGridElement.getShortTraderParam();
                if (
                        !gridElement.isHasShortOrder() &&
                                newLongFirst.compareTo(shortEntryPrice) >= 0
                        !downGridElement.isHasShortOrder() &&
                                downGridPrice.compareTo(shortEntryPrice) >= 0
                ){
                    placeEntryOrderWithPreFlag(gridElement, false,
                    placeEntryOrderWithPreFlag(downGridElement, false,
                            shortTraderParam.getEntryPrice(),
                            FuturesPriceTrigger.RuleEnum.NUMBER_2,
                            negate(config.getQuantity()));