Strategy vault / Trend following
EMA Crossover Strategy with Dynamic Trailing Stop Loss
This strategy is a trend-following trading system based on the 68-period Exponential Moving Average (EMA) combined with a dynamic stop-loss mechanism. It identifies market trends through price-EMA crossovers while…

Overview
This strategy is a trend-following trading system based on the 68-period Exponential Moving Average (EMA) combined with a dynamic stop-loss mechanism. It identifies market trends through price-EMA crossovers while employing initial and trailing stop-losses for risk management, enabling robust trading in trending markets.
Strategy Principles
The strategy utilizes a 68-period EMA as its core indicator for trend determination. Long positions are initiated when price crosses above the EMA, while short positions are taken when price crosses below. Risk management is implemented through a dual stop-loss mechanism: an initial stop-loss and a trailing stop-loss. The initial stop-loss is set at 20 points from the entry price, and when price moves favorably beyond the initial stop-loss distance, the stop-loss price adjusts by 10 points to lock in partial profits.
Strategy Advantages
- Strong trend-following capability: 68-period EMA effectively filters market noise and captures medium to long-term trends.
- Comprehensive risk control: Dual stop-loss mechanism protects capital while securing profits.
- Flexible parameters: EMA period and stop-loss points can be adjusted according to different market characteristics.
- Clear strategy logic: Well-defined entry and exit conditions facilitate live trading and monitoring.
- High automation level: Strategy can be fully automated, reducing manual intervention.
Strategy Risks
Sideways market risk: Frequent stop-losses may be triggered in range-bound markets. Recommended solution: Add trend confirmation indicators like ADX.
Gap risk: Large market gaps may cause actual stop-loss prices to deviate from expected levels. Recommended solution: Consider options hedging or position size adjustment.
Parameter optimization risk: Over-optimization may lead to strategy failure. Recommended solution: Implement out-of-sample testing to ensure parameter stability.
Strategy Optimization Directions
Trend confirmation mechanism: Suggest incorporating trend strength indicators (such as ADX, MACD) to improve trend identification accuracy.
Dynamic parameter adjustment: Automatically adjust EMA period and stop-loss parameters based on market volatility.
Position management optimization: Introduce volatility-based dynamic position sizing system.
Multi-timeframe correlation: Incorporate longer-term trend analysis to improve trade direction accuracy.
Summary
This strategy builds a complete trading system by combining EMA trend following with dynamic stop-loss management. Its core strengths lie in clear trading logic and comprehensive risk control mechanisms. Through the suggested optimization directions, the strategy's stability and profitability can be further enhanced. The strategy is suitable for medium to long-term investors seeking steady returns.
Source Pine Script
/*backtest
start: 2024-10-01 00:00:00
end: 2025-02-18 08:00:00
period: 2d
basePeriod: 2d
exchanges: [{"eid":"Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("EMA 68 with Trailing Stop-Loss", overlay=true)
// Inputs for customization
length_ema = input(68, title="EMA Length")
initial_stop_loss_points = input(20, title="Initial Stop Loss in Points")
trail_distance = input(10, title="Trailing Stop Adjustment in Points")
ema68 = ta.ema(close, length_ema)
// Plot EMA
plot(ema68, color=color.blue, title="68-Day EMA")
var float entry_price = na // Store entry price
var bool is_long = false // Track if we are in a long trade
var bool is_short = false // Track if we are in a short trade
// Buy Condition: Close above 68-day EMA
if ta.crossover(close, ema68)
strategy.entry("Long", strategy.long)
entry_price := close
is_long := true
is_short := false
// Sell Condition: Close below 68-day EMA
if ta.crossunder(close, ema68)
strategy.entry("Short", strategy.short)
entry_price := close
is_long := false
is_short := true
// Long Exit Conditions
if is_long
stop_loss = entry_price - initial_stop_loss_points
trail_price = entry_price + initial_stop_loss_points
if close >= trail_price
stop_loss := entry_price + trail_distance
strategy.exit("LongExit", "Long", stop=stop_loss, when=close < ema68)
// Short Exit Conditions
if is_short
stop_loss = entry_price + initial_stop_loss_points
trail_price = entry_price - initial_stop_loss_points
if close <= trail_price
stop_loss := entry_price - trail_distance
strategy.exit("ShortExit", "Short", stop=stop_loss, when=close > ema68)
Published by ianzeng123 on the FMZ strategy square · last modified 2025-02-20. 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/EMA-Crossover-Strategy-with-Dynamic-Trailing-Stop-Loss.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
- Dual EMA Crossover Oscillation Tracking StrategyPine Script · Trend following · 5m
- EMA SMA Multi Indicator Comprehensive Trend Following StrategyPine Script · Trend following · 1h
- Multi Indicator Dynamic Trend Following Strategy Based on EMA and SMAPine Script · Trend following · 1d
- Super Trend Triple StrategyPine Script · Trend following · 1h
- Dual EMA Crossover Trend Following Strategy with ATR and ADX FiltersPine Script · Trend following · 1d
- Dynamic Trend Identification Strategy with Exponential Moving Average and Adaptive Volatility ThresholdPine Script · Trend following · 1h