
This strategy is a trend reversal trading system based on moving averages and MACD indicators. It combines Fast Exponential Moving Average (EMA), Simple Moving Average (SMA), and MACD indicator to capture profit opportunities during market trend changes. The strategy focuses on price breakouts above moving averages and MACD bottom reversal signals below the zero line to position trades before potential market reversals.
The strategy uses EMA(10) and MA(20) as trend judgment benchmarks, combined with MACD indicator (12,26,9) for signal confirmation. Specifically, entry signals require the following conditions to be met simultaneously: 1. EMA(10) crosses above MA(20), indicating short-term momentum is becoming stronger than medium-term trend 2. Both MACD and signal lines are below the zero line, but MACD line is above the signal line, showing potential bottom reversal signals The exit condition is triggered when the MACD delta crosses below 0 while both MACD and signal lines are above the zero line, suggesting the uptrend may have ended.
This strategy constructs a relatively complete trend reversal trading system through the combination of moving average system and MACD indicator. Although it has certain inherent lag and false signal risks, it still holds practical value through reasonable parameter optimization and risk control measures. When implementing in live trading, it’s recommended to adjust strategy parameters based on market conditions and personal risk preference.
/*backtest
start: 2024-02-25 00:00:00
end: 2025-01-20 00:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Binance","currency":"SOL_USDT"}]
*/
//@version=5
strategy("MACD Strategy", overlay=true)
//Macd 参数
fastLength = input(12, title="快线长度")
slowLength = input(26, title="慢线长度")
MACDLength = input(9, title="MACD 信号线长度")
// 计算 MACD
MACD = ta.ema(close, fastLength) - ta.ema(close, slowLength)
aMACD = ta.ema(MACD, MACDLength)
delta = MACD - aMACD
// 计算 EMA(10) 和 MA(20)
ema10 = ta.ema(close, 10)
ma20 = ta.sma(close, 20)
// 在图表上绘制 EMA(10) 和 MA(20),用于调试
plot(ema10, title="EMA 10", color=color.blue, linewidth=2)
plot(ma20, title="MA 20", color=color.red, linewidth=2)
// 实时检查条件
// 检查 EMA(10) 是否高于 MA(20)
bool emaAboveMa = ema10 > ma20
// 检查 MACD 是否在信号线上方,且 MACD 和信号线均在 0 轴下方
bool macdCondition = (MACD > aMACD) and (MACD < 0) and (aMACD < 0)
// 添加调试信息 - 当条件满足时绘制图形
plotshape(emaAboveMa, title="EMA Above MA Condition", size=size.small, text="eam")
plotshape(macdCondition, title="MACD Condition", size=size.small, text="macd")
// 当两个条件都满足时,触发买入操作
if (emaAboveMa and macdCondition)
strategy.entry("多头", strategy.long, comment="买入信号")
// 显示买入信号的标签
label.new(bar_index, high, "买入", textcolor=color.white, style=label.style_label_up, size=size.normal)
// 平仓条件
if (ta.crossunder(delta, 0) and MACD > 0 and aMACD > 0)
strategy.close("MacdLE", comment="Close Long")
//if (ta.crossunder(delta, 0))
// strategy.entry("MacdSE", strategy.short, comment="MacdSE")
//plot(strategy.equity, title="equity", color=color.red, linewidth=2, style=plot.style_areabr)