OPEN-SOURCE SCRIPT

Gold Trading RSI

//version=6
strategy("Gold Trading RSI", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=10)

// Cấu hình tham số đầu vào
rsi_length = input.int(14, title="RSI Period") // Độ dài RSI
rsi_overbought = input.int(70, title="RSI Overbought Level") // Mức quá mua
rsi_oversold = input.int(30, title="RSI Oversold Level") // Mức quá bán
sl_percent = input.float(1.0, title="Stop Loss (%)") / 100 // Cắt lỗ theo %
tp_percent = input.float(2.0, title="Take Profit (%)") / 100 // Chốt lời theo %

capital = strategy.equity // Vốn hiện tại

// Tính RSI trên khung M5
rsi_m5 = ta.rsi(close, rsi_length)

// Lấy giờ hiện tại theo múi giờ của biểu đồ
current_hour = hour(time)

// Giới hạn giao dịch trong khung giờ 6h - 17h
is_trading_time = current_hour >= 6 and current_hour < 17

// Điều kiện vào lệnh
long_condition = is_trading_time and rsi_m5 < rsi_oversold
short_condition = is_trading_time and rsi_m5 > rsi_overbought

// Tính toán SL/TP
sl_long = close * (1 - sl_percent)
tp_long = close * (1 + tp_percent)

sl_short = close * (1 + sl_percent)
tp_short = close * (1 - tp_percent)

// Vào lệnh
if (long_condition)
strategy.entry("Buy", strategy.long)
strategy.exit("Exit Buy", from_entry="Buy", stop=sl_long, limit=tp_long)

if (short_condition)
strategy.entry("Sell", strategy.short)
strategy.exit("Exit Sell", from_entry="Sell", stop=sl_short, limit=tp_short)

Disclaimer