
This strategy is a trading system that combines dual EMA trend identification, ADX momentum filtering, and adaptive risk management. It uses 50 and 200-period Exponential Moving Averages (EMA) as the foundation for trend determination, confirms momentum through ADX and DMI indicators, and dynamically adjusts stop-loss and profit targets based on ATR.
The core logic consists of three parts: 1. Trend Identification: Uses the relative position of 50 and 200-period EMAs to determine the current trend direction, with EMA50 above EMA200 indicating an uptrend and vice versa. 2. Momentum Confirmation: Utilizes ADX and DMI indicators to confirm trend strength, requiring ADX above a set threshold (default 25) and DI+ greater than DI- to confirm uptrend, and vice versa for downtrend. 3. Entry Timing: After trend confirmation, price crossovers with EMA50 serve as specific entry signals, with upward crosses for long positions and downward crosses for short positions.
This is a structurally complete and logically clear trend-following strategy that achieves reliable trade signal generation and risk control through the coordinated use of multiple technical indicators. The strategy offers strong extensibility with significant optimization potential. Through appropriate parameter adjustments and optimization measures, it can adapt to different market environments.
/*backtest
start: 2025-02-10 00:00:00
end: 2025-02-17 00:00:00
period: 3m
basePeriod: 3m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("XAUUSD 15m Trend Strategy", overlay=true, margin_long=100, margin_short=100)
// Input parameters
emaFast = input.int(50, "Fast EMA Period", minval=1)
emaSlow = input.int(200, "Slow EMA Period", minval=1)
adxThreshold = input.int(25, "ADX Threshold", minval=1)
lookback = input.int(5, "Swing Lookback Period", minval=1)
riskReward = input.float(1.5, "Risk Reward Ratio", minval=1.0)
// Calculate indicators
ema50 = ta.ema(close, emaFast)
ema200 = ta.ema(close, emaSlow)
[diPlus, diMinus, adx] = ta.dmi(14, 14)
atr = ta.atr(14)
// Trend conditions
uptrend = ema50 > ema200 and adx >= adxThreshold and diPlus > diMinus
downtrend = ema50 < ema200 and adx >= adxThreshold and diMinus > diPlus
// Entry conditions
longCondition = uptrend and ta.crossover(close, ema50)
shortCondition = downtrend and ta.crossunder(close, ema50)
// Calculate risk levels
longStop = ta.lowest(low, lookback) - atr * 0.5
longProfit = close + (close - longStop) * riskReward
shortStop = ta.highest(high, lookback) + atr * 0.5
shortProfit = close - (shortStop - close) * riskReward
// Execute trades
if (longCondition)
strategy.entry("Long", strategy.long)
strategy.exit("Exit Long", "Long", stop=longStop, limit=longProfit)
if (shortCondition)
strategy.entry("Short", strategy.short)
strategy.exit("Exit Short", "Short", stop=shortStop, limit=shortProfit)
// Plotting
plot(ema50, "EMA 50", color=color.blue)
plot(ema200, "EMA 200", color=color.orange)
plot(strategy.position_size > 0 ? longStop : na, "Long Stop", color=color.red, style=plot.style_linebr)
plot(strategy.position_size > 0 ? longProfit : na, "Long Target", color=color.green, style=plot.style_linebr)
plot(strategy.position_size < 0 ? shortStop : na, "Short Stop", color=color.red, style=plot.style_linebr)
plot(strategy.position_size < 0 ? shortProfit : na, "Short Target", color=color.green, style=plot.style_linebr)
// Signal markers
plotshape(longCondition, "Buy Signal", shape.triangleup, location.belowbar, color=color.green, size=size.small)
plotshape(shortCondition, "Sell Signal", shape.triangledown, location.abovebar, color=color.red, size=size.small)