本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Auto Trendlines", shorttitle="Trendlines", overlay=true, max_lines_count=500)
// ─────────────────────────────────────────────
// General Inputs
// ─────────────────────────────────────────────
GRP_LINE = "Line Logic (all slots)"
showDown = input.bool(true, "Show Downtrend Lines (lower highs)", group=GRP_LINE)
showUp = input.bool(true, "Show Uptrend Lines (higher lows)", group=GRP_LINE)
requireClean = input.bool(true, "Require Clean Line (no close through it between pivots)", group=GRP_LINE)
extendRight = input.bool(true, "Extend Lines to the Right", group=GRP_LINE)
stopOnBreak = input.bool(true, "Stop Line at Close-Through Break", group=GRP_LINE, tooltip="A line stops when a close of ITS OWN timeframe goes through it.")
maxKeep = input.int(20, "Max Lines Kept Per Side (per slot)", minval=1, maxval=50, group=GRP_LINE)
maxSpan = input.int(300, "Max Bars Between Pivots (in that slot's bars)", minval=2, group=GRP_LINE)
// ─────────────────────────────────────────────
// Slot Inputs
// ─────────────────────────────────────────────
G1 = "Slot 1"
en1 = input.bool(true, "Enabled", group=G1)
tf1 = input.timeframe("", "Timeframe (blank = chart)", group=G1)
left1 = input.int(7, "Pivot Left Bars", minval=1, group=G1)
right1= input.int(7, "Pivot Right Bars", minval=1, group=G1)
col1 = input.color(#2962FF, "Line Color", group=G1)
sty1 = input.string("Dotted", "Line Style", options=["Dotted", "Dashed", "Solid"], group=G1)
w1 = input.int(1, "Line Width", minval=1, maxval=5, group=G1)
G2 = "Slot 2"
en2 = input.bool(true, "Enabled", group=G2)
tf2 = input.timeframe("240", "Timeframe", group=G2)
left2 = input.int(7, "Pivot Left Bars", minval=1, group=G2)
right2= input.int(7, "Pivot Right Bars", minval=1, group=G2)
col2 = input.color(#00897B, "Line Color", group=G2)
sty2 = input.string("Dotted", "Line Style", options=["Dotted", "Dashed", "Solid"], group=G2)
w2 = input.int(1, "Line Width", minval=1, maxval=5, group=G2)
G3 = "Slot 3"
en3 = input.bool(true, "Enabled", group=G3)
tf3 = input.timeframe("D", "Timeframe", group=G3)
left3 = input.int(7, "Pivot Left Bars", minval=1, group=G3)
right3= input.int(7, "Pivot Right Bars", minval=1, group=G3)
col3 = input.color(#FF6D00, "Line Color", group=G3)
sty3 = input.string("Dotted", "Line Style", options=["Dotted", "Dashed", "Solid"], group=G3)
w3 = input.int(1, "Line Width", minval=1, maxval=5, group=G3)
G4 = "Slot 4"
en4 = input.bool(true, "Enabled", group=G4)
tf4 = input.timeframe("W", "Timeframe", group=G4)
left4 = input.int(7, "Pivot Left Bars", minval=1, group=G4)
right4= input.int(7, "Pivot Right Bars", minval=1, group=G4)
col4 = input.color(#D500F9, "Line Color", group=G4)
sty4 = input.string("Dotted", "Line Style", options=["Dotted", "Dashed", "Solid"], group=G4)
w4 = input.int(1, "Line Width", minval=1, maxval=5, group=G4)
// ─────────────────────────────────────────────
// Types / helpers
// ─────────────────────────────────────────────
type Slot
array<line> dLines
array<bool> dLive
array<line> uLines
array<bool> uLive
array<float> sh
array<float> sl
array<float> sc
array<int> shBi
array<int> slBi
array<int> scBi
int leftLen
int rightLen
color col
string styleName
int wid
float lastPHP = na
int lastPHBi = na
int lastPHk = na
float lastPLP = na
int lastPLBi = na
int lastPLk = na
int lastT = na
bool dBrk = false
bool uBrk = false
newSlot(int L, int R, color c, string st, int w) =>
Slot.new(array.new<line>(), array.new<bool>(), array.new<line>(), array.new<bool>(), array.new<float>(), array.new<float>(), array.new<float>(), array.new<int>(), array.new<int>(), array.new<int>(), L, R, c, st, w)
getStyle(string name) =>
switch name
"Dotted" => line.style_dotted
"Dashed" => line.style_dashed
=> line.style_solid
lineAt(line ln, int x) =>
int x1 = line.get_x1(ln)
int x2 = line.get_x2(ln)
float y1 = line.get_y1(ln)
float y2 = line.get_y2(ln)
y1 + (y2 - y1) / (x2 - x1) * (x - x1)
// ─────────────────────────────────────────────
// State
// ─────────────────────────────────────────────
var Slot s1 = newSlot(left1, right1, col1, sty1, w1)
var Slot s2 = newSlot(left2, right2, col2, sty2, w2)
var Slot s3 = newSlot(left3, right3, col3, sty3, w3)
var Slot s4 = newSlot(left4, right4, col4, sty4, w4)
// chart bar history (used to locate higher-timeframe pivots on the chart)
var array<int> cbTime = array.new<int>()
var array<float> cbHigh = array.new<float>()
var array<float> cbLow = array.new<float>()
// ─────────────────────────────────────────────
// Core: process one closed bar of a slot's timeframe
// ─────────────────────────────────────────────
processBar(Slot s, float h, float l, float c, int hBi, int lBi, int cBi) =>
array.push(s.sh, h)
array.push(s.sl, l)
array.push(s.sc, c)
array.push(s.shBi, hBi)
array.push(s.slBi, lBi)
array.push(s.scBi, cBi)
int n = array.size(s.sc)
// 1) break detection on live lines
for [i, ln] in s.dLines
if array.get(s.dLive, i)
float px = lineAt(ln, cBi)
if c > px
array.set(s.dLive, i, false)
s.dBrk := true
if stopOnBreak
line.set_extend(ln, extend.none)
line.set_xy2(ln, cBi, px)
for [i, ln] in s.uLines
if array.get(s.uLive, i)
float px = lineAt(ln, cBi)
if c < px
array.set(s.uLive, i, false)
s.uBrk := true
if stopOnBreak
line.set_extend(ln, extend.none)
line.set_xy2(ln, cBi, px)
// 2) pivot detection
int L = s.leftLen
int R = s.rightLen
if n >= L + R + 1
int k = n - 1 - R
// ---- pivot high -> downtrend line ----
float pk = array.get(s.sh, k)
bool isH = true
for j = k - L to n - 1
if j != k
float v = array.get(s.sh, j)
if (j < k and v >= pk) or (j > k and v > pk)
isH := false
break
if isH
int pBi = array.get(s.shBi, k)
if showDown and not na(s.lastPHP) and pk < s.lastPHP and (k - s.lastPHk) <= maxSpan
float slope = (pk - s.lastPHP) / (pBi - s.lastPHBi)
bool ok = true
if requireClean and (k - s.lastPHk) > 1
for j = s.lastPHk + 1 to k - 1
if array.get(s.sc, j) > s.lastPHP + slope * (array.get(s.scBi, j) - s.lastPHBi)
ok := false
break
if ok
line nl = line.new(s.lastPHBi, s.lastPHP, pBi, pk, xloc.bar_index, extendRight ? extend.right : extend.none, s.col, getStyle(s.styleName), s.wid)
bool live = true
for j = k + 1 to n - 1
float px = lineAt(nl, array.get(s.scBi, j))
if array.get(s.sc, j) > px
live := false
if stopOnBreak
line.set_extend(nl, extend.none)
line.set_xy2(nl, array.get(s.scBi, j), px)
break
array.push(s.dLines, nl)
array.push(s.dLive, live)
if array.size(s.dLines) > maxKeep
line.delete(array.shift(s.dLines))
array.shift(s.dLive)
s.lastPHP := pk
s.lastPHBi := pBi
s.lastPHk := k
// ---- pivot low -> uptrend line ----
float ql = array.get(s.sl, k)
bool isL = true
for j = k - L to n - 1
if j != k
float v = array.get(s.sl, j)
if (j < k and v <= ql) or (j > k and v < ql)
isL := false
break
if isL
int qBi = array.get(s.slBi, k)
if showUp and not na(s.lastPLP) and ql > s.lastPLP and (k - s.lastPLk) <= maxSpan
float slope = (ql - s.lastPLP) / (qBi - s.lastPLBi)
bool ok = true
if requireClean and (k - s.lastPLk) > 1
for j = s.lastPLk + 1 to k - 1
if array.get(s.sc, j) < s.lastPLP + slope * (array.get(s.scBi, j) - s.lastPLBi)
ok := false
break
if ok
line nl = line.new(s.lastPLBi, s.lastPLP, qBi, ql, xloc.bar_index, extendRight ? extend.right : extend.none, s.col, getStyle(s.styleName), s.wid)
bool live = true
for j = k + 1 to n - 1
float px = lineAt(nl, array.get(s.scBi, j))
if array.get(s.sc, j) < px
live := false
if stopOnBreak
line.set_extend(nl, extend.none)
line.set_xy2(nl, array.get(s.scBi, j), px)
break
array.push(s.uLines, nl)
array.push(s.uLive, live)
if array.size(s.uLines) > maxKeep
line.delete(array.shift(s.uLines))
array.shift(s.uLive)
s.lastPLP := ql
s.lastPLBi := qBi
s.lastPLk := k
true
// ─────────────────────────────────────────────
// Feed a slot: mode 0 = off, 1 = chart timeframe, 2 = higher timeframe
// ─────────────────────────────────────────────
feed(Slot s, int mode, int t, float h, float l, float c) =>
s.dBrk := false
s.uBrk := false
if barstate.isconfirmed
if mode == 1
processBar(s, high, low, close, bar_index, bar_index, bar_index)
else if mode == 2 and not na(t) and (na(s.lastT) or t != s.lastT)
int sz = array.size(cbTime)
if sz > 0
int i0 = array.binary_search_leftmost(cbTime, t)
i0 := math.max(i0, 0)
while i0 < sz and array.get(cbTime, i0) < t
i0 += 1
if i0 < sz
float bh = array.get(cbHigh, i0)
float bl = array.get(cbLow, i0)
int hb = i0
int lb = i0
if sz - 1 > i0
for j = i0 + 1 to sz - 1
if array.get(cbHigh, j) > bh
bh := array.get(cbHigh, j)
hb := j
if array.get(cbLow, j) < bl
bl := array.get(cbLow, j)
lb := j
processBar(s, h, l, c, hb, lb, bar_index - 1)
s.lastT := t
true
// ─────────────────────────────────────────────
// Timeframe resolution
// ─────────────────────────────────────────────
eff1 = tf1 == "" ? timeframe.period : tf1
eff2 = tf2 == "" ? timeframe.period : tf2
eff3 = tf3 == "" ? timeframe.period : tf3
eff4 = tf4 == "" ? timeframe.period : tf4
chartSecs = timeframe.in_seconds(timeframe.period)
sec1 = timeframe.in_seconds(eff1)
sec2 = timeframe.in_seconds(eff2)
sec3 = timeframe.in_seconds(eff3)
sec4 = timeframe.in_seconds(eff4)
modeOf(bool en, int sec) =>
not en or sec < chartSecs ? 0 : sec == chartSecs ? 1 : 2
// Slots set to a timeframe lower than the chart are skipped, and a slot that
// duplicates an earlier enabled slot's timeframe is skipped as well.
mode1 = modeOf(en1, sec1)
mode2 = (en1 and sec2 == sec1) ? 0 : modeOf(en2, sec2)
mode3 = ((en1 and sec3 == sec1) or (en2 and sec3 == sec2)) ? 0 : modeOf(en3, sec3)
mode4 = ((en1 and sec4 == sec1) or (en2 and sec4 == sec2) or (en3 and sec4 == sec3)) ? 0 : modeOf(en4, sec4)
req1 = mode1 == 2 ? eff1 : timeframe.period
req2 = mode2 == 2 ? eff2 : timeframe.period
req3 = mode3 == 2 ? eff3 : timeframe.period
req4 = mode4 == 2 ? eff4 : timeframe.period
// Non-repainting: last CLOSED higher-timeframe bar only
[t1, h1, l1, c1] = request.security(syminfo.tickerid, req1, [time[1], high[1], low[1], close[1]], lookahead = barmerge.lookahead_on)
[t2, h2, l2, c2] = request.security(syminfo.tickerid, req2, [time[1], high[1], low[1], close[1]], lookahead = barmerge.lookahead_on)
[t3, h3, l3, c3] = request.security(syminfo.tickerid, req3, [time[1], high[1], low[1], close[1]], lookahead = barmerge.lookahead_on)
[t4, h4, l4, c4] = request.security(syminfo.tickerid, req4, [time[1], high[1], low[1], close[1]], lookahead = barmerge.lookahead_on)
feed(s1, mode1, t1, h1, l1, c1)
feed(s2, mode2, t2, h2, l2, c2)
feed(s3, mode3, t3, h3, l3, c3)
feed(s4, mode4, t4, h4, l4, c4)
// store chart bar history AFTER feeding (arrays hold bars before the current one)
if barstate.isconfirmed
array.push(cbTime, time)
array.push(cbHigh, high)
array.push(cbLow, low)
// ─────────────────────────────────────────────
// Alerts
// ─────────────────────────────────────────────
alertcondition(s1.dBrk, "Slot 1 - Downtrend Line Broken", "Slot 1: close above a downtrend line")
alertcondition(s1.uBrk, "Slot 1 - Uptrend Line Broken", "Slot 1: close below an uptrend line")
alertcondition(s2.dBrk, "Slot 2 - Downtrend Line Broken", "Slot 2: close above a downtrend line")
alertcondition(s2.uBrk, "Slot 2 - Uptrend Line Broken", "Slot 2: close below an uptrend line")
alertcondition(s3.dBrk, "Slot 3 - Downtrend Line Broken", "Slot 3: close above a downtrend line")
alertcondition(s3.uBrk, "Slot 3 - Uptrend Line Broken", "Slot 3: close below an uptrend line")
alertcondition(s4.dBrk, "Slot 4 - Downtrend Line Broken", "Slot 4: close above a downtrend line")
alertcondition(s4.uBrk, "Slot 4 - Uptrend Line Broken", "Slot 4: close below an uptrend line")