
This strategy uses the crossover of a fast EMA (9-period) and slow EMA (21-period) as entry signals, and incorporates a trailing stop loss to lock in profits and avoid excessive drawdowns.
When the fast EMA crosses above the slow EMA from below, a buy signal is generated. When the fast EMA crosses below the slow EMA from above, a sell signal is triggered.
Once entered, the strategy tracks the highest high in real-time and triggers a trailing stop loss when the current price falls 2% below the highest high, locking in profits.
Risk Solutions:
This strategy integrates the advantages of trend identification and risk control. Through parameter tuning and optimization, it can be adapted to different market types and trading instruments, and is worth further testing and practice.
/*backtest
start: 2023-12-12 00:00:00
end: 2023-12-19 00:00:00
period: 1m
basePeriod: 1m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=4
strategy("EMA Crossover with Trailing Stop-Loss", overlay=true)
fastEMA = ema(close, 9)
slowEMA = ema(close, 21)
// Entry conditions
longCondition = crossover(fastEMA, slowEMA)
shortCondition = crossunder(fastEMA, slowEMA)
// Trailing stop-loss calculation
var float trailingStop = na
var float highestHigh = na
if (longCondition)
highestHigh := na
trailingStop := na
if (longCondition and high > highestHigh)
highestHigh := high
if (strategy.position_size > 0)
trailingStop := highestHigh * (1 - 0.02) // Adjust the trailing percentage as needed
// Execute trades
strategy.entry("Long", strategy.long, when=longCondition)
strategy.entry("Short", strategy.short, when=shortCondition)
// Apply trailing stop-loss to long positions
strategy.exit("Long", from_entry="Long", loss=trailingStop)
// Plot EMAs and Trailing Stop-Loss
plot(fastEMA, color=color.green, title="Fast EMA")
plot(slowEMA, color=color.red, title="Slow EMA")
plot(trailingStop, color=color.orange, title="Trailing Stop-Loss", linewidth=2)