双均线交叉动态持仓策略是一种基于两条不同周期简单移动平均线(SMA)交叉信号进行交易的量化交易策略。该策略利用短期与长期移动平均线的交叉来判断市场趋势,并根据交叉信号和价格与长期均线的关系动态调整持仓方向。策略在日线图上运行,通过设置不同的移动平均线参数可以灵活调整策略的灵敏度和反应速度。
双均线交叉动态持仓策略是一种经典而实用的量化交易方法,通过捕捉均线交叉信号和动态调整持仓方向来把握市场趋势。该策略简单易懂、全自动化,具有较好的趋势跟踪能力和灵活性。然而,策略也存在震荡市表现不佳、信号滞后等潜在风险。通过引入其他技术指标、优化参数选择、加入止损机制等方式,可以进一步提升策略的稳定性和盈利能力。交易者在使用该策略时,需要根据具体交易品种和市场环境进行适当的参数调整和风险管理,以实现长期稳定的交易效果。
/*backtest
start: 2024-06-29 00:00:00
end: 2024-07-29 00:00:00
period: 2h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy(title="MA Cross Backtest", overlay=true, default_qty_type=strategy.cash, default_qty_value=10)
// Parâmetros das Médias Móveis
shortlen = input.int(9, "Short MA Length", minval=1)
longlen = input.int(21, "Long MA Length", minval=1)
// Cálculo das Médias Móveis
short = ta.sma(close, shortlen)
long = ta.sma(close, longlen)
// Plotagem das Médias Móveis
plot(short, color=color.orange, title="Short MA")
plot(long, color=color.green, title="Long MA")
// Sinal de Compra baseado no cruzamento das médias móveis
buySignal = ta.crossover(short, long)
// Sinal de Venda (Short) baseado no cruzamento das médias móveis
sellSignal = ta.crossunder(short, long)
// Plotagem dos Sinais de Compra e Venda
plotshape(series=buySignal, location=location.belowbar, color=color.blue, style=shape.labelup, text="Buy", title="Buy Signal")
plotshape(series=sellSignal, location=location.abovebar, color=color.red, style=shape.labeldown, text="Sell", title="Sell Signal")
// Condições para alertas
alertcondition(buySignal, title="Buy Signal", message="MA Cross Buy Signal")
alertcondition(sellSignal, title="Sell Signal", message="MA Cross Sell Signal")
// Lógica da Estratégia de Backtest
if (buySignal)
// Se não há posição aberta ou se a posição atual é curta, feche a posição curta antes de abrir uma nova posição longa
if (strategy.position_size < 0)
strategy.close("Short", comment="Closing Short Position before Long Entry")
strategy.entry("Long", strategy.long)
// Alerta de compra
alert("MA Cross Buy Signal", alert.freq_once_per_bar_close)
if (strategy.position_size > 0)
// Se o preço abrir abaixo da média longa
if (open < long)
strategy.close("Long", comment="Price Opened Below Long MA")
strategy.entry("Short", strategy.short, comment="Switched to Short")
// Alerta de venda
alert("Price Opened Below Long MA - Switched to Short", alert.freq_once_per_bar_close)
// Se a média móvel curta cruzar abaixo da média móvel longa
else if (sellSignal)
strategy.close("Long", comment="Short MA Crossed Below Long MA")
strategy.entry("Short", strategy.short, comment="Switched to Short")
// Alerta de venda
alert("Short MA Crossed Below Long MA - Switched to Short", alert.freq_once_per_bar_close)
if (strategy.position_size < 0)
// Se o preço abrir acima da média longa
if (open > long)
strategy.close("Short", comment="Price Opened Above Long MA")
strategy.entry("Long", strategy.long, comment="Switched to Long")
// Alerta de compra
alert("Price Opened Above Long MA - Switched to Long", alert.freq_once_per_bar_close)