| | |
| | | /** 多头是否活跃(有仓位) */ |
| | | private volatile boolean longActive = false; |
| | | |
| | | /** 当前已完成轮数,每次盈利重启时+1 */ |
| | | private volatile int currentRound = 0; |
| | | |
| | | /** 多头累计止损次数(始终递增,加仓订单成交后归零) */ |
| | | private volatile int accumulatedLongLossCount = 0; |
| | | /** 空头累计止损次数(始终递增,加仓订单成交后归零) */ |
| | |
| | | currentShortOrderIds.clear(); |
| | | // 每次重启重新获取当前本金 |
| | | refreshInitialPrincipal(); |
| | | currentRound = 0; |
| | | |
| | | log.info("[Gate] 网格策略已启动, 当前本金: {} USDT", initialPrincipal); |
| | | } |
| | |
| | | executor.submitTask(this::checkProfitAndReset); |
| | | |
| | | if (state == StrategyState.ACTIVE && |
| | | config.isPriceDriveEnabled() && |
| | | longActive == false && |
| | | longPositionSize.compareTo(BigDecimal.ZERO) == 0){ |
| | | processShortGrid(closePrice); |
| | |
| | | |
| | | |
| | | if (state == StrategyState.ACTIVE && |
| | | config.isPriceDriveEnabled() && |
| | | shortActive == false && |
| | | shortPositionSize.compareTo(BigDecimal.ZERO) == 0){ |
| | | processLongGrid(closePrice); |
| | |
| | | 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()); |
| | |
| | | // 提交到 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) { |
| | |
| | | // 提交到 executor 末尾:单线程FIFO保证前面所有平仓/取消任务完成后才重置 |
| | | executor.submitTask(() -> { |
| | | try { Thread.sleep(3000); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } |
| | | int savedRound = currentRound; |
| | | startGrid(); |
| | | currentRound = savedRound; |
| | | }); |
| | | log.info("[Gate] 重置策略"); |
| | | return; |