Strategies
高效趋势捕捉型指数移动平均线交叉与动态追踪止损策略-Advanced-Trend-Capture-EMA-Crossover-Strategy-with-Dynamic-Trailing-Stop
ianzeng123 · PineScript · Trend Following · 2025-04-08 10:23:52
Trend Following
High VolatilityLow VolatilityDowntrendHigh Volatility · AvoidLow Volatility · AvoidRange · Inferred
Filter
Sizing
Risk control
Exit
—
Context terms
No English write-up in the source (no author description).
Source code (81 lines)
/*backtest
start: 2025-03-08 00:00:00
end: 2025-04-07 00:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Futures_Binance","currency":"ETH_USDT"}]
*/
//@version=6
strategy("EMA Crossover (New Trailing Stop)", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=20, slippage=5)
// Define EMA and SMA lengths
shortEMALength = 13
midEMALength = 25
longEMALength = 33
sma100Length = 100
sma200Length = 200
// Calculate EMAs
shortEMA = ta.ema(close, shortEMALength)
midEMA = ta.ema(close, midEMALength)
longEMA = ta.ema(close, longEMALength)
// Calculate SMAs
sma100 = ta.sma(close, sma100Length)
sma200 = ta.sma(close, sma200Length)
// Plot EMAs and SMAs
plot(shortEMA, title="13 EMA", color=color.blue)
plot(midEMA, title="25 EMA", color=color.red)
plot(longEMA, title="33 EMA", color=color.green)
plot(sma100, title="100 SMA", color=color.purple)
plot(sma200, title="200 SMA", color=color.orange)
// ENTRY CONDITIONS
longCondition = shortEMA >= longEMA and strategy.position_size <= 0
shortCondition = shortEMA <= longEMA and strategy.position_size >= 0
// EXIT CONDITIONS
exitLong = shortEMA < longEMA // Exit long when 13 EMA falls below 33 EMA
exitShort = shortEMA > midEMA // Exit short when 13 EMA rises above 25 EMA
// Flag to track if an exit has been processed
var bool isExiting = false
// EXECUTE LONG
if (longCondition and not isExiting)
strategy.close("Short", comment="Close Short for Long Entry")
strategy.entry("Long", strategy.long, alert_message="FAST Long Entry: 13 EMA >= 33 EMA")
// EXECUTE SHORT
if (shortCondition and not isExiting)
strategy.close("Long", comment="Close Long for Short Entry")
strategy.entry("Short", strategy.short, alert_message="FAST Short Entry: 13 EMA <= 33 EMA")
// Trailing Stop Parameters
trailOffsetPts = 2
trail = 10
// Trailing Stop for Longs
if (strategy.position_size > 0 and not isExiting)
strategy.exit("Long Trail Exit", from_entry="Long", trail_offset=trailOffsetPts, trail_price=high - trail, comment="Long Trailing Stop")
isExiting := true
// Trailing Stop for Shorts
if (strategy.position_size < 0 and not isExiting)
strategy.exit("Short Trail Exit", from_entry="Short", trail_offset=trailOffsetPts, trail_price=low + trail, comment="Short Trailing Stop")
isExiting := true
// EXIT STRATEGY
if (exitLong and not isExiting)
strategy.close("Long", comment="Exit Long: 13 EMA < 33 EMA")
isExiting := true
if (exitShort and not isExiting)
strategy.close("Short", comment="Exit Short: 13 EMA > 25 EMA")
isExiting := true
// Reset the exit flag at the end of each bar
if (barstate.isconfirmed)
isExiting := false