
Chiến lược này là một hệ thống giao dịch theo dõi xu hướng dựa trên nhiều chỉ số trung bình di chuyển (EMA) và chỉ số sóng thực (ATR). Chiến lược này xác nhận hướng xu hướng bằng cách đánh giá hình dạng xếp hàng của nhiều đường ngang, tìm cơ hội mua lại trong xu hướng tăng và sử dụng ATR để thiết lập mục tiêu dừng lỗ và lợi nhuận. Phương pháp này đảm bảo sự ổn định của việc theo dõi xu hướng và thông qua ATR, khả năng thích ứng động với biến động của thị trường.
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ó cấu trúc toàn diện, logic nghiêm ngặt. Sự kết hợp của xác nhận xu hướng, thu hồi vào thị trường và quản lý rủi ro động ATR bằng nhiều đường trung bình đảm bảo sự ổn định của chiến lược và có khả năng thích ứng tốt. Mặc dù có một số rủi ro vốn có, nhưng hướng tối ưu hóa được đề xuất có thể nâng cao hơn nữa sự ổn định và khả năng sinh lợi của chiến lược.
/*backtest
start: 2019-12-23 08:00:00
end: 2024-11-27 00:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("EMA Crossover and ATR Target Strategy", overlay=true)
// Input parameters
emaShortLength = 20
emaMidLength1 = 50
emaMidLength2 = 100
emaLongLength = 200
atrLength = 14
// Calculate EMAs
ema20 = ta.ema(close, emaShortLength)
ema50 = ta.ema(close, emaMidLength1)
ema100 = ta.ema(close, emaMidLength2)
ema200 = ta.ema(close, emaLongLength)
ema21 = ta.ema(close, 21)
// Calculate ATR
atr = ta.atr(atrLength)
// Conditions for the strategy
emaCondition = ema20 > ema50 and ema50 > ema100 and ema100 > ema200
pullbackCondition = close <= ema21 and close >= ema50 //and close >= ema21 * 0.99 // Near 21 EMA (within 1%)
// Initialize variables for stop loss and take profitss
var float stopLossLevel = na
var float takeProfitLevel = na
// Check conditions on each bar close
if (bar_index > 0) // Ensures there is data to check
if emaCondition and pullbackCondition and strategy.position_size == 0 // Only buy if no open position
stopLossLevel := close - (1.5 * atr) // Set stop loss based on ATR at buy price
takeProfitLevel := close + (3.5 * atr) // Set take profit based on ATR at buy price
strategy.entry("Buy", strategy.long)
// Set stop loss and take profit for the active trade
if strategy.position_size > 0
strategy.exit("Take Profit", from_entry="Buy", limit=takeProfitLevel, stop=stopLossLevel)
// Plot EMAs for visualizationn
plot(ema20, color=color.blue, title="20 EMA")
plot(ema50, color=color.red, title="50 EMA")
plot(ema100, color=color.green, title="100 EMA")
plot(ema200, color=color.orange, title="200 EMA")
plot(ema21, color=color.purple, title="21 EMA")