From 4884ebffeed775d63dbf7bb7d0ece8182f10c344 Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Fri, 26 Dec 2025 18:19:46 +0800
Subject: [PATCH] fix(okxNewPrice): 解决交易订单为空时的空指针异常
---
src/main/java/com/xcong/excoin/modules/okxNewPrice/indicator/macdAndMatrategy/MACDCalculator.java | 107 +++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 107 insertions(+), 0 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
new file mode 100644
index 0000000..d68423e
--- /dev/null
+++ b/src/main/java/com/xcong/excoin/modules/okxNewPrice/indicator/macdAndMatrategy/MACDCalculator.java
@@ -0,0 +1,107 @@
+package com.xcong.excoin.modules.okxNewPrice.indicator.macdAndMatrategy;
+
+import java.math.BigDecimal;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * MACD(Moving Average Convergence Divergence)指标计算器
+ * <p>
+ * MACD指标由三部分组成:
+ * 1. DIF(Difference):短期EMA与长期EMA的差值
+ * 2. DEA(Signal Line):DIF的指数移动平均线,作为MACD的信号线
+ * 3. MACD柱状图(Histogram):DIF与DEA的差值,反映市场动量
+ * <p>
+ * 默认参数:短期周期=12,长期周期=26,信号周期=9
+ */
+public class MACDCalculator {
+
+ /**
+ * 计算MACD指标
+ *
+ * @param closePrices 收盘价列表(使用BigDecimal确保计算精度)
+ * @param shortPeriod 短期EMA周期(通常为12)
+ * @param longPeriod 长期EMA周期(通常为26)
+ * @param signalPeriod DEA的周期(通常为9)
+ * @return 包含MACD各部分数据的PriceData列表
+ * @throws IllegalArgumentException 如果数据点不足或参数无效
+ */
+ public static MACDResult calculateMACD(List<BigDecimal> closePrices, int shortPeriod, int longPeriod, int signalPeriod) {
+ // 参数校验:确保数据点足够
+ if (closePrices == null || closePrices.isEmpty()) {
+ throw new IllegalArgumentException("Close prices list cannot be null or empty.");
+ }
+ if (shortPeriod <= 0 || longPeriod <= 0 || signalPeriod <= 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 (closePrices.size() < Math.max(shortPeriod, longPeriod)) {
+ 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);
+
+ // 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);
+ }
+
+ // 4. 计算DEA(基于有效DIF数据的EMA)
+ List<BigDecimal> deaValues = EMACalculator.calculateEMA(difValues, signalPeriod, true);
+
+ // 5. 构建并填充结果(仅包含完整的MACD数据)
+ // 有效结果数量 = 有效DIF数量 - DEA周期 + 1
+ List<PriceData> result = new ArrayList<>(validLength - signalPeriod + 1);
+
+ // 从DEA开始有效的索引位置开始计算
+ for (int i = signalPeriod - 1; i < validLength; 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 - signalPeriod + 1)); // 调整DEA的索引位置
+ data.setMacdHist(data.getDif().subtract(data.getDea())); // MACD柱状图 = DIF - DEA
+
+ result.add(data);
+ }
+ System.out.println(result.get(result.size() -1));
+
+ return new MACDResult(result, startIdx);
+ }
+
+ /**
+ * 使用默认参数计算MACD指标
+ * <p>
+ * 默认参数:短期周期=12,长期周期=26,信号周期=9
+ *
+ * @param closePrices 收盘价列表
+ * @return 包含MACD各部分数据的PriceData列表
+ */
+ public static MACDResult calculateMACD(List<BigDecimal> closePrices) {
+ // 默认参数:短期周期12,长期周期26,信号周期9
+ return calculateMACD(closePrices, 12, 26, 9);
+ }
+}
+
--
Gitblit v1.9.1