From 9ff43c07c9ce3fe76ba4a21e28885097b2b721e3 Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Mon, 05 Jan 2026 14:08:47 +0800
Subject: [PATCH] feat(trading): 优化MACD策略算法适配欧意平台

---
 src/main/java/com/xcong/excoin/modules/okxNewPrice/indicator/macdAndMatrategy/MacdMaStrategy.java |  638 +++++++++++++++++++++++++++++++++++++--------------------
 1 files changed, 414 insertions(+), 224 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 b143213..7a80222 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
@@ -1,267 +1,457 @@
+/**
+ * MACD和MA组合交易策略实现类
+ * 基于15分钟K线数据生成交易信号并确定持仓方向
+ *
+ * 该策略综合考虑了EMA指标、MACD指标、价格突破信号和波动率因素,
+ * 形成了一套完整的开仓、平仓和持仓管理机制。
+ */
 package com.xcong.excoin.modules.okxNewPrice.indicator.macdAndMatrategy;
 
-import com.xcong.excoin.modules.okxNewPrice.indicator.*;
-import com.xcong.excoin.modules.okxNewPrice.okxWs.enums.CoinEnums;
-import com.xcong.excoin.modules.okxNewPrice.okxWs.enums.OrderParamEnums;
-import com.xcong.excoin.modules.okxNewPrice.okxWs.param.TradeRequestParam;
+import lombok.extern.slf4j.Slf4j;
 
 import java.math.BigDecimal;
 import java.util.List;
 
 /**
- * MACD+MA复合交易策略实现类
- * 
- * 【策略核心思想】
- * 结合移动平均线(MA)的趋势判断能力和MACD指标的动量分析能力,构建一个兼顾趋势跟踪和入场时机的复合交易策略。
- * 
- * 【策略主要特点】
- * 1. **趋势导向**:以长期MA(100日)作为ETH市场趋势的主要判断依据
- * 2. **精确入场**:利用MACD指标的动量变化和短期MA(30日)的支撑/阻力作用确定最佳入场点
- * 3. **风险控制**:通过RSI和波动率指标过滤掉风险较高的交易信号,并设置明确的止损止盈
- * 4. **分层离场**:结合技术指标、移动平均线和风险控制构建多重离场机制
- * 5. **动态调整**:MACD周期根据市场波动率自动调整,适应ETH高波动特性
- * 
- * 【核心交易逻辑】
- * 1. **趋势判断**:当前价格高于长期MA(100日)判定为牛市,低于则为熊市
- * 2. **入场条件**:
- *    - 牛市:MACD多头信号(DIF>DEA) 且 价格>短期MA(30日) 且 MACD柱状图>0 且 RSI在合理区间
- *    - 熊市:MACD空头信号(DIF<DEA) 且 价格<短期MA(30日) 且 MACD柱状图<0 且 RSI在合理区间
- * 3. **离场条件**:
- *    - 牛市多头持仓:MACD空头信号(DIF<DEA) 或 价格跌破短期MA 或 触及止损/止盈
- *    - 熊市空头持仓:MACD多头信号(DIF>DEA) 或 价格突破短期MA 或 触及止损/止盈
- * 4. **过滤条件**:
- *    - 高风险过滤:RSI>65时不追多,RSI<35时不追空
- *    - 低波动过滤:波动率<0.5%或>5%时不进行交易
- * 
- * 【适用场景】
- * 专为ETH合约设计,适用于ETH高波动、24/7交易的市场环境,适合中短线趋势交易。
- * 
- * 【风险提示】
- * 1. 策略需要至少200个价格数据点才能有效运行
- * 2. 在极端市场条件下(如黑天鹅事件)可能会产生较大亏损
- * 3. 建议结合其他风险控制手段(如止损设置)使用
+ * MACD和MA组合交易策略实现
+ * <p>
+ * 该策略利用EMA交叉、MACD指标、价格突破信号和波动率过滤,
+ * 为15分钟K线级别交易提供综合决策支持。
  */
+@Slf4j
 public class MacdMaStrategy {
-    /**
-     * 策略使用的技术指标实例(适配ETH合约特点)
-     */
-    // MACD指标:用于判断价格动量和趋势变化
-    private final MACD macd = new MACD();
-    // 30日移动平均线:ETH波动较大,使用更短的周期捕捉趋势变化
-    private final MovingAverage ma30 = new MovingAverage(30);
-    // 100日移动平均线:ETH作为高波动资产,长期趋势判断使用更短周期
-    private final MovingAverage ma100 = new MovingAverage(100);
-    // RSI指标(10周期):ETH波动快,使用更短周期提高响应速度
-    private final RSI rsi = new RSI(10);
-    // 波动率指标(15周期):ETH波动频繁,使用更短周期捕捉市场变化
-    private final Volatility volatility = new Volatility(15);
-    // 最新价格:用于策略判断
-    private BigDecimal lastPrice;
-    // 当前市场趋势:牛市/熊市
-    private TrendDirection trend;
+
+    /** 操作类型枚举 */
+    public enum OperationType {
+        /** 开仓 */
+        open,
+        /** 平仓 */
+        close
+    }
+
+    /** 持仓状态枚举 */
+    public enum PositionType {
+        /** 多头开仓 */
+        LONG_BUY,
+        /** 多头平仓 */
+        LONG_SELL,
+        /** 空头开仓 */
+        SHORT_SELL,
+        /** 空头平仓 */
+        SHORT_BUY,
+        /** 空仓 */
+        NONE
+    }
+
+    /** 交易指令类,封装side和posSide的组合 */
+    public static class TradingOrder {
+        private String side;    // buy或sell
+        private String posSide; // long或short
+
+        public TradingOrder(String side, String posSide) {
+            this.side = side;
+            this.posSide = posSide;
+        }
+
+        public String getSide() {
+            return side;
+        }
+
+        public String getPosSide() {
+            return posSide;
+        }
+
+        @Override
+        public String toString() {
+            return String.format("TradingOrder{side='%s', posSide='%s'}", side, posSide);
+        }
+    }
+
+    // 策略参数
+    private int shortPeriod;      // 短期EMA周期
+    private int longPeriod;       // 长期EMA周期
+    private int signalPeriod;     // MACD信号线周期
+    private int volatilityPeriod; // 波动率计算周期
+    private int trendPeriod = 200; // 趋势过滤EMA周期(200日)
+    private BigDecimal stopLossRatio;   // 止损比例
+    private BigDecimal takeProfitRatio; // 止盈比例
 
     /**
-     * 策略执行的主入口方法
+     * 默认构造函数,使用标准MACD参数
+     * 短期周期=12, 长期周期=26, 信号线周期=9, 波动率周期=20
+     * 止损比例=1%, 止盈比例=2%
+     */
+    public MacdMaStrategy() {
+        this(12, 26, 9, 20, new BigDecimal("0.01"), new BigDecimal("0.02"));
+    }
+
+    /**
+     * 自定义参数构造函数,使用默认趋势周期200
      *
-     * 第一步:更新所有技术指标
-     * 
-     * @param prices 历史价格数据列表
-     * @throws IllegalArgumentException 如果价格数据不足200个
+     * @param shortPeriod 短期EMA周期
+     * @param longPeriod 长期EMA周期
+     * @param signalPeriod MACD信号线周期
+     * @param volatilityPeriod 波动率计算周期
+     * @param stopLossRatio 止损比例
+     * @param takeProfitRatio 止盈比例
      */
-    public void execute(List<BigDecimal> prices) {
-        // 验证输入数据完整性:策略需要至少200个价格数据点
-        if (prices.size() < 200) {
-            throw new IllegalArgumentException("至少需要200个价格数据点才能运行该策略");
-        }
-        updateIndicators(prices);
-
-        trend = getTrend();
-
+    public MacdMaStrategy(int shortPeriod, int longPeriod, int signalPeriod, int volatilityPeriod,
+                          BigDecimal stopLossRatio, BigDecimal takeProfitRatio) {
+        this(shortPeriod, longPeriod, signalPeriod, volatilityPeriod, 200, stopLossRatio, takeProfitRatio);
     }
 
     /**
-     * 第二步:确定当前市场趋势
-      */
-    public TrendDirection getTrend(){
-        return determineTrend();
-    }
-
-
-    /**
-     * 第三步:检查是否需要跳过当前交易
-     */
-    public boolean getSkip(){
-        return shouldSkipTrade();
-    }
-
-    /**
-     * 第四步:是否允许开仓
-     */
-    public TradeRequestParam getOrderParamOpen(TradeRequestParam tradeRequestParam){
-        return checkEntrySignal(tradeRequestParam);
-    }
-
-    /**
-     * 第五步:是否允许平仓
-     */
-    public TradeRequestParam getOrderParamClose(TradeRequestParam tradeRequestParam){
-        return checkExitSignal(tradeRequestParam);
-    }
-
-    /**
-     * 更新所有技术指标的计算结果
+     * 自定义参数构造函数
      *
-     * @param prices 历史价格数据列表
+     * @param shortPeriod 短期EMA周期
+     * @param longPeriod 长期EMA周期
+     * @param signalPeriod MACD信号线周期
+     * @param volatilityPeriod 波动率计算周期
+     * @param trendPeriod 趋势过滤EMA周期(200日)
+     * @param stopLossRatio 止损比例
+     * @param takeProfitRatio 止盈比例
      */
-    private void updateIndicators(List<BigDecimal> prices) {
-        // 先计算波动率指标,因为MACD需要用它来动态调整周期
-        volatility.calculate(prices);
-        // 计算MACD指标并传入波动率参数,实现动态周期调整
-        macd.calculate(prices, volatility.getValue());
-        // 计算移动平均线指标
-        ma30.calculate(prices);      // 计算30日移动平均线
-        ma100.calculate(prices);     // 计算100日移动平均线
-        // 计算RSI指标
-        rsi.calculate(prices);
-        // 更新最新价格
-        lastPrice = prices.get(prices.size()-1);
+    public MacdMaStrategy(int shortPeriod, int longPeriod, int signalPeriod, int volatilityPeriod, int trendPeriod,
+                          BigDecimal stopLossRatio, BigDecimal takeProfitRatio) {
+        this.shortPeriod = shortPeriod;
+        this.longPeriod = longPeriod;
+        this.signalPeriod = signalPeriod;
+        this.volatilityPeriod = volatilityPeriod;
+        this.trendPeriod = trendPeriod;
+        this.stopLossRatio = stopLossRatio;
+        this.takeProfitRatio = takeProfitRatio;
     }
 
-
+    // 主流程方法
+    
     /**
-     * 确定当前市场趋势(牛市/熊市)
-     * 
+     * 分析历史价格数据并生成交易指令
+     *
+     * @param historicalPrices 历史价格序列
+     * @param historical1DayPrices 日线历史价格序列
+     * @param operation 操作类型(open/close)
+     * @return 交易指令(包含side和posSide),如果没有交易信号则返回null
      */
-    private TrendDirection determineTrend() {
-        BigDecimal currentMa100 = ma100.getMa(); // 获取当前100日移动平均线
-        // 根据最新价格与100日MA的关系判断趋势:价格高于100日MA为牛市,否则为熊市
-        return lastPrice.compareTo(currentMa100) > 0 ?
-                TrendDirection.BULLISH : TrendDirection.BEARISH;
+    public TradingOrder generateTradingOrder(List<BigDecimal> historicalPrices, List<BigDecimal> historical1DayPrices, String operation) {
+        PositionType signal = null;
+
+        if (OperationType.open.name().equals(operation)) {
+            signal = analyzeOpen(historicalPrices, historical1DayPrices);
+        } else if (OperationType.close.name().equals(operation)) {
+            signal = analyzeClose(historicalPrices, historical1DayPrices);
+        }
+        
+        // 根据信号生成交易指令
+        return convertSignalToTradingOrder(signal);
     }
 
     /**
-     * 检查是否需要跳过当前交易
-     * 
-     * @return true表示需要跳过交易,false表示可以进行交易
+     * 分析最新价格数据并生成开仓信号
+     *
+     * @param closePrices 收盘价序列
+     * @param close1DPrices 日线收盘价序列
+     * @return 生成的交易信号(LONG、SHORT或NONE)
      */
-    private boolean shouldSkipTrade() {
-        // 波动率过滤:当波动率小于1%时,市场活跃度不足,跳过交易
-        if (volatility.getValue().compareTo(new BigDecimal("0.01")) < 0) {
-            return true;
+    public PositionType analyzeOpen(List<BigDecimal> closePrices, List<BigDecimal> close1DPrices) {
+        // 数据检查:确保有足够的数据点进行计算(需要足够数据计算200日EMA)
+        if (closePrices == null || closePrices.size() < Math.max(34, trendPeriod) || 
+            close1DPrices == null || close1DPrices.size() < Math.max(34, trendPeriod)) {
+            return PositionType.NONE; // 数据不足,无法生成信号
         }
 
-        // RSI极端值过滤:
-        // 1. 牛市中RSI>65表示超买,避免追高
-        // 2. 熊市中RSI<35表示超卖,避免追空
-        if (trend == TrendDirection.BULLISH && rsi.getRsi().compareTo(new BigDecimal(65)) > 0) {
-            return true;
+        // 计算MACD指标
+        MACDResult macdResult = MACDCalculator.calculateMACD(
+                closePrices, shortPeriod, longPeriod, signalPeriod);
+         log.info("MACD计算结果:{}", macdResult.getMacdData().get(macdResult.getMacdData().size() - 1));
+
+        // 多头开仓条件检查
+        if (isLongEntryCondition(macdResult, closePrices, close1DPrices)) {
+            log.info("多头开仓信号,价格:{}", closePrices.get(closePrices.size() - 1));
+            return PositionType.LONG_BUY;
         }
-        if (trend == TrendDirection.BEARISH && rsi.getRsi().compareTo(new BigDecimal(35)) < 0) {
-            return true;
+
+        // 空头开仓条件检查
+        if (isShortEntryCondition(macdResult, closePrices, close1DPrices)) {
+            log.info("空头开仓信号,价格:{}", closePrices.get(closePrices.size() - 1));
+            return PositionType.SHORT_SELL;
+        }
+        
+        // 无信号
+        return PositionType.NONE;
+    }
+
+    /**
+     * 分析最新价格数据并生成平仓信号
+     *
+     * @param closePrices 收盘价序列
+     * @param close1DPrices 日线收盘价序列
+     * @return 生成的交易信号(LONG_SELL、SHORT_BUY或NONE)
+     */
+    public PositionType analyzeClose(List<BigDecimal> closePrices, List<BigDecimal> close1DPrices) {
+        // 数据检查:确保有足够的数据点进行计算
+        if (closePrices == null || closePrices.size() < Math.max(34, trendPeriod) || 
+            close1DPrices == null || close1DPrices.size() < Math.max(34, trendPeriod)) {
+            return PositionType.NONE; // 数据不足,无法生成信号
+        }
+
+        // 计算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;
+        }
+
+        // 无信号
+        return PositionType.NONE;
+    }
+
+    // 信号转换方法
+    
+    /**
+     * 将持仓信号转换为交易指令
+     *
+     * @param signal 持仓信号
+     * @return 交易指令,无信号则返回null
+     */
+    private TradingOrder convertSignalToTradingOrder(PositionType signal) {
+        if (signal == null) {
+            return null;
+        }
+        
+        switch (signal) {
+            case LONG_BUY:
+                // 开多:买入开多(side 填写 buy; posSide 填写 long )
+                return new TradingOrder("buy", "long");
+            case LONG_SELL:
+                // 平多:卖出平多(side 填写 sell; posSide 填写 long )
+                return new TradingOrder("sell", "long");
+            case SHORT_SELL:
+                // 开空:卖出开空(side 填写 sell; posSide 填写 short )
+                return new TradingOrder("sell", "short");
+            case SHORT_BUY:
+                // 平空:买入平空(side 填写 buy; posSide 填写 short )
+                return new TradingOrder("buy", "short");
+            default:
+                // 无信号
+                return null;
+        }
+    }
+
+    // 开仓条件检查方法
+    
+    /**
+     * 多头开仓条件检查
+     *
+     * @param macdResult MACD计算结果
+     * @param closePrices 收盘价序列
+     * @param close1DPrices 日线收盘价序列
+     * @return 是否满足多头开仓条件
+     */
+    private boolean isLongEntryCondition(MACDResult macdResult, List<BigDecimal> closePrices, List<BigDecimal> close1DPrices) {
+        // 1. 计算200日EMA(趋势过滤)
+        List<BigDecimal> trendEma = EMACalculator.calculateEMA(close1DPrices, trendPeriod, true);
+        BigDecimal latestTrendEma = trendEma.get(trendEma.size() - 1);
+        BigDecimal latestPrice = closePrices.get(closePrices.size() - 1);
+        
+        // 2. 价格必须位于200日EMA上方(多头趋势确认)
+        boolean isAboveTrend = latestPrice.compareTo(latestTrendEma) > 0;
+        
+        // 3. MACD金叉检查
+        boolean isGoldenCross = isGoldenCross(macdResult);
+        
+        // 4. MACD柱状线由负转正(动量转变)
+        boolean isMacdHistTurningPositive = isMacdHistTurningPositive(macdResult);
+        
+        // 5. 底背离检查(增强多头信号可靠性)
+        boolean isBottomDivergence = MACDCalculator.isBottomDivergence(closePrices, macdResult);
+
+        log.info("多头信号检查, 价格位于200日EMA上方: {}, 金叉: {}, MACD柱状线由负转正: {}, 底背离: {}",
+                isAboveTrend, isGoldenCross, isMacdHistTurningPositive, isBottomDivergence);
+        
+        // 多头开仓条件:趋势向上 + 金叉 + (柱状线转强或底背离)
+        return isAboveTrend && isGoldenCross && (isMacdHistTurningPositive || isBottomDivergence);
+    }
+
+    /**
+     * 空头开仓条件检查
+     * 
+     * @param macdResult MACD计算结果
+     * @param closePrices 收盘价序列
+     * @param close1DPrices 日线收盘价序列
+     * @return 是否满足空头开仓条件
+     */
+    private boolean isShortEntryCondition(MACDResult macdResult, List<BigDecimal> closePrices, List<BigDecimal> close1DPrices) {
+        // 1. 计算200日EMA(趋势过滤)
+        List<BigDecimal> trendEma = EMACalculator.calculateEMA(close1DPrices, trendPeriod, true);
+        BigDecimal latestTrendEma = trendEma.get(trendEma.size() - 1);
+        BigDecimal latestPrice = closePrices.get(closePrices.size() - 1);
+        
+        // 2. 价格必须位于200日EMA下方(空头趋势确认)
+        boolean isBelowTrend = latestPrice.compareTo(latestTrendEma) < 0;
+        
+        // 3. MACD死叉检查
+        boolean isDeathCross = isDeathCross(macdResult);
+        
+        // 4. MACD柱状线由正转负(动量转变)
+        boolean isMacdHistTurningNegative = isMacdHistTurningNegative(macdResult);
+        
+        // 5. 顶背离检查(增强空头信号可靠性)
+        boolean isTopDivergence = MACDCalculator.isTopDivergence(closePrices, macdResult);
+
+        log.info("空头信号检查, 价格位于200日EMA下方: {}, 死叉: {}, MACD柱状线由正转负: {}, 顶背离: {}",
+                isBelowTrend, isDeathCross, isMacdHistTurningNegative, isTopDivergence);
+        
+        // 空头开仓条件:趋势向下 + 死叉 + (柱状线转弱或顶背离)
+        return isBelowTrend && isDeathCross && (isMacdHistTurningNegative || isTopDivergence);
+    }
+
+    // 平仓条件检查方法
+    
+    /**
+     * 多头平仓条件检查
+     * 
+     * @param macdResult MACD计算结果
+     * @param currentPrice 当前价格
+     * @return 是否满足多头平仓条件
+     */
+    private boolean isLongExitCondition(MACDResult macdResult, BigDecimal currentPrice) {
+        // 多头平仓条件:MACD柱状线动量减弱(由正转弱)
+        List<PriceData> macdData = macdResult.getMacdData();
+        if (macdData.size() >= 2) {
+            PriceData latest = macdData.get(macdData.size() - 1);
+            PriceData previous = macdData.get(macdData.size() - 2);
+
+            // 柱状线由正转弱:前一根为正,当前绝对值减小
+            boolean momentumWeakening = previous.getMacdHist().compareTo(BigDecimal.ZERO) >= 0 && 
+                                      latest.getMacdHist().abs().compareTo(previous.getMacdHist().abs()) < 0;
+            
+            return momentumWeakening;
         }
         return false;
     }
-
+    
     /**
-     * 检查是否满足入场信号条件
+     * 空头平仓条件检查
+     * 
+     * @param macdResult MACD计算结果
+     * @param currentPrice 当前价格
+     * @return 是否满足空头平仓条件
      */
-    private TradeRequestParam checkEntrySignal(TradeRequestParam tradeRequestParam) {
-        String poSide = null;
-        String side = null;
-        BigDecimal currentMa30 = ma30.getMa(); // 获取当前30日移动平均线
-
-        // 获取MACD的最新值用于判断动量方向
-        BigDecimal currentDif = macd.getDif();
-        BigDecimal currentDea = macd.getDea();
-        BigDecimal macdBar = macd.getMacdBar(); // 获取MACD柱状图值
+    private boolean isShortExitCondition(MACDResult macdResult, BigDecimal currentPrice) {
+        // 空头平仓条件:MACD柱状线动量减弱(由负转弱)
+        List<PriceData> macdData = macdResult.getMacdData();
+        if (macdData.size() >= 2) {
+            PriceData latest = macdData.get(macdData.size() - 1);
+            PriceData previous = macdData.get(macdData.size() - 2);
+            
+            // 柱状线由负转弱:前一根为负,当前绝对值减小
+            boolean momentumWeakening = previous.getMacdHist().compareTo(BigDecimal.ZERO) <= 0 &&
+                                      latest.getMacdHist().abs().compareTo(previous.getMacdHist().abs()) < 0;
+            
+            return momentumWeakening;
+        }
         
-        // 获取RSI的最新值
-        BigDecimal currentRsi = rsi.getRsi();
-        // 获取波动率的最新值
-        BigDecimal currentVolatility = volatility.getValue();
-
-        // 根据市场趋势判断入场条件
-        if (trend == TrendDirection.BULLISH) {
-            // 牛市入场条件增强:
-            // 1. MACD多头信号(DIF>DEA)
-            // 2. MACD柱状图为正(确认多头力量)
-            // 3. 价格在30日MA之上且有一定偏离(避免假突破)
-            // 4. RSI处于合理区间(40-65),避免在超买区域入场
-            // 5. 波动率适中(>0.5%且<5%),避免极端波动环境
-            boolean macdBull = currentDif.compareTo(currentDea) > 0;
-            boolean macdBarPositive = macdBar.compareTo(BigDecimal.ZERO) > 0;
-            BigDecimal priceMaDiff = lastPrice.subtract(currentMa30);
-            boolean priceAboveMAWithStrength = priceMaDiff.compareTo(currentMa30.multiply(new BigDecimal("0.005"))) > 0;
-            boolean rsiInRange = currentRsi.compareTo(new BigDecimal(40)) > 0 && currentRsi.compareTo(new BigDecimal(65)) < 0;
-            boolean volatilityModerate = currentVolatility.compareTo(new BigDecimal("0.005")) > 0 && 
-                                         currentVolatility.compareTo(new BigDecimal("0.05")) < 0;
-            
-            if (macdBull && macdBarPositive && priceAboveMAWithStrength && rsiInRange && volatilityModerate) {
-                poSide = CoinEnums.POSSIDE_LONG.getCode();
-                side = CoinEnums.SIDE_BUY.getCode();
-            }
-        } else if (trend == TrendDirection.BEARISH){
-            // 熊市入场条件增强:
-            // 1. MACD空头信号(DIF<DEA)
-            // 2. MACD柱状图为负(确认空头力量)
-            // 3. 价格在30日MA之下且有一定偏离(避免假突破)
-            // 4. RSI处于合理区间(35-60),避免在超卖区域入场
-            // 5. 波动率适中(>0.5%且<5%),避免极端波动环境
-            boolean macdBear = currentDif.compareTo(currentDea) < 0;
-            boolean macdBarNegative = macdBar.compareTo(BigDecimal.ZERO) < 0;
-            BigDecimal priceMaDiff = currentMa30.subtract(lastPrice);
-            boolean priceBelowMAWithStrength = priceMaDiff.compareTo(currentMa30.multiply(new BigDecimal("0.005"))) > 0;
-            boolean rsiInRange = currentRsi.compareTo(new BigDecimal(35)) > 0 && currentRsi.compareTo(new BigDecimal(60)) < 0;
-            boolean volatilityModerate = currentVolatility.compareTo(new BigDecimal("0.005")) > 0 && 
-                                         currentVolatility.compareTo(new BigDecimal("0.05")) < 0;
-            
-            if (macdBear && macdBarNegative && priceBelowMAWithStrength && rsiInRange && volatilityModerate) {
-                poSide = CoinEnums.POSSIDE_SHORT.getCode();
-                side = CoinEnums.SIDE_SELL.getCode();
-            }
-        }
-        tradeRequestParam.setPosSide(poSide);
-        tradeRequestParam.setSide(side);
-        return tradeRequestParam;
+        return false;
     }
 
+    // MACD信号辅助方法
+    
     /**
-     * 检查是否满足离场信号条件
+     * 简单金叉判断
+     * <p>
+     * 条件:DIF线从下往上穿过DEA线
+     * 
+     * @param macdResult MACD计算结果
+     * @return 是否形成金叉
      */
-    private TradeRequestParam checkExitSignal(TradeRequestParam tradeRequestParam) {
-        BigDecimal currentMa30 = ma30.getMa(); // 获取当前30日移动平均线
-
-        // 获取MACD的最新值用于判断动量变化
-        BigDecimal currentDif = macd.getDif();
-        BigDecimal currentDea = macd.getDea();
-        String posSide = tradeRequestParam.getPosSide();
-        if (posSide == CoinEnums.POSSIDE_LONG.getCode()) {
-            // 多头持仓离场条件:
-            // 1. MACD转为空头信号(DIF<DEA)
-            // 2. 价格跌破30日MA
-            boolean macdExit = currentDif.compareTo(currentDea) < 0;
-            boolean priceExit = lastPrice.compareTo(currentMa30) < 0;
-
-            if (macdExit && priceExit) {
-                tradeRequestParam.setSide(CoinEnums.SIDE_SELL.getCode());
-            }
-        } else if (posSide == CoinEnums.POSSIDE_SHORT.getCode()){
-            // 空头持仓离场条件:
-            // 1. MACD转为多头信号(DIF>DEA)
-            // 2. 价格突破30日MA
-            boolean macdExit = currentDif.compareTo(currentDea) > 0;
-            boolean priceExit = lastPrice.compareTo(currentMa30) > 0;
-
-            if (macdExit && priceExit) {
-                tradeRequestParam.setSide(CoinEnums.SIDE_BUY.getCode());
-            }
+    private boolean isGoldenCross(MACDResult macdResult) {
+        List<PriceData> macdData = macdResult.getMacdData();
+        if (macdData.size() < 2) {
+            return false;
         }
-        return tradeRequestParam;
+
+        PriceData latest = macdData.get(macdData.size() - 1);
+        PriceData previous = macdData.get(macdData.size() - 2);
+        
+        // 金叉判断:DIF从下往上穿过DEA
+        return previous.getDif().compareTo(previous.getDea()) <= 0 && 
+               latest.getDif().compareTo(latest.getDea()) > 0;
+    }
+    
+    /**
+     * 简单死叉判断
+     * <p>
+     * 条件:DIF线从上往下穿过DEA线
+     * 
+     * @param macdResult MACD计算结果
+     * @return 是否形成死叉
+     */
+    private boolean isDeathCross(MACDResult macdResult) {
+        List<PriceData> macdData = macdResult.getMacdData();
+        if (macdData.size() < 2) {
+            return false;
+        }
+
+        PriceData latest = macdData.get(macdData.size() - 1);
+        PriceData previous = macdData.get(macdData.size() - 2);
+        
+        // 死叉判断:DIF从上往下穿过DEA
+        return previous.getDif().compareTo(previous.getDea()) >= 0 && 
+               latest.getDif().compareTo(latest.getDea()) < 0;
+    }
+    
+    /**
+     * MACD柱状线由负转正判断
+     * <p>
+     * 条件:前一根柱状线为负,当前柱状线为正
+     * 
+     * @param macdResult MACD计算结果
+     * @return 是否由负转正
+     */
+    private boolean isMacdHistTurningPositive(MACDResult macdResult) {
+        List<PriceData> macdData = macdResult.getMacdData();
+        if (macdData.size() < 2) {
+            return false;
+        }
+
+        PriceData latest = macdData.get(macdData.size() - 1);
+        PriceData previous = macdData.get(macdData.size() - 2);
+        
+        // 柱状线由负转正:前一根为负,当前为正
+        return previous.getMacdHist().compareTo(BigDecimal.ZERO) <= 0 && 
+               latest.getMacdHist().compareTo(BigDecimal.ZERO) > 0;
+    }
+    
+    /**
+     * MACD柱状线由正转负判断
+     * <p>
+     * 条件:前一根柱状线为正,当前柱状线为负
+     * 
+     * @param macdResult MACD计算结果
+     * @return 是否由正转负
+     */
+    private boolean isMacdHistTurningNegative(MACDResult macdResult) {
+        List<PriceData> macdData = macdResult.getMacdData();
+        if (macdData.size() < 2) {
+            return false;
+        }
+
+        PriceData latest = macdData.get(macdData.size() - 1);
+        PriceData previous = macdData.get(macdData.size() - 2);
+        
+        // 柱状线由正转负:前一根为正,当前为负
+        return previous.getMacdHist().compareTo(BigDecimal.ZERO) >= 0 && 
+               latest.getMacdHist().compareTo(BigDecimal.ZERO) < 0;
     }
 
-
-    // 枚举定义
-    enum PositionStatus { FLAT, LONG, SHORT }
-    enum TrendDirection { BULLISH, BEARISH }
 }
-

--
Gitblit v1.9.1