| | |
| | | public class MacdMaStrategy { |
| | | |
| | | /** 持仓状态枚举 */ |
| | | public enum OperationType { |
| | | /** 开仓平仓 */ |
| | | open, |
| | | close |
| | | } |
| | | |
| | | /** 持仓状态枚举 */ |
| | | public enum PositionType { |
| | | /** 多头持仓 */ |
| | | LONG_BUY, |
| | |
| | | private int volatilityPeriod; // 波动率计算周期 |
| | | private BigDecimal stopLossRatio; // 止损比例 |
| | | private BigDecimal takeProfitRatio; // 止盈比例 |
| | | |
| | | // 持仓信息 |
| | | private BigDecimal entryPrice; // 开仓价格 |
| | | private long entryTime; // 开仓时间戳 |
| | | |
| | | /** |
| | | * 默认构造函数,使用标准MACD参数 |
| | |
| | | this.stopLossRatio = stopLossRatio; |
| | | this.takeProfitRatio = takeProfitRatio; |
| | | |
| | | // 初始化持仓状态为空仓 |
| | | this.entryPrice = BigDecimal.ZERO; |
| | | this.entryTime = 0; |
| | | } |
| | | |
| | | /** |
| | |
| | | * @param closePrices 收盘价序列 |
| | | * @return 生成的交易信号(LONG、SHORT或NONE) |
| | | */ |
| | | public PositionType analyze(List<BigDecimal> closePrices) { |
| | | public PositionType analyzeOpen(List<BigDecimal> closePrices) { |
| | | // 数据检查:确保有足够的数据点进行计算 |
| | | if (closePrices == null || closePrices.size() < 34) { |
| | | return PositionType.NONE; // 数据不足,无法生成信号 |
| | |
| | | // 多头开仓条件检查 |
| | | if (isLongEntryCondition(macdResult, closePrices, volatility.getValue())) { |
| | | // 执行开多 |
| | | this.entryPrice = latestPrice; |
| | | this.entryTime = System.currentTimeMillis(); |
| | | log.info( "多头开仓信号,价格:{}", latestPrice); |
| | | return PositionType.LONG_BUY; |
| | | } |
| | | |
| | | // 空头开仓条件检查 |
| | | if (isShortEntryCondition(macdResult, closePrices, volatility.getValue())) { |
| | | // 执行开空 |
| | | this.entryPrice = latestPrice; |
| | | this.entryTime = System.currentTimeMillis(); |
| | | log.info( "空头开仓信号,价格:{}", latestPrice); |
| | | return PositionType.SHORT_SELL; |
| | | } |
| | | if (isLongExitCondition(macdResult, latestPrice)) { |
| | | // 执行平多 |
| | | return PositionType.LONG_SELL; |
| | | // 无信号 |
| | | return PositionType.NONE; |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 分析最新价格数据并生成交易信号 |
| | | * |
| | | * @param closePrices 收盘价序列 |
| | | * @return 生成的交易信号(LONG、SHORT或NONE) |
| | | */ |
| | | public PositionType analyzeClose(List<BigDecimal> closePrices) { |
| | | // 数据检查:确保有足够的数据点进行计算 |
| | | if (closePrices == null || closePrices.size() < 34) { |
| | | return PositionType.NONE; // 数据不足,无法生成信号 |
| | | } |
| | | if (isShortExitCondition(macdResult, latestPrice)) { |
| | | return PositionType.SHORT_BUY; |
| | | |
| | | // 1. 计算MACD指标 |
| | | MACDResult macdResult = MACDCalculator.calculateMACD( |
| | | closePrices, shortPeriod, longPeriod, signalPeriod); |
| | | |
| | | // 最新收盘价 |
| | | BigDecimal latestPrice = closePrices.get(closePrices.size() - 1); |
| | | |
| | | if (isLongExitCondition(macdResult, latestPrice)) { |
| | | // 执行平多 |
| | | log.info( "多头平仓信号,价格:{}", latestPrice); |
| | | return PositionType.LONG_SELL; |
| | | } |
| | | if (isShortExitCondition(macdResult, latestPrice)) { |
| | | // 执行平空 |
| | | log.info( "空头平仓信号,价格:{}", latestPrice); |
| | | return PositionType.SHORT_BUY; |
| | | } |
| | | |
| | | // 无信号 |
| | |
| | | * @param historicalPrices 历史价格序列 |
| | | * @return 交易指令(包含side和posSide),如果没有交易信号则返回null |
| | | */ |
| | | public TradingOrder generateTradingOrder(List<BigDecimal> historicalPrices) { |
| | | PositionType signal = analyze(historicalPrices); |
| | | public TradingOrder generateTradingOrder(List<BigDecimal> historicalPrices,String operation) { |
| | | PositionType signal = null; |
| | | |
| | | if ( operation == OperationType.open.name()){ |
| | | |
| | | signal = analyzeOpen(historicalPrices); |
| | | |
| | | }else if ( operation == OperationType.close.name()){ |
| | | |
| | | signal = analyzeClose(historicalPrices); |
| | | |
| | | } |
| | | // 根据信号和当前持仓状态生成交易指令 |
| | | if (signal == PositionType.LONG_BUY) { |
| | | // 开多:买入开多(side 填写 buy; posSide 填写 long ) |
| | |
| | | // 4. 波动率过滤(必须在合理范围内) |
| | | boolean volatilityFilter = isVolatilityInRange(volatility); |
| | | |
| | | if (macdPositive && volatilityFilter && isMacdFavorable) { |
| | | log.info("多头信号形成, MACD有利状态: {}, 柱状线为正: {}, 波动率过滤: {}", |
| | | isMacdFavorable, macdPositive, volatilityFilter); |
| | | } |
| | | // 5. 底背离检查(增强多头信号可靠性) |
| | | boolean isBottomDivergence = MACDCalculator.isBottomDivergence(closePrices, macdResult); |
| | | |
| | | // 所有条件必须同时满足 |
| | | return macdPositive && volatilityFilter && isMacdFavorable; |
| | | boolean result = macdPositive && volatilityFilter && (isMacdFavorable || isBottomDivergence); |
| | | |
| | | if (result) { |
| | | log.info("多头信号形成, MACD有利状态: {}, 柱状线为正: {}, 波动率过滤: {}, 底背离: {}", |
| | | isMacdFavorable, macdPositive, volatilityFilter, isBottomDivergence); |
| | | } |
| | | |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | |
| | | // 4. 波动率过滤(必须在合理范围内) |
| | | boolean volatilityFilter = isVolatilityInRange(volatility); |
| | | |
| | | if (macdNegative && volatilityFilter && isMacdFavorable) { |
| | | log.info("空头信号形成, MACD有利状态: {}, 柱状线为负: {}, 波动率过滤: {}", |
| | | isMacdFavorable, macdNegative, volatilityFilter); |
| | | } |
| | | // 5. 顶背离检查(增强空头信号可靠性) |
| | | boolean isTopDivergence = MACDCalculator.isTopDivergence(closePrices, macdResult); |
| | | |
| | | // 所有条件必须同时满足 |
| | | return macdNegative && volatilityFilter && isMacdFavorable; |
| | | boolean result = macdNegative && volatilityFilter && (isMacdFavorable || isTopDivergence); |
| | | |
| | | if (result) { |
| | | log.info("空头信号形成, MACD有利状态: {}, 柱状线为负: {}, 波动率过滤: {}, 顶背离: {}", |
| | | isMacdFavorable, macdNegative, volatilityFilter, isTopDivergence); |
| | | } |
| | | |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | |
| | | // 金叉判断:DIF从下往上穿过DEA |
| | | boolean isGoldenCross = prevPrev.getDif().compareTo(prevPrev.getDea()) <= 0 && |
| | | previous.getDif().compareTo(previous.getDea()) > 0; |
| | | |
| | | boolean isUp = latest.getDif().compareTo(BigDecimal.ZERO) > 0 && latest.getDea().compareTo(BigDecimal.ZERO) > 0; |
| | | |
| | | // 柱状线扩张判断:连续正值且绝对值增大 |
| | | boolean isExpanding = latest.getMacdHist().compareTo(BigDecimal.ZERO) > 0 && |
| | | boolean isExpanding = latest.getMacdHist().compareTo(BigDecimal.ZERO) > 0 && |
| | | previous.getMacdHist().compareTo(BigDecimal.ZERO) > 0 && |
| | | previous.getMacdHist().abs().compareTo(latest.getMacdHist().abs()) < 0; |
| | | |
| | | // 金叉或柱状线扩张任一满足即可 |
| | | return isGoldenCross || isExpanding; |
| | | return isGoldenCross && isExpanding && isUp; |
| | | } |
| | | |
| | | /** |
| | |
| | | boolean isDeathCross = prevPrev.getDif().compareTo(prevPrev.getDea()) >= 0 && |
| | | previous.getDif().compareTo(previous.getDea()) < 0; |
| | | |
| | | // 柱状线收缩判断:连续负值且绝对值减小 |
| | | boolean isContracting = latest.getMacdHist().compareTo(BigDecimal.ZERO) < 0 && |
| | | previous.getMacdHist().compareTo(BigDecimal.ZERO) < 0 && |
| | | previous.getMacdHist().abs().compareTo(latest.getMacdHist().abs()) > 0; |
| | | boolean isDown = latest.getDif().compareTo(BigDecimal.ZERO) < 0 && latest.getDea().compareTo(BigDecimal.ZERO) < 0; |
| | | |
| | | // 优化后的死叉柱状线条件:空头趋势中,死叉应伴随柱状线扩张(绝对值增大) |
| | | boolean isExpanding = latest.getMacdHist().compareTo(BigDecimal.ZERO) < 0 && |
| | | previous.getMacdHist().compareTo(BigDecimal.ZERO) < 0 && |
| | | previous.getMacdHist().abs().compareTo(latest.getMacdHist().abs()) < 0; |
| | | |
| | | // 死叉或柱状线收缩任一满足即可 |
| | | return isDeathCross || isContracting; |
| | | return isDeathCross && isExpanding && isDown; |
| | | } |
| | | |
| | | /** |
| | |
| | | volatility.compareTo(maxVolatility) <= 0; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 获取开仓价格 |
| | | * |
| | | * @return 开仓价格 |
| | | */ |
| | | public BigDecimal getEntryPrice() { |
| | | return entryPrice; |
| | | } |
| | | |
| | | /** |
| | | * 获取开仓时间戳 |
| | | * |
| | | * @return 开仓时间戳 |
| | | */ |
| | | public long getEntryTime() { |
| | | return entryTime; |
| | | } |
| | | } |