该策略是一个基于多重指数移动平均线(EMA)的自动交易系统,通过对5周期、20周期和50周期EMA的分层判断建立交易信号。系统设计独特之处在于采用了基于高点、低点和收盘价的多重EMA判断,并结合了动态止损和跟踪止盈机制,有效地控制风险的同时锁定盈利。
策略基于多重时间周期的EMA交叉和位置关系进行交易决策。具体来说: 1. 使用了5周期、20周期(分别基于最高价、最低价和收盘价)以及50周期的EMA 2. 建仓条件要求所有指标呈现明确的多头排列:50EMA < 20EMA(低点) < 20EMA(收盘) < 20EMA(高点) < 5EMA 3. 同时要求当前价格位于所有均线之上,确保强势趋势 4. 出场采用双重机制:当价格跌破5周期EMA时获利了结,或触及20周期低点EMA时止损
这是一个设计严谨的多重均线交易系统,通过分层过滤和动态止损有效控制风险。虽然可能会错过一些快速行情,但在trending market中表现稳定。建议根据不同市场特点适当调整参数,并考虑加入成交量等辅助指标提高可靠性。该策略适合追求稳健收益的中长期投资者使用。
/*backtest
start: 2019-12-23 08:00:00
end: 2024-12-03 00:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("Automated EMA Strategy with Hierarchical Conditions", overlay=true)
// Inputs for EMA lengths
length_5 = 5
length_20 = 20
length_50 = 50
// Calculating EMAs
ema_5 = ta.ema(close, length_5)
ema_20_high = ta.ema(high, length_20)
ema_20_low = ta.ema(low, length_20)
ema_20_close = ta.ema(close, length_20)
ema_50 = ta.ema(close, length_50)
// Buy condition: 50 EMA < 20 EMA (Close) < 20 EMA (High) < 20 EMA (Low) < 5 EMA
// and LTP above all EMAs
buy_condition = ema_50 < ema_20_low and ema_20_low < ema_20_close and ema_20_close < ema_20_high and ema_20_high < ema_5 and close > ema_5 and close > ema_20_close and close > ema_20_high and close > ema_20_low and close > ema_50
// Stop-loss and target levels
stop_loss = ema_20_low
// Target condition: Close below 5 EMA
target_condition = close < ema_5
// Check if there's an open position
is_in_position = strategy.position_size > 0
// Execute Buy Signal only if no position is open
if (buy_condition and not is_in_position)
strategy.entry("Buy", strategy.long)
// Exit conditions: Stop-loss or target (close below 5 EMA)
if (is_in_position and (target_condition or close < stop_loss))
strategy.close("Buy")
// Plotting the EMAs
plot(ema_5, color=color.blue, title="5 EMA")
plot(ema_20_high, color=color.green, title="20 EMA (High)")
plot(ema_20_low, color=color.red, title="20 EMA (Low)")
plot(ema_20_close, color=color.purple, title="20 EMA (Close)")
plot(ema_50, color=color.orange, title="50 EMA")