本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// ╔══════════════════════════════════════════════════════════════════════╗
// ║ BROWN COMPOSITE INDEX PRO — Divergences · Levels · Timing · Alerts ║
// ╚══════════════════════════════════════════════════════════════════════╝
// Constance Brown, "Technical Analysis for the Trading Professional" (2nd ed.):
// Ch. 12 Composite Index = 9-period momentum of RSI(14) + 3-period SMA of RSI(3), with 13 & 33 SMAs.
// Diverges where RSI fails to warn. Not bounded, so its value is its displacement from zero:
// levels where it turned before tend to hold again; old extremes get retested.
// Ch. 5/6/12 A sharp move often follows when the oscillator tests the point where its two averages cross.
// Ch. 10 Composite average cross = early warning; RSI cross = the true timing signal.
// Divergence pivots fewer than 9 bars apart = very strong.
// Ch. 4 An oscillator moving disproportionately to price means you are on the wrong side of the market.
//
// ALERTS: Create Alert → Condition: "Brown Composite Index Pro" → "Any alert() function call".
indicator("Brown Composite Index Pro", shorttitle = "Composite Pro", overlay = false, max_lines_count = 500, max_labels_count = 500, max_bars_back = 500)
// ═════════════════════════════ INPUTS ═════════════════════════════
grpC = "📈 Composite Index"
src = input.source(close, "Source", group = grpC)
rsiLen = input.int(14, "RSI length", group = grpC, inline = "a")
momLen = input.int(9, "Momentum", group = grpC, inline = "a")
fastLen = input.int(3, "Short RSI", group = grpC, inline = "b")
fastSm = input.int(3, "Smoothing", group = grpC, inline = "b")
ma1Len = input.int(13, "Fast SMA", group = grpC, inline = "m")
ma2Len = input.int(33, "Slow SMA", group = grpC, inline = "m")
grpD = "🔀 Divergences"
showReg = input.bool(true, "Regular (vs price)", group = grpD)
showHid = input.bool(true, "Hidden (vs price)", group = grpD)
showCvR = input.bool(true, "Composite vs RSI", tooltip = "Composite makes a lower high while RSI makes a higher high (bearish), or the reverse at lows — the warning RSI alone misses.", group = grpD)
divPrice = input.string("Close", "Price for divergence", options = ["Close", "High/Low"], group = grpD)
lb = input.int(5, "Pivot lookback left", minval = 1, group = grpD, inline = "pv")
rb = input.int(3, "right", minval = 1, group = grpD, inline = "pv")
minSpan = input.int(4, "Bars between pivots: min", minval = 2, group = grpD, inline = "sp")
maxSpan = input.int(60, "max", minval = 3, group = grpD, inline = "sp")
strongBars = input.int(9, "★ Strong when pivots are fewer than (bars) apart", minval = 2, tooltip = "Book Ch. 10: divergence peaks less than nine periods apart are very strong signals.", group = grpD)
onPrice = input.bool(true, "Also draw on price chart", group = grpD)
grpL = "📏 Historical levels (Ch. 12)"
showLv = input.bool(true, "Levels where the Composite turned before", tooltip = "Finds displacement levels the Composite has reversed at repeatedly. A new swing at one of these levels is a high-probability reversal (Brown: a double test at the same zone).", group = grpL)
lvMem = input.int(80, "Swings remembered", minval = 20, maxval = 200, group = grpL, inline = "lv1")
lvTouch = input.int(3, "Min touches", minval = 2, maxval = 10, group = grpL, inline = "lv1")
lvMax = input.int(4, "Max levels", minval = 1, maxval = 8, group = grpL, inline = "lv2")
lvTolK = input.float(0.15, "Level width (× std dev)", minval = 0.02, step = 0.01, group = grpL, inline = "lv2")
showExt = input.bool(true, "Mark historic extremes", tooltip = "Brown keeps historic oscillator extremes on the chart: when a new extreme breaks an old one, the old range is nearly always retested later.", group = grpL)
grpX = "✖ Average-crossover tests (Ch. 5/6/12)"
showXZ = input.bool(true, "Composite tests its averages where they cross", tooltip = "Sell: Composite rallies into the underside of its averages as the fast crosses down through the slow.\nBuy: Composite pulls back to its averages as the fast crosses up through the slow.", group = grpX)
xzK = input.float(0.25, "Averages count as crossing within (× std dev)", minval = 0.02, step = 0.01, group = grpX)
grpTS = "⏱ Two-step timing (Ch. 10)"
showTS = input.bool(true, "Composite cross → RSI cross", tooltip = "Step 1: Composite crosses its averages (early warning).\nStep 2: RSI(14) crosses its own average in the same direction within N bars (the timing signal).", group = grpTS)
rsiMaLen = input.int(9, "RSI average length", minval = 1, group = grpTS, inline = "ts")
tsWait = input.int(10, "Within (bars)", minval = 1, group = grpTS, inline = "ts")
grpDP = "⚖ Price not following (Ch. 4)"
showDP = input.bool(true, "Warn when the Composite moves but price does not", tooltip = "Brown: when an oscillator advances or declines disproportionately to the market's movement, you are on the wrong side if positioned with the oscillator.", group = grpDP)
dpLen = input.int(5, "Over (bars)", minval = 2, group = grpDP, inline = "dp")
dpThr = input.float(1.5, "Composite move (std devs)", minval = 0.5, step = 0.1, group = grpDP, inline = "dp")
grpH = "⏫ Higher timeframe"
htfMode = input.string("Auto (4×)", "Higher timeframe", options = ["Auto (4×)", "Custom"], tooltip = "Brown pairs timeframes about 4:1.", group = grpH, inline = "h")
htfCust = input.timeframe("D", "", group = grpH, inline = "h")
grpA = "🔔 Alerts"
aReg = input.bool(true, "Regular divergences", group = grpA)
aHid = input.bool(true, "Hidden divergences", group = grpA)
aCvR = input.bool(true, "Composite vs RSI divergences", group = grpA)
aLv = input.bool(true, "Reaction at a historical level", group = grpA)
aLvT = input.bool(false, "Composite reaches a historical level", group = grpA)
aExt = input.bool(true, "New historic extreme", group = grpA)
aXZ = input.bool(true, "Average-crossover tests", group = grpA)
aTS = input.bool(true, "Two-step timing signals", group = grpA)
aDP = input.bool(true, "Price not following", group = grpA)
aX2 = input.bool(false, "Composite crosses slow SMA", group = grpA)
aRib = input.bool(false, "Fast/slow SMA cross", group = grpA)
aHtf = input.bool(false, "Higher-timeframe trend flips", group = grpA)
grpT = "🎨 Style"
cBull = input.color(#00E5A8, "Bull", group = grpT, inline = "c")
cBear = input.color(#FF4D6D, "Bear", group = grpT, inline = "c")
cNeut = input.color(#8C9EFF, "Neutral", group = grpT, inline = "c")
cMa1 = input.color(#FFB74D, "Fast SMA", group = grpT, inline = "c2")
cMa2 = input.color(#B388FF, "Slow SMA", group = grpT, inline = "c2")
cLvl = input.color(#FFD54F, "Levels", group = grpT, inline = "c2")
glow = input.bool(true, "Glow", group = grpT, inline = "fx")
ribbon = input.bool(true, "SMA ribbon", group = grpT, inline = "fx")
lblSzIn = input.string("Small", "Label size", options = ["Tiny", "Small", "Normal"], group = grpT)
cInk = #0B0F14
cText = #C9D1D9
sz = lblSzIn == "Tiny" ? size.tiny : lblSzIn == "Normal" ? size.normal : size.small
// ═════════════════════════════ CORE ═════════════════════════════
f_ci() =>
ta.mom(ta.rsi(src, rsiLen), momLen) + ta.sma(ta.rsi(src, fastLen), fastSm)
r14 = ta.rsi(src, rsiLen)
rsiMa = ta.sma(r14, rsiMaLen)
ci = f_ci()
ma1 = ta.sma(ci, ma1Len)
ma2 = ta.sma(ci, ma2Len)
sdRaw = ta.stdev(ci, 200)
sd30 = ta.stdev(ci, 30)
sd = na(sdRaw) ? nz(sd30, 10.0) : sdRaw
tolLv = lvTolK * sd
conv = xzK * sd
trendUp = ci > ma2
cLine = ci > ma1 and ci > ma2 ? cBull : ci < ma1 and ci < ma2 ? cBear : cNeut
// Higher timeframe (non-repainting: confirmed HTF bar)
htf = htfMode == "Auto (4×)" ? timeframe.from_seconds(timeframe.in_seconds() * 4) : htfCust
f_htf() =>
c = f_ci()
m = ta.sma(c, ma2Len)
[c[1], m[1]]
[hCi, hMa2] = request.security(syminfo.tickerid, htf, f_htf(), lookahead = barmerge.lookahead_on)
hUp = hCi > hMa2
hFlipUp = hUp and not hUp[1]
hFlipDn = not hUp and hUp[1]
htfTxt = na(hCi) ? "–" : (hUp ? "Above ↑" : "Below ↓")
// ═════════════════════════════ VISUALS ═════════════════════════════
hline(0, "Zero", color = color.new(cText, 75), linestyle = hline.style_dotted)
pM1 = plot(ma1, "Fast SMA", color = cMa1, linewidth = 1)
pM2 = plot(ma2, "Slow SMA", color = cMa2, linewidth = 1)
fill(pM1, pM2, color = ribbon ? (ma1 > ma2 ? color.new(cBull, 85) : color.new(cBear, 85)) : na, title = "Ribbon")
plot(glow ? ci : na, "Glow outer", color = color.new(cLine, 88), linewidth = 7, editable = false, display = display.pane)
plot(glow ? ci : na, "Glow inner", color = color.new(cLine, 72), linewidth = 4, editable = false, display = display.pane)
plot(ci, "Composite Index", color = cLine, linewidth = 2)
x2Up = ta.crossover(ci, ma2)
x2Dn = ta.crossunder(ci, ma2)
ribUp = ta.crossover(ma1, ma2)
ribDn = ta.crossunder(ma1, ma2)
rsiXUp = ta.crossover(r14, rsiMa)
rsiXDn = ta.crossunder(r14, rsiMa)
plotshape(x2Up ? ci : na, "Cross above slow SMA", shape.circle, location.absolute, cBull, size = size.tiny)
plotshape(x2Dn ? ci : na, "Cross below slow SMA", shape.circle, location.absolute, cBear, size = size.tiny)
// ═════════════════════════════ PIVOTS ═════════════════════════════
pl = ta.pivotlow(ci, lb, rb)
ph = ta.pivothigh(ci, lb, rb)
var string lastSig = "–"
var color lastSigC = cText
var int lastSigB = na
f_draw(int x1, float y1, int x2, float y2, color c, string st, string txt, bool up, string tip) =>
line.new(x1, y1, x2, y2, color = c, width = 2, style = st)
label.new(x2, y2, txt, style = up ? label.style_label_up : label.style_label_down, color = color.new(c, st == line.style_solid ? 10 : 45), textcolor = cInk, size = sz, tooltip = tip)
f_drawPrice(int x1, float y1, int x2, float y2, color c, string st, string txt, bool up) =>
line.new(x1, y1, x2, y2, color = c, width = 2, style = st, force_overlay = true)
label.new(x2, y2, txt, style = up ? label.style_label_up : label.style_label_down, color = color.new(c, 15), textcolor = cInk, size = size.tiny, force_overlay = true)
f_mark(int x, float y, string txt, color c, bool up, string tip) =>
label.new(x, y, txt, style = up ? label.style_label_up : label.style_label_down, color = color.new(c, 55), textcolor = c, size = size.tiny, tooltip = tip)
// ═════════════════════════════ 1. HISTORICAL LEVELS ═════════════════════════════
var array<float> pivVals = array.new<float>()
var array<float> lvVals = array.new<float>()
var array<int> lvCnt = array.new<int>()
f_rebuild(float tol) =>
array.clear(lvVals)
array.clear(lvCnt)
n = array.size(pivVals)
if n >= lvTouch
array<int> cnt = array.new<int>(n, 0)
for i = 0 to n - 1
vi = array.get(pivVals, i)
int c = 0
for j = 0 to n - 1
if math.abs(vi - array.get(pivVals, j)) <= tol
c += 1
array.set(cnt, i, c)
for k = 0 to lvMax - 1
int best = -1
int bc = 0
for i = 0 to n - 1
ci_ = array.get(cnt, i)
vi = array.get(pivVals, i)
bool taken = false
if array.size(lvVals) > 0
for lv in lvVals
if math.abs(vi - lv) <= 2 * tol
taken := true
if not taken and ci_ > bc
bc := ci_
best := i
if best < 0 or bc < lvTouch
break
vb = array.get(pivVals, best)
float sum = 0.0
int m = 0
for v in pivVals
if math.abs(v - vb) <= tol
sum += v
m += 1
array.push(lvVals, sum / m)
array.push(lvCnt, bc)
// Is a value at an established level? returns index or -1
f_atLevel(float v, float tol) =>
int hit = -1
if array.size(lvVals) > 0
for i = 0 to array.size(lvVals) - 1
if hit < 0 and math.abs(v - array.get(lvVals, i)) <= tol
hit := i
hit
bool lvRes = false
bool lvSup = false
float lvHitV = na
if showLv and not na(ph)
h = f_atLevel(ph, tolLv)
if h >= 0
lvRes := true
lvHitV := array.get(lvVals, h)
label.new(bar_index - rb, ph, "⟂ Res", style = label.style_label_down, color = color.new(cLvl, 15), textcolor = cInk, size = sz, tooltip = "Composite turned down at a historical level (" + str.tostring(lvHitV, "#.0") + ", " + str.tostring(array.get(lvCnt, h)) + " prior touches) — high-probability reversal zone")
if showLv and not na(pl)
h = f_atLevel(pl, tolLv)
if h >= 0
lvSup := true
lvHitV := array.get(lvVals, h)
label.new(bar_index - rb, pl, "⟂ Sup", style = label.style_label_up, color = color.new(cLvl, 15), textcolor = cInk, size = sz, tooltip = "Composite turned up at a historical level (" + str.tostring(lvHitV, "#.0") + ", " + str.tostring(array.get(lvCnt, h)) + " prior touches) — high-probability reversal zone")
if not na(ph) or not na(pl)
array.push(pivVals, not na(ph) ? ph : pl)
if not na(ph) and not na(pl)
array.push(pivVals, pl)
while array.size(pivVals) > lvMem
array.shift(pivVals)
f_rebuild(tolLv)
// Real-time: Composite reaches a level
int nearIdx = showLv ? f_atLevel(ci, tolLv) : -1
lvTouchNow = nearIdx >= 0 and nz(nearIdx[1], -1) < 0
// Historic extremes
var float extHi = na
var float extLo = na
var int extHiI = na
var int extLoI = na
var float oldHi = na
var float oldLo = na
bool newExtHi = false
bool newExtLo = false
warm = bar_index > ma2Len * 3
if warm and (na(extHi) or ci > extHi)
if not na(extHi) and bar_index - extHiI > lb
newExtHi := true
oldHi := extHi
extHi := ci
extHiI := bar_index
if warm and (na(extLo) or ci < extLo)
if not na(extLo) and bar_index - extLoI > lb
newExtLo := true
oldLo := extLo
extLo := ci
extLoI := bar_index
if showExt and newExtHi
label.new(bar_index, ci, "NEW HIGH EXTREME", style = label.style_label_down, color = color.new(cLvl, 10), textcolor = cInk, size = size.tiny, tooltip = "Composite broke its historic high (" + str.tostring(oldHi, "#.0") + "). Brown: the old range is nearly always retested once this extreme is relieved.")
if showExt and newExtLo
label.new(bar_index, ci, "NEW LOW EXTREME", style = label.style_label_up, color = color.new(cLvl, 10), textcolor = cInk, size = size.tiny, tooltip = "Composite broke its historic low (" + str.tostring(oldLo, "#.0") + "). Brown: the old range is nearly always retested once this extreme is relieved.")
// Draw levels + extremes on the last bar
var array<line> lvLines = array.new<line>()
var array<label> lvLabels = array.new<label>()
if barstate.islast
for l in lvLines
line.delete(l)
for t in lvLabels
label.delete(t)
array.clear(lvLines)
array.clear(lvLabels)
if showLv and array.size(lvVals) > 0
for i = 0 to array.size(lvVals) - 1
v = array.get(lvVals, i)
array.push(lvLines, line.new(bar_index - 1, v, bar_index, v, extend = extend.both, color = color.new(cLvl, 35), style = line.style_dashed, width = 1))
array.push(lvLabels, label.new(bar_index + 2, v, "⟂ " + str.tostring(v, "#.0") + " ×" + str.tostring(array.get(lvCnt, i)), style = label.style_label_left, color = color.new(cLvl, 80), textcolor = cLvl, size = size.tiny, tooltip = "Historical Composite level: " + str.tostring(array.get(lvCnt, i)) + " swings turned here"))
if showExt and not na(extHi)
array.push(lvLines, line.new(extHiI, extHi, bar_index, extHi, extend = extend.right, color = color.new(cLvl, 10), width = 2))
array.push(lvLabels, label.new(bar_index + 2, extHi, "Extreme " + str.tostring(extHi, "#.0"), style = label.style_label_left, color = color.new(cLvl, 20), textcolor = cInk, size = size.tiny))
if not na(oldHi)
array.push(lvLines, line.new(extHiI, oldHi, bar_index, oldHi, extend = extend.right, color = color.new(cLvl, 40), style = line.style_dotted, width = 1))
if showExt and not na(extLo)
array.push(lvLines, line.new(extLoI, extLo, bar_index, extLo, extend = extend.right, color = color.new(cLvl, 10), width = 2))
array.push(lvLabels, label.new(bar_index + 2, extLo, "Extreme " + str.tostring(extLo, "#.0"), style = label.style_label_left, color = color.new(cLvl, 20), textcolor = cInk, size = size.tiny))
if not na(oldLo)
array.push(lvLines, line.new(extLoI, oldLo, bar_index, oldLo, extend = extend.right, color = color.new(cLvl, 40), style = line.style_dotted, width = 1))
// ═════════════════════════════ 2. AVERAGE-CROSSOVER TESTS ═════════════════════════════
bool xzSell = false
bool xzBuy = false
if showXZ and not na(ph)
m1 = ma1[rb]
m2 = ma2[rb]
if math.abs(m1 - m2) <= conv and ph >= math.min(m1, m2) - tolLv and ph <= math.max(m1, m2) + tolLv and m1 < ma1[rb + 3]
xzSell := true
f_mark(bar_index - rb, ph, "✖ Sell", cBear, false, "Composite rallied into its averages as the fast average crossed down — Brown: a sharp move often follows")
if showXZ and not na(pl)
m1 = ma1[rb]
m2 = ma2[rb]
if math.abs(m1 - m2) <= conv and pl >= math.min(m1, m2) - tolLv and pl <= math.max(m1, m2) + tolLv and m1 > ma1[rb + 3]
xzBuy := true
f_mark(bar_index - rb, pl, "✖ Buy", cBull, true, "Composite held support where its averages cross up — Brown: frequently leads to a strong move")
// ═════════════════════════════ 3. DIVERGENCES (★ strong) ═════════════════════════════
var float wLoV = na
var float wLoP = na
var float wLoR = na
var int wLoI = na
var float wHiV = na
var float wHiP = na
var float wHiR = na
var int wHiI = na
bool regBull = false
bool regBear = false
bool hidBull = false
bool hidBear = false
bool cvrBull = false
bool cvrBear = false
bool strongLo = false
bool strongHi = false
if not na(pl)
xI = bar_index - rb
xP = divPrice == "Close" ? close[rb] : low[rb]
xR = r14[rb]
span = xI - wLoI
if not na(wLoI) and span >= minSpan and span <= maxSpan
st = span < strongBars
star = st ? " ★" : ""
stTip = st ? "\n★ Strong: pivots only " + str.tostring(span) + " bars apart" : ""
if showReg and xP < wLoP and pl > wLoV
regBull := true
strongLo := st
f_draw(wLoI, wLoV, xI, pl, cBull, line.style_solid, "▲ Bull" + star, true, "Regular bullish divergence: price lower low, Composite higher low" + stTip)
if onPrice
f_drawPrice(wLoI, wLoP, xI, xP, cBull, line.style_solid, "CI bull div" + star, true)
if showHid and xP > wLoP and pl < wLoV
hidBull := true
strongLo := strongLo or st
f_draw(wLoI, wLoV, xI, pl, cBull, line.style_dashed, "H Bull" + star, true, "Hidden bullish divergence: price higher low, Composite lower low (trend continuation)" + stTip)
if onPrice
f_drawPrice(wLoI, wLoP, xI, xP, color.new(cBull, 30), line.style_dashed, "CI hidden bull", true)
if showCvR and pl > wLoV and xR < wLoR
cvrBull := true
strongLo := strongLo or st
f_draw(wLoI, wLoV, xI, pl, cNeut, line.style_dotted, "CI>RSI" + star, true, "Composite vs RSI bullish divergence: Composite higher low while RSI made a lower low" + stTip)
wLoV := pl
wLoP := xP
wLoR := xR
wLoI := xI
if not na(ph)
xI = bar_index - rb
xP = divPrice == "Close" ? close[rb] : high[rb]
xR = r14[rb]
span = xI - wHiI
if not na(wHiI) and span >= minSpan and span <= maxSpan
st = span < strongBars
star = st ? " ★" : ""
stTip = st ? "\n★ Strong: pivots only " + str.tostring(span) + " bars apart" : ""
if showReg and xP > wHiP and ph < wHiV
regBear := true
strongHi := st
f_draw(wHiI, wHiV, xI, ph, cBear, line.style_solid, "▼ Bear" + star, false, "Regular bearish divergence: price higher high, Composite lower high" + stTip)
if onPrice
f_drawPrice(wHiI, wHiP, xI, xP, cBear, line.style_solid, "CI bear div" + star, false)
if showHid and xP < wHiP and ph > wHiV
hidBear := true
strongHi := strongHi or st
f_draw(wHiI, wHiV, xI, ph, cBear, line.style_dashed, "H Bear" + star, false, "Hidden bearish divergence: price lower high, Composite higher high (trend continuation)" + stTip)
if onPrice
f_drawPrice(wHiI, wHiP, xI, xP, color.new(cBear, 30), line.style_dashed, "CI hidden bear", false)
if showCvR and ph < wHiV and xR > wHiR
cvrBear := true
strongHi := strongHi or st
f_draw(wHiI, wHiV, xI, ph, cNeut, line.style_dotted, "CI<RSI" + star, false, "Composite vs RSI bearish divergence: Composite lower high while RSI made a higher high — the warning RSI misses" + stTip)
wHiV := ph
wHiP := xP
wHiR := xR
wHiI := xI
// ═════════════════════════════ 5. TWO-STEP TIMING ═════════════════════════════
var int tsDir = 0
var int tsBar = na
if x2Dn or ribDn
tsDir := -1
tsBar := bar_index
if x2Up or ribUp
tsDir := 1
tsBar := bar_index
bool tsSell = false
bool tsBuy = false
if showTS and tsDir == -1 and rsiXDn and bar_index - tsBar <= tsWait
tsSell := true
tsDir := 0
label.new(bar_index, ci, "⏱ Sell", style = label.style_label_down, color = color.new(cBear, 5), textcolor = cInk, size = sz, tooltip = "Two-step timing: Composite crossed its averages " + str.tostring(bar_index - tsBar) + " bars ago, now RSI crossed below its " + str.tostring(rsiMaLen) + " SMA")
if showTS and tsDir == 1 and rsiXUp and bar_index - tsBar <= tsWait
tsBuy := true
tsDir := 0
label.new(bar_index, ci, "⏱ Buy", style = label.style_label_up, color = color.new(cBull, 5), textcolor = cInk, size = sz, tooltip = "Two-step timing: Composite crossed its averages " + str.tostring(bar_index - tsBar) + " bars ago, now RSI crossed above its " + str.tostring(rsiMaLen) + " SMA")
if tsDir != 0 and bar_index - tsBar > tsWait
tsDir := 0
// ═════════════════════════════ 6. PRICE NOT FOLLOWING ═════════════════════════════
dOsc = ci - ci[dpLen]
dPx = close - close[dpLen]
sdO = ta.stdev(dOsc, 100)
sdP = ta.stdev(dPx, 100)
zO = sdO > 0 ? dOsc / sdO : 0.0
zP = sdP > 0 ? dPx / sdP : 0.0
dpBearRaw = zO >= dpThr and zP <= zO * 0.3
dpBullRaw = zO <= -dpThr and zP >= zO * 0.3
dpBear = showDP and dpBearRaw and not dpBearRaw[1]
dpBull = showDP and dpBullRaw and not dpBullRaw[1]
plotshape(dpBear ? ci : na, "Price not following (bearish)", shape.xcross, location.absolute, cBear, size = size.tiny)
plotshape(dpBull ? ci : na, "Price not following (bullish)", shape.xcross, location.absolute, cBull, size = size.tiny)
// ═════════════════════════════ LAST SIGNAL ═════════════════════════════
string sigNow = tsSell ? "⏱ Sell timing" : tsBuy ? "⏱ Buy timing" : regBull ? "▲ Bull divergence" + (strongLo ? " ★" : "") : regBear ? "▼ Bear divergence" + (strongHi ? " ★" : "") : cvrBear ? "CI<RSI bearish" : cvrBull ? "CI>RSI bullish" : lvRes ? "⟂ Level resistance" : lvSup ? "⟂ Level support" : xzSell ? "✖ Crossover sell" : xzBuy ? "✖ Crossover buy" : hidBull ? "Hidden bull" : hidBear ? "Hidden bear" : newExtHi ? "New high extreme" : newExtLo ? "New low extreme" : dpBear ? "Price not following ↓" : dpBull ? "Price not following ↑" : na
color sigCol = (tsBuy or regBull or cvrBull or lvSup or xzBuy or hidBull or newExtLo or dpBull) and not (tsSell or regBear or cvrBear) ? cBull : cBear
if not na(sigNow)
lastSig := sigNow
lastSigC := sigCol
lastSigB := bar_index
// ═════════════════════════════ DASHBOARD ═════════════════════════════
grpTb = "🗂 Dashboard"
tbPosIn = input.string("Top right", "Position", options = ["Top right", "Top center", "Top left", "Middle right", "Middle left", "Bottom right", "Bottom center", "Bottom left", "Hidden"], group = grpTb)
tbTitle = input.string("COMPOSITE", "Title", group = grpTb)
tbLayout = input.string("Vertical", "Layout", options = ["Vertical", "Horizontal"], group = grpTb, inline = "l1")
tbSizeIn = input.string("Small", "Size", options = ["Tiny", "Small", "Normal", "Large"], group = grpTb, inline = "l1")
tbTheme = input.string("Midnight", "Theme", options = ["Midnight", "Neon", "Glass", "Light", "Custom"], group = grpTb)
tbHeadSty = input.string("Bold", "Header font", options = ["Plain", "Bold", "Bold Italic", "Monospace", "Small caps", "Wide"], group = grpTb, inline = "f1")
tbBodySty = input.string("Plain", "Body font", options = ["Plain", "Bold", "Bold Italic", "Monospace", "Small caps", "Wide"], group = grpTb, inline = "f1")
tbFamily = input.string("Default", "Base typeface", options = ["Default", "Monospace"], group = grpTb)
tbOpacity = input.int(92, "Background opacity %", minval = 0, maxval = 100, group = grpTb, inline = "o0")
tbBorderW = input.int(1, "Border", minval = 0, maxval = 5, group = grpTb, inline = "o0")
tbSigCol = input.bool(true, "Color values by signal", group = grpTb, inline = "o1")
tbAccent = input.bool(true, "Trend-colored header", group = grpTb, inline = "o1")
cuBg = input.color(#0D1117, "Custom ▸ bg", group = grpTb, inline = "cu")
cuBorder = input.color(#30363D, "border", group = grpTb, inline = "cu")
cuKey = input.color(#8B949E, "labels", group = grpTb, inline = "cu")
cuVal = input.color(#E6EDF3, "values", group = grpTb, inline = "cu")
rwVs = input.bool(true, "vs slow SMA", group = grpTb, inline = "rw0")
rwRib = input.bool(true, "Ribbon", group = grpTb, inline = "rw0")
rwHtf = input.bool(true, "HTF", group = grpTb, inline = "rw0")
rwLvl = input.bool(true, "Nearest level", group = grpTb, inline = "rw1")
rwExt = input.bool(true, "Extremes", group = grpTb, inline = "rw1")
rwLast = input.bool(true, "Last signal", group = grpTb, inline = "rw1")
thBg = tbTheme == "Neon" ? #0A0118 : tbTheme == "Glass" ? #000000 : tbTheme == "Light" ? #FFFFFF : tbTheme == "Custom" ? cuBg : #0D1117
thBorder = tbTheme == "Neon" ? #7C4DFF : tbTheme == "Glass" ? #FFFFFF : tbTheme == "Light" ? #D0D7DE : tbTheme == "Custom" ? cuBorder : #30363D
thKey = tbTheme == "Neon" ? #8C9EFF : tbTheme == "Glass" ? #B8C1CC : tbTheme == "Light" ? #57606A : tbTheme == "Custom" ? cuKey : #8B949E
thVal = tbTheme == "Neon" ? #F5F3FF : tbTheme == "Glass" ? #FFFFFF : tbTheme == "Light" ? #1F2328 : tbTheme == "Custom" ? cuVal : #E6EDF3
thBgT = tbTheme == "Glass" ? math.max(100 - tbOpacity, 75) : 100 - tbOpacity
thBordT = tbTheme == "Glass" ? 70 : 0
tbSize = tbSizeIn == "Tiny" ? size.tiny : tbSizeIn == "Normal" ? size.normal : tbSizeIn == "Large" ? size.large : size.small
tbFont = tbFamily == "Monospace" ? font.family_monospace : font.family_default
tbHoriz = tbLayout == "Horizontal"
tbPos = switch tbPosIn
"Top center" => position.top_center
"Top left" => position.top_left
"Middle right" => position.middle_right
"Middle left" => position.middle_left
"Bottom right" => position.bottom_right
"Bottom center" => position.bottom_center
"Bottom left" => position.bottom_left
=> position.top_right
// Unicode "fonts" (Pine tables only ship Default + Monospace typefaces)
var array<string> F_SRC = array.from("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9")
var array<string> F_BOLD = array.from("𝗔", "𝗕", "𝗖", "𝗗", "𝗘", "𝗙", "𝗚", "𝗛", "𝗜", "𝗝", "𝗞", "𝗟", "𝗠", "𝗡", "𝗢", "𝗣", "𝗤", "𝗥", "𝗦", "𝗧", "𝗨", "𝗩", "𝗪", "𝗫", "𝗬", "𝗭", "𝗮", "𝗯", "𝗰", "𝗱", "𝗲", "𝗳", "𝗴", "𝗵", "𝗶", "𝗷", "𝗸", "𝗹", "𝗺", "𝗻", "𝗼", "𝗽", "𝗾", "𝗿", "𝘀", "𝘁", "𝘂", "𝘃", "𝘄", "𝘅", "𝘆", "𝘇", "𝟬", "𝟭", "𝟮", "𝟯", "𝟰", "𝟱", "𝟲", "𝟳", "𝟴", "𝟵")
var array<string> F_BI = array.from("𝘼", "𝘽", "𝘾", "𝘿", "𝙀", "𝙁", "𝙂", "𝙃", "𝙄", "𝙅", "𝙆", "𝙇", "𝙈", "𝙉", "𝙊", "𝙋", "𝙌", "𝙍", "𝙎", "𝙏", "𝙐", "𝙑", "𝙒", "𝙓", "𝙔", "𝙕", "𝙖", "𝙗", "𝙘", "𝙙", "𝙚", "𝙛", "𝙜", "𝙝", "𝙞", "𝙟", "𝙠", "𝙡", "𝙢", "𝙣", "𝙤", "𝙥", "𝙦", "𝙧", "𝙨", "𝙩", "𝙪", "𝙫", "𝙬", "𝙭", "𝙮", "𝙯", "𝟬", "𝟭", "𝟮", "𝟯", "𝟰", "𝟱", "𝟲", "𝟳", "𝟴", "𝟵")
var array<string> F_MONO = array.from("𝙰", "𝙱", "𝙲", "𝙳", "𝙴", "𝙵", "𝙶", "𝙷", "𝙸", "𝙹", "𝙺", "𝙻", "𝙼", "𝙽", "𝙾", "𝙿", "𝚀", "𝚁", "𝚂", "𝚃", "𝚄", "𝚅", "𝚆", "𝚇", "𝚈", "𝚉", "𝚊", "𝚋", "𝚌", "𝚍", "𝚎", "𝚏", "𝚐", "𝚑", "𝚒", "𝚓", "𝚔", "𝚕", "𝚖", "𝚗", "𝚘", "𝚙", "𝚚", "𝚛", "𝚜", "𝚝", "𝚞", "𝚟", "𝚠", "𝚡", "𝚢", "𝚣", "𝟶", "𝟷", "𝟸", "𝟹", "𝟺", "𝟻", "𝟼", "𝟽", "𝟾", "𝟿")
var array<string> F_SC = array.from("ᴀ", "ʙ", "ᴄ", "ᴅ", "ᴇ", "ꜰ", "ɢ", "ʜ", "ɪ", "ᴊ", "ᴋ", "ʟ", "ᴍ", "ɴ", "ᴏ", "ᴘ", "ǫ", "ʀ", "ꜱ", "ᴛ", "ᴜ", "ᴠ", "ᴡ", "x", "ʏ", "ᴢ", "ᴀ", "ʙ", "ᴄ", "ᴅ", "ᴇ", "ꜰ", "ɢ", "ʜ", "ɪ", "ᴊ", "ᴋ", "ʟ", "ᴍ", "ɴ", "ᴏ", "ᴘ", "ǫ", "ʀ", "ꜱ", "ᴛ", "ᴜ", "ᴠ", "ᴡ", "x", "ʏ", "ᴢ", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9")
var array<string> F_WIDE = array.from("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9")
f_sty(string s, string sty) =>
string out = s
if sty != "Plain"
array<string> m = F_WIDE
if sty == "Bold"
m := F_BOLD
else if sty == "Bold Italic"
m := F_BI
else if sty == "Monospace"
m := F_MONO
else if sty == "Small caps"
m := F_SC
out := ""
for c in str.split(s, "")
int ix = array.indexof(F_SRC, c)
out += ix >= 0 ? array.get(m, ix) : c
out
var table dash = table.new(tbPos, tbHoriz ? 7 : 2, tbHoriz ? 2 : 7, bgcolor = color.new(thBg, thBgT), border_color = color.new(thBorder, thBordT), border_width = tbBorderW, frame_color = color.new(thBorder, thBordT), frame_width = tbBorderW)
f_cell(int col, int row, string txt, color tc, string sty, string halign, color bg) =>
table.cell(dash, col, row, f_sty(txt, sty), text_color = tc, text_size = tbSize, text_halign = halign, text_font_family = tbFont, bgcolor = bg)
f_head(string title, string status, color accent) =>
color hb = tbAccent ? color.new(accent, 70) : color.new(thBorder, thBordT)
f_cell(0, 0, title, thVal, tbHeadSty, text.align_left, hb)
f_cell(tbHoriz ? 0 : 1, tbHoriz ? 1 : 0, status, tbSigCol ? accent : thVal, tbHeadSty, tbHoriz ? text.align_left : text.align_right, hb)
f_row(int i, bool show, string k, string v, color c) =>
int n = i
if show
f_cell(tbHoriz ? n : 0, tbHoriz ? 0 : n, k, thKey, tbBodySty, text.align_left, na)
f_cell(tbHoriz ? n : 1, tbHoriz ? 1 : n, v, tbSigCol ? c : thVal, tbBodySty, tbHoriz ? text.align_left : text.align_right, na)
n += 1
n
if barstate.islast and tbPosIn != "Hidden"
table.clear(dash, 0, 0, tbHoriz ? 6 : 1, tbHoriz ? 1 : 6)
f_head(tbTitle, str.tostring(ci, "#.0"), cLine)
nearTxt = "–"
if array.size(lvVals) > 0
float best = na
int bc = 0
for k = 0 to array.size(lvVals) - 1
v = array.get(lvVals, k)
if na(best) or math.abs(v - ci) < math.abs(best - ci)
best := v
bc := array.get(lvCnt, k)
nearTxt := str.tostring(best, "#.0") + " ×" + str.tostring(bc) + (best > ci ? " above" : " below")
int i = 1
i := f_row(i, rwVs, "vs slow SMA", trendUp ? "Above ↑" : "Below ↓", trendUp ? cBull : cBear)
i := f_row(i, rwRib, "Ribbon", ma1 > ma2 ? "Bullish" : "Bearish", ma1 > ma2 ? cBull : cBear)
i := f_row(i, rwHtf, "HTF " + htf, htfTxt, na(hCi) ? thKey : hUp ? cBull : cBear)
i := f_row(i, rwLvl, "Nearest level", nearTxt, cLvl)
i := f_row(i, rwExt, "Extremes", na(extHi) ? "–" : str.tostring(extLo, "#.0") + " / " + str.tostring(extHi, "#.0"), cLvl)
i := f_row(i, rwLast, "Last signal", lastSig + (na(lastSigB) ? "" : " · " + str.tostring(bar_index - lastSigB) + " bars"), lastSigC)
// ═════════════════════════════ ALERTS ═════════════════════════════
string am = ""
if aReg and regBull
am += "▲ Regular BULLISH divergence" + (strongLo ? " ★ STRONG" : "") + "\n"
if aReg and regBear
am += "▼ Regular BEARISH divergence" + (strongHi ? " ★ STRONG" : "") + "\n"
if aHid and hidBull
am += "↗ Hidden BULLISH divergence (uptrend continuation)\n"
if aHid and hidBear
am += "↘ Hidden BEARISH divergence (downtrend continuation)\n"
if aCvR and cvrBull
am += "🔷 Composite vs RSI BULLISH divergence\n"
if aCvR and cvrBear
am += "🔶 Composite vs RSI BEARISH divergence — RSI is not warning\n"
if aLv and lvRes
am += "⟂ Composite turned DOWN at historical level " + str.tostring(lvHitV, "#.0") + "\n"
if aLv and lvSup
am += "⟂ Composite turned UP at historical level " + str.tostring(lvHitV, "#.0") + "\n"
if aLvT and lvTouchNow
am += "📏 Composite reached historical level " + str.tostring(array.get(lvVals, nearIdx), "#.0") + "\n"
if aExt and newExtHi
am += "🚩 NEW historic HIGH extreme on the Composite (old " + str.tostring(oldHi, "#.0") + " likely retested later)\n"
if aExt and newExtLo
am += "🚩 NEW historic LOW extreme on the Composite (old " + str.tostring(oldLo, "#.0") + " likely retested later)\n"
if aXZ and xzSell
am += "✖ SELL: Composite rallied into its averages at the bearish crossover\n"
if aXZ and xzBuy
am += "✖ BUY: Composite held its averages at the bullish crossover\n"
if aTS and tsSell
am += "⏱ SELL timing: Composite warned, RSI confirmed\n"
if aTS and tsBuy
am += "⏱ BUY timing: Composite warned, RSI confirmed\n"
if aDP and dpBear
am += "⚖ Composite rising but price not following — bearish\n"
if aDP and dpBull
am += "⚖ Composite falling but price not following — bullish\n"
if aX2 and x2Up
am += "🟢 Composite crossed above its " + str.tostring(ma2Len) + " SMA\n"
if aX2 and x2Dn
am += "🔴 Composite crossed below its " + str.tostring(ma2Len) + " SMA\n"
if aRib and ribUp
am += "🟢 Fast SMA crossed above slow SMA\n"
if aRib and ribDn
am += "🔴 Fast SMA crossed below slow SMA\n"
if aHtf and hFlipUp
am += "⏫ Higher timeframe (" + htf + ") Composite turned UP\n"
if aHtf and hFlipDn
am += "⏬ Higher timeframe (" + htf + ") Composite turned DOWN\n"
if am != ""
alert(syminfo.ticker + " · " + timeframe.period + " · close " + str.tostring(close, format.mintick) + " · CI " + str.tostring(ci, "#.0") + " · HTF " + htf + " " + htfTxt + "\n" + am, alert.freq_once_per_bar_close)
alertcondition(regBull, "Regular bullish divergence", "{{ticker}} {{interval}}: Composite regular bullish divergence")
alertcondition(regBear, "Regular bearish divergence", "{{ticker}} {{interval}}: Composite regular bearish divergence")
alertcondition(hidBull, "Hidden bullish divergence", "{{ticker}} {{interval}}: Composite hidden bullish divergence")
alertcondition(hidBear, "Hidden bearish divergence", "{{ticker}} {{interval}}: Composite hidden bearish divergence")
alertcondition(cvrBull, "Composite vs RSI bullish", "{{ticker}} {{interval}}: Composite vs RSI bullish divergence")
alertcondition(cvrBear, "Composite vs RSI bearish", "{{ticker}} {{interval}}: Composite vs RSI bearish divergence")
alertcondition(lvRes or lvSup, "Reaction at historical level", "{{ticker}} {{interval}}: Composite reversed at a historical level")
alertcondition(newExtHi or newExtLo, "New historic extreme", "{{ticker}} {{interval}}: Composite made a new historic extreme")
alertcondition(xzSell, "Crossover test: sell", "{{ticker}} {{interval}}: Composite rallied into its bearish average crossover")
alertcondition(xzBuy, "Crossover test: buy", "{{ticker}} {{interval}}: Composite held its bullish average crossover")
alertcondition(tsSell, "Two-step timing: sell", "{{ticker}} {{interval}}: Composite warned, RSI confirmed — sell timing")
alertcondition(tsBuy, "Two-step timing: buy", "{{ticker}} {{interval}}: Composite warned, RSI confirmed — buy timing")
alertcondition(dpBear or dpBull, "Price not following", "{{ticker}} {{interval}}: Composite moving disproportionately to price")