src/main/java/com/xcong/excoin/modules/gateApi/GateConfig.java
@@ -107,8 +107,10 @@ private final int stopLossCount; /** 止盈网格跨度:相邻止盈单跨越的网格数量,默认 2(即隔一格挂一个止盈单) */ private final int takeProfitGridSpan; /** 价格驱动开关:true=启用K线推送中的价格驱动逻辑,false=禁用。默认 false */ /** 价格驱动开关:true=启用K线推送中的价格驱动逻辑,false=禁用。默认 true */ private final boolean priceDriveEnabled; /** 策略运行轮数:达到盈利后重启算一轮,达到上限后不再重启。0=不限轮数 */ private final int rounds; /** 网格绝对步长(shortBaseEntryPrice × gridRate),运行时由队列生成逻辑设置 */ private BigDecimal step; /** 网格元素列表,由队列初始化时同步填充,包含完整的多空仓挂单状态 */ @@ -143,6 +145,7 @@ this.stopLossCount = builder.stopLossCount; this.takeProfitGridSpan = builder.takeProfitGridSpan; this.priceDriveEnabled = builder.priceDriveEnabled; this.rounds = builder.rounds; } // ==================== REST/WS 地址 ==================== @@ -236,6 +239,8 @@ public int getTakeProfitGridSpan() { return takeProfitGridSpan; } /** @return 价格驱动开关:true=启用K线推送中的价格驱动逻辑,false=禁用 */ public boolean isPriceDriveEnabled() { return priceDriveEnabled; } /** @return 策略运行轮数上限,0=不限 */ public int getRounds() { return rounds; } // ==================== 运行时参数 ==================== @@ -334,6 +339,8 @@ private int takeProfitGridSpan = 2; /** 价格驱动开关:true=启用K线推送中的价格驱动逻辑,false=禁用。默认 true */ private boolean priceDriveEnabled = true; /** 策略运行轮数上限,0=不限轮数。默认 0 */ private int rounds = 0; /** 设置 API Key */ public Builder apiKey(String apiKey) { this.apiKey = apiKey; return this; } @@ -383,6 +390,8 @@ public Builder takeProfitGridSpan(int takeProfitGridSpan) { this.takeProfitGridSpan = takeProfitGridSpan; return this; } /** 设置价格驱动开关:true=启用K线推送中的价格驱动逻辑,false=禁用 */ public Builder priceDriveEnabled(boolean priceDriveEnabled) { this.priceDriveEnabled = priceDriveEnabled; return this; } /** 设置策略运行轮数上限:达到盈利后重启计数,达到此值后不再重启。0=不限 */ public Builder rounds(int rounds) { this.rounds = rounds; return this; } public GateConfig build() { return new GateConfig(this); src/main/java/com/xcong/excoin/modules/gateApi/GateConfigDTO.java
@@ -55,6 +55,8 @@ private boolean isProduction; /** 价格驱动开关:true=启用K线推送中的价格驱动逻辑,false=禁用。null=未设置(默认true) */ private Boolean priceDriveEnabled; /** 策略运行轮数上限:达到盈利后重启计数,达到此值后不再重启。0=不限 */ private int rounds; /** * 从 GateConfig 构建 DTO(不暴露 apiSecret)。 @@ -81,6 +83,7 @@ ? config.getUnrealizedPnlPriceMode().name() : "LAST_PRICE") .isProduction(config.isProduction()) .priceDriveEnabled(config.isPriceDriveEnabled()) .rounds(config.getRounds()) .build(); } } src/main/java/com/xcong/excoin/modules/gateApi/GateConfigPersistenceService.java
@@ -105,6 +105,7 @@ .stopLossCount(dto.getStopLossCount()) .takeProfitGridSpan(dto.getTakeProfitGridSpan()) .priceDriveEnabled(nvl(dto.getPriceDriveEnabled(), true)) .rounds(dto.getRounds()) .build(); } src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
@@ -135,6 +135,9 @@ /** 多头是否活跃(有仓位) */ private volatile boolean longActive = false; /** 当前已完成轮数,每次盈利重启时+1 */ private volatile int currentRound = 0; /** 多头累计止损次数(始终递增,加仓订单成交后归零) */ private volatile int accumulatedLongLossCount = 0; /** 空头累计止损次数(始终递增,加仓订单成交后归零) */ @@ -322,6 +325,7 @@ currentShortOrderIds.clear(); // 每次重启重新获取当前本金 refreshInitialPrincipal(); currentRound = 0; log.info("[Gate] 网格策略已启动, 当前本金: {} USDT", initialPrincipal); } @@ -432,7 +436,17 @@ BigDecimal totalEquity = new BigDecimal(account.getTotal()).add(new BigDecimal(account.getUnrealisedPnl())); if (totalEquity.compareTo(target) > 0) { log.info("[Gate] 盈亏达标(净权益{}>目标{}),重置策略", totalEquity, target); currentRound++; int maxRounds = config.getRounds(); log.info("[Gate] 盈亏达标(净权益{}>目标{}),第{}轮完成", totalEquity, target, currentRound); if (maxRounds > 0 && currentRound >= maxRounds) { log.info("[Gate] 已达到运行轮数上限({}),策略停止", maxRounds); stopGrid(); return; } log.info("[Gate] 重置策略,开始第{}轮...", currentRound); state = StrategyState.STOPPED; try { futuresApi.cancelPriceTriggeredOrderList(SETTLE, config.getContract()); @@ -443,7 +457,10 @@ // 提交到 executor 末尾:单线程FIFO保证前面所有平仓/取消任务完成后才重置 executor.submitTask(() -> { try { Thread.sleep(3000); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } // 注意:startGrid() 会将 currentRound 归零,这里需要保留 int savedRound = currentRound; startGrid(); currentRound = savedRound; }); } } catch (Exception e) { @@ -542,7 +559,9 @@ // 提交到 executor 末尾:单线程FIFO保证前面所有平仓/取消任务完成后才重置 executor.submitTask(() -> { try { Thread.sleep(3000); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } int savedRound = currentRound; startGrid(); currentRound = savedRound; }); log.info("[Gate] 重置策略"); return; src/main/java/com/xcong/excoin/modules/gateApi/GateWebSocketClientManager.java
@@ -95,6 +95,7 @@ .maxPositionSize(4) .stopLossCount(0) .priceDriveEnabled(true) .rounds(0) .build(); // 确保配置文件存在 persistenceService.ensureExists(DEFAULT_API_KEY, defaults); src/main/resources/static/gate-config.html
@@ -116,6 +116,7 @@ <div class="form-group"><label>止损阶梯次数</label><input id="stopLossCount" placeholder="0"></div> <div class="form-group"><label>止盈网格跨度</label><input id="takeProfitGridSpan" placeholder="2"></div> <div class="form-group"><label>价格驱动开关</label><select id="priceDriveEnabled"><option value="true" selected>开启</option><option value="false">关闭</option></select></div> <div class="form-group"><label>运行轮数(0=不限)</label><input id="rounds" type="number" placeholder="0"></div> </form> </div> @@ -191,7 +192,7 @@ } catch(e) { // 文件不存在 → 用默认值填充,用户可编辑后保存 fillForm({ gridRate:0.005, expectedProfit:0.15, maxLoss:1.5, baseQuantity:'2', quantity:'2', maxPositionSize:4, stopLossCount:0, takeProfitGridSpan:2, priceDriveEnabled: true }); baseQuantity:'2', quantity:'2', maxPositionSize:4, stopLossCount:0, takeProfitGridSpan:2, priceDriveEnabled: true, rounds: 0 }); toast('未找到配置,已加载默认值,编辑后请保存', 'success'); } currentApiKey = key; @@ -210,6 +211,7 @@ byId('stopLossCount').value = d.stopLossCount ?? ''; byId('takeProfitGridSpan').value = d.takeProfitGridSpan ?? ''; byId('priceDriveEnabled').value = (d.priceDriveEnabled === true || d.priceDriveEnabled === 'true') ? 'true' : 'false'; byId('rounds').value = d.rounds ?? 0; } function collectForm() { @@ -223,7 +225,8 @@ maxPositionSize: parseInt(byId('maxPositionSize').value) || 0, stopLossCount: parseInt(byId('stopLossCount').value) || 0, takeProfitGridSpan: parseInt(byId('takeProfitGridSpan').value) || 2, priceDriveEnabled: byId('priceDriveEnabled').value === 'true' priceDriveEnabled: byId('priceDriveEnabled').value === 'true', rounds: parseInt(byId('rounds').value) || 0 }; }