From b22f1797312ac54028f24c50ef9277f75c8ef9fc Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Mon, 29 Dec 2025 12:43:33 +0800
Subject: [PATCH] refactor(indicator): 重构MACD_MA策略的持仓状态和交易逻辑
---
src/main/java/com/xcong/excoin/modules/okxNewPrice/indicator/macdAndMatrategy/MacdMaStrategy.java | 302 ++++++++++++++++++++++++++++++++-----------------
1 files changed, 196 insertions(+), 106 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 44acbf2..078c0ad 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
@@ -10,8 +10,6 @@
import lombok.extern.slf4j.Slf4j;
import java.math.BigDecimal;
-import java.math.RoundingMode;
-import java.util.ArrayList;
import java.util.List;
/**
@@ -26,9 +24,11 @@
/** 持仓状态枚举 */
public enum PositionType {
/** 多头持仓 */
- LONG,
+ LONG_BUY,
+ LONG_SELL,
/** 空头持仓 */
- SHORT,
+ SHORT_SELL,
+ SHORT_BUY,
/** 空仓 */
NONE
}
@@ -111,7 +111,7 @@
// 执行开多
this.entryPrice = latestPrice;
this.entryTime = System.currentTimeMillis();
- return PositionType.LONG;
+ return PositionType.LONG_BUY;
}
// 空头开仓条件检查
@@ -119,7 +119,14 @@
// 执行开空
this.entryPrice = latestPrice;
this.entryTime = System.currentTimeMillis();
- return PositionType.SHORT;
+ return PositionType.SHORT_SELL;
+ }
+ if (isLongExitCondition(macdResult, latestPrice)) {
+ // 执行平多
+ return PositionType.LONG_SELL;
+ }
+ if (isShortExitCondition(macdResult, latestPrice)) {
+ return PositionType.SHORT_BUY;
}
// 无信号
@@ -163,12 +170,18 @@
PositionType signal = analyze(historicalPrices);
// 根据信号和当前持仓状态生成交易指令
- if (signal == PositionType.LONG) {
+ if (signal == PositionType.LONG_BUY) {
// 开多:买入开多(side 填写 buy; posSide 填写 long )
return new TradingOrder("buy", "long");
- } else if (signal == PositionType.SHORT) {
+ } else if (signal == PositionType.LONG_SELL) {
+ // 开空:卖出开空(side 填写 sell; posSide 填写 short )
+ return new TradingOrder("sell", "long");
+ } else if (signal == PositionType.SHORT_SELL) {
// 开空:卖出开空(side 填写 sell; posSide 填写 short )
return new TradingOrder("sell", "short");
+ } else if (signal == PositionType.SHORT_BUY) {
+ // 开空:卖出开空(side 填写 sell; posSide 填写 short )
+ return new TradingOrder("buy", "short");
}
// 没有交易信号
return null;
@@ -183,108 +196,108 @@
* @return 是否满足多头开仓条件
*/
private boolean isLongEntryCondition(MACDResult macdResult, List<BigDecimal> closePrices, BigDecimal volatility) {
- // 1. EMA金叉检查(简化为只检查当前短期EMA > 当前长期EMA)
- List<PriceData> macdData = macdResult.getMacdData();
- if (macdData.size() < 1) {
- return false;
+
+ // 2. MACD金叉且柱状线扩张检查
+ boolean isMacdFavorable = isMacdGoldenCrossAndExpanding(macdResult);
+
+ // 3. MACD柱状线必须为正
+ boolean macdPositive = isMacdPositive(macdResult);
+
+ // 4. 波动率过滤(必须在合理范围内)
+ boolean volatilityFilter = isVolatilityInRange(volatility);
+
+ if (macdPositive && volatilityFilter && isMacdFavorable) {
+ log.info("多头信号形成, MACD有利状态: {}, 柱状线为正: {}, 波动率过滤: {}",
+ isMacdFavorable, macdPositive, volatilityFilter);
}
- boolean emaGoldenCross = isMacdGoldenCrossAndExpanding(macdResult);
-
- PriceData latest = macdData.get(macdData.size() - 1);
- // 2. MACD柱状线为正
- boolean macdPositive = latest.getMacdHist().compareTo(BigDecimal.ZERO) > 0;
-
- // 3. 简化的波动率检查
- boolean volatilityFilter = volatility.compareTo(BigDecimal.ZERO) > 0;
-
- if ( emaGoldenCross && macdPositive && volatilityFilter){
- log.info( "EMA金叉: {}", emaGoldenCross);
- log.info( "MACD柱状线为正: {}" ,macdPositive);
- log.info( "波动率过滤: {}" ,volatilityFilter);
- }
- // 只需要EMA短期在长期上方、MACD柱状线为正且波动率大于0
- return emaGoldenCross && macdPositive && volatilityFilter;
+
+ // 所有条件必须同时满足
+ return macdPositive && volatilityFilter && isMacdFavorable;
}
/**
* 空头开仓条件检查
- *
+ *
* @param macdResult MACD计算结果
* @param closePrices 收盘价序列
* @param volatility 当前波动率
* @return 是否满足空头开仓条件
*/
- private boolean isShortEntryCondition(MACDResult macdResult, List<BigDecimal> closePrices,
- BigDecimal volatility) {
- List<PriceData> macdData = macdResult.getMacdData();
- if (macdData.size() < 1) {
- return false;
- }
- // 2. MACD柱状线收缩+死叉检查
- boolean macdDeathCross = isMacdDeathCrossAndContracting(macdResult);
-
- // 2. MACD柱状线为负
- PriceData latest = macdData.get(macdData.size() - 1);
- boolean macdPositive = latest.getMacdHist().compareTo(BigDecimal.ZERO) < 0;
-
-
- // 4. 波动率过滤检查(0.5% ~ 5%)
+ private boolean isShortEntryCondition(MACDResult macdResult, List<BigDecimal> closePrices, BigDecimal volatility) {
+ // 2. MACD死叉且柱状线收缩检查
+ boolean isMacdFavorable = isMacdDeathCrossAndContracting(macdResult);
+
+ // 3. MACD柱状线必须为负
+ boolean macdNegative = isMacdNegative(macdResult);
+
+ // 4. 波动率过滤(必须在合理范围内)
boolean volatilityFilter = isVolatilityInRange(volatility);
- if ( macdDeathCross&& volatilityFilter){
- log.info( "MACD柱状线收缩+死叉: {}", macdDeathCross);
- log.info( "MACD柱状线为负: {}" ,macdPositive);
- log.info( "波动率过滤: {}", volatilityFilter);
+
+ if (macdNegative && volatilityFilter && isMacdFavorable) {
+ log.info("空头信号形成, MACD有利状态: {}, 柱状线为负: {}, 波动率过滤: {}",
+ isMacdFavorable, macdNegative, volatilityFilter);
}
-
+
// 所有条件必须同时满足
- return macdDeathCross && volatilityFilter && macdPositive;
+ return macdNegative && volatilityFilter && isMacdFavorable;
}
-
+
/**
- * EMA金叉检查
- *
+ * 多头平仓条件检查
+ *
* @param macdResult MACD计算结果
- * @return 是否形成EMA金叉
+ * @param currentPrice 当前价格
+ * @return 是否满足多头平仓条件
*/
- private boolean isEmaGoldenCross(MACDResult macdResult) {
+ private boolean isLongExitCondition(MACDResult macdResult, BigDecimal currentPrice) {
+ // 1. MACD柱状线由正转负(动量转变)
List<PriceData> macdData = macdResult.getMacdData();
- if (macdData.size() < 2) {
- return false;
+ if (macdData.size() >= 2) {
+ PriceData latest = macdData.get(macdData.size() - 1);
+ PriceData previous = macdData.get(macdData.size() - 2);
+
+ boolean momentumShift = previous.getMacdHist().compareTo(BigDecimal.ZERO) >= 0 &&
+ latest.getMacdHist().abs().compareTo(previous.getMacdHist().abs()) < 0;
+ if (momentumShift) {
+ return true;
+ }
}
-
- PriceData latest = macdData.get(macdData.size() - 1);
- PriceData previous = macdData.get(macdData.size() - 2);
-
- // 当前短期EMA > 当前长期EMA,并且前一期短期EMA <= 前一期长期EMA
- return latest.getEmaShort().compareTo(latest.getEmaLong()) > 0 &&
- previous.getEmaShort().compareTo(previous.getEmaLong()) <= 0;
+ return false;
}
-
+
/**
- * EMA死叉检查
- *
+ * 空头平仓条件检查
+ *
* @param macdResult MACD计算结果
- * @return 是否形成EMA死叉
+ * @param currentPrice 当前价格
+ * @return 是否满足空头平仓条件
*/
- private boolean isEmaDeathCross(MACDResult macdResult) {
+ private boolean isShortExitCondition(MACDResult macdResult, BigDecimal currentPrice) {
+ // 1. MACD柱状线由负转正(动量转变)
List<PriceData> macdData = macdResult.getMacdData();
- if (macdData.size() < 2) {
- return false;
+ if (macdData.size() >= 2) {
+ PriceData latest = macdData.get(macdData.size() - 1);
+ PriceData previous = macdData.get(macdData.size() - 2);
+
+ boolean momentumShift = previous.getMacdHist().compareTo(BigDecimal.ZERO) <= 0 &&
+ latest.getMacdHist().abs().compareTo(previous.getMacdHist().abs()) < 0;
+ if (momentumShift) {
+ return true;
+ }
}
-
- PriceData latest = macdData.get(macdData.size() - 1);
- PriceData previous = macdData.get(macdData.size() - 2);
-
- // 当前短期EMA < 当前长期EMA,并且前一期短期EMA >= 前一期长期EMA
- return latest.getEmaShort().compareTo(latest.getEmaLong()) < 0 &&
- previous.getEmaShort().compareTo(previous.getEmaLong()) >= 0;
+
+ return false;
}
-
+
/**
* MACD金叉且柱状线扩张检查
- *
+ * <p>
+ * 条件:
+ * 1. DIF线从下往上穿过DEA线(金叉)
+ * 2. MACD柱状线绝对值增大且为正值(动量增强)
+ *
* @param macdResult MACD计算结果
- * @return 是否形成MACD金叉且柱状线扩张
+ * @return 是否形成MACD金叉或柱状线扩张
*/
private boolean isMacdGoldenCrossAndExpanding(MACDResult macdResult) {
List<PriceData> macdData = macdResult.getMacdData();
@@ -294,26 +307,30 @@
PriceData latest = macdData.get(macdData.size() - 1);
PriceData previous = macdData.get(macdData.size() - 2);
-
- // 1. MACD金叉检查(DIF上穿DEA)
- boolean goldenCross = previous.getDif().compareTo(previous.getDea()) <= 0 &&
- latest.getDif().compareTo(latest.getDea()) > 0;
-
- // 2. MACD柱状线扩张检查
- boolean histogramExpanding =
- previous.getMacdHist().compareTo(latest.getMacdHist()) < 0 &&
- latest.getMacdHist().compareTo(BigDecimal.ZERO) > 0;
-
- return goldenCross && histogramExpanding;
+ PriceData prevPrev = macdData.get(macdData.size() - 3);
+
+ // 金叉判断:DIF从下往上穿过DEA
+ boolean isGoldenCross = prevPrev.getDif().compareTo(prevPrev.getDea()) <= 0 &&
+ previous.getDif().compareTo(previous.getDea()) > 0;
+
+ // 柱状线扩张判断:连续正值且绝对值增大
+ boolean isExpanding = latest.getMacdHist().compareTo(BigDecimal.ZERO) > 0 &&
+ previous.getMacdHist().compareTo(BigDecimal.ZERO) > 0 &&
+ previous.getMacdHist().abs().compareTo(latest.getMacdHist().abs()) < 0;
+
+ // 金叉或柱状线扩张任一满足即可
+ return isGoldenCross || isExpanding;
}
-
+
/**
- * MACD死叉且柱状线收缩检查
- *
+ * MACD柱状线扩张检查
+ * <p>
+ * 条件:当前MACD柱状线绝对值大于前一根
+ *
* @param macdResult MACD计算结果
- * @return 是否形成MACD死叉且柱状线收缩
+ * @return MACD柱状线是否扩张
*/
- private boolean isMacdDeathCrossAndContracting(MACDResult macdResult) {
+ private boolean isExpanding(MACDResult macdResult) {
List<PriceData> macdData = macdResult.getMacdData();
if (macdData.size() < 2) {
return false;
@@ -321,18 +338,91 @@
PriceData latest = macdData.get(macdData.size() - 1);
PriceData previous = macdData.get(macdData.size() - 2);
+
+ // MACD柱状线扩张:当前绝对值大于前一根
+ return latest.getMacdHist().abs().compareTo(previous.getMacdHist().abs()) > 0;
+ }
- // 1. MACD死叉检查(DIF下穿DEA)
- boolean deathCross = previous.getDif().compareTo(previous.getDea()) >= 0 &&
- latest.getDif().compareTo(latest.getDea()) < 0;
+ /**
+ * MACD死叉且柱状线收缩检查
+ * <p>
+ * 条件:
+ * 1. DIF线从上往下穿过DEA线(死叉)
+ * 2. MACD柱状线绝对值减小且为负值(动量减弱)
+ *
+ * @param macdResult MACD计算结果
+ * @return 是否形成MACD死叉或柱状线收缩
+ */
+ private boolean isMacdDeathCrossAndContracting(MACDResult macdResult) {
+ List<PriceData> macdData = macdResult.getMacdData();
+ if (macdData.size() < 3) {
+ return false;
+ }
- // 2. MACD柱状线收缩检查(绝对值减小)
- boolean histogramContracting =
- previous.getMacdHist().abs().compareTo(
- latest.getMacdHist().abs()) < 0 &&
- latest.getMacdHist().compareTo(BigDecimal.ZERO) < 0;
+ PriceData latest = macdData.get(macdData.size() - 1);
+ PriceData previous = macdData.get(macdData.size() - 2);
+ PriceData prevPrev = macdData.get(macdData.size() - 3);
+
+ // 死叉判断:DIF从上往下穿过DEA
+ boolean isDeathCross = prevPrev.getDif().compareTo(prevPrev.getDea()) >= 0 &&
+ previous.getDif().compareTo(previous.getDea()) < 0;
+
+ // 柱状线收缩判断:连续负值且绝对值减小
+ boolean isContracting = latest.getMacdHist().compareTo(BigDecimal.ZERO) < 0 &&
+ previous.getMacdHist().compareTo(BigDecimal.ZERO) < 0 &&
+ previous.getMacdHist().abs().compareTo(latest.getMacdHist().abs()) > 0;
+
+ // 死叉或柱状线收缩任一满足即可
+ return isDeathCross || isContracting;
+ }
+
+ /**
+ * MACD柱状线收缩检查
+ * <p>
+ * 条件:前一根MACD柱状线绝对值大于当前
+ *
+ * @param macdResult MACD计算结果
+ * @return MACD柱状线是否收缩
+ */
+ private boolean isContracting(MACDResult macdResult) {
+ List<PriceData> macdData = macdResult.getMacdData();
+ if (macdData.size() < 2) {
+ return false;
+ }
- return deathCross && histogramContracting;
+ PriceData latest = macdData.get(macdData.size() - 1);
+ PriceData previous = macdData.get(macdData.size() - 2);
+
+ // MACD柱状线收缩:前一根绝对值大于当前
+ return previous.getMacdHist().abs().compareTo(latest.getMacdHist().abs()) > 0;
+ }
+
+ /**
+ * 检查MACD柱状线是否为正值
+ *
+ * @param macdResult MACD计算结果
+ * @return MACD柱状线是否为正值
+ */
+ private boolean isMacdPositive(MACDResult macdResult) {
+ List<PriceData> macdData = macdResult.getMacdData();
+ if (macdData.isEmpty()) {
+ return false;
+ }
+ return macdData.get(macdData.size() - 1).getMacdHist().compareTo(BigDecimal.ZERO) > 0;
+ }
+
+ /**
+ * 检查MACD柱状线是否为负值
+ *
+ * @param macdResult MACD计算结果
+ * @return MACD柱状线是否为负值
+ */
+ private boolean isMacdNegative(MACDResult macdResult) {
+ List<PriceData> macdData = macdResult.getMacdData();
+ if (macdData.isEmpty()) {
+ return false;
+ }
+ return macdData.get(macdData.size() - 1).getMacdHist().compareTo(BigDecimal.ZERO) < 0;
}
/**
--
Gitblit v1.9.1