
쌍평평선 교차 동적 포지션 전략은 두 개의 다른 주기 간단한 이동 평균 (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)