本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// =============================================================================
// APEX EDGE · RSI TREND LINES · v2.0 · 2026-09-29
// © Apex Edge. Original code.
// -----------------------------------------------------------------------------
// Trend lines drawn on RSI itself. Resistance joins lower RSI highs, support
// joins higher RSI lows. A line is only kept if RSI respected it between its
// two pivots. A break = a CLOSED candle with RSI beyond the line; the line
// then stops at the break and stays on the pane as history.
//
// NON-REPAINT: pivots are confirmed 'lookback' bars later (by definition) and
// every line, break and alert commits on closed candles only.
// =============================================================================
indicator("Apex Edge - RSI Trend Lines", "AE RSI TL", overlay = false, max_lines_count = 500, max_labels_count = 500)
// ============================================================================= INPUTS
const string G_R = "1 · RSI"
const string G_L = "2 · Trend lines"
const string G_V = "3 · Levels & look"
const string G_A = "4 · Alerts"
int rsiLen = input.int(14, "RSI length", minval = 2, group = G_R)
float src = input.source(close, "Source", group = G_R)
int lb = input.int(10, "Pivot lookback", minval = 2, maxval = 50, group = G_L, tooltip = "Bars each side needed to confirm an RSI pivot. A pivot is only known this many bars after it forms, so lines appear with that delay. Lower = more, earlier lines; higher = fewer, stronger lines.")
string lineMode = input.string("Trend only", "Line type", options = ["Trend only", "All slopes"], group = G_L, tooltip = "Trend only: resistance through LOWER highs, support through HIGHER lows (classic RSI trend lines).\nAll slopes: any two consecutive pivots are joined, as in v1.")
bool extAct = input.bool(true, "Extend active lines to the right", group = G_L, tooltip = "On: active lines run to the current bar until they break. Off: lines stop at their second pivot (breaks are still detected and marked).")
int maxSpan = input.int(200, "Max bars between the two pivots", minval = 20, maxval = 1000, group = G_L, tooltip = "Pivots further apart than this are not joined.")
bool showHist = input.bool(true, "Keep broken lines (history)", group = G_L)
int keepN = input.int(5, "Lines kept per side", minval = 1, maxval = 50, group = G_L, tooltip = "Newest lines kept for resistance and for support, active and broken together. Older ones are removed.")
bool showBrk = input.bool(true, "Mark breaks on the RSI pane (◆)", group = G_L)
bool showPx = input.bool(false, "Mark breaks on the price chart", group = G_L, tooltip = "Small triangles on the price chart where RSI broke a line: ▲ below the candle = resistance broken, ▼ above = support broken.")
float upLvl = input.float(70.0, "Upper level", group = G_V, inline = "u")
float loLvl = input.float(30.0, "Lower level", group = G_V, inline = "l")
bool showMid = input.bool(true, "50 midline", group = G_V)
bool showFill = input.bool(true, "Shade RSI beyond the upper / lower level", group = G_V)
color colRsi = input.color(#C9DDF0, "RSI line", group = G_V, inline = "r")
int wRsi = input.int(2, "", minval = 1, maxval = 4, group = G_V, inline = "r")
color colRes = input.color(#FF5C7A, "Resistance / support lines", group = G_V, inline = "tl")
color colSup = input.color(#3FB6FF, "", group = G_V, inline = "tl")
int wLine = input.int(2, "Line width", minval = 1, maxval = 4, group = G_V, inline = "w")
int histT = input.int(60, "History transparency %", minval = 0, maxval = 90, group = G_V, inline = "w")
color colBrk = input.color(#F2C14E, "Break marker", group = G_V)
color colLvl = input.color(#7C8BA8, "Levels / midline", group = G_V)
bool aRes = input.bool(true, "RSI broke resistance", group = G_A, tooltip = "Create ONE alert on this indicator with 'Any alert() function call'. These boxes choose what it sends. Closed candles only.")
bool aSup = input.bool(true, "RSI broke support", group = G_A)
bool aNew = input.bool(false, "New line formed", group = G_A)
// ============================================================================= RSI + LEVELS
float rsi = ta.rsi(src, rsiLen)
pR = plot(rsi, "RSI", color = colRsi, linewidth = wRsi)
max_bars_back(rsi, 1100)
hline(upLvl, "Upper level", color = color.new(colLvl, 20), linestyle = hline.style_dotted)
hline(loLvl, "Lower level", color = color.new(colLvl, 20), linestyle = hline.style_dotted)
hline(50, "Midline 50", color = showMid ? color.new(colLvl, 30) : color.new(colLvl, 100), linestyle = hline.style_dashed)
// shading only where RSI is beyond a level (the gradient is transparent inside the range)
pUp = plot(upLvl, "Upper (fill)", display = display.none)
pLo = plot(loLvl, "Lower (fill)", display = display.none)
fill(pR, pUp, 100, upLvl, showFill ? color.new(colRes, 55) : color.new(colRes, 100), color.new(colRes, 100), "Above upper")
fill(pR, pLo, loLvl, 0, color.new(colSup, 100), showFill ? color.new(colSup, 55) : color.new(colSup, 100), "Below lower")
// ============================================================================= ENGINE
type TL
int x1
float y1
int x2
float y2
int side // 1 resistance, -1 support
bool broken = false
int bBar = na
line ln
label mk
f_at(TL t, int x) => t.y1 + (t.y2 - t.y1) / (t.x2 - t.x1) * (x - t.x1)
var array<TL> tlRes = array.new<TL>()
var array<TL> tlSup = array.new<TL>()
var int phX = na
var float phY = na
var int plX = na
var float plY = na
f_drop(array<TL> a, int i) =>
TL t = array.get(a, i)
line.delete(t.ln)
label.delete(t.mk)
array.remove(a, i)
true
f_prune(array<TL> a) =>
while array.size(a) > keepN
f_drop(a, 0)
true
// RSI must have respected the line between the pivots and since the second pivot
f_valid(int x1, float y1, int x2, float y2, int side) =>
bool ok = true
float m = (y2 - y1) / (x2 - x1)
for k = x1 + 1 to bar_index
if k != x2
float v = rsi[bar_index - k]
float p = y1 + m * (k - x1)
if side == 1 ? v > p : v < p
ok := false
break
ok
// a new line from pivot (x1,y1) to (x2,y2); returns true when drawn
f_make(array<TL> a, int x1, float y1, int x2, float y2, int side) =>
bool made = false
bool slopeOk = lineMode == "All slopes" or (side == 1 ? y2 < y1 : y2 > y1)
if not na(x1) and x2 > x1 and x2 - x1 <= maxSpan and slopeOk and f_valid(x1, y1, x2, y2, side)
color c = side == 1 ? colRes : colSup
line l = line.new(x1, y1, extAct ? bar_index : x2, extAct ? y1 + (y2 - y1) / (x2 - x1) * (bar_index - x1) : y2, color = c, width = wLine)
array.push(a, TL.new(x1, y1, x2, y2, side, ln = l))
f_prune(a)
made := true
made
// update active lines: extend, or mark the break; returns the number broken this bar
f_update(array<TL> a) =>
int nb = 0
if array.size(a) > 0
for i = array.size(a) - 1 to 0
TL t = array.get(a, i)
if not t.broken
float p = f_at(t, bar_index)
bool br = t.side == 1 ? rsi > p : rsi < p
if br
t.broken := true
t.bBar := bar_index
nb += 1
if showHist
color c = t.side == 1 ? colRes : colSup
line.set_xy2(t.ln, bar_index, p)
line.set_color(t.ln, color.new(c, histT))
line.set_style(t.ln, line.style_dotted)
if showBrk
t.mk := label.new(bar_index, p, "◆", style = label.style_label_center, color = color.new(colBrk, 100), textcolor = colBrk, size = size.small)
else
f_drop(a, i)
else if extAct
line.set_xy2(t.ln, bar_index, p)
nb
float ph = ta.pivothigh(rsi, lb, lb)
float pl = ta.pivotlow(rsi, lb, lb)
bool evRes = false
bool evSup = false
bool evNew = false
if barstate.isconfirmed and not na(rsi)
// breaks first, so a line never breaks on the bar it is born
evRes := f_update(tlRes) > 0
evSup := f_update(tlSup) > 0
if not na(ph)
int x2 = bar_index - lb
evNew := f_make(tlRes, phX, phY, x2, ph, 1) or evNew
phX := x2
phY := ph
if not na(pl)
int x2 = bar_index - lb
evNew := f_make(tlSup, plX, plY, x2, pl, -1) or evNew
plX := x2
plY := pl
// ============================================================================= MARKERS ON PRICE (optional)
plotshape(showPx and evRes, "Resistance broken (price)", shape.triangleup, location.belowbar, colSup, size = size.tiny, force_overlay = true, display = display.pane)
plotshape(showPx and evSup, "Support broken (price)", shape.triangledown, location.abovebar, colRes, size = size.tiny, force_overlay = true, display = display.pane)
// ============================================================================= ALERTS (closed candles)
string hdr = syminfo.ticker + " " + timeframe.period + " · RSI " + str.tostring(rsi, "0.0") + " · "
if aRes and evRes
alert(hdr + "RSI broke a resistance trend line", alert.freq_once_per_bar_close)
if aSup and evSup
alert(hdr + "RSI broke a support trend line", alert.freq_once_per_bar_close)
if aNew and evNew
alert(hdr + "new RSI trend line formed", alert.freq_once_per_bar_close)
alertcondition(evRes, "RSI breaks resistance", "{{ticker}} {{interval}}: RSI broke a resistance trend line")
alertcondition(evSup, "RSI breaks support", "{{ticker}} {{interval}}: RSI broke a support trend line")