From 212e20fa6e9d0cc69d7f70339ecd47d4ec286533 Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Sun, 04 Jan 2026 11:03:41 +0800
Subject: [PATCH] feat(okxNewPrice): 优化MACD策略交易判断逻辑

---
 src/main/java/com/xcong/excoin/modules/okxNewPrice/indicator/macdAndMatrategy/MacdMaStrategy.java |  592 +++++++++++++++++++++++++---------------------------------
 1 files changed, 257 insertions(+), 335 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 4c47269..a335dd8 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
@@ -7,9 +7,9 @@
  */
 package com.xcong.excoin.modules.okxNewPrice.indicator.macdAndMatrategy;
 
+import lombok.extern.slf4j.Slf4j;
+
 import java.math.BigDecimal;
-import java.math.RoundingMode;
-import java.util.ArrayList;
 import java.util.List;
 
 /**
@@ -18,14 +18,24 @@
  * 该策略利用EMA交叉、MACD指标、价格突破信号和波动率过滤,
  * 为15分钟K线级别交易提供综合决策支持。
  */
+@Slf4j
 public class MacdMaStrategy {
+
+    /** 持仓状态枚举 */
+    public enum OperationType {
+        /** 开仓平仓 */
+        open,
+        close
+    }
 
     /** 持仓状态枚举 */
     public enum PositionType {
         /** 多头持仓 */
-        LONG,
+        LONG_BUY,
+        LONG_SELL,
         /** 空头持仓 */
-        SHORT,
+        SHORT_SELL,
+        SHORT_BUY,
         /** 空仓 */
         NONE
     }
@@ -37,11 +47,6 @@
     private int volatilityPeriod; // 波动率计算周期
     private BigDecimal stopLossRatio;   // 止损比例
     private BigDecimal takeProfitRatio; // 止盈比例
-
-    // 持仓信息
-    private PositionType currentPosition; // 当前持仓状态
-    private BigDecimal entryPrice;       // 开仓价格
-    private long entryTime;              // 开仓时间戳
 
     /**
      * 默认构造函数,使用标准MACD参数
@@ -71,10 +76,6 @@
         this.stopLossRatio = stopLossRatio;
         this.takeProfitRatio = takeProfitRatio;
 
-        // 初始化持仓状态为空仓
-        this.currentPosition = PositionType.NONE;
-        this.entryPrice = BigDecimal.ZERO;
-        this.entryTime = 0;
     }
 
     /**
@@ -83,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; // 数据不足,无法生成信号
@@ -104,42 +105,58 @@
         // 最新收盘价
         BigDecimal latestPrice = closePrices.get(closePrices.size() - 1);
 
-        // 3. 检查开仓条件
-        if (currentPosition == PositionType.NONE) {
-            // 多头开仓条件检查
-            if (isLongEntryCondition(macdResult, closePrices, volatility.getValue())) {
-                // 执行开多
-                this.currentPosition = PositionType.LONG;
-                this.entryPrice = latestPrice;
-                this.entryTime = System.currentTimeMillis();
-                return PositionType.LONG;
-            }
-
-            // 空头开仓条件检查
-            if (isShortEntryCondition(macdResult, closePrices, volatility.getValue())) {
-                // 执行开空
-                this.currentPosition = PositionType.SHORT;
-                this.entryPrice = latestPrice;
-                this.entryTime = System.currentTimeMillis();
-                return PositionType.SHORT;
-            }
-
-            // 无信号
-            return PositionType.NONE;
-        } else {
-            // 4. 检查平仓条件
-            if (shouldClosePosition(macdResult, closePrices, latestPrice)) {
-                // 执行平仓
-                PositionType closedPosition = currentPosition;
-                this.currentPosition = PositionType.NONE;
-                this.entryPrice = BigDecimal.ZERO;
-                this.entryTime = 0;
-                return PositionType.NONE; // 返回空仓信号表示平仓
-            }
-
-            // 保持当前持仓
-            return currentPosition;
+        // 3. 检查开平仓条件
+        // 多头开仓条件检查
+        if (isLongEntryCondition(macdResult, closePrices, volatility.getValue())) {
+            // 执行开多
+            log.info( "多头开仓信号,价格:{}", latestPrice);
+            return PositionType.LONG_BUY;
         }
+
+        // 空头开仓条件检查
+        if (isShortEntryCondition(macdResult, closePrices, volatility.getValue())) {
+            // 执行开空
+             log.info( "空头开仓信号,价格:{}", latestPrice);
+            return PositionType.SHORT_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; // 数据不足,无法生成信号
+        }
+
+        // 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;
+        }
+
+        // 无信号
+        return PositionType.NONE;
+
     }
 
     /**
@@ -174,27 +191,32 @@
      * @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()){
+
+            signal = analyzeClose(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.NONE && currentPosition != PositionType.NONE) {
-            // 平仓操作
-            if (currentPosition == PositionType.LONG) {
-                // 平多:卖出平多(side 填写 sell;posSide 填写 long )
-                return new TradingOrder("sell", "long");
-            } else if (currentPosition == PositionType.SHORT) {
-                // 平空:买入平空(side 填写 buy; posSide 填写 short )
-                return new TradingOrder("buy", "short");
-            }
+        } else if (signal == PositionType.SHORT_BUY) {
+            // 开空:卖出开空(side 填写 sell; posSide 填写 short )
+            return new TradingOrder("buy", "short");
         }
-
         // 没有交易信号
         return null;
     }
@@ -207,123 +229,109 @@
      * @param volatility 当前波动率
      * @return 是否满足多头开仓条件
      */
-    private boolean isLongEntryCondition(MACDResult macdResult, List<BigDecimal> closePrices,
-                                         BigDecimal volatility) {
-        // 1. EMA金叉检查(短期EMA > 长期EMA)
-        boolean emaGoldenCross = isEmaGoldenCross(macdResult);
+    private boolean isLongEntryCondition(MACDResult macdResult, List<BigDecimal> closePrices, BigDecimal volatility) {
 
-        // 2. MACD柱状线扩张+金叉检查
-        boolean macdGoldenCross = isMacdGoldenCrossAndExpanding(macdResult);
-
-        // 3. 价格突破前高检查
-        boolean priceBreakout = BullishSignalDetector.isBullishSignalFormed(macdResult, closePrices);
-
-        // 4. 波动率过滤检查(0.5% ~ 5%)
+        // 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);
+        }
+        
         // 所有条件必须同时满足
-        return emaGoldenCross && macdGoldenCross && priceBreakout && volatilityFilter;
+        return macdPositive && volatilityFilter && isMacdFavorable;
     }
 
     /**
      * 空头开仓条件检查
-     *
+     * 
      * @param macdResult MACD计算结果
      * @param closePrices 收盘价序列
      * @param volatility 当前波动率
      * @return 是否满足空头开仓条件
      */
-    private boolean isShortEntryCondition(MACDResult macdResult, List<BigDecimal> closePrices,
-                                          BigDecimal volatility) {
-        // 1. EMA死叉检查(短期EMA < 长期EMA)
-        boolean emaDeathCross = isEmaDeathCross(macdResult);
-
-        // 2. MACD柱状线收缩+死叉检查
-        boolean macdDeathCross = isMacdDeathCrossAndContracting(macdResult);
-
-        // 3. 价格跌破前低检查
-        boolean priceBreakdown = BearishSignalDetector.isBearishSignalFormed(macdResult, closePrices);
-
-        // 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 (macdNegative && volatilityFilter && isMacdFavorable) {
+            log.info("空头信号形成, MACD有利状态: {}, 柱状线为负: {}, 波动率过滤: {}",
+                    isMacdFavorable, macdNegative, volatilityFilter);
+        }
+        
         // 所有条件必须同时满足
-        return emaDeathCross && macdDeathCross && priceBreakdown && volatilityFilter;
+        return macdNegative && volatilityFilter && isMacdFavorable;
     }
-
+    
     /**
-     * 平仓条件检查
-     *
+     * 多头平仓条件检查
+     * 
      * @param macdResult MACD计算结果
-     * @param closePrices 收盘价序列
      * @param currentPrice 当前价格
-     * @return 是否应该平仓
+     * @return 是否满足多头平仓条件
      */
-    private boolean shouldClosePosition(MACDResult macdResult, List<BigDecimal> closePrices,
-                                        BigDecimal currentPrice) {
-        // 1. 检查止损条件
-        if (isStopLossTriggered(currentPrice)) {
-            return true;
-        }
+    private boolean isLongExitCondition(MACDResult macdResult, BigDecimal currentPrice) {
+        // 1. MACD柱状线由正转负(动量转变)
+        List<PriceData> macdData = macdResult.getMacdData();
+        if (macdData.size() >= 2) {
+            PriceData latest = macdData.get(macdData.size() - 1);
+            PriceData previous = macdData.get(macdData.size() - 2);
 
-        // 2. 检查止盈条件
-        if (isTakeProfitTriggered(currentPrice)) {
-            return true;
+            boolean momentumShift = previous.getMacdHist().compareTo(BigDecimal.ZERO) >= 0 && 
+                                   latest.getMacdHist().abs().compareTo(previous.getMacdHist().abs()) < 0;
+            if (momentumShift) {
+                return true;
+            }
         }
-
-        // 3. 检查MACD反向信号
-        if (isMacdReversalSignal(macdResult)) {
-            return true;
-        }
-
         return false;
     }
-
+    
     /**
-     * EMA金叉检查
-     *
+     * 空头平仓条件检查
+     * 
      * @param macdResult MACD计算结果
-     * @return 是否形成EMA金叉
+     * @param currentPrice 当前价格
+     * @return 是否满足空头平仓条件
      */
-    private boolean isEmaGoldenCross(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;
     }
-
-    /**
-     * EMA死叉检查
-     *
-     * @param macdResult MACD计算结果
-     * @return 是否形成EMA死叉
-     */
-    private boolean isEmaDeathCross(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);
-
-        // 当前短期EMA < 当前长期EMA,并且前一期短期EMA >= 前一期长期EMA
-        return latest.getEmaShort().compareTo(latest.getEmaLong()) < 0 &&
-                previous.getEmaShort().compareTo(previous.getEmaLong()) >= 0;
-    }
-
+    
     /**
      * MACD金叉且柱状线扩张检查
-     *
+     * <p>
+     * 条件:
+     * 1. DIF线从下往上穿过DEA线(金叉)
+     * 2. MACD柱状线绝对值增大且为正值(动量增强)
+     * 
      * @param macdResult MACD计算结果
-     * @return 是否形成MACD金叉且柱状线扩张
+     * @return 是否形成MACD金叉或柱状线扩张
      */
     private boolean isMacdGoldenCrossAndExpanding(MACDResult macdResult) {
         List<PriceData> macdData = macdResult.getMacdData();
@@ -334,24 +342,52 @@
         PriceData latest = macdData.get(macdData.size() - 1);
         PriceData previous = macdData.get(macdData.size() - 2);
         PriceData prevPrev = macdData.get(macdData.size() - 3);
+        
+        // 金叉判断:DIF从下往上穿过DEA
+        boolean isGoldenCross = prevPrev.getDif().compareTo(prevPrev.getDea()) <= 0 && 
+                              previous.getDif().compareTo(previous.getDea()) > 0;
 
-        // 1. MACD金叉检查(DIF上穿DEA)
-        boolean goldenCross = previous.getDif().compareTo(previous.getDea()) <= 0 &&
-                latest.getDif().compareTo(latest.getDea()) > 0;
+        boolean isUp = latest.getDif().compareTo(BigDecimal.ZERO) > 0 && latest.getDea().compareTo(BigDecimal.ZERO) > 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 && isUp;
+    }
+    
+    /**
+     * MACD柱状线扩张检查
+     * <p>
+     * 条件:当前MACD柱状线绝对值大于前一根
+     * 
+     * @param macdResult MACD计算结果
+     * @return MACD柱状线是否扩张
+     */
+    private boolean isExpanding(MACDResult macdResult) {
+        List<PriceData> macdData = macdResult.getMacdData();
+        if (macdData.size() < 2) {
+            return false;
+        }
 
-        // 2. MACD柱状线扩张检查
-        boolean histogramExpanding = prevPrev.getMacdHist().compareTo(previous.getMacdHist()) <= 0 &&
-                previous.getMacdHist().compareTo(latest.getMacdHist()) < 0 &&
-                latest.getMacdHist().compareTo(BigDecimal.ZERO) > 0;
-
-        return goldenCross && histogramExpanding;
+        PriceData latest = macdData.get(macdData.size() - 1);
+        PriceData previous = macdData.get(macdData.size() - 2);
+        
+        // MACD柱状线扩张:当前绝对值大于前一根
+        return latest.getMacdHist().abs().compareTo(previous.getMacdHist().abs()) > 0;
     }
 
     /**
      * MACD死叉且柱状线收缩检查
-     *
+     * <p>
+     * 条件:
+     * 1. DIF线从上往下穿过DEA线(死叉)
+     * 2. MACD柱状线绝对值减小且为负值(动量减弱)
+     * 
      * @param macdResult MACD计算结果
-     * @return 是否形成MACD死叉且柱状线收缩
+     * @return 是否形成MACD死叉或柱状线收缩
      */
     private boolean isMacdDeathCrossAndContracting(MACDResult macdResult) {
         List<PriceData> macdData = macdResult.getMacdData();
@@ -362,19 +398,69 @@
         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 isDown  = latest.getDif().compareTo(BigDecimal.ZERO) < 0 && latest.getDea().compareTo(BigDecimal.ZERO) < 0;
 
-        // 1. MACD死叉检查(DIF下穿DEA)
-        boolean deathCross = previous.getDif().compareTo(previous.getDea()) >= 0 &&
-                latest.getDif().compareTo(latest.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 && isDown;
+    }
+    
+    /**
+     * MACD柱状线收缩检查
+     * <p>
+     * 条件:前一根MACD柱状线绝对值大于当前
+     * 
+     * @param macdResult MACD计算结果
+     * @return MACD柱状线是否收缩
+     */
+    private boolean isContracting(MACDResult macdResult) {
+        List<PriceData> macdData = macdResult.getMacdData();
+        if (macdData.size() < 2) {
+            return false;
+        }
 
-        // 2. MACD柱状线收缩检查(绝对值减小)
-        boolean histogramContracting = prevPrev.getMacdHist().abs().compareTo(
-                previous.getMacdHist().abs()) >= 0 &&
-                previous.getMacdHist().abs().compareTo(
-                        latest.getMacdHist().abs()) > 0 &&
-                latest.getMacdHist().compareTo(BigDecimal.ZERO) < 0;
-
-        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;
     }
 
     /**
@@ -384,175 +470,11 @@
      * @return 波动率是否在0.5%~5%范围内
      */
     private boolean isVolatilityInRange(BigDecimal volatility) {
-        BigDecimal minVolatility = new BigDecimal("0.5");
+        BigDecimal minVolatility = new BigDecimal("0.1"); // 降低最小波动率阈值
         BigDecimal maxVolatility = new BigDecimal("5.0");
 
         return volatility.compareTo(minVolatility) >= 0 &&
                 volatility.compareTo(maxVolatility) <= 0;
     }
 
-    /**
-     * 止损触发检查
-     *
-     * @param currentPrice 当前价格
-     * @return 是否触发止损
-     */
-    private boolean isStopLossTriggered(BigDecimal currentPrice) {
-        if (entryPrice.compareTo(BigDecimal.ZERO) == 0) {
-            return false;
-        }
-
-        if (currentPosition == PositionType.LONG) {
-            // 多头持仓:价格下跌超过止损比例
-            BigDecimal stopLossPrice = entryPrice.multiply(
-                    BigDecimal.ONE.subtract(stopLossRatio));
-            return currentPrice.compareTo(stopLossPrice) < 0;
-        } else if (currentPosition == PositionType.SHORT) {
-            // 空头持仓:价格上涨超过止损比例
-            BigDecimal stopLossPrice = entryPrice.multiply(
-                    BigDecimal.ONE.add(stopLossRatio));
-            return currentPrice.compareTo(stopLossPrice) > 0;
-        }
-
-        return false;
-    }
-
-    /**
-     * 止盈触发检查
-     *
-     * @param currentPrice 当前价格
-     * @return 是否触发止盈
-     */
-    private boolean isTakeProfitTriggered(BigDecimal currentPrice) {
-        if (entryPrice.compareTo(BigDecimal.ZERO) == 0) {
-            return false;
-        }
-
-        if (currentPosition == PositionType.LONG) {
-            // 多头持仓:价格上涨超过止盈比例
-            BigDecimal takeProfitPrice = entryPrice.multiply(
-                    BigDecimal.ONE.add(takeProfitRatio));
-            return currentPrice.compareTo(takeProfitPrice) > 0;
-        } else if (currentPosition == PositionType.SHORT) {
-            // 空头持仓:价格下跌超过止盈比例
-            BigDecimal takeProfitPrice = entryPrice.multiply(
-                    BigDecimal.ONE.subtract(takeProfitRatio));
-            return currentPrice.compareTo(takeProfitPrice) < 0;
-        }
-
-        return false;
-    }
-
-    /**
-     * MACD反向信号检查
-     *
-     * @param macdResult MACD计算结果
-     * @return 是否出现MACD反向信号
-     */
-    private boolean isMacdReversalSignal(MACDResult macdResult) {
-        if (currentPosition == PositionType.LONG) {
-            // 多头持仓:检查MACD死叉信号
-            return isMacdDeathCrossAndContracting(macdResult);
-        } else if (currentPosition == PositionType.SHORT) {
-            // 空头持仓:检查MACD金叉信号
-            return isMacdGoldenCrossAndExpanding(macdResult);
-        }
-
-        return false;
-    }
-
-    /**
-     * 获取当前持仓状态
-     *
-     * @return 当前持仓状态
-     */
-    public PositionType getCurrentPosition() {
-        return currentPosition;
-    }
-
-    /**
-     * 获取开仓价格
-     *
-     * @return 开仓价格
-     */
-    public BigDecimal getEntryPrice() {
-        return entryPrice;
-    }
-
-    /**
-     * 获取开仓时间戳
-     *
-     * @return 开仓时间戳
-     */
-    public long getEntryTime() {
-        return entryTime;
-    }
-
-    /**
-     * 重置策略状态(清空持仓)
-     */
-    public void reset() {
-        this.currentPosition = PositionType.NONE;
-        this.entryPrice = BigDecimal.ZERO;
-        this.entryTime = 0;
-    }
-
-    /**
-     * 示例主方法,展示如何使用MACD和MA组合交易策略
-     *
-     * @param args 命令行参数(未使用)
-     */
-    public static void main(String[] args) {
-        // 创建策略实例(使用默认参数)
-        MacdMaStrategy strategy = new MacdMaStrategy();
-
-        // 示例:模拟历史价格数据(实际应用中应从数据源获取)
-        List<BigDecimal> historicalPrices = new ArrayList<>();
-
-        // 生成一些示例价格数据(这里仅作演示)
-        // 实际应用中应替换为真实的历史K线数据
-        for (int i = 0; i < 50; i++) {
-            // 模拟价格数据(示例中使用简单递增的价格)
-            historicalPrices.add(new BigDecimal("100.00").add(new BigDecimal(i * 0.5)));
-        }
-
-        // 模拟实时价格流处理
-        System.out.println("===== MACD和MA组合交易策略示例 =====");
-        System.out.println("开始处理价格数据并生成交易信号...");
-
-        // 模拟实时数据流(假设我们有更多的价格数据)
-        for (int i = 0; i < 20; i++) {
-            // 添加新的价格数据(这里仅作演示)
-            BigDecimal newPrice = new BigDecimal("125.00").add(new BigDecimal(i * 0.2));
-            historicalPrices.add(newPrice);
-
-            // 使用策略分析最新价格数据并生成交易指令
-            TradingOrder order = strategy.generateTradingOrder(historicalPrices);
-
-            // 输出交易信号和指令
-            System.out.printf("价格: %s, 当前持仓: %s, 交易指令: %s\n",
-                    newPrice.setScale(2, RoundingMode.HALF_UP),
-                    strategy.getCurrentPosition().name(),
-                    order != null ? order.toString() : "无交易指令");
-
-            // 示例:在实际应用中,你可能需要根据指令执行交易操作
-            if (order != null) {
-                if (order.getSide().equals("buy") && order.getPosSide().equals("long")) {
-                    System.out.println("[交易操作] 买入开多");
-                } else if (order.getSide().equals("sell") && order.getPosSide().equals("short")) {
-                    System.out.println("[交易操作] 卖出开空");
-                } else if (order.getSide().equals("sell") && order.getPosSide().equals("long")) {
-                    System.out.println("[交易操作] 卖出平多");
-                } else if (order.getSide().equals("buy") && order.getPosSide().equals("short")) {
-                    System.out.println("[交易操作] 买入平空");
-                }
-            }
-        }
-
-        // 打印策略最终状态
-        System.out.println("\n===== 策略最终状态 =====");
-        System.out.println("当前持仓: " + strategy.getCurrentPosition().name());
-        System.out.println("开仓价格: " + strategy.getEntryPrice());
-        System.out.println("开仓时间戳: " + strategy.getEntryTime());
-    }
 }
\ No newline at end of file

--
Gitblit v1.9.1