本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Linear Regression Channel - Touch Statistics [Dots3Red]",
shorttitle = "LinReg Channel [D3R]",
overlay = true,
max_labels_count = 500,
max_lines_count = 10,
max_bars_back = 300)
// Inputs
C_TXT = color.new(#f8fafc, 0)
C_DIM = color.new(#94a3b8, 0)
C_BG = color.new(#131722, 5)
C_BORD = color.new(#334155, 0)
C_GOOD = color.new(#00c896, 0)
C_BAD = color.new(#ff4466, 0)
C_UP = color.new(#00f0ff, 0)
C_DN = color.new(#ff00aa, 0)
C_AMBER = color.new(#ffb700, 0)
GRP_LR = "📐 Regression Channel"
GRP_ENG = "🎯 Touch Grading"
GRP_VIS = "🎨 Visuals"
GRP_HUD = "🖥️ Dashboard"
len = input.int(100, "Length", minval=10, maxval=300, group=GRP_LR)
src = input.source(close, "Source", group=GRP_LR)
mult = input.float(2.0, "Channel Width (× σ)", minval=0.5, step=0.1, group=GRP_LR,
tooltip="Upper and lower lines sit this many standard deviations of the regression residuals away from the regression line.")
stop_sd = input.float(1.0, "Failure Beyond Band (× σ)", minval=0.1, step=0.1, group=GRP_ENG,
tooltip="A touch fails if price pushes this many standard deviations beyond the band before returning to the regression line.")
horizon = input.int(20, "Max Bars to Resolve", minval=3, maxval=200, group=GRP_ENG,
tooltip="A touch that reaches neither the regression line nor the failure level within this many bars counts as failed.")
min_n = input.int(10, "Min Touches to Show Rate", minval=3, group=GRP_ENG)
show_cur = input.bool(true, "Current Channel", group=GRP_VIS)
ext_right = input.bool(true, "Extend Current Channel Right", group=GRP_VIS)
show_fill = input.bool(true, "Fill Current Channel", group=GRP_VIS)
fill_tr = input.int(92, "Fill Transparency", minval=50, maxval=100, group=GRP_VIS)
show_roll = input.bool(false, "Rolling Bands (history)", group=GRP_VIS,
tooltip="Plots where the channel edges were on every past bar. This is what the touches are measured against.")
col_mid = input.color(#ffb700, "Regression Line Color", group=GRP_VIS)
col_up = input.color(#ff00aa, "Upper Line Color", group=GRP_VIS)
col_dn = input.color(#00f0ff, "Lower Line Color", group=GRP_VIS)
w_line = input.int(2, "Line Thickness", minval=1, maxval=6, group=GRP_VIS)
show_lbl = input.bool(true, "Touch Labels", group=GRP_VIS)
lbl_up = input.color(#ff00aa, "Upper Touch Label Color", group=GRP_VIS)
lbl_dn = input.color(#00f0ff, "Lower Touch Label Color", group=GRP_VIS)
show_hud = input.bool(true, "Show Dashboard", group=GRP_HUD)
hud_pos = input.string("Top Right", "Position",
options=["Top Right", "Top Left", "Bottom Right", "Bottom Left"], group=GRP_HUD)
float lr0 = ta.linreg(src, len, 0)
float lr1 = ta.linreg(src, len, 1)
float slope = lr0 - lr1
float r_val = ta.correlation(src, bar_index, len)
float sd = na
if not na(lr0) and not na(lr1)
float ss = 0.0
for i = 0 to len - 1
float res = src[i] - (lr0 - slope * i)
ss += res * res
sd := math.sqrt(ss / len)
float up = lr0 + mult * sd
float dn = lr0 - mult * sd
var int[] p_bar = array.new<int>(4, -1)
var float[] p_tgt = array.new<float>(4, na)
var float[] p_stp = array.new<float>(4, na)
var float[] st_n = array.new<float>(4, 0.0)
var float[] st_h = array.new<float>(4, 0.0)
string[] touch_nm = array.from("Upper", "Upper", "Lower", "Lower")
string[] chan_nm = array.from("Rising", "Falling", "Rising", "Falling")
f_rate(int i) =>
float n = array.get(st_n, i)
n >= min_n ? array.get(st_h, i) / n * 100.0 : na
bool t_up = false
bool t_dn = false
if barstate.isconfirmed and not na(sd) and sd > 0 and not na(up[1])
for i = 0 to 3
int pb = array.get(p_bar, i)
if pb >= 0 and bar_index > pb
bool upper = i < 2
float tg = array.get(p_tgt, i)
float sp = array.get(p_stp, i)
bool hit_t = upper ? low <= tg : high >= tg
bool hit_s = upper ? high >= sp : low <= sp
bool timed = bar_index - pb >= horizon
if hit_t or hit_s or timed
array.set(st_n, i, array.get(st_n, i) + 1.0)
if hit_t and not hit_s
array.set(st_h, i, array.get(st_h, i) + 1.0)
array.set(p_bar, i, -1)
t_up := high >= up and high[1] < up[1]
t_dn := low <= dn and low[1] > dn[1]
int rising = slope > 0 ? 0 : 1
if t_up
int s = rising
if array.get(p_bar, s) < 0
array.set(p_bar, s, bar_index)
array.set(p_tgt, s, lr0)
array.set(p_stp, s, up + stop_sd * sd)
if show_lbl
float r = f_rate(s)
label.new(bar_index, high, "▼" + (na(r) ? "" : " " + str.tostring(math.round(r)) + "%"),
style=label.style_label_down, color=color.new(#000000, 100), textcolor=lbl_up, size=size.small)
if t_dn
int s = 2 + rising
if array.get(p_bar, s) < 0
array.set(p_bar, s, bar_index)
array.set(p_tgt, s, lr0)
array.set(p_stp, s, dn - stop_sd * sd)
if show_lbl
float r = f_rate(s)
label.new(bar_index, low, "▲" + (na(r) ? "" : " " + str.tostring(math.round(r)) + "%"),
style=label.style_label_up, color=color.new(#000000, 100), textcolor=lbl_dn, size=size.small)
// Dashboard
f_hud_pos(string s) =>
switch s
"Top Right" => position.top_right
"Top Left" => position.top_left
"Bottom Right" => position.bottom_right
=> position.bottom_left
var table hud = table.new(f_hud_pos(hud_pos), 4, 8,
bgcolor=C_BG, border_color=C_BORD,
border_width=1, frame_color=C_BORD, frame_width=2)
f_row(int row, int i, color bg) =>
float n = array.get(st_n, i)
float r = f_rate(i)
table.cell(hud, 0, row, array.get(touch_nm, i), text_color = i < 2 ? C_DN : C_UP, bgcolor=bg, text_size=size.small)
table.cell(hud, 1, row, array.get(chan_nm, i), text_color=C_TXT, bgcolor=bg, text_size=size.small, text_halign=text.align_center)
table.cell(hud, 2, row, str.tostring(int(n)), text_color=C_TXT, bgcolor=bg, text_size=size.small, text_halign=text.align_center)
table.cell(hud, 3, row, na(r) ? "Collecting…" : str.tostring(math.round(r)) + "%",
text_color = na(r) ? C_DIM : r >= 55 ? C_GOOD : r <= 45 ? C_BAD : C_TXT,
bgcolor=bg, text_size=size.small, text_halign=text.align_center)
if show_hud and barstate.islast
color bgA = color.new(#1e293b, 40)
color bgB = color.new(#0f172a, 40)
table.cell(hud, 0, 0, "LINREG CHANNEL [D3R]", text_color=C_TXT,
bgcolor=color.new(#1e293b, 0), text_size=size.small, text_halign=text.align_center)
table.merge_cells(hud, 0, 0, 3, 0)
table.cell(hud, 0, 1, "Touch", text_color=C_DIM, bgcolor=bgB, text_size=size.small)
table.cell(hud, 1, 1, "Channel", text_color=C_DIM, bgcolor=bgB, text_size=size.small, text_halign=text.align_center)
table.cell(hud, 2, 1, "Graded", text_color=C_DIM, bgcolor=bgB, text_size=size.small, text_halign=text.align_center)
table.cell(hud, 3, 1, "Reverted", text_color=C_DIM, bgcolor=bgB, text_size=size.small, text_halign=text.align_center,
tooltip="Share of graded first touches where price returned to the regression line (its value at the moment of the touch) before pushing " + str.tostring(stop_sd) + "σ beyond the band, within " + str.tostring(horizon) + " bars.")
f_row(2, 0, bgA)
f_row(3, 1, bgB)
f_row(4, 2, bgA)
f_row(5, 3, bgB)
float pos = sd > 0 ? (close - lr0) / sd : na
table.cell(hud, 0, 6, "Channel Now", text_color=C_DIM, bgcolor=bgA, text_size=size.small)
table.cell(hud, 1, 6, (slope > 0 ? "Rising" : "Falling") + " · r " + str.tostring(r_val, "#.##"),
text_color = slope > 0 ? C_UP : C_DN, bgcolor=bgA, text_size=size.small, text_halign=text.align_center,
tooltip="r is the correlation between price and time over the channel length. Near +1 or −1 means price has followed a straight line closely; near 0 means it hasn't.")
table.merge_cells(hud, 1, 6, 3, 6)
table.cell(hud, 0, 7, "Price vs Line", text_color=C_DIM, bgcolor=bgB, text_size=size.small)
table.cell(hud, 1, 7, na(pos) ? "—" : (pos >= 0 ? "+" : "") + str.tostring(pos, "#.##") + " σ",
text_color = na(pos) ? C_DIM : pos >= 0 ? C_DN : C_UP, bgcolor=bgB, text_size=size.small, text_halign=text.align_center)
table.merge_cells(hud, 1, 7, 3, 7)
// Visual
plot(show_roll ? up : na, "Rolling Upper", color=color.new(col_up, 50), linewidth=1)
plot(show_roll ? lr0 : na, "Rolling Line", color=color.new(col_mid, 50), linewidth=1)
plot(show_roll ? dn : na, "Rolling Lower", color=color.new(col_dn, 50), linewidth=1)
var line l_mid = na
var line l_up = na
var line l_dn = na
var linefill l_fl = na
if barstate.islast and show_cur and not na(sd)
int x1 = bar_index - len + 1
float y1 = lr0 - slope * (len - 1)
string ext = ext_right ? extend.right : extend.none
if na(l_mid)
l_mid := line.new(x1, y1, bar_index, lr0, color=col_mid, width=w_line, extend=ext)
l_up := line.new(x1, y1 + mult * sd, bar_index, up, color=col_up, width=w_line, extend=ext)
l_dn := line.new(x1, y1 - mult * sd, bar_index, dn, color=col_dn, width=w_line, extend=ext)
if show_fill
l_fl := linefill.new(l_up, l_dn, color.new(col_mid, fill_tr))
else
line.set_xy1(l_mid, x1, y1)
line.set_xy2(l_mid, bar_index, lr0)
line.set_xy1(l_up, x1, y1 + mult * sd)
line.set_xy2(l_up, bar_index, up)
line.set_xy1(l_dn, x1, y1 - mult * sd)
line.set_xy2(l_dn, bar_index, dn)
// Alert
alertcondition(t_up, "Upper Band Touch", "D3R LinReg Channel: price touched the upper channel line on {{ticker}}")
alertcondition(t_dn, "Lower Band Touch", "D3R LinReg Channel: price touched the lower channel line on {{ticker}}")