
Chiến lược giữ vị trí động chéo chéo là một chiến lược giao dịch định lượng dựa trên hai tín hiệu chéo trung bình di chuyển đơn giản (SMA) với hai chu kỳ khác nhau. Chiến lược này sử dụng chéo trung bình di chuyển ngắn hạn và dài hạn để đánh giá xu hướng thị trường và điều chỉnh hướng giữ vị trí dựa trên tín hiệu chéo và động lực của giá cả với đường trung bình dài hạn. Chiến lược hoạt động trên biểu đồ mặt trời, có thể điều chỉnh độ nhạy và tốc độ phản ứng của chiến lược một cách linh hoạt bằng cách đặt các tham số trung bình di chuyển khác nhau.
Chiến lược giữ vị trí động giao dịch chéo song song là một phương pháp giao dịch định lượng cổ điển và thực tế để nắm bắt xu hướng thị trường bằng cách nắm bắt tín hiệu giao dịch chéo song song và điều chỉnh động theo hướng giữ vị trí. Chiến lược này đơn giản, dễ hiểu, hoàn toàn tự động, có khả năng theo dõi xu hướng tốt và linh hoạt. Tuy nhiên, chiến lược cũng có rủi ro tiềm ẩn như thị trường không hoạt động tốt, tín hiệu chậm trễ.
/*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)