该策略基于两条移动平均线(EMA)的交叉来产生交易信号。当短期EMA(20日)从下向上穿过长期EMA(50日)时,产生买入信号;当短期EMA从上向下穿过长期EMA时,产生卖出信号。同时,该策略还绘制了一条200日EMA作为长期趋势的参考。该策略的主要思路是利用不同周期移动平均线的交叉来捕捉市场趋势的转变,从而进行交易。
EMA双均线交叉策略是一个简单易懂、适合趋势市场的交易策略。它利用了短期和长期移动平均线的交叉来捕捉市场趋势的转变,同时引入了长期趋势参考。尽管该策略存在一些局限性,如在震荡市场中表现不佳,以及移动平均线的滞后性,但通过引入其他指标、优化参数、加入风控措施等方式,可以进一步提升策略的稳健性和盈利能力。
/*backtest
start: 2023-03-23 00:00:00
end: 2024-03-28 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("EMA Crossover Strategy by Peter Gangmei", overlay=true)
// Define the length for moving averages
short_ma_length = input.int(20, "Short MA Length")
long_ma_length = input.int(50, "Long MA Length")
long_ma_200_length = input.int(200, "Long MA 200 Length")
// Define start time for testing
start_time = timestamp(2024, 01, 01, 00, 00)
// Calculate current date and time
current_time = timenow
// Calculate moving averages
ema20 = ta.ema(close, short_ma_length)
ema50 = ta.ema(close, long_ma_length)
ema200 = ta.ema(close, long_ma_200_length)
// Crossing conditions
crossed_above = ta.crossover(ema20, ema50)
crossed_below = ta.crossunder(ema20, ema50)
// Entry and exit conditions within the specified time frame
if true
if (crossed_above)
strategy.entry("Buy", strategy.long)
alert("Buy Condition", alert.freq_once_per_bar_close)
if (crossed_below)
strategy.entry("Sell", strategy.short)
alert("Sell Condition", alert.freq_once_per_bar_close)
// Plotting moving averages for visualization
plot(ema20, color=color.green, title="EMA20")
plot(ema50, color=color.red, title="EMA50")
plot(ema200, color=color.blue, title="EMA200")
// Placing buy and sell markers
plotshape(series=crossed_above, style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small, title="Buy Signal")
plotshape(series=crossed_below, style=shape.triangledown, location=location.abovebar, color=color.red, size=size.small, title="Sell Signal")