
This strategy is a trading system that combines moving average trend following with dynamic stop-loss management. It utilizes MACD (Moving Average Convergence Divergence) for momentum capture, EMA (Exponential Moving Average) for trend confirmation, and ATR (Average True Range) for dynamic stop-loss positioning. This multi-dimensional analysis approach effectively captures market opportunities while maintaining robust risk control.
The core logic comprises three dimensions: 1. Using MACD indicator crossovers (fast line crossing above slow line) for long entry signals and crossunders (fast line crossing below slow line) for exit signals. 2. Employing a 20-period EMA as a trend filter, allowing long positions only when price is above EMA, thus avoiding entries in downtrends. 3. Setting dynamic stop-loss levels based on ATR, which adapts to market volatility. When trailing stop is enabled, the stop-loss level moves up with price increases to lock in profits.
This strategy builds a comprehensive trading system by combining trend following, momentum analysis, and dynamic risk control. Its main feature is achieving effective market opportunity capture and dynamic risk control while maintaining strategy robustness. While inherent risks exist, the strategy holds good practical application value through appropriate parameter settings and continuous optimization.
/*backtest
start: 2024-09-25 00:00:00
end: 2025-02-19 08:00:00
period: 2h
basePeriod: 2h
exchanges: [{"eid":"Binance","currency":"SOL_USDT"}]
*/
//@version=5
strategy("MACD + ATR Dynamic Stop-Loss Strategy", overlay=true)
// Input parameters
macdFastLength = input.int(12, title="MACD Fast Length")
macdSlowLength = input.int(26, title="MACD Slow Length")
macdSignalSmoothing = input.int(9, title="MACD Signal Smoothing")
atrLength = input.int(14, title="ATR Length")
stopLossMultiplier = input.float(1.0, title="Stop-Loss ATR Multiplier")
useTrailingStop = input.bool(true, title="Use Trailing Stop")
trailATRMultiplier = input.float(2.0, title="Trailing Stop ATR Multiplier")
emaLength = input.int(20, title="EMA Length")
// Calculate MACD
[macdLine, signalLine, _] = ta.macd(close, macdFastLength, macdSlowLength, macdSignalSmoothing)
// Calculate ATR
atr = ta.atr(atrLength)
// Calculate 20-period EMA
ema20 = ta.ema(close, emaLength)
// Entry Conditions
buyCondition = ta.crossover(macdLine, signalLine) and close > ema20
sellCondition = ta.crossunder(macdLine, signalLine)
// Plot Buy and Sell Signals
plotshape(series=buyCondition, title="Buy Signal", location=location.belowbar, color=color.green, style=shape.labelup, text="BUY")
plotshape(series=sellCondition, title="Sell Signal", location=location.abovebar, color=color.red, style=shape.labeldown, text="SELL")
// Dynamic Stop-Loss and Trailing Stop Logic
var float stopLossLevel = na
var float trailingStopLevel = na
if (buyCondition)
stopLossLevel := close - atr * stopLossMultiplier
trailingStopLevel := close - atr * trailATRMultiplier
if (strategy.position_size > 0)
if (useTrailingStop)
trailingStopLevel := math.max(trailingStopLevel, close - atr * trailATRMultiplier)
stopLossLevel := trailingStopLevel
strategy.exit("Trailing Stop", stop=stopLossLevel)
// Execute Trades
if (buyCondition)
strategy.entry("Long", strategy.long)
if (sellCondition)
strategy.close("Long")
// Plot Stop-Loss Level
plot(stopLossLevel, title="Stop-Loss Level", color=color.red, linewidth=1, style=plot.style_linebr)