| | |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * 技术指标基础类,提供通用计算方法 |
| | | * Technical indicators base class, provides common calculation methods |
| | | * |
| | | * 指标组合策略: |
| | | * 1. 趋势判断(MA/AdvancedMA/MACD):判断价格的整体走势方向 |
| | | * 2. 动量确认(RSI/KDJ):确认当前趋势的强度和可持续性 |
| | | * 3. 波动参考(BOLL):确定价格的合理波动范围和突破时机 |
| | | * Indicator combination strategy: |
| | | * 1. Trend judgment (MA/AdvancedMA/MACD): Determine the overall trend direction of prices |
| | | * 2. Momentum confirmation (RSI/KDJ): Confirm the strength and sustainability of the current trend |
| | | * 3. Volatility reference (BOLL): Determine reasonable price volatility range and breakthrough timing |
| | | * |
| | | * 多空方向选择逻辑: |
| | | * - 多头信号:MA多头排列 + MACD金叉 + RSI(30-70) + BOLL价格在上轨与中轨之间 |
| | | * - 空头信号:MA空头排列 + MACD死叉 + RSI(30-70) + BOLL价格在下轨与中轨之间 |
| | | * - 震荡信号:AdvancedMA三线粘合 + RSI(40-60) + BOLL带宽收窄 |
| | | * Long/Short direction selection logic: |
| | | * - Long signal: MA bullish arrangement + MACD golden cross + RSI(30-70) + BOLL price between upper and middle band |
| | | * - Short signal: MA bearish arrangement + MACD death cross + RSI(30-70) + BOLL price between lower and middle band |
| | | * - Consolidation signal: AdvancedMA three-line convergence + RSI(40-60) + BOLL bandwidth narrowing |
| | | * |
| | | * 开仓和平仓策略: |
| | | * - 开多:MA金叉 + MACD金叉 + KDJ金叉 + RSI(30-70) + 价格突破BOLL中轨 |
| | | * - 开空:MA死叉 + MACD死叉 + KDJ死叉 + RSI(30-70) + 价格跌破BOLL中轨 |
| | | * - 平多:MA死叉 + MACD死叉 + RSI超买(>70) + 价格跌破BOLL中轨 |
| | | * - 平空:MA金叉 + MACD金叉 + RSI超卖(<30) + 价格突破BOLL中轨 |
| | | * Open and close position strategies: |
| | | * - Open long: MA golden cross + MACD golden cross + KDJ golden cross + RSI(30-70) + price breaks through BOLL middle band |
| | | * - Open short: MA death cross + MACD death cross + KDJ death cross + RSI(30-70) + price breaks below BOLL middle band |
| | | * - Close long: MA death cross + MACD death cross + RSI overbought(>70) + price breaks below BOLL middle band |
| | | * - Close short: MA golden cross + MACD golden cross + RSI oversold(<30) + price breaks through BOLL middle band |
| | | */ |
| | | public abstract class IndicatorBase { |
| | | |
| | | /** |
| | | * 计算移动平均值 |
| | | * @param prices 价格列表 |
| | | * @param period 周期 |
| | | * @return 移动平均值 |
| | | * Calculate moving average |
| | | * @param prices Price list |
| | | * @param period Period |
| | | * @return Moving average value |
| | | */ |
| | | protected BigDecimal calculateMA(List<BigDecimal> prices, int period) { |
| | | if (prices == null || prices.size() < period) { |
| | |
| | | } |
| | | |
| | | /** |
| | | * 计算指数移动平均值 |
| | | * @param prices 价格列表 |
| | | * @param period 周期 |
| | | * @param prevEMA 前一个EMA值 |
| | | * @return 指数移动平均值 |
| | | * Calculate exponential moving average |
| | | * @param prices Price list |
| | | * @param period Period |
| | | * @param prevEMA Previous EMA value |
| | | * @return Exponential moving average value |
| | | */ |
| | | protected BigDecimal calculateEMA(List<BigDecimal> prices, int period, BigDecimal prevEMA) { |
| | | if (prices == null || prices.size() == 0) { |
| | |
| | | } |
| | | |
| | | /** |
| | | * 计算标准差 |
| | | * @param prices 价格列表 |
| | | * @param period 周期 |
| | | * @return 标准差 |
| | | * Calculate standard deviation |
| | | * @param prices Price list |
| | | * @param period Period |
| | | * @return Standard deviation |
| | | */ |
| | | protected BigDecimal calculateStdDev(List<BigDecimal> prices, int period) { |
| | | if (prices == null || prices.size() < period) { |
| | |
| | | } |
| | | |
| | | /** |
| | | * 计算平方根(简化实现) |
| | | * @param value 输入值 |
| | | * @return 平方根 |
| | | * Calculate square root (simplified implementation) |
| | | * @param value Input value |
| | | * @return Square root |
| | | */ |
| | | protected BigDecimal sqrt(BigDecimal value) { |
| | | if (value.compareTo(BigDecimal.ZERO) < 0) { |
| | |
| | | } |
| | | |
| | | /** |
| | | * 获取最近的价格数据 |
| | | * @param prices 所有价格数据 |
| | | * @param period 周期 |
| | | * @return 最近period个价格数据 |
| | | * Get recent price data |
| | | * @param prices All price data |
| | | * @param period Period |
| | | * @return Recent period price data |
| | | */ |
| | | protected List<BigDecimal> getRecentPrices(List<BigDecimal> prices, int period) { |
| | | if (prices == null || prices.size() == 0) { |
| | |
| | | } |
| | | |
| | | /** |
| | | * 计算ATR(Average True Range) |
| | | * @param high 最高价列表 |
| | | * @param low 最低价列表 |
| | | * @param close 收盘价列表 |
| | | * @param period 周期 |
| | | * @return ATR值 |
| | | * Calculate ATR (Average True Range) |
| | | * @param high High price list |
| | | * @param low Low price list |
| | | * @param close Close price list |
| | | * @param period Period |
| | | * @return ATR value |
| | | */ |
| | | protected BigDecimal calculateATR(List<BigDecimal> high, List<BigDecimal> low, List<BigDecimal> close, int period) { |
| | | if (high == null || low == null || close == null || |
| | |
| | | trList.add(trueRange); |
| | | } |
| | | |
| | | // 使用简单移动平均计算ATR |
| | | // Use simple moving average to calculate ATR |
| | | return calculateMA(trList, Math.min(period, trList.size())); |
| | | } |
| | | |
| | | /** |
| | | * 计算真实波幅(True Range) |
| | | * @param high 当前最高价 |
| | | * @param low 当前最低价 |
| | | * @param prevClose 前收盘价 |
| | | * @return 真实波幅 |
| | | * Calculate True Range |
| | | * @param high Current high price |
| | | * @param low Current low price |
| | | * @param prevClose Previous close price |
| | | * @return True range |
| | | */ |
| | | protected BigDecimal calculateTrueRange(BigDecimal high, BigDecimal low, BigDecimal prevClose) { |
| | | BigDecimal h1 = high.subtract(low); |
| | |
| | | } |
| | | |
| | | /** |
| | | * 计算标准化波动率(基于ATR) |
| | | * @param close 收盘价列表 |
| | | * @param atr ATR值 |
| | | * @return 标准化波动率(百分比) |
| | | * Calculate normalized volatility (based on ATR) |
| | | * @param close Close price list |
| | | * @param atr ATR value |
| | | * @return Normalized volatility (percentage) |
| | | */ |
| | | protected BigDecimal normalizeVolatility(List<BigDecimal> close, BigDecimal atr) { |
| | | if (close == null || close.size() == 0 || atr.compareTo(BigDecimal.ZERO) == 0) { |