
Chiến lược này là một hệ thống giao dịch định lượng toàn diện dựa trên chỉ số sức mạnh tương đối (RSI), đường trung bình động hàm mũ (EMA) và phạm vi thực trung bình (ATR). Chiến lược này sử dụng EMA để làm mịn RSI, kích hoạt giao dịch thông qua các tín hiệu đột phá RSI ở các mức quan trọng và sử dụng ATR để thiết lập mức dừng lỗ và chốt lời một cách linh hoạt nhằm kiểm soát rủi ro hiệu quả. Đồng thời, chiến lược này còn bao gồm chức năng đếm và ghi lại các tín hiệu giao dịch, giúp các nhà giao dịch kiểm tra lại và tối ưu hóa các chiến lược.
Logic cốt lõi của chiến lược bao gồm những phần chính sau:
Chiến lược này xây dựng một hệ thống giao dịch định lượng hoàn chỉnh bằng cách kết hợp ba chỉ báo kỹ thuật cổ điển: RSI, EMA và ATR. Chiến lược này có tính thực tiễn cao về mặt tạo tín hiệu, kiểm soát rủi ro và thực hiện giao dịch. Thông qua việc tối ưu hóa và cải tiến liên tục, chiến lược này dự kiến sẽ đạt được hiệu suất ổn định trong giao dịch thực tế. Tuy nhiên, người dùng cần chú ý đến tác động của môi trường thị trường đến hiệu suất chiến lược, thiết lập các thông số hợp lý và kiểm soát tốt rủi ro.
/*backtest
start: 2019-12-23 08:00:00
end: 2025-01-04 08:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=6
strategy("RSI Trading Strategy with EMA and ATR Stop Loss/Take Profit", overlay=true)
length = input.int(14, minval=1, title="RSI Length")
src = input(close, title="Source")
rsi = ta.rsi(src, length)
smoothingLength = input.int(14, minval=1, title="Smoothing Length")
smoothedRsi = ta.ema(rsi, smoothingLength) // استفاده از EMA برای صاف کردن RSI
atrLength = input.int(14, title="ATR Length")
atrMultiplier = input.float(1, title="ATR Multiplier")
atrValue = ta.atr(atrLength) // محاسبه ATR
level1 = 30
level2 = 70
// تنظیمات استراتژی
var table crossingTable = table.new(position.top_right, 2, 5, border_width=1)
var int crossCount = 0
var float crossPrice = na
// شرط ورود به معامله خرید زمانی که RSI از سطح 70 به بالا عبور میکند
if (ta.crossover(smoothedRsi, level2))
strategy.entry("Long", strategy.long)
// تنظیم حد سود و حد ضرر
strategy.exit("Take Profit/Stop Loss", "Long", stop=close - atrMultiplier * atrValue, limit=close + atrMultiplier * atrValue, comment="")
crossCount := crossCount + 1
crossPrice := close
// شرط ورود به معامله فروش زمانی که RSI از سطح 70 به پایین عبور میکند
if (ta.crossunder(smoothedRsi, level2))
strategy.entry("Short", strategy.short)
// تنظیم حد سود و حد ضرر
strategy.exit("Take Profit/Stop Loss", "Short", stop=close + atrMultiplier * atrValue, limit=close - atrMultiplier * atrValue, comment="")
crossCount := crossCount + 1
crossPrice := close
// شرط ورود به معامله خرید زمانی که RSI از سطح 30 به بالا عبور میکند
if (ta.crossover(smoothedRsi, level1))
strategy.entry("Long", strategy.long)
// تنظیم حد سود و حد ضرر
strategy.exit("Take Profit/Stop Loss", "Long", stop=close - atrMultiplier * atrValue, limit=close + atrMultiplier * atrValue, comment="")
crossCount := crossCount + 1
crossPrice := close
// شرط ورود به معامله فروش زمانی که RSI از سطح 30 به پایین عبور میکند
if (ta.crossunder(smoothedRsi, level1))
strategy.entry("Short", strategy.short)
// تنظیم حد سود و حد ضرر
strategy.exit("Take Profit/Stop Loss", "Short", stop=close + atrMultiplier * atrValue, limit=close - atrMultiplier * atrValue, comment="")
crossCount := crossCount + 1
crossPrice := close
if (not na(crossPrice))
table.cell(crossingTable, 0, crossCount % 5, text=str.tostring(crossCount), bgcolor=color.green)
table.cell(crossingTable, 1, crossCount % 5, text=str.tostring(crossPrice), bgcolor=color.green)
// ترسیم خطوط و مقادیر RSI
plot(smoothedRsi, title="Smoothed RSI", color=color.blue)
hline(level1, "Level 30", color=color.red)
hline(level2, "Level 70", color=color.green)