From bfb3c88962ab015056d9c0ebe3ca0abaf8af8000 Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Mon, 05 Jan 2026 17:21:35 +0800
Subject: [PATCH] refactor(indicator): 优化MACD计算逻辑并修复数据处理顺序问题

---
 src/main/java/com/xcong/excoin/modules/okxNewPrice/indicator/macdAndMatrategy/MACDCalculator.java |  148 ++++++++++++++++++++++++++++++------------------
 1 files changed, 92 insertions(+), 56 deletions(-)

diff --git a/src/main/java/com/xcong/excoin/modules/okxNewPrice/indicator/macdAndMatrategy/MACDCalculator.java b/src/main/java/com/xcong/excoin/modules/okxNewPrice/indicator/macdAndMatrategy/MACDCalculator.java
index 37c0ec0..5538216 100644
--- a/src/main/java/com/xcong/excoin/modules/okxNewPrice/indicator/macdAndMatrategy/MACDCalculator.java
+++ b/src/main/java/com/xcong/excoin/modules/okxNewPrice/indicator/macdAndMatrategy/MACDCalculator.java
@@ -2,6 +2,7 @@
 
 import java.math.BigDecimal;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
 
 /**
@@ -20,74 +21,101 @@
      * 计算MACD指标
      *
      * @param closePrices 收盘价列表(使用BigDecimal确保计算精度)
-     * @param shortPeriod 短期EMA周期(通常为12)
-     * @param longPeriod  长期EMA周期(通常为26)
-     * @param signalPeriod DEA的周期(通常为9)
+     * @param fastlen 短期EMA周期(通常为12)
+     * @param slowlen 长期EMA周期(通常为26)
+     * @param siglen DEA的周期(通常为9)
      * @return 包含MACD各部分数据的PriceData列表
      * @throws IllegalArgumentException 如果数据点不足或参数无效
      */
-    public static MACDResult calculateMACD(List<BigDecimal> closePrices, int shortPeriod, int longPeriod, int signalPeriod) {
+    public static MACDResult calculateMACD(List<BigDecimal> closePrices, int fastlen, int slowlen, int siglen) {
         // 参数校验:确保数据点足够
         if (closePrices == null || closePrices.isEmpty()) {
             throw new IllegalArgumentException("Close prices list cannot be null or empty.");
         }
-        if (shortPeriod <= 0 || longPeriod <= 0 || signalPeriod <= 0) {
+        if (fastlen <= 0 || slowlen <= 0 || siglen <= 0) {
             throw new IllegalArgumentException("All periods must be positive integers.");
         }
-        if (shortPeriod >= longPeriod) {
-            throw new IllegalArgumentException("Short period must be less than long period.");
+        if (fastlen >= slowlen) {
+            throw new IllegalArgumentException("Fast period must be less than slow period.");
         }
-        if (closePrices.size() < Math.max(shortPeriod, longPeriod)) {
+        if (closePrices.size() < Math.max(fastlen, slowlen)) {
             throw new IllegalArgumentException("Insufficient data points for the specified periods.");
         }
 
-        // 1. 计算短期和长期EMA(使用SMA作为初始值,提高计算准确性)
-        List<BigDecimal> emaShort = EMACalculator.calculateEMA(closePrices, shortPeriod, true);
-        List<BigDecimal> emaLong = EMACalculator.calculateEMA(closePrices, longPeriod, true);
+        // 反转数据,确保从旧到新处理(因为用户提供的数据是从新到旧)
+        List<BigDecimal> prices = new ArrayList<>(closePrices);
+        Collections.reverse(prices);
 
-        // 2. 确定公共有效起始点(从较长周期的EMA开始计算)
-        int startIdx = Math.max(shortPeriod, longPeriod) - 1; // 因为EMA从第period个数据点开始有效
-        int validLength = closePrices.size() - startIdx;      // 有效数据点数量
+        // 1. 计算快速EMA和慢速EMA,使用SMA作为初始值
+        // 当initialSMA=true时,EMA列表长度为prices.size() - period + 1
+        List<BigDecimal> fastEma = EMACalculator.calculateEMA(prices, fastlen, true);
+        List<BigDecimal> slowEma = EMACalculator.calculateEMA(prices, slowlen, true);
 
-        // 3. 计算DIF(仅在有效区间内计算)
-        List<BigDecimal> difValues = new ArrayList<>(validLength);
-        for (int i = 0; i < validLength; i++) {
-            // 计算EMA的有效索引
-            int idxEmaShort = startIdx - shortPeriod + 1 + i; // 短期EMA的当前索引
-            int idxEmaLong = startIdx - longPeriod + 1 + i;   // 长期EMA的当前索引
-
-            // DIF = 短期EMA - 长期EMA
-            BigDecimal dif = emaShort.get(idxEmaShort).subtract(emaLong.get(idxEmaLong));
-            difValues.add(dif);
+        // 2. 计算MACD线(快速EMA减去慢速EMA)
+        List<BigDecimal> macdLine = new ArrayList<>();
+        // EMA列表的起始索引与价格列表的对应关系
+        int slowEmaStartIdx = slowlen - 1; // slowEma中第一个有效值对应的价格索引
+        int fastEmaStartIdx = fastlen - 1; // fastEma中第一个有效值对应的价格索引
+        
+        for (int i = 0; i < prices.size(); i++) {
+            if (i < slowEmaStartIdx) {
+                // 在慢速EMA开始有效之前,MACD线值为0
+                macdLine.add(BigDecimal.ZERO);
+            } else {
+                // MACD线 = 快速EMA - 慢速EMA
+                // 需要将价格索引转换为EMA列表索引
+                int slowEmaIdx = i - slowEmaStartIdx;
+                int fastEmaIdx = i - fastEmaStartIdx;
+                BigDecimal macdValue = fastEma.get(fastEmaIdx).subtract(slowEma.get(slowEmaIdx));
+                macdLine.add(macdValue);
+            }
         }
 
-        // 4. 计算DEA(基于有效DIF数据的EMA)
-        List<BigDecimal> deaValues = EMACalculator.calculateEMA(difValues, signalPeriod, false);
+        // 3. 计算信号线(MACD线的siglen周期EMA),使用SMA作为初始值
+        List<BigDecimal> signalLine = EMACalculator.calculateEMA(macdLine, siglen, true);
 
-        // 5. 构建并填充结果(包含所有MACD数据)
-        List<PriceData> result = new ArrayList<>(deaValues.size());
+        // 4. 计算柱状图(MACD线与信号线的差值)
+        List<BigDecimal> histogram = new ArrayList<>();
+        int signalLineStartIdx = siglen - 1; // signalLine中第一个有效值对应的macdLine索引
+        
+        for (int i = 0; i < macdLine.size(); i++) {
+            if (i < slowEmaStartIdx + signalLineStartIdx) {
+                // 在信号线开始有效之前,柱状图值为0
+                histogram.add(BigDecimal.ZERO);
+            } else {
+                // 将macdLine索引转换为signalLine索引
+                int signalLineIdx = i - signalLineStartIdx;
+                // 柱状图 = (MACD线 - 信号线) * 2(放大信号)
+                BigDecimal histValue = macdLine.get(i).subtract(signalLine.get(signalLineIdx)).multiply(new BigDecimal("2"));
+                histogram.add(histValue);
+            }
+        }
 
-        // 从第一个DEA值开始构建结果
-        for (int i = 0; i < deaValues.size(); i++) {
-            int closeIdx = startIdx + i; // 对应原收盘价列表的索引
-
-            // 创建价格数据对象
-            PriceData data = new PriceData(closePrices.get(closeIdx));
-
-            // 设置EMA(使用正确的偏移位置)
-            data.setEmaShort(emaShort.get(closeIdx - shortPeriod + 1));
-            data.setEmaLong(emaLong.get(closeIdx - longPeriod + 1));
-
-            // 设置DIF、DEA和MACD柱状图
-            data.setDif(difValues.get(i));
-            data.setDea(deaValues.get(i)); // DEA索引直接对应
-            data.setMacdHist(data.getDif().subtract(data.getDea())); // MACD柱状图 = DIF - DEA
-
+        // 5. 构建结果数据
+        List<PriceData> result = new ArrayList<>();
+        int startIndex = slowEmaStartIdx + signalLineStartIdx; // 从信号线开始有效的位置开始
+        
+        for (int i = startIndex; i < prices.size(); i++) {
+            PriceData data = new PriceData(prices.get(i));
+            
+            // 设置EMA值(需要转换索引)
+            int fastEmaIdx = i - fastEmaStartIdx;
+            int slowEmaIdx = i - slowEmaStartIdx;
+            data.setEmaShort(fastEma.get(fastEmaIdx));
+            data.setEmaLong(slowEma.get(slowEmaIdx));
+            
+            // 设置MACD指标值
+            data.setDif(macdLine.get(i));
+            data.setDea(signalLine.get(i - signalLineStartIdx));
+            data.setMacdHist(histogram.get(i));
+            
             result.add(data);
         }
+        
+        // 反转结果列表,恢复为从新到旧的顺序
+        Collections.reverse(result);
 
-
-        return new MACDResult(result, startIdx);
+        return new MACDResult(result, startIndex);
     }
 
     /**
@@ -99,7 +127,7 @@
      * @return 包含MACD各部分数据的PriceData列表
      */
     public static MACDResult calculateMACD(List<BigDecimal> closePrices) {
-        // 默认参数:短期周期12,长期周期26,信号周期9
+        // 默认参数:快速周期12,慢速周期26,信号周期9
         return calculateMACD(closePrices, 12, 26, 9);
     }
 
@@ -122,14 +150,18 @@
             return false;
         }
         
+        // 反转原始价格列表,确保从旧到新处理
+        List<BigDecimal> prices = new ArrayList<>(closePrices);
+        Collections.reverse(prices);
+        
         // 找到最近的价格高点和对应的DIF值
-        int recentPriceHighIdx = IndicatorUtils.findRecentHighIndex(closePrices, startIdx);
+        int recentPriceHighIdx = IndicatorUtils.findRecentHighIndex(prices, startIdx);
         if (recentPriceHighIdx < startIdx + 2 || recentPriceHighIdx == -1) {
             return false;
         }
         
         // 找到之前的价格高点和对应的DIF值
-        int previousPriceHighIdx = IndicatorUtils.findPreviousHighIndex(closePrices, startIdx, recentPriceHighIdx);
+        int previousPriceHighIdx = IndicatorUtils.findPreviousHighIndex(prices, startIdx, recentPriceHighIdx);
         if (previousPriceHighIdx < startIdx || previousPriceHighIdx == -1) {
             return false;
         }
@@ -143,8 +175,8 @@
             return false;
         }
         
-        BigDecimal recentPrice = closePrices.get(recentPriceHighIdx);
-        BigDecimal previousPrice = closePrices.get(previousPriceHighIdx);
+        BigDecimal recentPrice = prices.get(recentPriceHighIdx);
+        BigDecimal previousPrice = prices.get(previousPriceHighIdx);
         BigDecimal recentDif = macdData.get(recentDifIdx).getDif();
         BigDecimal previousDif = macdData.get(previousDifIdx).getDif();
         
@@ -172,14 +204,18 @@
             return false;
         }
         
+        // 反转原始价格列表,确保从旧到新处理
+        List<BigDecimal> prices = new ArrayList<>(closePrices);
+        Collections.reverse(prices);
+        
         // 找到最近的价格低点和对应的DIF值
-        int recentPriceLowIdx = IndicatorUtils.findRecentLowIndex(closePrices, startIdx);
+        int recentPriceLowIdx = IndicatorUtils.findRecentLowIndex(prices, startIdx);
         if (recentPriceLowIdx < startIdx + 2 || recentPriceLowIdx == -1) {
             return false;
         }
         
         // 找到之前的价格低点和对应的DIF值
-        int previousPriceLowIdx = IndicatorUtils.findPreviousLowIndex(closePrices, startIdx, recentPriceLowIdx);
+        int previousPriceLowIdx = IndicatorUtils.findPreviousLowIndex(prices, startIdx, recentPriceLowIdx);
         if (previousPriceLowIdx < startIdx || previousPriceLowIdx == -1) {
             return false;
         }
@@ -193,8 +229,8 @@
             return false;
         }
         
-        BigDecimal recentPrice = closePrices.get(recentPriceLowIdx);
-        BigDecimal previousPrice = closePrices.get(previousPriceLowIdx);
+        BigDecimal recentPrice = prices.get(recentPriceLowIdx);
+        BigDecimal previousPrice = prices.get(previousPriceLowIdx);
         BigDecimal recentDif = macdData.get(recentDifIdx).getDif();
         BigDecimal previousDif = macdData.get(previousDifIdx).getDif();
         
@@ -202,4 +238,4 @@
         return recentPrice.compareTo(previousPrice) < 0 && 
                recentDif.compareTo(previousDif) > 0;
     }
-}
\ No newline at end of file
+}

--
Gitblit v1.9.1