| | |
| | | longPriceQueue.clear(); |
| | | currentLongOrderIds.clear(); |
| | | currentShortOrderIds.clear(); |
| | | stopLossManager.resetAllEntryQuantities(); |
| | | |
| | | // 每次重启重新获取当前本金,确保盈亏对比基准正确 |
| | | refreshInitialPrincipal(); |
| | |
| | | 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; |
| | |
| | | if (shortGridElement != null && shortGridElement.isHasShortOrder()) { |
| | | int filledQty = Integer.parseInt(shortGridElement.getShortTraderParam().getQuantity()); |
| | | stopLossManager.clearShortEntryState(shortGridElement); |
| | | stopLossManager.resetShortEntryQty(); |
| | | stopLossManager.extendShortStopLoss(filledQty); |
| | | log.info("[OKX] 空单成交 gridId:{}, qty:{}, 追挂止损", shortGridElement.getId(), filledQty); |
| | | return; |
| | |
| | | if (longGridElement != null && longGridElement.isHasLongOrder()) { |
| | | int filledQty = Integer.parseInt(longGridElement.getLongTraderParam().getQuantity()); |
| | | stopLossManager.clearLongEntryState(longGridElement); |
| | | stopLossManager.resetLongEntryQty(); |
| | | stopLossManager.extendLongStopLoss(filledQty); |
| | | log.info("[OKX] 多单成交 gridId:{}, qty:{}, 追挂止损", longGridElement.getId(), filledQty); |
| | | return; |
| | |
| | | state = StrategyState.STOPPED; |
| | | closeExistingPositions(); |
| | | executor.cancelAllAlgoOrders(); |
| | | startGrid(); |
| | | // 提交到 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(); |
| | | startGrid(); |
| | | // 提交到 executor 末尾:FIFO保证平仓完成后再重置 |
| | | executor.submitTask(() -> { |
| | | try { Thread.sleep(3000); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } |
| | | startGrid(); |
| | | }); |
| | | } |
| | | |
| | | // ---- getters ---- |