| | |
| | | updateUnrealizedPnl(); |
| | | |
| | | if (state == StrategyState.STOPPED) { |
| | | executor.cancelAllAlgoOrders(); |
| | | closeExistingPositions(); |
| | | // stopGrid() 已做清理,仅打印日志不重复操作 |
| | | BigDecimal totalPnl = cumulativePnl.add(unrealizedPnl); |
| | | log.info("[OKX] 已实现:{}, 未实现:{}, 合计:{}", cumulativePnl, unrealizedPnl, totalPnl); |
| | | startGrid(); |
| | | return; |
| | | } |
| | | |
| | |
| | | } |
| | | } else { |
| | | if (longActive && state == StrategyState.ACTIVE) { |
| | | log.info("[OKX] 多仓持仓归零,重置策略"); |
| | | handlePositionZeroAndReset("多仓"); |
| | | } |
| | | longActive = false; |
| | |
| | | } |
| | | } else { |
| | | if (shortActive && state == StrategyState.ACTIVE) { |
| | | log.info("[OKX] 空仓持仓归零,重置策略"); |
| | | handlePositionZeroAndReset("空仓"); |
| | | } |
| | | shortActive = false; |
| | |
| | | state = StrategyState.STOPPED; |
| | | closeExistingPositions(); |
| | | executor.cancelAllAlgoOrders(); |
| | | // 提交到 executor 末尾:单线程FIFO保证前面所有平仓/取消任务完成后才重置 |
| | | executor.submitTask(() -> { |
| | | try { Thread.sleep(3000); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } |
| | | startGrid(); |
| | | }); |
| | | } |
| | | } catch (Exception e) { |
| | | log.warn("[OKX] 盈亏检查失败", e); |
| | |
| | | } |
| | | |
| | | private void handlePositionZeroAndReset(String direction) { |
| | | log.info("[OKX] {}持仓归零,重置策略", direction); |
| | | state = StrategyState.STOPPED; |
| | | executor.cancelAllAlgoOrders(); |
| | | closeExistingPositions(); |
| | | // 提交到 executor 末尾:FIFO保证平仓完成后再重置 |
| | | executor.submitTask(() -> { |
| | | try { Thread.sleep(3000); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } |
| | | startGrid(); |
| | | }); |
| | | } |
| | | |
| | | // ---- getters ---- |