
The Dynamic Position Dual Moving Average Crossover Strategy is a quantitative trading approach that utilizes the crossover signals of two Simple Moving Averages (SMAs) with different periods to execute trades. This strategy leverages the crossover of short-term and long-term moving averages to determine market trends and dynamically adjusts position direction based on crossover signals and the relationship between price and the long-term average. The strategy operates on a daily timeframe and allows for flexibility in sensitivity and reaction speed through adjustable moving average parameters.
The Dynamic Position Dual Moving Average Crossover Strategy is a classic and practical quantitative trading method that captures market trends by leveraging MA crossover signals and dynamically adjusting positions. This strategy is simple to understand, fully automatable, and demonstrates good trend-following capabilities with flexibility. However, it also faces potential risks such as poor performance in choppy markets and lagging signals. By incorporating additional technical indicators, optimizing parameter selection, and implementing stop-loss mechanisms, the strategy’s stability and profitability can be further enhanced. Traders employing this strategy should adjust parameters and manage risks according to specific trading instruments and market environments to achieve long-term, stable trading results.
/*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)