Strategy vault / Trend following
Multi EMA Crossover Trend Following Strategy with Dynamic Stop Loss and Take Profit Optimization
This strategy is a trend following system based on multiple Exponential Moving Average (EMA) crossovers, combined with dynamic stop-loss and take-profit mechanisms. The strategy employs three EMAs - 21-period…

Overview
This strategy is a trend following system based on multiple Exponential Moving Average (EMA) crossovers, combined with dynamic stop-loss and take-profit mechanisms. The strategy employs three EMAs - 21-period, 50-period, and 200-period - generating trading signals through short-term and medium-term EMA crossovers while using the long-term EMA to confirm overall trend direction. It includes flexible stop-loss and take-profit levels for risk management. The strategy is particularly suitable for markets with significant volatility and medium to long-term trend trading.
Strategy Principles
The core logic is based on the synergistic effect of a triple EMA system:
- Uses 21-period EMA as the fast moving average to reflect short-term price movements
- Employs 50-period EMA as the medium-term moving average for signal generation
- Utilizes 200-period EMA as the long-term moving average for trend confirmation
- Generates long signals when the 21-period EMA crosses above the 50-period EMA and price is above the 200-period EMA
- Generates short signals when the 21-period EMA crosses below the 50-period EMA and price is below the 200-period EMA
- Each trading signal is equipped with corresponding stop-loss and take-profit levels calculated based on current price and user-defined ticks
Strategy Advantages
- Multiple timeframe validation: Effectively reduces false breakout risks through triple EMA coordination
- Trend confirmation mechanism: Uses 200-period EMA as a trend filter to improve directional accuracy
- Comprehensive risk management: Built-in dynamic stop-loss and take-profit mechanism for precise risk control
- Flexible parameters: Adjustable stop-loss and take-profit levels for different market characteristics
- Strong visualization: Clear graphical interface showing all trading signals and risk control levels
- Simple logic: Easy to understand and maintain, suitable for both novice and professional traders
Strategy Risks
- Choppy market risk: May generate frequent false signals in ranging markets
- Slippage impact: Actual execution prices may differ significantly from signal prices during volatile periods
- Fixed stop-loss risk: Preset tick values may not suit all market conditions
- Trend reversal risk: Potential for significant drawdowns at trend turning points
- Parameter optimization risk: Over-optimization may lead to poor real-world performance
Optimization Directions
- Incorporate volatility indicators: Dynamically adjust stop-loss and take-profit levels based on ATR
- Add volume confirmation: Use trading volume as a supplementary signal confirmation
- Optimize entry timing: Consider waiting for pullbacks after EMA crossovers
- Add trend strength filtering: Incorporate ADX or similar indicators to evaluate trend strength
- Improve stop-loss mechanism: Implement trailing stops or support/resistance-based intelligent stops
- Develop adaptive parameters: Dynamically adjust EMA periods based on market conditions
Summary
This strategy effectively captures market trends through the coordination of multiple EMA systems. Its comprehensive risk management mechanism and clear trading logic make it a practical trading tool. Through continuous optimization and improvement, the strategy can better adapt to different market environments, enhancing trading efficiency and stability. Traders are advised to conduct thorough backtesting and parameter optimization before live implementation, making appropriate adjustments based on market characteristics and individual risk preferences.
Source Pine Script
/*backtest
start: 2019-12-23 08:00:00
end: 2024-11-17 00:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("EMA Crossover with SL and TP Levels", overlay=true)
// Input settings for stop loss and take profit
slTicks = input.int(50, title="Stop Loss (ticks)", minval=1)
tpTicks = input.int(100, title="Take Profit (ticks)", minval=1)
// Input settings for moving averages
shortMAPeriod = input.int(21, title="Short MA Period")
longMAPeriod = input.int(50, title="Long MA Period")
thirdMAPeriod = input.int(200, title="Third MA Period")
// Calculate moving averages
shortMA = ta.ema(close, shortMAPeriod) // Short EMA (21-period)
longMA = ta.ema(close, longMAPeriod) // Long EMA (50-period)
thirdMA = ta.ema(close, thirdMAPeriod) // Third EMA (200-period)
// Detect crossovers for entry signals
bullishCross = ta.crossover(shortMA, longMA) and close > thirdMA
bearishCross = ta.crossunder(shortMA, longMA) and close < thirdMA
// Initialize variables for SL and TP
var float longSL = na
var float longTP = na
var float shortSL = na
var float shortTP = na
// Execute trades based on crossovers
if (bullishCross)
longSL := close - slTicks * syminfo.mintick
longTP := close + tpTicks * syminfo.mintick
strategy.entry("Long", strategy.long)
strategy.exit("Exit Long", "Long", stop=longSL, limit=longTP)
if (bearishCross)
shortSL := close + slTicks * syminfo.mintick
shortTP := close - tpTicks * syminfo.mintick
strategy.entry("Short", strategy.short)
strategy.exit("Exit Short", "Short", stop=shortSL, limit=shortTP)
// Plot the MAs
plot(shortMA, color=color.green, linewidth=2, title="21-period EMA")
plot(longMA, color=color.red, linewidth=2, title="50-period EMA")
plot(thirdMA, color=color.blue, linewidth=2, title="200-period EMA")
// Plot buy/sell signals
plotshape(series=bullishCross, title="Buy Signal", location=location.belowbar, color=color.green, style=shape.labelup, text="BUY", size=size.small, offset=-1)
plotshape(series=bearishCross, title="Sell Signal", location=location.abovebar, color=color.red, style=shape.labeldown, text="SELL", size=size.small, offset=-1)
// // Draw SL and TP lines for Long positions
// if (bullishCross)
// line.new(x1=bar_index, y1=longSL, x2=bar_index + 1, y2=longSL, color=color.red, width=2, style=line.style_dotted)
// line.new(x1=bar_index, y1=longTP, x2=bar_index + 1, y2=longTP, color=color.green, width=2, style=line.style_dotted)
// label.new(bar_index, longSL, text="Long SL", style=label.style_label_down, color=color.red, textcolor=color.white, size=size.small)
// label.new(bar_index, longTP, text="Long TP", style=label.style_label_up, color=color.green, textcolor=color.white, size=size.small)
// // Draw SL and TP lines for Short positions
// if (bearishCross)
// line.new(x1=bar_index, y1=shortSL, x2=bar_index + 1, y2=shortSL, color=color.red, width=2, style=line.style_dotted)
// line.new(x1=bar_index, y1=shortTP, x2=bar_index + 1, y2=shortTP, color=color.green, width=2, style=line.style_dotted)
// label.new(bar_index, shortSL, text="Short SL", style=label.style_label_down, color=color.red, textcolor=color.white, size=size.small)
// label.new(bar_index, shortTP, text="Short TP", style=label.style_label_up, color=color.green, textcolor=color.white, size=size.small)
Published by ChaoZhang on the FMZ strategy square · last modified 2024-11-18. Collected in the Quant Trading Vault. Unvalidated research: re-test with your own data and costs before trusting it.
Build this with AgenKit
A spec is not a trading system. Paste this into your coding agent and AgenKit runs a specialist team — architecture, test-first build, risk gates, review — with paper trading on by default.
/agenkit build a production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/Multi-EMA-Crossover-Trend-Following-Strategy-with-Dynamic-Stop-Loss-and-Take-Profit-Optimization.md- npx agenkit install engineering-kit in your project
- Paste the command into your agent and approve each phase
- Get tests, backtest, risk controls and a paper-trading switch
Understand it first
Before you run this, read the lesson behind the idea and how to backtest it honestly. Specs like this one are the survivors of an unknown number of trials; deflate their results accordingly.
Related specs
- Dynamic Dual EMA Trend Following StrategyPine Script · Trend following · 1d
- Fixed Range Volume Profile And Anchored VWAP Trend Identification Strategy With Dynamic Stop LossPine Script · Trend following · 1h
- Multi Exponential Moving Average Crossover Strategy with Volume Based ATR Dynamic Stop Loss OptimizationPine Script · Trend following · 2d
- Multi Factor Trend Price Action Strategy with Dynamic Risk Management SystemPine Script · Trend following · 2h
- Multi Period Moving Average Ribbon and MACD Crossover Strategy SystemPine Script · Trend following · 1h
- Multi Period Trend Following with Volume Confirmation StrategyPine Script · Trend following · 5d