
Chiến lược này là một hệ thống giao dịch theo dõi xu hướng kết hợp các đường trung bình di chuyển chỉ số (EMA) và đường trung bình di chuyển đơn giản (SMA). Chiến lược chủ yếu sử dụng EMA50 và EMA150 để tạo ra tín hiệu giao dịch, đồng thời sử dụng SMA150 làm đường dừng lỗ và bao gồm cơ chế nhập lại sau khi dừng lỗ. Thiết kế này có thể nắm bắt xu hướng trung hạn và dài hạn và kiểm soát rủi ro hiệu quả.
Logic cốt lõi của chiến lược bao gồm các yếu tố chính sau:
Đây là một chiến lược theo dõi xu hướng được thiết kế hợp lý, nắm bắt xu hướng thông qua đường chéo đồng nhất và được trang bị cơ chế kiểm soát rủi ro hoàn hảo. Ưu điểm chính của chiến lược là khả năng theo dõi xu hướng của hệ thống và thiết kế quản lý rủi ro, nhưng trong ứng dụng thực tế, cần chú ý đến ảnh hưởng của môi trường thị trường đối với hiệu suất chiến lược.
/*backtest
start: 2024-02-22 00:00:00
end: 2025-02-19 08:00:00
period: 1h
basePeriod: 1h
exchanges: [{"eid":"Binance","currency":"SOL_USDT"}]
*/
//@version=6
strategy("EMA 50 and EMA 150 with SMA150 Stop-loss and Re-Entry #ganges", overlay=true, commission_type=strategy.commission.percent, commission_value=0.1, slippage=3)
// EMA and SMA Calculations
ema50 = ta.ema(close, 50)
ema150 = ta.ema(close, 150)
sma150 = ta.sma(close, 150)
// Conditions for Buy, Sell, and Stop-Loss
ema50CrossAboveEMA150 = ta.crossover(ema50, ema150) // Buy signal
ema50CrossBelowEMA150 = ta.crossunder(ema50, ema150) // Sell signal
priceCrossAboveEMA150 = ta.crossover(close, ema150) // Price crosses EMA 150 from below
priceCloseBelowSMA150 = close < sma150 // Stop-loss for long positions
// Track stop-loss hit state
var bool stopLossHit = false
// Strategy Logic
// Buy Logic: EMA 50 crosses EMA 150 from below
if ema50CrossAboveEMA150
strategy.entry("Buy Signal", strategy.long, qty=1)
stopLossHit := false // Reset stop-loss state when a new buy position is opened
// Sell Logic: EMA 50 crosses EMA 150 from above
if ema50CrossBelowEMA150
strategy.entry("Sell Signal", strategy.short, qty=1)
stopLossHit := false // Reset stop-loss state when a new sell position is opened
// Stop-Loss for Long Positions: Close if price falls below SMA 150
if strategy.position_size > 0 and priceCloseBelowSMA150
strategy.close("Buy Signal")
stopLossHit := true // Mark stop-loss hit
// Re-Entry Logic After Stop-Loss
if stopLossHit
if priceCrossAboveEMA150 // Re-buy logic: PRICE crosses EMA 150 from below
strategy.entry("Re-Buy Signal", strategy.long, qty=1)
stopLossHit := false // Reset stop-loss state after re-entry
if ema50CrossBelowEMA150 // Re-sell logic: EMA 50 crosses EMA 150 from above
strategy.entry("Re-Sell Signal", strategy.short, qty=1)
stopLossHit := false // Reset stop-loss state after re-entry
// Plot EMA and SMA Lines
plot(ema50, color=color.blue, title="EMA 50")
plot(ema150, color=color.red, title="EMA 150")
plot(sma150, color=color.orange, title="SMA 150")
// // Calculate Recent All-Time High
// highestHigh = ta.highest(high, 500) // Lookback period of 500 bars
// percentageFall = ((highestHigh - close) / highestHigh) * 100
// // Display Percentage Fall on the Most Recent Candle Only
// isLastBar = bar_index == ta.max(bar_index)
// if isLastBar
// labelText = str.tostring(percentageFall, "#.##") + "% Fall from ATH"
// labelPosition = high + ta.atr(14) * 2 // Positioning label above the candle
// label.new(bar_index, labelPosition, labelText, color=color.red, textcolor=color.white, size=size.small, style=label.style_label_down)