| | |
| | | lastKlinePrice = closePrice; |
| | | updateUnrealizedPnl(); |
| | | if (state == StrategyState.STOPPED) { |
| | | try { |
| | | futuresApi.cancelPriceTriggeredOrderList(SETTLE, config.getContract()); |
| | | } catch (ApiException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | closeExistingPositions(); |
| | | |
| | | BigDecimal totalPnl = cumulativePnl.add(unrealizedPnl); |
| | | log.info("[Gate] 已实现:{}, 未实现:{}, 合计:{}", |
| | | cumulativePnl, unrealizedPnl, totalPnl); |
| | | return; |
| | | } |
| | | |
| | |
| | | List<GridElement> allShortOrders = GridElement.findAllShortOrders(longEntryPrice); |
| | | if (CollUtil.isNotEmpty(allShortOrders)){ |
| | | for (GridElement e : allShortOrders) { |
| | | executor.cancelOrder(e.getShortOrderId()); |
| | | executor.cancelConditionalOrder( |
| | | e.getShortOrderId(), |
| | | orderId -> { |
| | | shortEntryTraderIdParam( |
| | | e, |
| | | null, |
| | | false |
| | | ); |
| | | } |
| | | ); |
| | | |
| | | if (e.getShortTakeProfitOrderId() != null){ |
| | | executor.cancelConditionalOrder( |
| | | e.getShortTakeProfitOrderId(), |
| | | orderId -> { |
| | | shortTakeProfitTraderIdParam( |
| | | e, |
| | | null, |
| | | false |
| | | ); |
| | | } |
| | | ); |
| | | } |
| | | } |
| | | } |
| | | } |
| | |
| | | List<GridElement> allLongOrders = GridElement.findAllLongOrders(shortEntryPrice); |
| | | if (CollUtil.isNotEmpty(allLongOrders)){ |
| | | for (GridElement e : allLongOrders) { |
| | | executor.cancelOrder(e.getLongOrderId()); |
| | | executor.cancelConditionalOrder( |
| | | e.getLongOrderId(), |
| | | orderId -> { |
| | | longEntryTraderIdParam( |
| | | e, |
| | | null, |
| | | false |
| | | ); |
| | | } |
| | | ); |
| | | if (e.getLongTakeProfitOrderId() != null){ |
| | | executor.cancelConditionalOrder( |
| | | e.getLongTakeProfitOrderId(), |
| | | orderId -> { |
| | | longTakeProfitTraderIdParam( |
| | | e, |
| | | null, |
| | | false |
| | | ); |
| | | } |
| | | ); |
| | | } |
| | | } |
| | | } |
| | | } |
| | |
| | | return; |
| | | } |
| | | cumulativePnl = cumulativePnl.add(pnl); |
| | | log.info("[Gate] 盈亏累加:{}, 方向:{}, 累计:{}", pnl, side, cumulativePnl); |
| | | BigDecimal totalPnl = cumulativePnl.add(unrealizedPnl); |
| | | log.info("[Gate] 已实现:{}, 未实现:{}, 合计:{}", |
| | | cumulativePnl, unrealizedPnl, totalPnl); |
| | | |
| | | if (cumulativePnl.compareTo(config.getOverallTp()) >= 0) { |
| | | log.info("[Gate] 已达止盈目标 {}→已停止", cumulativePnl); |
| | | if (totalPnl.compareTo(config.getOverallTp()) >= 0) { |
| | | log.info("[Gate] 已达止盈目标(合计{})→已停止, 已实现:{}, 未实现:{}", |
| | | totalPnl, cumulativePnl, unrealizedPnl); |
| | | state = StrategyState.STOPPED; |
| | | } else if (cumulativePnl.compareTo(config.getMaxLoss().negate()) <= 0) { |
| | | log.info("[Gate] 已达亏损上限 {}→已停止", cumulativePnl); |
| | | } else if (totalPnl.compareTo(config.getMaxLoss().negate()) <= 0) { |
| | | log.info("[Gate] 已达亏损上限(合计{})→已停止, 已实现:{}, 未实现:{}", |
| | | totalPnl, cumulativePnl, unrealizedPnl); |
| | | state = StrategyState.STOPPED; |
| | | } |
| | | } |
| | |
| | | null, |
| | | false |
| | | ); |
| | | longEntryTraderIdParam( |
| | | byLongTakeProfitOrderId, |
| | | null, |
| | | false |
| | | ); |
| | | // longEntryTraderIdParam( |
| | | // byLongTakeProfitOrderId, |
| | | // null, |
| | | // false |
| | | // ); |
| | | } |
| | | GridElement byShortTakeProfitOrderId = GridElement.findByShortTakeProfitOrderId(orderId); |
| | | if (byShortTakeProfitOrderId != null){ |
| | |
| | | null, |
| | | false |
| | | ); |
| | | shortEntryTraderIdParam( |
| | | byShortTakeProfitOrderId, |
| | | null, |
| | | false |
| | | ); |
| | | // shortEntryTraderIdParam( |
| | | // byShortTakeProfitOrderId, |
| | | // null, |
| | | // false |
| | | // ); |
| | | } |
| | | |
| | | /** |
| | |
| | | GridElement longGridElement = GridElement.findByLongOrderId(orderId); |
| | | if (longGridElement != null) { |
| | | if (longGridElement.isHasLongOrder()){ |
| | | longEntryTraderIdParam( |
| | | longGridElement, |
| | | null, |
| | | false |
| | | ); |
| | | if (longGridElement.getLongTakeProfitOrderId() == null){ |
| | | BigDecimal longTp = longGridElement.getLongTraderParam().getTakeProfitPrice(); |
| | | if (longTp != null) { |
| | |
| | | GridElement shortGridElement = GridElement.findByShortOrderId(orderId); |
| | | if (shortGridElement != null) { |
| | | if (shortGridElement.isHasShortOrder()){ |
| | | shortEntryTraderIdParam( |
| | | shortGridElement, |
| | | null, |
| | | false |
| | | ); |
| | | if (shortGridElement.getShortTakeProfitOrderId() == null){ |
| | | BigDecimal shortTp = shortGridElement.getShortTraderParam().getTakeProfitPrice(); |
| | | if (shortTp != null) { |
| | |
| | | * @param reason 变更原因 |
| | | * @param orderType 订单类型(plan-close-long-position 等) |
| | | */ |
| | | public void onAutoOrder(String orderId, String status, String reason, String orderType) { |
| | | public void onAutoOrder(String orderId, String status, String reason, String orderType, String tradeId) { |
| | | if (state == StrategyState.STOPPED) { |
| | | return; |
| | | } |
| | |
| | | */ |
| | | GridElement longGridElement = GridElement.findByLongOrderId(orderId); |
| | | if (longGridElement != null) { |
| | | if (longGridElement.isHasLongOrder()){ |
| | | if (longGridElement.isHasLongOrder() && !tradeId.equals("0")){ |
| | | if (longGridElement.getLongTakeProfitOrderId() == null){ |
| | | BigDecimal longTp = longGridElement.getLongTraderParam().getTakeProfitPrice(); |
| | | if (longTp != null) { |
| | |
| | | } |
| | | GridElement shortGridElement = GridElement.findByShortOrderId(orderId); |
| | | if (shortGridElement != null) { |
| | | if (shortGridElement.isHasShortOrder()){ |
| | | if (shortGridElement.isHasShortOrder() && !tradeId.equals("0")){ |
| | | if (shortGridElement.getShortTakeProfitOrderId() == null){ |
| | | BigDecimal shortTp = shortGridElement.getShortTraderParam().getTakeProfitPrice(); |
| | | if (shortTp != null) { |
| | |
| | | // 判断网格是否能开空仓,如果不能则跳过 |
| | | if (UpGridElement != null) { |
| | | |
| | | if (!UpGridElement.isHasShortOrder()) { |
| | | if (!UpGridElement.isHasShortOrder() && shortEntryPrice.compareTo(newLongFirst) > 0) { |
| | | |
| | | //挂空仓条件单 |
| | | TraderParam upShortTraderParam = UpGridElement.getShortTraderParam(); |
| | |
| | | ){ |
| | | executor.placeConditionalEntryOrder( |
| | | downShortTraderParam.getEntryPrice(), |
| | | FuturesPriceTrigger.RuleEnum.NUMBER_2, |
| | | FuturesPriceTrigger.RuleEnum.NUMBER_1, |
| | | negate(downShortTraderParam.getQuantity()), |
| | | orderId -> |
| | | { |
| | |
| | | // 判断网格是否能开多仓,如果不能则跳过 |
| | | if (UpGridElement != null) { |
| | | |
| | | if (!UpGridElement.isHasLongOrder()) { |
| | | if (!UpGridElement.isHasLongOrder() && longEntryPrice.compareTo(newLongFirst) < 0) { |
| | | //挂多仓条件单 |
| | | TraderParam upLongTraderParam = UpGridElement.getLongTraderParam(); |
| | | executor.placeConditionalEntryOrder( |
| | |
| | | ){ |
| | | executor.placeConditionalEntryOrder( |
| | | downLongTraderParam.getEntryPrice(), |
| | | FuturesPriceTrigger.RuleEnum.NUMBER_1, |
| | | FuturesPriceTrigger.RuleEnum.NUMBER_2, |
| | | config.getQuantity(), |
| | | orderId -> |
| | | { |