| | |
| | | package com.xcong.excoin.modules.okxNewPrice.indicator.macdAndMatrategy; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.math.RoundingMode; |
| | | import java.util.ArrayList; |
| | | import java.util.Collections; |
| | | import java.util.List; |
| | | |
| | | /** |
| | |
| | | throw new IllegalArgumentException("Insufficient data points for the specified periods."); |
| | | } |
| | | |
| | | // 反转数据,确保从旧到新处理(因为用户提供的数据是从新到旧) |
| | | List<BigDecimal> prices = new ArrayList<>(closePrices); |
| | | Collections.reverse(prices); |
| | | |
| | | // 1. 计算快速EMA和慢速EMA,使用SMA作为初始值 |
| | | // 当initialSMA=true时,EMA列表长度为prices.size() - period + 1 |
| | | List<BigDecimal> fastEma = EMACalculator.calculateEMA(closePrices, fastlen, true); |
| | | List<BigDecimal> slowEma = EMACalculator.calculateEMA(closePrices, slowlen, true); |
| | | List<BigDecimal> fastEma = EMACalculator.calculateEMA(prices, fastlen, true); |
| | | List<BigDecimal> slowEma = EMACalculator.calculateEMA(prices, slowlen, true); |
| | | |
| | | // 2. 计算MACD线(快速EMA减去慢速EMA) |
| | | List<BigDecimal> macdLine = new ArrayList<>(); |
| | |
| | | int slowEmaStartIdx = slowlen - 1; // slowEma中第一个有效值对应的价格索引 |
| | | int fastEmaStartIdx = fastlen - 1; // fastEma中第一个有效值对应的价格索引 |
| | | |
| | | for (int i = 0; i < closePrices.size(); i++) { |
| | | for (int i = 0; i < prices.size(); i++) { |
| | | if (i < slowEmaStartIdx) { |
| | | // 在慢速EMA开始有效之前,MACD线值为0 |
| | | macdLine.add(BigDecimal.ZERO); |
| | |
| | | } else { |
| | | // 将macdLine索引转换为signalLine索引 |
| | | int signalLineIdx = i - signalLineStartIdx; |
| | | BigDecimal histValue = macdLine.get(i).subtract(signalLine.get(signalLineIdx)); |
| | | // 柱状图 = (MACD线 - 信号线) * 2(放大信号) |
| | | BigDecimal histValue = macdLine.get(i).subtract(signalLine.get(signalLineIdx)).multiply(new BigDecimal("2")); |
| | | histogram.add(histValue); |
| | | } |
| | | } |
| | |
| | | List<PriceData> result = new ArrayList<>(); |
| | | int startIndex = slowEmaStartIdx + signalLineStartIdx; // 从信号线开始有效的位置开始 |
| | | |
| | | for (int i = startIndex; i < closePrices.size(); i++) { |
| | | PriceData data = new PriceData(closePrices.get(i)); |
| | | for (int i = startIndex; i < prices.size(); i++) { |
| | | PriceData data = new PriceData(prices.get(i)); |
| | | |
| | | // 设置EMA值(需要转换索引) |
| | | int fastEmaIdx = i - fastEmaStartIdx; |
| | |
| | | |
| | | result.add(data); |
| | | } |
| | | |
| | | // 反转结果列表,恢复为从新到旧的顺序 |
| | | Collections.reverse(result); |
| | | |
| | | return new MACDResult(result, startIndex); |
| | | } |
| | |
| | | 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; |
| | | } |
| | |
| | | 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(); |
| | | |
| | |
| | | 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; |
| | | } |
| | |
| | | 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(); |
| | | |