From b70f32814aa9dc23ad284b43e91bbc6c96c70366 Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Mon, 05 Jan 2026 15:32:08 +0800
Subject: [PATCH] feat(indicator): 添加MACD指标计算功能并优化策略参数

---
 src/main/java/com/xcong/excoin/modules/okxNewPrice/indicator/macdAndMatrategy/MACDCalculator.java |  208 +++++++++++++++------------------------------------
 1 files changed, 61 insertions(+), 147 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 a70869e..c91ed60 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
@@ -20,74 +20,79 @@
      * 计算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);
+        // 1. 计算快速EMA和慢速EMA,使用SMA作为初始值
+        List<BigDecimal> fastEma = EMACalculator.calculateEMA(closePrices, fastlen, true);
+        List<BigDecimal> slowEma = EMACalculator.calculateEMA(closePrices, slowlen, true);
 
-        // 2. 确定公共有效起始点(从较长周期的EMA开始计算)
-        int startIdx = Math.max(shortPeriod, longPeriod) - 1; // 因为EMA从第period个数据点开始有效
-        int validLength = closePrices.size() - startIdx;      // 有效数据点数量
-
-        // 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<>();
+        for (int i = 0; i < closePrices.size(); i++) {
+            if (i < slowlen - 1) {
+                // 在慢速EMA开始有效之前,MACD线值为0
+                macdLine.add(BigDecimal.ZERO);
+            } else {
+                // MACD线 = 快速EMA - 慢速EMA
+                BigDecimal macdValue = fastEma.get(i).subtract(slowEma.get(i));
+                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<>();
+        for (int i = 0; i < macdLine.size(); i++) {
+            if (i < slowlen + siglen - 2) {
+                // 在信号线开始有效之前,柱状图值为0
+                histogram.add(BigDecimal.ZERO);
+            } else {
+                BigDecimal histValue = macdLine.get(i).subtract(signalLine.get(i));
+                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 = slowlen + siglen - 2; // 从信号线开始有效的位置开始
+        
+        for (int i = startIndex; i < closePrices.size(); i++) {
+            PriceData data = new PriceData(closePrices.get(i));
+            
+            // 设置EMA值
+            data.setEmaShort(fastEma.get(i));
+            data.setEmaLong(slowEma.get(i));
+            
+            // 设置MACD指标值
+            data.setDif(macdLine.get(i));
+            data.setDea(signalLine.get(i));
+            data.setMacdHist(histogram.get(i));
+            
             result.add(data);
         }
-        System.out.println(result.get(result.size() -1));
 
-        return new MACDResult(result, startIdx);
+        return new MACDResult(result, startIndex);
     }
 
     /**
@@ -99,7 +104,7 @@
      * @return 包含MACD各部分数据的PriceData列表
      */
     public static MACDResult calculateMACD(List<BigDecimal> closePrices) {
-        // 默认参数:短期周期12,长期周期26,信号周期9
+        // 默认参数:快速周期12,慢速周期26,信号周期9
         return calculateMACD(closePrices, 12, 26, 9);
     }
 
@@ -123,14 +128,14 @@
         }
         
         // 找到最近的价格高点和对应的DIF值
-        int recentPriceHighIdx = findRecentHighIndex(closePrices, startIdx);
-        if (recentPriceHighIdx < startIdx + 2) {
+        int recentPriceHighIdx = IndicatorUtils.findRecentHighIndex(closePrices, startIdx);
+        if (recentPriceHighIdx < startIdx + 2 || recentPriceHighIdx == -1) {
             return false;
         }
         
         // 找到之前的价格高点和对应的DIF值
-        int previousPriceHighIdx = findPreviousHighIndex(closePrices, startIdx, recentPriceHighIdx);
-        if (previousPriceHighIdx < startIdx) {
+        int previousPriceHighIdx = IndicatorUtils.findPreviousHighIndex(closePrices, startIdx, recentPriceHighIdx);
+        if (previousPriceHighIdx < startIdx || previousPriceHighIdx == -1) {
             return false;
         }
         
@@ -173,14 +178,14 @@
         }
         
         // 找到最近的价格低点和对应的DIF值
-        int recentPriceLowIdx = findRecentLowIndex(closePrices, startIdx);
-        if (recentPriceLowIdx < startIdx + 2) {
+        int recentPriceLowIdx = IndicatorUtils.findRecentLowIndex(closePrices, startIdx);
+        if (recentPriceLowIdx < startIdx + 2 || recentPriceLowIdx == -1) {
             return false;
         }
         
         // 找到之前的价格低点和对应的DIF值
-        int previousPriceLowIdx = findPreviousLowIndex(closePrices, startIdx, recentPriceLowIdx);
-        if (previousPriceLowIdx < startIdx) {
+        int previousPriceLowIdx = IndicatorUtils.findPreviousLowIndex(closePrices, startIdx, recentPriceLowIdx);
+        if (previousPriceLowIdx < startIdx || previousPriceLowIdx == -1) {
             return false;
         }
         
@@ -202,95 +207,4 @@
         return recentPrice.compareTo(previousPrice) < 0 && 
                recentDif.compareTo(previousDif) > 0;
     }
-
-    /**
-     * 找到最近的价格高点索引
-     *
-     * @param prices 价格列表
-     * @param startIdx 起始索引
-     * @return 最近的价格高点索引
-     */
-    private static int findRecentHighIndex(List<BigDecimal> prices, int startIdx) {
-        int highIdx = startIdx;
-        BigDecimal highPrice = prices.get(startIdx);
-        
-        for (int i = startIdx + 1; i < prices.size(); i++) {
-            BigDecimal currentPrice = prices.get(i);
-            if (currentPrice.compareTo(highPrice) > 0) {
-                highPrice = currentPrice;
-                highIdx = i;
-            }
-        }
-        
-        return highIdx;
-    }
-
-    /**
-     * 找到最近的价格低点索引
-     *
-     * @param prices 价格列表
-     * @param startIdx 起始索引
-     * @return 最近的价格低点索引
-     */
-    private static int findRecentLowIndex(List<BigDecimal> prices, int startIdx) {
-        int lowIdx = startIdx;
-        BigDecimal lowPrice = prices.get(startIdx);
-        
-        for (int i = startIdx + 1; i < prices.size(); i++) {
-            BigDecimal currentPrice = prices.get(i);
-            if (currentPrice.compareTo(lowPrice) < 0) {
-                lowPrice = currentPrice;
-                lowIdx = i;
-            }
-        }
-        
-        return lowIdx;
-    }
-
-    /**
-     * 找到最近价格高点之前的价格高点索引
-     *
-     * @param prices 价格列表
-     * @param startIdx 起始索引
-     * @param recentHighIdx 最近的价格高点索引
-     * @return 之前的价格高点索引
-     */
-    private static int findPreviousHighIndex(List<BigDecimal> prices, int startIdx, int recentHighIdx) {
-        int highIdx = startIdx;
-        BigDecimal highPrice = prices.get(startIdx);
-        
-        for (int i = startIdx + 1; i < recentHighIdx; i++) {
-            BigDecimal currentPrice = prices.get(i);
-            if (currentPrice.compareTo(highPrice) > 0) {
-                highPrice = currentPrice;
-                highIdx = i;
-            }
-        }
-        
-        return highIdx;
-    }
-
-    /**
-     * 找到最近价格低点之前的价格低点索引
-     *
-     * @param prices 价格列表
-     * @param startIdx 起始索引
-     * @param recentLowIdx 最近的价格低点索引
-     * @return 之前的价格低点索引
-     */
-    private static int findPreviousLowIndex(List<BigDecimal> prices, int startIdx, int recentLowIdx) {
-        int lowIdx = startIdx;
-        BigDecimal lowPrice = prices.get(startIdx);
-        
-        for (int i = startIdx + 1; i < recentLowIdx; i++) {
-            BigDecimal currentPrice = prices.get(i);
-            if (currentPrice.compareTo(lowPrice) < 0) {
-                lowPrice = currentPrice;
-                lowIdx = i;
-            }
-        }
-        
-        return lowIdx;
-    }
 }
-

--
Gitblit v1.9.1