This strategy is a dynamic trend following system based on dual EMA crossover signals, which identifies market trend changes through the crossover of short-term 20-day Exponential Moving Average (EMA) and long-term 50-day EMA, executing buy and sell operations automatically. The strategy employs mature technical analysis methods, combining trend following with dynamic position management, suitable for markets with significant volatility.
The core logic of the strategy is based on the following key elements:
This strategy is a modern implementation of a classic trend following system, systematizing and standardizing the traditional dual EMA crossover strategy through programmatic trading. While inherent risks exist, the strategy has good application prospects through continuous optimization and improvement. It is recommended to conduct thorough parameter optimization and backtesting before live trading.
/*backtest start: 2019-12-23 08:00:00 end: 2025-01-04 08:00:00 period: 1d basePeriod: 1d exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}] */ //@version=5 strategy("EMA Crossover Buy/Sell Signals", overlay=true) // Input parameters for EMAs emaShortLength = input.int(20, title="Short EMA Length") emaLongLength = input.int(50, title="Long EMA Length") // Calculating EMAs emaShort = ta.ema(close, emaShortLength) emaLong = ta.ema(close, emaLongLength) // Plotting EMA crossover lines plot(emaShort, color=color.green, title="20 EMA") plot(emaLong, color=color.red, title="50 EMA") // Buy and Sell signal logic longCondition = ta.crossover(emaShort, emaLong) exitLongCondition = ta.crossunder(emaShort, emaLong) shortCondition = ta.crossunder(emaShort, emaLong) exitShortCondition = ta.crossover(emaShort, emaLong) // Plot buy and sell signals on the chart plotshape(series=longCondition, location=location.belowbar, color=color.green, style=shape.labelup, title="Buy Signal") plotshape(series=exitLongCondition, location=location.abovebar, color=color.red, style=shape.labeldown, title="Sell Exit") plotshape(series=shortCondition, location=location.abovebar, color=color.red, style=shape.labeldown, title="Sell Signal") plotshape(series=exitShortCondition, location=location.belowbar, color=color.green, style=shape.labelup, title="Buy Exit") // Backtesting strategy logic var float entryPrice = na var int position = 0 // 1 for long, -1 for short, 0 for no position if (longCondition and position == 0) entryPrice := close position := 1 if (shortCondition and position == 0) entryPrice := close position := -1 if (exitLongCondition and position == 1) strategy.exit("Exit Long", from_entry="Long", limit=close) position := 0 if (exitShortCondition and position == -1) strategy.exit("Exit Short", from_entry="Short", limit=close) position := 0 if (longCondition) strategy.entry("Long", strategy.long) if (shortCondition) strategy.entry("Short", strategy.short)