OPEN-SOURCE SCRIPT
RSI LE Helper SL & Targets (stable v5)

//version=5
indicator("RSI LE Helper SL & Targets (stable v5)", overlay=true)
// ---- Inputs ----
lenRSI = input.int(14, "RSI Length")
rsiBuyLvl = input.int(30, "RSI Cross Above Level")
atrMultSL = input.float(1.5, "ATR SL x", step=0.1)
pivotLeft = input.int(2, "Pivot Left")
pivotRight = input.int(2, "Pivot Right")
bufPct = input.float(0.006, "SL Buffer % (0.6%)", step=0.001)
showEMATrl = input.bool(true, "Show EMA-20 Trail")
// ---- Signal (example rule: RSI crosses up from oversold) ----
rsiVal = ta.rsi(close, lenRSI)
longSig = ta.crossover(rsiVal, rsiBuyLvl)
// ---- Persistent vars to hold last computed values (optional) ----
var float entry = na
var float sl = na
var float t1 = na
var float t2 = na
var float t3 = na
if longSig
// Entry = close of signal bar
entry := close
// --- SL candidates ---
float pivLow = ta.pivotlow(low, pivotLeft, pivotRight) // may be na on the signal bar
float sl_pivot = na(pivLow) ? na : (pivLow * (1 - bufPct))
float atrVal = ta.atr(14)
float sl_atr = close - atrMultSL * atrVal
// --- choose stricter (higher) SL with NA safety (if/else only) ---
if na(sl_pivot) and na(sl_atr)
sl := na
else if na(sl_pivot)
sl := sl_atr
else if na(sl_atr)
sl := sl_pivot
else
sl := math.max(sl_pivot, sl_atr)
// --- Targets (R multiples) ---
float rRisk = (na(entry) or na(sl)) ? na : (entry - sl)
if na(rRisk)
t1 := na
t2 := na
t3 := na
else
t1 := entry + 1.0 * rRisk
t2 := entry + 1.5 * rRisk
t3 := entry + 2.0 * rRisk
// --- Labels on the signal bar ---
if not na(entry)
label.new(bar_index, entry, "ENTRY", style=label.style_label_up, textcolor=color.white)
if not na(sl)
label.new(bar_index, sl, "SL", style=label.style_label_down, textcolor=color.white)
if not na(t1)
label.new(bar_index, t1, "T1 (1R)", style=label.style_label_up, textcolor=color.white)
if not na(t2)
label.new(bar_index, t2, "T2 (1.5R)", style=label.style_label_up, textcolor=color.white)
if not na(t3)
label.new(bar_index, t3, "T3 (2R)", style=label.style_label_up, textcolor=color.white)
// ---- Optional EMA-20 trail ----
ema20 = ta.ema(close, 20)
plot(showEMATrl ? ema20 : na, title="EMA-20", linewidth=1)
indicator("RSI LE Helper SL & Targets (stable v5)", overlay=true)
// ---- Inputs ----
lenRSI = input.int(14, "RSI Length")
rsiBuyLvl = input.int(30, "RSI Cross Above Level")
atrMultSL = input.float(1.5, "ATR SL x", step=0.1)
pivotLeft = input.int(2, "Pivot Left")
pivotRight = input.int(2, "Pivot Right")
bufPct = input.float(0.006, "SL Buffer % (0.6%)", step=0.001)
showEMATrl = input.bool(true, "Show EMA-20 Trail")
// ---- Signal (example rule: RSI crosses up from oversold) ----
rsiVal = ta.rsi(close, lenRSI)
longSig = ta.crossover(rsiVal, rsiBuyLvl)
// ---- Persistent vars to hold last computed values (optional) ----
var float entry = na
var float sl = na
var float t1 = na
var float t2 = na
var float t3 = na
if longSig
// Entry = close of signal bar
entry := close
// --- SL candidates ---
float pivLow = ta.pivotlow(low, pivotLeft, pivotRight) // may be na on the signal bar
float sl_pivot = na(pivLow) ? na : (pivLow * (1 - bufPct))
float atrVal = ta.atr(14)
float sl_atr = close - atrMultSL * atrVal
// --- choose stricter (higher) SL with NA safety (if/else only) ---
if na(sl_pivot) and na(sl_atr)
sl := na
else if na(sl_pivot)
sl := sl_atr
else if na(sl_atr)
sl := sl_pivot
else
sl := math.max(sl_pivot, sl_atr)
// --- Targets (R multiples) ---
float rRisk = (na(entry) or na(sl)) ? na : (entry - sl)
if na(rRisk)
t1 := na
t2 := na
t3 := na
else
t1 := entry + 1.0 * rRisk
t2 := entry + 1.5 * rRisk
t3 := entry + 2.0 * rRisk
// --- Labels on the signal bar ---
if not na(entry)
label.new(bar_index, entry, "ENTRY", style=label.style_label_up, textcolor=color.white)
if not na(sl)
label.new(bar_index, sl, "SL", style=label.style_label_down, textcolor=color.white)
if not na(t1)
label.new(bar_index, t1, "T1 (1R)", style=label.style_label_up, textcolor=color.white)
if not na(t2)
label.new(bar_index, t2, "T2 (1.5R)", style=label.style_label_up, textcolor=color.white)
if not na(t3)
label.new(bar_index, t3, "T3 (2R)", style=label.style_label_up, textcolor=color.white)
// ---- Optional EMA-20 trail ----
ema20 = ta.ema(close, 20)
plot(showEMATrl ? ema20 : na, title="EMA-20", linewidth=1)
Mã nguồn mở
Theo đúng tinh thần TradingView, người tạo ra tập lệnh này đã biến tập lệnh thành mã nguồn mở để các nhà giao dịch có thể xem xét và xác minh công năng. Xin dành lời khen tặng cho tác giả! Mặc dù bạn có thể sử dụng miễn phí, nhưng lưu ý nếu đăng lại mã, bạn phải tuân theo Quy tắc nội bộ của chúng tôi.
Thông báo miễn trừ trách nhiệm
Thông tin và ấn phẩm không có nghĩa là và không cấu thành, tài chính, đầu tư, kinh doanh, hoặc các loại lời khuyên hoặc khuyến nghị khác được cung cấp hoặc xác nhận bởi TradingView. Đọc thêm trong Điều khoản sử dụng.
Mã nguồn mở
Theo đúng tinh thần TradingView, người tạo ra tập lệnh này đã biến tập lệnh thành mã nguồn mở để các nhà giao dịch có thể xem xét và xác minh công năng. Xin dành lời khen tặng cho tác giả! Mặc dù bạn có thể sử dụng miễn phí, nhưng lưu ý nếu đăng lại mã, bạn phải tuân theo Quy tắc nội bộ của chúng tôi.
Thông báo miễn trừ trách nhiệm
Thông tin và ấn phẩm không có nghĩa là và không cấu thành, tài chính, đầu tư, kinh doanh, hoặc các loại lời khuyên hoặc khuyến nghị khác được cung cấp hoặc xác nhận bởi TradingView. Đọc thêm trong Điều khoản sử dụng.