本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Titpaud
// This work is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International
// https://creativecommons.org/licenses/by-nc-sa/4.0/
// Based on "Pivot Channel TrendLines" © BigBeluga (same license). Multi-layer version © Titpaud.
// v1 (2026-10-25): the pivot channel is computed on several pivot sizes at once on the chart TF.
// A layer with pivots k times larger is close to the same channel on a TF k times larger
// (a 4h bar holds four 1h bars), so the channels of several TFs are superposed without requesting other TFs.
// v2 (2026-10-25): extensions longer than 500 bars into the future no longer stop the script (TradingView limit):
// the line is drawn up to the limit and lengthened bar after bar. Layer x16 shown by default.
// v3 (2026-10-25): pivot dots hidden by default (option), pivot high color red by default.
// v4 (2026-10-25): options to extend the latest lines or all lines whatever their direction
// (these extra extensions draw no Up / Down label).
// v5 (2026-10-25): the two options are replaced by a number of latest lines extended in any direction.
// v6 (2026-10-25): each layer keeps its own last Up / Down label (the x1 labels no longer erase the x4 / x16 ones).
// Defaults: extension 140, 4 latest lines extended in any direction.
//@version=6
indicator("Titpaud Pivot Channel MTF", "TitpaudTLC", overlay = true, max_lines_count = 500, max_labels_count = 500)
// ───────────────────────────────────── INPUTS ─────────────────────────────────────
const string G_BASE = "Base settings"
int baseLen = input.int(15, "Pivot lookback length (x1 layer)", minval = 1, maxval = 100, group = G_BASE, tooltip = "Bars before and after a pivot on the x1 layer. The other layers multiply it.")
int baseExt = input.int(140, "Trendline extension length (x1 layer)", minval = 1, group = G_BASE, tooltip = "Bars of the x1 layer. Multiplied by each layer's factor.")
string extStyIn = input.string("Dotted line", "Extension line style", options = ["Dashed line", "Dotted line", "Solid line"], group = G_BASE)
color colHigh = input.color(color.red, "Pivot high color", group = G_BASE)
color colLow = input.color(color.lime, "Pivot low color", group = G_BASE)
bool showDots = input.bool(false, "Show pivot dots", group = G_BASE, tooltip = "Circle on every pivot, larger on the larger layers.")
int extLastN = input.int(4, "Latest lines extended in any direction", minval = 0, maxval = 500, group = G_BASE, tooltip = "Number of lines, counted from the latest one, extended on each side (highs and lows) of each layer even when they do not follow the channel rule (falling highs, rising lows). 1 = latest lines only, 2 = the two latest, and so on. 0 = off, a large number = all lines. These extra extensions draw no Up / Down label.")
const string G_L1 = "Layer x1 (chart TF)"
bool on1 = input.bool(true, "Show layer x1", group = G_L1)
int w1 = input.int(1, "Line width x1", minval = 1, maxval = 6, group = G_L1)
int tr1 = input.int(40, "Transparency x1", minval = 0, maxval = 90, group = G_L1)
bool dir1 = input.bool(true, "Up and Down labels x1", group = G_L1)
const string G_L2 = "Layer x4 (TF four times larger)"
bool on2 = input.bool(true, "Show layer x4", group = G_L2)
int k2 = input.int(4, "Factor of layer x4", minval = 2, maxval = 30, group = G_L2, tooltip = "Pivot length and extension are multiplied by this factor. 4 = close to the TF four times larger (4h on a 1h chart).")
int w2 = input.int(2, "Line width x4", minval = 1, maxval = 6, group = G_L2)
int tr2 = input.int(15, "Transparency x4", minval = 0, maxval = 90, group = G_L2)
bool dir2 = input.bool(true, "Up and Down labels x4", group = G_L2)
const string G_L3 = "Layer x16 (TF sixteen times larger)"
bool on3 = input.bool(true, "Show layer x16", group = G_L3)
int k3 = input.int(16, "Factor of layer x16", minval = 2, maxval = 100, group = G_L3, tooltip = "16 = close to the TF sixteen times larger (16h, almost 1D, on a 1h chart).")
int w3 = input.int(3, "Line width x16", minval = 1, maxval = 6, group = G_L3)
int tr3 = input.int(0, "Transparency x16", minval = 0, maxval = 90, group = G_L3)
bool dir3 = input.bool(false, "Up and Down labels x16", group = G_L3)
const string G_DS = "Last pivot price dashes (layer x1)"
bool showDash = input.bool(true, "Show last pivot price", group = G_DS, tooltip = "Horizontal dashes and price labels for the latest high and low pivots of the x1 layer.")
string dashStyIn = input.string("Dashed line", "Dash line style", options = ["Dashed line", "Dotted line", "Solid line"], group = G_DS)
f_style(string s) => s == "Dashed line" ? line.style_dashed : s == "Dotted line" ? line.style_dotted : line.style_solid
string extSty = f_style(extStyIn)
string dashSty = f_style(dashStyIn)
// ───────────────────────────────────── ONE LAYER ─────────────────────────────────────
type Layer
int len // pivot lookback of this layer
int ext // extension length in chart bars
float k // factor of this layer
int width
int transp
bool dirLbl
string tag // "" for x1, " x4"...
array<line> pend // extensions not drawn to their full length yet (500 bars into the future at most)
array<line> xHi // extra extensions of the latest high lines, one slot per line (na when the line already has its channel extension)
array<line> xLo
label dirLb = na // last Up / Down label of this layer
float phVal = na
int phIdx = na
float plVal = na
int plIdx = na
int upStart = na
float upY = na
float upSlope = na
bool upAct = false
int loStart = na
float loY = na
float loSlope = na
bool loAct = false
// draws an extension of a line from (x, y) with the given slope, at most 500 bars into the future
f_extLine(Layer L, int x, float y, float slope, color col) =>
int endX = math.min(x + L.ext, bar_index + 499)
line ln = line.new(x, y, endX, y + slope * (endX - x), style = extSty, color = col, width = L.width)
if endX < x + L.ext
L.pend.push(ln)
if L.pend.size() > 30
L.pend.shift()
ln
// keeps the extra extensions of the latest extLastN lines only: adds the slot of the new line, deletes the oldest ones
f_keepLast(Layer L, array<line> xs, line ln) =>
xs.push(ln)
while xs.size() > extLastN
line old = xs.shift()
if not na(old)
int i = L.pend.indexof(old)
if i >= 0
L.pend.remove(i)
line.delete(old)
// draws the pivot channel of one layer: zigzag between pivots of the same kind, extension when two highs fall
// (or two lows rise) by at least one ATR of this layer, Up / Down label when price closes beyond the extension
f_layer(Layer L, float ph, float pl, float atrL) =>
color cH = color.new(colHigh, L.transp)
color cL = color.new(colLow, L.transp)
int pb = bar_index - L.len
string dotSz = L.k >= 16 ? size.normal : L.k >= 4 ? size.small : size.tiny
if not na(ph)
if not na(L.phVal)
line.new(L.phIdx, L.phVal, pb, ph, width = L.width, color = cH)
float val = (ph - L.phVal) / (pb - L.phIdx)
if ph < L.phVal and (L.phVal - ph) >= atrL
f_extLine(L, pb, ph, val, cH)
L.upStart := pb
L.upY := ph
L.upSlope := val
L.upAct := true
f_keepLast(L, L.xHi, na)
else
line xl = na
if extLastN > 0
xl := f_extLine(L, pb, ph, val, cH)
f_keepLast(L, L.xHi, xl)
if showDots
label.new(pb, ph, style = label.style_circle, size = dotSz, color = color.new(colHigh, math.max(L.transp, 50)))
L.phVal := ph
L.phIdx := pb
if not na(pl)
if not na(L.plVal)
line.new(L.plIdx, L.plVal, pb, pl, width = L.width, color = cL)
float val = (pl - L.plVal) / (pb - L.plIdx)
if pl > L.plVal and (pl - L.plVal) >= atrL
f_extLine(L, pb, pl, val, cL)
L.loStart := pb
L.loY := pl
L.loSlope := val
L.loAct := true
f_keepLast(L, L.xLo, na)
else
line xl = na
if extLastN > 0
xl := f_extLine(L, pb, pl, val, cL)
f_keepLast(L, L.xLo, xl)
if showDots
label.new(pb, pl, style = label.style_circle, size = dotSz, color = color.new(colLow, math.max(L.transp, 50)))
L.plVal := pl
L.plIdx := pb
// breakout of the active extensions
if L.upAct
int n = bar_index - L.upStart
if n > L.ext
L.upAct := false
else if n >= 0 and close > L.upY + L.upSlope * n and barstate.isconfirmed
if L.dirLbl
label.delete(L.dirLb)
L.dirLb := label.new(bar_index, low, "Up" + L.tag, style = label.style_label_up, color = colHigh, textcolor = color.white, size = size.small)
L.upAct := false
if L.loAct
int n = bar_index - L.loStart
if n > L.ext
L.loAct := false
else if n >= 0 and close < L.loY + L.loSlope * n and barstate.isconfirmed
if L.dirLbl
label.delete(L.dirLb)
L.dirLb := label.new(bar_index, high, "Down" + L.tag, style = label.style_label_down, color = colLow, textcolor = color.white, size = size.small)
L.loAct := false
// lengthens the extensions that hit the 500 bars limit, until they reach their full length
f_extend(Layer L) =>
int n = L.pend.size()
if n > 0
for i = n - 1 to 0
line ln = L.pend.get(i)
int x1 = ln.get_x1()
int x2 = ln.get_x2()
int tgt = x1 + L.ext
int nx = math.min(tgt, bar_index + 499)
if nx > x2
float sl = (ln.get_y2() - ln.get_y1()) / (x2 - x1)
ln.set_xy2(nx, ln.get_y1() + sl * (nx - x1))
if nx >= tgt
L.pend.remove(i)
// ───────────────────────────────────── CALCULATIONS ─────────────────────────────────────
int len2 = baseLen * k2
int len3 = baseLen * k3
// pivots of every layer, computed on every bar (required by Pine)
float ph1 = ta.pivothigh(baseLen, baseLen)
float pl1 = ta.pivotlow(baseLen, baseLen)
float ph2 = ta.pivothigh(len2, len2)
float pl2 = ta.pivotlow(len2, len2)
float ph3 = ta.pivothigh(len3, len3)
float pl3 = ta.pivotlow(len3, len3)
// ATR200 of the chart; a TF k times larger has an ATR about sqrt(k) times larger
float atr = ta.atr(200)
var Layer L1 = Layer.new(baseLen, baseExt, 1.0, w1, tr1, dir1, "", array.new<line>(), array.new<line>(), array.new<line>())
var Layer L2 = Layer.new(len2, baseExt * k2, float(k2), w2, tr2, dir2, " x" + str.tostring(k2), array.new<line>(), array.new<line>(), array.new<line>())
var Layer L3 = Layer.new(len3, baseExt * k3, float(k3), w3, tr3, dir3, " x" + str.tostring(k3), array.new<line>(), array.new<line>(), array.new<line>())
if not na(atr)
if on3
f_layer(L3, ph3, pl3, atr * math.sqrt(k3))
f_extend(L3)
if on2
f_layer(L2, ph2, pl2, atr * math.sqrt(k2))
f_extend(L2)
if on1
f_layer(L1, ph1, pl1, atr)
f_extend(L1)
// ───────────────────────────────────── LAST PIVOT DASHES (x1) ─────────────────────────────────────
var line phDash = na
var label phLbl = na
var line plDash = na
var label plLbl = na
if showDash and on1
if not na(ph1)
line.delete(phDash)
label.delete(phLbl)
phDash := line.new(bar_index - baseLen, ph1, bar_index, ph1, style = dashSty, color = colHigh, width = 1)
phLbl := label.new(bar_index, ph1, str.tostring(ph1, format.mintick), style = label.style_label_left, color = colHigh, textcolor = color.white, size = size.small)
if not na(pl1)
line.delete(plDash)
label.delete(plLbl)
plDash := line.new(bar_index - baseLen, pl1, bar_index, pl1, style = dashSty, color = colLow, width = 1)
plLbl := label.new(bar_index, pl1, str.tostring(pl1, format.mintick), style = label.style_label_left, color = colLow, textcolor = color.white, size = size.small)
if barstate.islast
if not na(phDash)
phDash.set_x2(bar_index)
phLbl.set_x(bar_index)
if not na(plDash)
plDash.set_x2(bar_index)
plLbl.set_x(bar_index)