本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Prev HTF Close Cross v3 (Cluster + Retest + Confluence)", overlay=true, max_labels_count=500, max_boxes_count=50)
// ============================================================
// SETTINGS
// ============================================================
grpSig = "Signals"
showLong = input.bool(true, "Show LONG", group=grpSig)
showShort = input.bool(true, "Show SHORT", group=grpSig)
onlyConfirmed = input.bool(true, "Signals on bar close only (no repaint)", group=grpSig)
minConf = input.int(0, "Min. confluence (0 = off)", minval=0, maxval=8, group=grpSig,
tooltip="For LONG, price must be ABOVE at least this many HTF closes; for SHORT, BELOW. Applies to both cross and retest signals.")
grpTF = "Timeframes"
use15m = input.bool(true, "15m", group=grpTF)
use1h = input.bool(true, "1H", group=grpTF)
use2h = input.bool(true, "2H", group=grpTF)
use4h = input.bool(true, "4H", group=grpTF)
use8h = input.bool(true, "8H", group=grpTF)
use1d = input.bool(true, "1D", group=grpTF)
use1w = input.bool(true, "1W", group=grpTF)
use1m = input.bool(true, "1M", group=grpTF)
grpRT = "Retest"
showRetest = input.bool(true, "Show retest signals", group=grpRT)
rtWindow = input.int(20, "Retest window (bars)", minval=1, group=grpRT,
tooltip="Maximum number of bars after a cross in which a retest is searched for.")
rtTolMult = input.float(0.25, "Retest zone (ATR x)", minval=0.0, step=0.05, group=grpRT,
tooltip="Touching within this many ATRs of the level counts as a retest.")
grpCl = "Cluster"
showCluster = input.bool(true, "Show cluster boxes", group=grpCl)
clTolPct = input.float(0.30, "Cluster range (%)", minval=0.01, step=0.05, group=grpCl,
tooltip="Adjacent levels whose gap does not exceed this percentage belong to the same cluster.")
clMin = input.int(2, "Min. TF count", minval=2, maxval=8, group=grpCl)
clExtend = input.int(30, "Box left start (bars back)", minval=1, group=grpCl)
clFill = input.color(color.new(color.yellow, 80), "Box fill", group=grpCl)
clBorder = input.color(color.new(color.yellow, 20), "Box border", group=grpCl)
grpTbl = "Dashboard"
showTable = input.bool(true, "Show table", group=grpTbl)
tblPosIn = input.string("Top right", "Table position",
options=["Top right", "Top left", "Bottom right", "Bottom left"], group=grpTbl)
grpLbl = "Labels"
longColor = input.color(color.lime, "LONG color", group=grpLbl)
shortColor = input.color(color.red, "SHORT color", group=grpLbl)
longTxtCol = input.color(color.black, "LONG text", group=grpLbl)
shortTxtCol = input.color(color.white, "SHORT text", group=grpLbl)
rtLongColor = input.color(color.aqua, "Retest LONG color", group=grpLbl)
rtShortColor = input.color(color.orange, "Retest SHORT color", group=grpLbl)
// ============================================================
// TF FUNCTION
// - Previous HTF close (non-repainting)
// - TFs not higher than the chart TF are disabled
// - Cross + retest state machine (separate state per TF)
// ============================================================
f_tf(tf, enabled, win, tol) =>
htfOk = enabled and timeframe.in_seconds(tf) > timeframe.in_seconds(timeframe.period)
p = request.security(syminfo.tickerid, tf, close[1], gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_on)
valid = htfOk and not na(p)
up = valid and close > p and close[1] <= p
dn = valid and close < p and close[1] >= p
var float lvl = na
var int dir = 0 // 1: waiting for retest after upward cross, -1: after downward cross
var int age = 0
retUp = false
retDn = false
if dir != 0
age += 1
if up
dir := 1
lvl := p
age := 0
else if dn
dir := -1
lvl := p
age := 0
else if dir == 1
if age > win or close < lvl
dir := 0 // window expired or level lost
else if low <= lvl + tol and close > lvl
retUp := true // touched the level and closed above it
dir := 0
else if dir == -1
if age > win or close > lvl
dir := 0
else if high >= lvl - tol and close < lvl
retDn := true
dir := 0
[p, valid, up, dn, retUp, retDn]
// Join multiple TF names into a single text
f_txt(b15, b1h, b2h, b4h, b8h, b1d, b1w, b1m) =>
str.trim((b15 ? "15m " : "") + (b1h ? "1H " : "") + (b2h ? "2H " : "") + (b4h ? "4H " : "") +
(b8h ? "8H " : "") + (b1d ? "1D " : "") + (b1w ? "1W " : "") + (b1m ? "1M " : ""))
// ============================================================
// CALCULATION
// ============================================================
atr = ta.atr(14)
tol = atr * rtTolMult
[p15, v15, l15, s15, rl15, rs15] = f_tf("15", use15m, rtWindow, tol)
[p1h, v1h, l1h, s1h, rl1h, rs1h] = f_tf("60", use1h, rtWindow, tol)
[p2h, v2h, l2h, s2h, rl2h, rs2h] = f_tf("120", use2h, rtWindow, tol)
[p4h, v4h, l4h, s4h, rl4h, rs4h] = f_tf("240", use4h, rtWindow, tol)
[p8h, v8h, l8h, s8h, rl8h, rs8h] = f_tf("480", use8h, rtWindow, tol)
[p1d, v1d, l1d, s1d, rl1d, rs1d] = f_tf("D", use1d, rtWindow, tol)
[p1w, v1w, l1w, s1w, rl1w, rs1w] = f_tf("W", use1w, rtWindow, tol)
[p1m, v1m, l1m, s1m, rl1m, rs1m] = f_tf("M", use1m, rtWindow, tol)
names = array.from("15m", "1H", "2H", "4H", "8H", "1D", "1W", "1M")
levels = array.from(p15, p1h, p2h, p4h, p8h, p1d, p1w, p1m)
valids = array.from(v15, v1h, v2h, v4h, v8h, v1d, v1w, v1m)
ens = array.from(use15m, use1h, use2h, use4h, use8h, use1d, use1w, use1m)
// ---------- Confluence score ----------
validCnt = 0
aboveCnt = 0
belowCnt = 0
for i = 0 to 7
if array.get(valids, i)
validCnt += 1
lv = array.get(levels, i)
if close > lv
aboveCnt += 1
else if close < lv
belowCnt += 1
longConfOk = minConf == 0 or aboveCnt >= minConf
shortConfOk = minConf == 0 or belowCnt >= minConf
// ---------- Signal texts ----------
longTxt = f_txt(l15, l1h, l2h, l4h, l8h, l1d, l1w, l1m)
shortTxt = f_txt(s15, s1h, s2h, s4h, s8h, s1d, s1w, s1m)
rtLongTxt = f_txt(rl15, rl1h, rl2h, rl4h, rl8h, rl1d, rl1w, rl1m)
rtShortTxt = f_txt(rs15, rs1h, rs2h, rs4h, rs8h, rs1d, rs1w, rs1m)
sigOk = not onlyConfirmed or barstate.isconfirmed
longSig = showLong and sigOk and longConfOk and longTxt != ""
shortSig = showShort and sigOk and shortConfOk and shortTxt != ""
rtLongSig = showRetest and showLong and sigOk and longConfOk and rtLongTxt != ""
rtShortSig = showRetest and showShort and sigOk and shortConfOk and rtShortTxt != ""
scoreUp = "▲ " + str.tostring(aboveCnt) + "/" + str.tostring(validCnt)
scoreDown = "▼ " + str.tostring(belowCnt) + "/" + str.tostring(validCnt)
// ---------- Cluster detection ----------
clLo = array.new_float()
clHi = array.new_float()
clTxt = array.new_string()
lvArr = array.new_float()
nmArr = array.new_string()
for i = 0 to 7
if array.get(valids, i)
array.push(lvArr, array.get(levels, i))
array.push(nmArr, array.get(names, i))
n = array.size(lvArr)
if n >= clMin
idx = array.sort_indices(lvArr, order.ascending)
float cLo = na
float cHi = na
string cTxt = ""
int cCnt = 0
for k = 0 to n - 1
j = array.get(idx, k)
v = array.get(lvArr, j)
nm = array.get(nmArr, j)
if cCnt > 0 and (v - cHi) / v * 100 <= clTolPct
cHi := v
cTxt += " " + nm
cCnt += 1
else
if cCnt >= clMin
array.push(clLo, cLo)
array.push(clHi, cHi)
array.push(clTxt, cTxt)
cLo := v
cHi := v
cTxt := nm
cCnt := 1
if cCnt >= clMin
array.push(clLo, cLo)
array.push(clHi, cHi)
array.push(clTxt, cTxt)
// Did price enter a cluster zone?
padF = clTolPct / 200
inCluster = false
inTxt = ""
if array.size(clLo) > 0
for c = 0 to array.size(clLo) - 1
zLo = array.get(clLo, c) * (1 - padF)
zHi = array.get(clHi, c) * (1 + padF)
if close >= zLo and close <= zHi
inCluster := true
inTxt := array.get(clTxt, c)
clusterEnter = inCluster and not inCluster[1] and sigOk
// ============================================================
// DRAWING
// ============================================================
if longSig
label.new(bar_index, low, longTxt + "\n" + scoreUp, style=label.style_label_up,
color=longColor, textcolor=longTxtCol, size=size.normal)
if shortSig
label.new(bar_index, high, shortTxt + "\n" + scoreDown, style=label.style_label_down,
color=shortColor, textcolor=shortTxtCol, size=size.normal)
if rtLongSig
label.new(bar_index, low, "RT " + rtLongTxt + "\n" + scoreUp, style=label.style_label_up,
color=rtLongColor, textcolor=color.black, size=size.small)
if rtShortSig
label.new(bar_index, high, "RT " + rtShortTxt + "\n" + scoreDown, style=label.style_label_down,
color=rtShortColor, textcolor=color.black, size=size.small)
// Cluster boxes: drawn on the last bar only (current state)
var box[] boxes = array.new_box()
if barstate.islast and showCluster
for b in boxes
box.delete(b)
array.clear(boxes)
if array.size(clLo) > 0
for c = 0 to array.size(clLo) - 1
bx = box.new(math.max(0, bar_index - clExtend), array.get(clHi, c) * (1 + padF),
bar_index, array.get(clLo, c) * (1 - padF),
border_color=clBorder, bgcolor=clFill, extend=extend.right,
text=array.get(clTxt, c), text_color=color.new(color.white, 0),
text_size=size.small, text_halign=text.align_right)
array.push(boxes, bx)
// Dashboard table
tblPos = switch tblPosIn
"Top left" => position.top_left
"Bottom right" => position.bottom_right
"Bottom left" => position.bottom_left
=> position.top_right
var table tbl = table.new(tblPos, 4, 11, bgcolor=color.new(color.black, 25),
border_width=1, border_color=color.new(color.gray, 60))
if barstate.islast and showTable
table.cell(tbl, 0, 0, "TF", text_color=color.white, text_size=size.small)
table.cell(tbl, 1, 0, "Prev Close", text_color=color.white, text_size=size.small)
table.cell(tbl, 2, 0, "Distance", text_color=color.white, text_size=size.small)
table.cell(tbl, 3, 0, "Status", text_color=color.white, text_size=size.small)
for i = 0 to 7
r = i + 1
if array.get(valids, i)
lv = array.get(levels, i)
dist = (close - lv) / lv * 100
isUp = close >= lv
table.cell(tbl, 0, r, array.get(names, i), text_color=color.white, text_size=size.small)
table.cell(tbl, 1, r, str.tostring(lv, format.mintick), text_color=color.white, text_size=size.small)
table.cell(tbl, 2, r, str.tostring(math.round(dist, 2)) + "%",
text_color=isUp ? longColor : shortColor, text_size=size.small)
table.cell(tbl, 3, r, isUp ? "▲" : "▼",
text_color=isUp ? longColor : shortColor, text_size=size.small)
else
table.cell(tbl, 0, r, array.get(names, i), text_color=color.gray, text_size=size.small)
table.cell(tbl, 1, r, "-", text_color=color.gray, text_size=size.small)
table.cell(tbl, 2, r, "-", text_color=color.gray, text_size=size.small)
table.cell(tbl, 3, r, array.get(ens, i) ? "TF too low" : "off", text_color=color.gray, text_size=size.small)
table.cell(tbl, 0, 9, "Confluence", text_color=color.white, text_size=size.small)
table.cell(tbl, 1, 9, scoreUp, text_color=longColor, text_size=size.small)
table.cell(tbl, 2, 9, scoreDown, text_color=shortColor, text_size=size.small)
table.cell(tbl, 3, 9, "", text_size=size.small)
table.cell(tbl, 0, 10, "Chart TF", text_color=color.white, text_size=size.small)
table.cell(tbl, 1, 10, timeframe.period + " | active HTF: " + str.tostring(validCnt),
text_color=validCnt == 0 ? color.yellow : color.white, text_size=size.small)
table.cell(tbl, 2, 10, validCnt == 0 ? "No active HTF!" : "", text_color=color.yellow, text_size=size.small)
table.cell(tbl, 3, 10, "", text_size=size.small)
// ============================================================
// ALERTS
// ============================================================
alertcondition(longSig, "LONG cross", "Previous HTF close crossed up")
alertcondition(shortSig, "SHORT cross", "Previous HTF close crossed down")
alertcondition(rtLongSig, "LONG retest", "Broken HTF level retested from above")
alertcondition(rtShortSig, "SHORT retest", "Broken HTF level retested from below")
alertcondition(clusterEnter, "Cluster entry", "Price entered an HTF cluster zone")
// Dynamic-message alert: choose "Any alert() function call" when creating the alert
if longSig
alert("LONG | " + syminfo.ticker + " | " + longTxt + " | " + scoreUp, alert.freq_once_per_bar_close)
if shortSig
alert("SHORT | " + syminfo.ticker + " | " + shortTxt + " | " + scoreDown, alert.freq_once_per_bar_close)
if rtLongSig
alert("RETEST LONG | " + syminfo.ticker + " | " + rtLongTxt, alert.freq_once_per_bar_close)
if rtShortSig
alert("RETEST SHORT | " + syminfo.ticker + " | " + rtShortTxt, alert.freq_once_per_bar_close)
if clusterEnter
alert("CLUSTER | " + syminfo.ticker + " | " + inTxt, alert.freq_once_per_bar_close)