| | |
| | | difValues.add(dif); |
| | | } |
| | | |
| | | // 4. 计算DEA(基于有效DIF数据的EMA) |
| | | List<BigDecimal> deaValues = EMACalculator.calculateEMA(difValues, signalPeriod, false); |
| | | // 4. 计算DEA(基于有效DIF数据的EMA),欧意平台使用SMA作为初始值 |
| | | List<BigDecimal> deaValues = EMACalculator.calculateEMA(difValues, signalPeriod, true); |
| | | |
| | | // 5. 构建并填充结果(包含所有MACD数据) |
| | | List<PriceData> result = new ArrayList<>(deaValues.size()); |
| | |
| | | // 设置DIF、DEA和MACD柱状图 |
| | | data.setDif(difValues.get(i)); |
| | | data.setDea(deaValues.get(i)); // DEA索引直接对应 |
| | | data.setMacdHist(data.getDif().subtract(data.getDea())); // MACD柱状图 = DIF - DEA |
| | | data.setMacdHist(data.getDif().subtract(data.getDea()).multiply(BigDecimal.valueOf(2))); // MACD柱状图 = (DIF - DEA) × 2 (欧意平台标准) |
| | | |
| | | result.add(data); |
| | | } |
| | | System.out.println(result.get(result.size() -1)); |
| | | |
| | | |
| | | return new MACDResult(result, startIdx); |
| | | } |
| | |
| | | } |
| | | |
| | | // 找到最近的价格高点和对应的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; |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | // 找到最近的价格低点和对应的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; |
| | | } |
| | | |
| | |
| | | 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; |
| | | } |
| | | } |
| | | |
| | | } |