/** * 指标计算工具类 * 封装MACD策略中常用的通用功能,如高低点查找等 */ package com.xcong.excoin.modules.okxNewPrice.indicator.macdAndMatrategy; import java.math.BigDecimal; import java.util.List; /** * 指标计算工具类,提供MACD策略中常用的通用功能 */ public class IndicatorUtils { /** * 找到最近的价格高点索引 * * @param prices 价格列表 * @param startIdx 起始索引 * @return 最近的价格高点索引 */ public static int findRecentHighIndex(List prices, int startIdx) { if (prices == null || startIdx < 0 || startIdx >= prices.size()) { return -1; } 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 最近的价格低点索引 */ public static int findRecentLowIndex(List prices, int startIdx) { if (prices == null || startIdx < 0 || startIdx >= prices.size()) { return -1; } 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 之前的价格高点索引 */ public static int findPreviousHighIndex(List prices, int startIdx, int recentHighIdx) { if (prices == null || startIdx < 0 || recentHighIdx <= startIdx || recentHighIdx >= prices.size()) { return -1; } 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 之前的价格低点索引 */ public static int findPreviousLowIndex(List prices, int startIdx, int recentLowIdx) { if (prices == null || startIdx < 0 || recentLowIdx <= startIdx || recentLowIdx >= prices.size()) { return -1; } 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; } /** * 寻找最近的价格高点(带有回调确认) * * @param prices 价格列表 * @param startIndex 起始索引 * @param endIndex 结束索引 * @return 符合条件的价格高点索引,未找到则返回-1 */ public static int findRecentHighWithRetrace(List prices, int startIndex, int endIndex) { if (prices == null || startIndex < 0 || endIndex >= prices.size() || startIndex >= endIndex) { return -1; } int highIndex = -1; BigDecimal highPrice = BigDecimal.ZERO; // 从右向左搜索,找到第一个有效高点 for (int i = endIndex; i >= startIndex; i--) { if (prices.get(i).compareTo(highPrice) > 0) { highPrice = prices.get(i); highIndex = i; } // 检查高点后是否有回调 if (highIndex != -1 && i < endIndex) { if (prices.get(i + 1).compareTo(highPrice) < 0) { return highIndex; // 找到确认回调的高点 } } } return highIndex; } }