/**
|
* MACD和MA组合交易策略实现类
|
* 基于15分钟K线数据生成交易信号并确定持仓方向
|
*
|
* 该策略综合考虑了EMA指标、MACD指标、价格突破信号和波动率因素,
|
* 形成了一套完整的开仓、平仓和持仓管理机制。
|
*/
|
package com.xcong.excoin.modules.okxNewPrice.indicator.macdAndMatrategy;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import java.math.BigDecimal;
|
import java.util.List;
|
|
/**
|
* MACD和MA组合交易策略实现
|
* <p>
|
* 该策略利用EMA交叉、MACD指标、价格突破信号和波动率过滤,
|
* 为15分钟K线级别交易提供综合决策支持。
|
*/
|
@Slf4j
|
public class MacdMaStrategy {
|
|
/** 持仓状态枚举 */
|
public enum PositionType {
|
/** 多头持仓 */
|
LONG,
|
/** 空头持仓 */
|
SHORT,
|
/** 空仓 */
|
NONE
|
}
|
|
// 策略参数
|
private int shortPeriod; // 短期EMA周期
|
private int longPeriod; // 长期EMA周期
|
private int signalPeriod; // MACD信号线周期
|
private int volatilityPeriod; // 波动率计算周期
|
private BigDecimal stopLossRatio; // 止损比例
|
private BigDecimal takeProfitRatio; // 止盈比例
|
|
// 持仓信息
|
private BigDecimal entryPrice; // 开仓价格
|
private long entryTime; // 开仓时间戳
|
|
/**
|
* 默认构造函数,使用标准MACD参数
|
* 短期周期=12, 长期周期=26, 信号线周期=9, 波动率周期=20
|
* 止损比例=1%, 止盈比例=2%
|
*/
|
public MacdMaStrategy() {
|
this(12, 26, 9, 20, new BigDecimal("0.01"), new BigDecimal("0.02"));
|
}
|
|
/**
|
* 自定义参数构造函数
|
*
|
* @param shortPeriod 短期EMA周期
|
* @param longPeriod 长期EMA周期
|
* @param signalPeriod MACD信号线周期
|
* @param volatilityPeriod 波动率计算周期
|
* @param stopLossRatio 止损比例
|
* @param takeProfitRatio 止盈比例
|
*/
|
public MacdMaStrategy(int shortPeriod, int longPeriod, int signalPeriod, int volatilityPeriod,
|
BigDecimal stopLossRatio, BigDecimal takeProfitRatio) {
|
this.shortPeriod = shortPeriod;
|
this.longPeriod = longPeriod;
|
this.signalPeriod = signalPeriod;
|
this.volatilityPeriod = volatilityPeriod;
|
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) {
|
// 数据检查:确保有足够的数据点进行计算
|
if (closePrices == null || closePrices.size() < 34) {
|
return PositionType.NONE; // 数据不足,无法生成信号
|
}
|
|
// 1. 计算MACD指标
|
MACDResult macdResult = MACDCalculator.calculateMACD(
|
closePrices, shortPeriod, longPeriod, signalPeriod);
|
|
// 2. 计算波动率
|
Volatility volatility = new Volatility(volatilityPeriod);
|
for (int i = Math.max(0, closePrices.size() - volatilityPeriod);
|
i < closePrices.size(); i++) {
|
volatility.addPrice(closePrices.get(i));
|
}
|
volatility.calculate();
|
|
// 最新收盘价
|
BigDecimal latestPrice = closePrices.get(closePrices.size() - 1);
|
|
// 3. 检查开平仓条件
|
// 多头开仓条件检查
|
if (isLongEntryCondition(macdResult, closePrices, volatility.getValue())) {
|
// 执行开多
|
this.entryPrice = latestPrice;
|
this.entryTime = System.currentTimeMillis();
|
return PositionType.LONG;
|
}
|
|
// 空头开仓条件检查
|
if (isShortEntryCondition(macdResult, closePrices, volatility.getValue())) {
|
// 执行开空
|
this.entryPrice = latestPrice;
|
this.entryTime = System.currentTimeMillis();
|
return PositionType.SHORT;
|
}
|
|
// 无信号
|
return PositionType.NONE;
|
|
}
|
|
/**
|
* 交易指令类,封装side和posSide的组合
|
*/
|
public static class TradingOrder {
|
private String side; // buy或sell
|
private String posSide; // long或short
|
|
public TradingOrder(String side, String posSide) {
|
this.side = side;
|
this.posSide = posSide;
|
}
|
|
public String getSide() {
|
return side;
|
}
|
|
public String getPosSide() {
|
return posSide;
|
}
|
|
@Override
|
public String toString() {
|
return String.format("TradingOrder{side='%s', posSide='%s'}", side, posSide);
|
}
|
}
|
|
/**
|
* 分析历史价格数据并生成交易指令
|
*
|
* @param historicalPrices 历史价格序列
|
* @return 交易指令(包含side和posSide),如果没有交易信号则返回null
|
*/
|
public TradingOrder generateTradingOrder(List<BigDecimal> historicalPrices) {
|
PositionType signal = analyze(historicalPrices);
|
|
// 根据信号和当前持仓状态生成交易指令
|
if (signal == PositionType.LONG) {
|
// 开多:买入开多(side 填写 buy; posSide 填写 long )
|
return new TradingOrder("buy", "long");
|
} else if (signal == PositionType.SHORT) {
|
// 开空:卖出开空(side 填写 sell; posSide 填写 short )
|
return new TradingOrder("sell", "short");
|
}
|
// 没有交易信号
|
return null;
|
}
|
|
/**
|
* 多头开仓条件检查
|
*
|
* @param macdResult MACD计算结果
|
* @param closePrices 收盘价序列
|
* @param volatility 当前波动率
|
* @return 是否满足多头开仓条件
|
*/
|
private boolean isLongEntryCondition(MACDResult macdResult, List<BigDecimal> closePrices, BigDecimal volatility) {
|
// 1. EMA金叉检查(简化为只检查当前短期EMA > 当前长期EMA)
|
List<PriceData> macdData = macdResult.getMacdData();
|
if (macdData.size() < 1) {
|
return false;
|
}
|
boolean bullishSignalFormed = BullishSignalDetector.isBullishSignalFormed(macdResult, closePrices);
|
boolean isExpanding = isExpanding(macdResult);
|
|
PriceData latest = macdData.get(macdData.size() - 1);
|
// 2. MACD柱状线为正
|
boolean macdPositive = latest.getMacdHist().compareTo(BigDecimal.ZERO) > 0;
|
|
// 3. 简化的波动率检查
|
boolean volatilityFilter = volatility.compareTo(BigDecimal.ZERO) > 0;
|
|
if ( bullishSignalFormed && macdPositive && volatilityFilter){
|
log.info( "EMA金叉: {},扩张:{}", bullishSignalFormed,isExpanding);
|
log.info( "MACD柱状线为正: {}" ,macdPositive);
|
log.info( "波动率过滤: {}" ,volatilityFilter);
|
}
|
// 只需要EMA短期在长期上方、MACD柱状线为正且波动率大于0
|
return bullishSignalFormed && macdPositive && isExpanding && volatilityFilter;
|
}
|
|
/**
|
* 空头开仓条件检查
|
*
|
* @param macdResult MACD计算结果
|
* @param closePrices 收盘价序列
|
* @param volatility 当前波动率
|
* @return 是否满足空头开仓条件
|
*/
|
private boolean isShortEntryCondition(MACDResult macdResult, List<BigDecimal> closePrices,
|
BigDecimal volatility) {
|
List<PriceData> macdData = macdResult.getMacdData();
|
if (macdData.size() < 1) {
|
return false;
|
}
|
boolean bearishSignalFormed = BearishSignalDetector.isBearishSignalFormed(macdResult, closePrices);
|
// 2. MACD柱状线收缩+死叉检查
|
boolean isContracting = isContracting(macdResult);
|
|
// 2. MACD柱状线为负
|
PriceData latest = macdData.get(macdData.size() - 1);
|
boolean macdPositive = latest.getMacdHist().compareTo(BigDecimal.ZERO) < 0;
|
|
|
// 4. 波动率过滤检查(0.5% ~ 5%)
|
boolean volatilityFilter = isVolatilityInRange(volatility);
|
if ( isContracting&& volatilityFilter){
|
log.info( "MACD柱状线死叉: {},收缩: {}", bearishSignalFormed,isContracting);
|
log.info( "MACD柱状线为负: {}" ,macdPositive);
|
log.info( "波动率过滤: {}", volatilityFilter);
|
}
|
|
// 所有条件必须同时满足
|
return isContracting && volatilityFilter && macdPositive && bearishSignalFormed;
|
}
|
|
/**
|
* MACD金叉且柱状线扩张检查
|
*
|
* @param macdResult MACD计算结果
|
* @return 是否形成MACD金叉且柱状线扩张
|
*/
|
private boolean isExpanding(MACDResult macdResult) {
|
List<PriceData> macdData = macdResult.getMacdData();
|
if (macdData.size() < 3) {
|
return false;
|
}
|
|
PriceData latest = macdData.get(macdData.size() - 1);
|
PriceData previous = macdData.get(macdData.size() - 2);
|
|
|
// 2. MACD柱状线扩张检查
|
boolean histogramExpanding =
|
previous.getMacdHist().compareTo(latest.getMacdHist()) < 0 &&
|
latest.getMacdHist().compareTo(BigDecimal.ZERO) > 0;
|
|
return histogramExpanding;
|
}
|
|
/**
|
* MACD死叉且柱状线收缩检查
|
*
|
* @param macdResult MACD计算结果
|
* @return 是否形成MACD死叉且柱状线收缩
|
*/
|
private boolean isContracting(MACDResult macdResult) {
|
List<PriceData> macdData = macdResult.getMacdData();
|
if (macdData.size() < 2) {
|
return false;
|
}
|
|
PriceData latest = macdData.get(macdData.size() - 1);
|
PriceData previous = macdData.get(macdData.size() - 2);
|
|
// 2. MACD柱状线收缩检查(绝对值减小)
|
boolean histogramContracting =
|
previous.getMacdHist().abs().compareTo(
|
latest.getMacdHist().abs()) < 0 &&
|
latest.getMacdHist().compareTo(BigDecimal.ZERO) < 0;
|
|
return histogramContracting;
|
}
|
|
/**
|
* 波动率过滤检查
|
*
|
* @param volatility 当前波动率
|
* @return 波动率是否在0.5%~5%范围内
|
*/
|
private boolean isVolatilityInRange(BigDecimal volatility) {
|
BigDecimal minVolatility = new BigDecimal("0.1"); // 降低最小波动率阈值
|
BigDecimal maxVolatility = new BigDecimal("5.0");
|
|
return volatility.compareTo(minVolatility) >= 0 &&
|
volatility.compareTo(maxVolatility) <= 0;
|
}
|
|
|
/**
|
* 获取开仓价格
|
*
|
* @return 开仓价格
|
*/
|
public BigDecimal getEntryPrice() {
|
return entryPrice;
|
}
|
|
/**
|
* 获取开仓时间戳
|
*
|
* @return 开仓时间戳
|
*/
|
public long getEntryTime() {
|
return entryTime;
|
}
|
}
|