From 24bd0399d73a2d48e50233326fca7d9526f06259 Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Mon, 29 Dec 2025 14:15:59 +0800
Subject: [PATCH] refactor(trading): 重构MACD策略交易信号生成逻辑
---
src/main/java/com/xcong/excoin/modules/okxNewPrice/indicator/macdAndMatrategy/MacdMaStrategy.java | 94 +++++++++++++++++++++++++++-------------------
1 files changed, 55 insertions(+), 39 deletions(-)
diff --git a/src/main/java/com/xcong/excoin/modules/okxNewPrice/indicator/macdAndMatrategy/MacdMaStrategy.java b/src/main/java/com/xcong/excoin/modules/okxNewPrice/indicator/macdAndMatrategy/MacdMaStrategy.java
index 078c0ad..2490c43 100644
--- a/src/main/java/com/xcong/excoin/modules/okxNewPrice/indicator/macdAndMatrategy/MacdMaStrategy.java
+++ b/src/main/java/com/xcong/excoin/modules/okxNewPrice/indicator/macdAndMatrategy/MacdMaStrategy.java
@@ -22,6 +22,13 @@
public class MacdMaStrategy {
/** 持仓状态枚举 */
+ public enum OperationType {
+ /** 开仓平仓 */
+ open,
+ close
+ }
+
+ /** 持仓状态枚举 */
public enum PositionType {
/** 多头持仓 */
LONG_BUY,
@@ -40,10 +47,6 @@
private int volatilityPeriod; // 波动率计算周期
private BigDecimal stopLossRatio; // 止损比例
private BigDecimal takeProfitRatio; // 止盈比例
-
- // 持仓信息
- private BigDecimal entryPrice; // 开仓价格
- private long entryTime; // 开仓时间戳
/**
* 默认构造函数,使用标准MACD参数
@@ -73,9 +76,6 @@
this.stopLossRatio = stopLossRatio;
this.takeProfitRatio = takeProfitRatio;
- // 初始化持仓状态为空仓
- this.entryPrice = BigDecimal.ZERO;
- this.entryTime = 0;
}
/**
@@ -84,7 +84,7 @@
* @param closePrices 收盘价序列
* @return 生成的交易信号(LONG、SHORT或NONE)
*/
- public PositionType analyze(List<BigDecimal> closePrices) {
+ public PositionType analyzeOpen(List<BigDecimal> closePrices) {
// 数据检查:确保有足够的数据点进行计算
if (closePrices == null || closePrices.size() < 34) {
return PositionType.NONE; // 数据不足,无法生成信号
@@ -109,24 +109,49 @@
// 多头开仓条件检查
if (isLongEntryCondition(macdResult, closePrices, volatility.getValue())) {
// 执行开多
- this.entryPrice = latestPrice;
- this.entryTime = System.currentTimeMillis();
+ log.info( "多头开仓信号,价格:{}", latestPrice);
return PositionType.LONG_BUY;
}
// 空头开仓条件检查
if (isShortEntryCondition(macdResult, closePrices, volatility.getValue())) {
// 执行开空
- this.entryPrice = latestPrice;
- this.entryTime = System.currentTimeMillis();
+ log.info( "空头开仓信号,价格:{}", latestPrice);
return PositionType.SHORT_SELL;
}
- if (isLongExitCondition(macdResult, latestPrice)) {
- // 执行平多
- return PositionType.LONG_SELL;
+ // 无信号
+ return PositionType.NONE;
+
+ }
+
+ /**
+ * 分析最新价格数据并生成交易信号
+ *
+ * @param closePrices 收盘价序列
+ * @return 生成的交易信号(LONG、SHORT或NONE)
+ */
+ public PositionType analyzeClose(List<BigDecimal> closePrices) {
+ // 数据检查:确保有足够的数据点进行计算
+ if (closePrices == null || closePrices.size() < 34) {
+ return PositionType.NONE; // 数据不足,无法生成信号
}
- if (isShortExitCondition(macdResult, latestPrice)) {
- return PositionType.SHORT_BUY;
+
+ // 1. 计算MACD指标
+ MACDResult macdResult = MACDCalculator.calculateMACD(
+ closePrices, shortPeriod, longPeriod, signalPeriod);
+
+ // 最新收盘价
+ BigDecimal latestPrice = closePrices.get(closePrices.size() - 1);
+
+ if (isLongExitCondition(macdResult, latestPrice)) {
+ // 执行平多
+ log.info( "多头平仓信号,价格:{}", latestPrice);
+ return PositionType.LONG_SELL;
+ }
+ if (isShortExitCondition(macdResult, latestPrice)) {
+ // 执行平空
+ log.info( "空头平仓信号,价格:{}", latestPrice);
+ return PositionType.SHORT_BUY;
}
// 无信号
@@ -166,9 +191,18 @@
* @param historicalPrices 历史价格序列
* @return 交易指令(包含side和posSide),如果没有交易信号则返回null
*/
- public TradingOrder generateTradingOrder(List<BigDecimal> historicalPrices) {
- PositionType signal = analyze(historicalPrices);
+ public TradingOrder generateTradingOrder(List<BigDecimal> historicalPrices,String operation) {
+ PositionType signal = null;
+ if ( operation == OperationType.open.name()){
+
+ signal = analyzeOpen(historicalPrices);
+
+ }else if ( operation == OperationType.close.name()){
+
+ analyzeClose(historicalPrices);
+
+ }
// 根据信号和当前持仓状态生成交易指令
if (signal == PositionType.LONG_BUY) {
// 开多:买入开多(side 填写 buy; posSide 填写 long )
@@ -319,7 +353,7 @@
previous.getMacdHist().abs().compareTo(latest.getMacdHist().abs()) < 0;
// 金叉或柱状线扩张任一满足即可
- return isGoldenCross || isExpanding;
+ return isGoldenCross && isExpanding;
}
/**
@@ -373,7 +407,7 @@
previous.getMacdHist().abs().compareTo(latest.getMacdHist().abs()) > 0;
// 死叉或柱状线收缩任一满足即可
- return isDeathCross || isContracting;
+ return isDeathCross && isContracting;
}
/**
@@ -439,22 +473,4 @@
volatility.compareTo(maxVolatility) <= 0;
}
-
- /**
- * 获取开仓价格
- *
- * @return 开仓价格
- */
- public BigDecimal getEntryPrice() {
- return entryPrice;
- }
-
- /**
- * 获取开仓时间戳
- *
- * @return 开仓时间戳
- */
- public long getEntryTime() {
- return entryTime;
- }
}
\ No newline at end of file
--
Gitblit v1.9.1