| | |
| | | private BigDecimal shortEntryPrice; |
| | | private Long longOrderId; |
| | | private Long shortOrderId; |
| | | private Long longTpOrderId; |
| | | private Long longSlOrderId; |
| | | private Long shortTpOrderId; |
| | | private Long shortSlOrderId; |
| | | |
| | | private volatile BigDecimal lastClosePrice; |
| | | |
| | |
| | | */ |
| | | public void stopGrid() { |
| | | strategyActive = false; |
| | | cancelTpSl(longTpOrderId); |
| | | cancelTpSl(longSlOrderId); |
| | | cancelTpSl(shortTpOrderId); |
| | | cancelTpSl(shortSlOrderId); |
| | | closeAllPositions(); |
| | | log.info("[GateGrid] 网格策略已停止, 总盈亏: {}, 循环: {}", totalProfit, currentCycle); |
| | | } |
| | |
| | | /** |
| | | * K线回调:收到新的收盘价 |
| | | */ |
| | | public void onKline(BigDecimal closePrice, boolean isKlineClosed) { |
| | | public void onKline(BigDecimal closePrice) { |
| | | lastClosePrice = closePrice; |
| | | if (!strategyActive || !isKlineClosed) { |
| | | if (!strategyActive) { |
| | | return; |
| | | } |
| | | checkPositions(closePrice); |
| | |
| | | |
| | | private void placeLongTpSl(BigDecimal entryPrice) { |
| | | BigDecimal tpPrice = entryPrice.multiply(BigDecimal.ONE.add(gridRate)).setScale(1, RoundingMode.HALF_UP); |
| | | BigDecimal slPrice = entryPrice.multiply(BigDecimal.ONE.subtract(gridRate)).setScale(1, RoundingMode.HALF_UP); |
| | | longTpOrderId = placePriceTriggeredOrder(tpPrice, FuturesPriceTrigger.RuleEnum.NUMBER_1, "close-long-position", "close_long"); |
| | | longSlOrderId = placePriceTriggeredOrder(slPrice, FuturesPriceTrigger.RuleEnum.NUMBER_2, "close-long-position", "close_long"); |
| | | log.info("[GateGrid] 多头TP/SL已设置, TP:{}, SL:{}, tpId:{}, slId:{}", tpPrice, slPrice, longTpOrderId, longSlOrderId); |
| | | placePriceTriggeredOrder(tpPrice, FuturesPriceTrigger.RuleEnum.NUMBER_1, "close-long-position", "close_long"); |
| | | log.info("[GateGrid] 多头止盈已设置, TP:{}", tpPrice); |
| | | } |
| | | |
| | | private void placeShortTpSl(BigDecimal entryPrice) { |
| | | BigDecimal tpPrice = entryPrice.multiply(BigDecimal.ONE.subtract(gridRate)).setScale(1, RoundingMode.HALF_UP); |
| | | BigDecimal slPrice = entryPrice.multiply(BigDecimal.ONE.add(gridRate)).setScale(1, RoundingMode.HALF_UP); |
| | | shortTpOrderId = placePriceTriggeredOrder(tpPrice, FuturesPriceTrigger.RuleEnum.NUMBER_2, "close-short-position", "close_short"); |
| | | shortSlOrderId = placePriceTriggeredOrder(slPrice, FuturesPriceTrigger.RuleEnum.NUMBER_1, "close-short-position", "close_short"); |
| | | log.info("[GateGrid] 空头TP/SL已设置, TP:{}, SL:{}, tpId:{}, slId:{}", tpPrice, slPrice, shortTpOrderId, shortSlOrderId); |
| | | placePriceTriggeredOrder(tpPrice, FuturesPriceTrigger.RuleEnum.NUMBER_2, "close-short-position", "close_short"); |
| | | log.info("[GateGrid] 空头止盈已设置, TP:{}", tpPrice); |
| | | } |
| | | |
| | | private Long placePriceTriggeredOrder(BigDecimal triggerPrice, |
| | | private void placePriceTriggeredOrder(BigDecimal triggerPrice, |
| | | FuturesPriceTrigger.RuleEnum rule, |
| | | String orderType, |
| | | String autoSize) { |
| | |
| | | order.setOrderType(orderType); |
| | | |
| | | TriggerOrderResponse response = futuresApi.createPriceTriggeredOrder(SETTLE, order); |
| | | log.info("[GateGrid] 条件单已创建, triggerPrice:{}, rule:{}, orderType:{}, autoSize:{}, id:{}", |
| | | log.info("[GateGrid] 止盈条件单已创建, triggerPrice:{}, rule:{}, orderType:{}, autoSize:{}, id:{}", |
| | | triggerPrice, rule, orderType, autoSize, response.getId()); |
| | | return response.getId(); |
| | | } catch (Exception e) { |
| | | log.error("[GateGrid] 创建条件单失败, triggerPrice:{}, rule:{}, orderType:{}, autoSize:{}", |
| | | log.error("[GateGrid] 止盈条件单创建失败, triggerPrice:{}, rule:{}, orderType:{}, autoSize:{}", |
| | | triggerPrice, rule, orderType, autoSize, e); |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | private void cancelTpSl(Long orderId) { |
| | | if (orderId == null) { |
| | | return; |
| | | } |
| | | try { |
| | | futuresApi.cancelPriceTriggeredOrder(SETTLE, orderId); |
| | | log.info("[GateGrid] 已取消条件单, id:{}", orderId); |
| | | } catch (Exception e) { |
| | | log.warn("[GateGrid] 取消条件单失败, id:{}", orderId, e); |
| | | } |
| | | } |
| | | |
| | |
| | | } |
| | | longEntryPrice = null; |
| | | longOrderId = null; |
| | | cancelTpSl(longTpOrderId); |
| | | cancelTpSl(longSlOrderId); |
| | | longTpOrderId = null; |
| | | longSlOrderId = null; |
| | | } |
| | | |
| | | private void closeShortPosition() { |
| | |
| | | } |
| | | shortEntryPrice = null; |
| | | shortOrderId = null; |
| | | cancelTpSl(shortTpOrderId); |
| | | cancelTpSl(shortSlOrderId); |
| | | shortTpOrderId = null; |
| | | shortSlOrderId = null; |
| | | } |
| | | |
| | | private void closeAllPositions() { |