Administrator
2026-07-17 81f159294e63405748fd5da3475157f20e4a130f
feat(gate): 添加止盈网格跨度配置功能

- 在前端配置页面添加止盈网格跨度输入框
- 更新默认配置对象,添加takeProfitGridSpan字段默认值为2
- 在GateConfig类中新增takeProfitGridSpan属性及相关getter方法
- 在GateConfig.Builder中添加takeProfitGridSpan构建方法
- 在GateConfigDTO中新增takeProfitGridSpan字段并完善转换逻辑
- 在持久化服务中添加对takeProfitGridSpan的处理
- 修改止盈单挂单位置计算逻辑,使用配置的网格跨度替代固定值2
5 files modified
26 ■■■■ changed files
src/main/java/com/xcong/excoin/modules/gateApi/GateConfig.java 9 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/excoin/modules/gateApi/GateConfigDTO.java 3 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/excoin/modules/gateApi/GateConfigPersistenceService.java 1 ●●●● patch | view | raw | blame | history
src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java 6 ●●●●● patch | view | raw | blame | history
src/main/resources/static/gate-config.html 7 ●●●● patch | view | raw | blame | history
src/main/java/com/xcong/excoin/modules/gateApi/GateConfig.java
@@ -105,6 +105,8 @@
    private final int restartGridSpan;
    /** 止损阶梯次数:止损触发次数≤该值时挂单量=止损次数,超过后恢复默认逻辑。0=禁用,默认 0 */
    private final int stopLossCount;
    /** 止盈网格跨度:相邻止盈单跨越的网格数量,默认 2(即隔一格挂一个止盈单) */
    private final int takeProfitGridSpan;
    /** 网格绝对步长(shortBaseEntryPrice × gridRate),运行时由队列生成逻辑设置 */
    private BigDecimal step;
    /** 网格元素列表,由队列初始化时同步填充,包含完整的多空仓挂单状态 */
@@ -137,6 +139,7 @@
        this.maxPositionSize = builder.maxPositionSize;
        this.restartGridSpan = builder.restartGridSpan;
        this.stopLossCount = builder.stopLossCount;
        this.takeProfitGridSpan = builder.takeProfitGridSpan;
    }
    // ==================== REST/WS 地址 ====================
@@ -226,6 +229,8 @@
    public int getRestartGridSpan() { return restartGridSpan; }
    /** @return 止损阶梯次数:止损触发次数≤该值时挂单量=止损次数,超过后恢复默认逻辑。0=禁用 */
    public int getStopLossCount() { return stopLossCount; }
    /** @return 止盈网格跨度:相邻止盈单跨越的网格数量,默认 2 */
    public int getTakeProfitGridSpan() { return takeProfitGridSpan; }
    // ==================== 运行时参数 ====================
@@ -320,6 +325,8 @@
        private int restartGridSpan = 0;
        /** 止损阶梯次数:止损触发次数≤该值时挂单量=止损次数,超过后恢复默认逻辑。0=禁用,默认 0 */
        private int stopLossCount = 0;
        /** 止盈网格跨度:相邻止盈单跨越的网格数量,默认 2 */
        private int takeProfitGridSpan = 2;
        /** 设置 API Key */
        public Builder apiKey(String apiKey) { this.apiKey = apiKey; return this; }
@@ -365,6 +372,8 @@
        public Builder restartGridSpan(int restartGridSpan) { this.restartGridSpan = restartGridSpan; return this; }
        /** 设置止损阶梯次数:止损触发次数≤该值时挂单量=止损次数,超过后恢复默认逻辑。0=禁用 */
        public Builder stopLossCount(int stopLossCount) { this.stopLossCount = stopLossCount; return this; }
        /** 设置止盈网格跨度:相邻止盈单跨越的网格数量,默认 2 */
        public Builder takeProfitGridSpan(int takeProfitGridSpan) { this.takeProfitGridSpan = takeProfitGridSpan; return this; }
        public GateConfig build() {
            return new GateConfig(this);
src/main/java/com/xcong/excoin/modules/gateApi/GateConfigDTO.java
@@ -41,6 +41,8 @@
    private int maxPositionSize;
    /** 止损阶梯次数,0=禁用 */
    private int stopLossCount;
    /** 止盈网格跨度:相邻止盈单跨越的网格数量,默认 2 */
    private int takeProfitGridSpan;
    /** 策略重启跨度阈值,0=禁用 */
    private int restartGridSpan;
    /** 价格精度 */
@@ -69,6 +71,7 @@
                .quantity(config.getQuantity())
                .maxPositionSize(config.getMaxPositionSize())
                .stopLossCount(config.getStopLossCount())
                .takeProfitGridSpan(config.getTakeProfitGridSpan())
                .restartGridSpan(config.getRestartGridSpan())
                .priceScale(config.getPriceScale())
                .contractMultiplier(config.getContractMultiplier())
src/main/java/com/xcong/excoin/modules/gateApi/GateConfigPersistenceService.java
@@ -103,6 +103,7 @@
                .quantity(nvl(dto.getQuantity(), "2"))
                .maxPositionSize(dto.getMaxPositionSize())
                .stopLossCount(dto.getStopLossCount())
                .takeProfitGridSpan(dto.getTakeProfitGridSpan())
                .build();
    }
src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
@@ -672,8 +672,9 @@
                int shortGridQty = Integer.parseInt(config.getQuantity());
                int shortTpCount = posSize > shortBaseQty ? (posSize - shortBaseQty) / shortGridQty : 0;
                int tpSpan = config.getTakeProfitGridSpan();
                for (int i = 0; i < shortTpCount; i++) {
                    int tpGridId = shortGridElement.getId() - 2 * (i + 1);
                    int tpGridId = shortGridElement.getId() - tpSpan * (i + 1);
                    GridElement tpElem = GridElement.findById(tpGridId);
                    // 用 takeProfitPlaced 做同步标记,避免异步回调未执行时重复挂单
                    if (tpElem == null || tpElem.getShortTraderParam().isTakeProfitPlaced()) {
@@ -740,8 +741,9 @@
                int longGridQty = Integer.parseInt(config.getQuantity());
                int longTpCount = posSize > longBaseQty ? (posSize - longBaseQty) / longGridQty : 0;
                int tpSpan = config.getTakeProfitGridSpan();
                for (int i = 0; i < longTpCount; i++) {
                    int tpGridId = longGridElement.getId() + 2 * (i + 1);
                    int tpGridId = longGridElement.getId() + tpSpan * (i + 1);
                    GridElement tpElem = GridElement.findById(tpGridId);
                    // 用 takeProfitPlaced 做同步标记,避免异步回调未执行时重复挂单
                    if (tpElem == null || tpElem.getLongTraderParam().isTakeProfitPlaced()) {
src/main/resources/static/gate-config.html
@@ -114,6 +114,7 @@
            <div class="form-group"><label>每次下单张数</label><input id="quantity" placeholder="2"></div>
            <div class="form-group"><label>最大持仓张数</label><input id="maxPositionSize" placeholder="4"></div>
            <div class="form-group"><label>止损阶梯次数</label><input id="stopLossCount" placeholder="0"></div>
            <div class="form-group"><label>止盈网格跨度</label><input id="takeProfitGridSpan" placeholder="2"></div>
        </form>
    </div>
@@ -189,7 +190,7 @@
    } catch(e) {
        // 文件不存在 → 用默认值填充,用户可编辑后保存
        fillForm({ gridRate:0.005, expectedProfit:0.15, maxLoss:1.5,
            baseQuantity:'2', quantity:'2', maxPositionSize:4, stopLossCount:0 });
            baseQuantity:'2', quantity:'2', maxPositionSize:4, stopLossCount:0, takeProfitGridSpan:2 });
        toast('未找到配置,已加载默认值,编辑后请保存', 'success');
    }
    currentApiKey = key;
@@ -206,6 +207,7 @@
    byId('quantity').value = d.quantity || '';
    byId('maxPositionSize').value = d.maxPositionSize ?? '';
    byId('stopLossCount').value = d.stopLossCount ?? '';
    byId('takeProfitGridSpan').value = d.takeProfitGridSpan ?? '';
}
function collectForm() {
@@ -217,7 +219,8 @@
        baseQuantity: byId('baseQuantity').value,
        quantity: byId('quantity').value,
        maxPositionSize: parseInt(byId('maxPositionSize').value) || 0,
        stopLossCount: parseInt(byId('stopLossCount').value) || 0
        stopLossCount: parseInt(byId('stopLossCount').value) || 0,
        takeProfitGridSpan: parseInt(byId('takeProfitGridSpan').value) || 2
    };
}