Ultimate Confluence NQ Composite

profitgang · study · 220 行 · 点赞 5 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=6
indicator("Ultimate Confluence — NQ Composite", overlay=false)
 
f_clamp(x, lo, hi) => math.min(math.max(x, lo), hi)
f_scoreBg(v) => v >= 60 ? color.new(color.green, 0) : v <= 40 ? color.new(color.red, 0) : color.new(color.gray, 70)
f_arrow(v) => v > 50 ? "↑" : v < 50 ? "↓" : "→"
f_safeStr(x) => str.tostring(nz(x))
f_fmt(x, fmt) => na(x) ? "—" : str.tostring(x, fmt)
f_ago(n) => n == 0 ? "this bar" : n == 1 ? "1 bar ago" : str.tostring(n) + " bars ago"
 
// Non-repainting cross-timeframe request.
// Higher TF -> value from the last CLOSED higher-TF bar (history matches what you'd have seen live).
// Same/lower TF -> passes straight through.
f_secure(tf, src) =>
    tfn = tf == "" ? timeframe.period : tf
    higher = timeframe.in_seconds(tfn) > timeframe.in_seconds()
    request.security(syminfo.tickerid, tfn, src[higher ? 1 : 0], lookahead = higher ? barmerge.lookahead_on : barmerge.lookahead_off)
 
trendWeight = input.float(1.3, "Trend Weight", minval=0.0, step=0.1)
momWeight = input.float(1.2, "Momentum Weight", minval=0.0, step=0.1)
structWeight = input.float(1.0, "Structure Weight", minval=0.0, step=0.1)
volWeight = input.float(1.0, "Volume Weight", minval=0.0, step=0.1)
compositeSmooth = input.int(3, "Composite Smoothing Length", minval=1)
buyLevel = input.float(58.0, "Buy Level", step=0.5)
sellLevel = input.float(42.0, "Sell Level", step=0.5)
mtfMinAgreement = input.int(2, "MTF Min Agreement", minval=0, maxval=3)
requireEma21Buy = input.bool(false, "Require close > EMA21 for buys")
requireEma21Sell = input.bool(false, "Require close < EMA21 for sells")
requireRelVol = input.bool(false, "Require relVol > 1.0")
showClimax = input.bool(true, "Show CLX climax labels")
colorBg = input.bool(true, "Color Background")
pivotLen = input.int(5, "Structure Pivot Length", minval=1)
structLookback = input.int(100, "Structure Lookback", minval=10)
climaxMult = input.float(2.5, "Climax Multiple", minval=1.0, step=0.1)
quietAtrMin = input.float(12.0, "Quiet ATR Minimum (points)", minval=0.0, step=0.25)
atrGateTf = input.timeframe("", "Quiet ATR Timeframe (blank = chart)")
mtfTf1 = input.timeframe("5", "MTF Timeframe 1")
mtfTf2 = input.timeframe("15", "MTF Timeframe 2")
mtfTf3 = input.timeframe("60", "MTF Timeframe 3")
confirmClose = input.bool(true, "Signals only on bar close (no intrabar flicker)")
 
ema9 = ta.ema(close, 9)
ema21 = ta.ema(close, 21)
ema50 = ta.ema(close, 50)
ema200 = ta.ema(close, 200)
atr14 = ta.atr(14)
 
// ── TREND ──
bullStack = (close > ema9 ? 1 : 0) + (ema9 > ema21 ? 1 : 0) + (ema21 > ema50 ? 1 : 0) + (ema50 > ema200 ? 1 : 0)
bearStack = (close < ema9 ? 1 : 0) + (ema9 < ema21 ? 1 : 0) + (ema21 < ema50 ? 1 : 0) + (ema50 < ema200 ? 1 : 0)
stackScore = f_clamp(50.0 + 12.5 * (bullStack - bearStack), 0.0, 100.0)
trendCenter = ta.ema(close, 20)
trendUpper = trendCenter + 1.5 * atr14
trendLower = trendCenter - 1.5 * atr14
var int bandDir = 1
bandDir := close > nz(trendUpper[1], trendUpper) ? 1 : close < nz(trendLower[1], trendLower) ? -1 : nz(bandDir[1], 1)
bandScore = bandDir == 1 ? 100.0 : 0.0
trendScore = f_clamp((stackScore + bandScore) / 2.0, 0.0, 100.0)
trendDir = trendScore >= 50.0 ? 1 : -1
 
// ── MOMENTUM ──
rsiScore = f_clamp(ta.rsi(close, 14), 0.0, 100.0)
stochScore = f_clamp(nz(ta.sma(ta.stoch(close, high, low, 14), 3), 50.0), 0.0, 100.0)
[_, _, macdHist] = ta.macd(close, 12, 26, 9)
macdScore = f_clamp(50.0 + (atr14 != 0.0 ? (macdHist / atr14) * 25.0 : 0.0), 0.0, 100.0)
cciScore = f_clamp(50.0 + ta.cci(hlc3, 20) / 4.0, 0.0, 100.0)
weightRSI = input.float(1.0, "Momentum Weight RSI", minval=0.0, step=0.1)
weightStoch = input.float(1.0, "Momentum Weight Stoch", minval=0.0, step=0.1)
weightMACD = input.float(1.0, "Momentum Weight MACD", minval=0.0, step=0.1)
weightCCI = input.float(1.0, "Momentum Weight CCI", minval=0.0, step=0.1)
momWsum = math.max(weightRSI + weightStoch + weightMACD + weightCCI, 0.0001)
momentumScore = (rsiScore * weightRSI + stochScore * weightStoch + macdScore * weightMACD + cciScore * weightCCI) / momWsum
momentumRising = momentumScore > nz(momentumScore[1], momentumScore)
 
// ── STRUCTURE ──
ph = ta.pivothigh(high, pivotLen, pivotLen)
pl = ta.pivotlow(low, pivotLen, pivotLen)
var float lastRes = na
var float lastSup = na
if not na(ph)
    lastRes := ph
if not na(pl)
    lastSup := pl
if not na(lastRes) and close > lastRes
    lastRes := na
if not na(lastSup) and close < lastSup
    lastSup := na
upperLevel = nz(lastRes, ta.highest(high, structLookback))
lowerLevel = nz(lastSup, ta.lowest(low, structLookback))
range_ = math.max(upperLevel - lowerLevel, syminfo.mintick)
structScore = f_clamp(100.0 * (upperLevel - close) / range_, 0.0, 100.0)
 
// ── VOLUME ──
volSma20 = ta.sma(volume, 20)
relVol = volume / math.max(nz(volSma20, 1.0), 1.0)
volBias = close > open ? 1.0 : close < open ? -1.0 : 0.0
// Only ABOVE-average volume moves the score. Below-average volume = neutral 50
// (previously a quiet red candle scored bullish and a quiet green candle scored bearish).
volScore = f_clamp(50.0 + 50.0 * f_clamp(math.max(relVol - 1.0, 0.0) * volBias, -1.0, 1.0), 0.0, 100.0)
climaxAvg = ta.sma(volume, 30)
climaxVol = volume > climaxMult * nz(climaxAvg, volume)
// Wyckoff naming: heavy-volume DOWN bar = selling climax, heavy-volume UP bar = buying climax.
sellingClimax = climaxVol and close < open
buyingClimax = climaxVol and close > open
if sellingClimax
    volScore := 0.0
if buyingClimax
    volScore := 100.0
 
// ── REGIME ──
tfGate = atrGateTf == "" ? timeframe.period : atrGateTf
gateAtr = f_secure(tfGate, ta.atr(14))
atrAvg = ta.sma(atr14, 50)
atrRatio = atrAvg != 0.0 ? atr14 / atrAvg : 1.0
quietRegime = na(gateAtr) or gateAtr < quietAtrMin
expanding = atrRatio > 1.0 and not quietRegime
contracting = atrRatio <= 1.0 and not quietRegime
regimeFlipQuiet = quietRegime and not (bar_index > 0 and quietRegime[1])
regimeFlipExpanding = expanding and not (bar_index > 0 and expanding[1])
 
// ── MTF (non-repainting) ──
mtfDir1 = f_secure(mtfTf1, close > ta.ema(close, 50) ? 1 : -1)
mtfDir2 = f_secure(mtfTf2, close > ta.ema(close, 50) ? 1 : -1)
mtfDir3 = f_secure(mtfTf3, close > ta.ema(close, 50) ? 1 : -1)
mtfBull = (mtfDir1 == 1 ? 1 : 0) + (mtfDir2 == 1 ? 1 : 0) + (mtfDir3 == 1 ? 1 : 0)
mtfBear = (mtfDir1 == -1 ? 1 : 0) + (mtfDir2 == -1 ? 1 : 0) + (mtfDir3 == -1 ? 1 : 0)
fullBull = mtfBull == 3
fullBear = mtfBear == 3
 
// ── COMPOSITE + SIGNALS ──
sumW = math.max(trendWeight + momWeight + structWeight + volWeight, 0.0001)
raw = (trendScore * trendWeight + momentumScore * momWeight + structScore * structWeight + volScore * volWeight) / sumW
composite = ta.ema(f_clamp(raw, 0.0, 100.0), compositeSmooth)
priceBullOk = not requireEma21Buy or close > ema21
priceBearOk = not requireEma21Sell or close < ema21
relVolOk = not requireRelVol or relVol > 1.0
buyCross = ta.crossover(composite, buyLevel)
sellCross = ta.crossunder(composite, sellLevel)
buySetup = not quietRegime and priceBullOk and relVolOk and buyCross and mtfBull >= mtfMinAgreement and momentumRising
sellSetup = not quietRegime and priceBearOk and relVolOk and sellCross and mtfBear >= mtfMinAgreement and not momentumRising
barOk = not confirmClose or barstate.isconfirmed
buySignal = buySetup and barOk
sellSignal = sellSetup and barOk
stateText = quietRegime ? "NO TRADE" : buySignal ? "BUY" : sellSignal ? "SELL" : buySetup ? "BUY (forming)" : sellSetup ? "SELL (forming)" : "WAIT"
 
bsBuy = ta.barssince(buySignal)
bsSell = ta.barssince(sellSignal)
lastSigText = na(bsBuy) and na(bsSell) ? "Last: none" : na(bsSell) or (not na(bsBuy) and bsBuy <= bsSell) ? "Last: BUY " + f_ago(bsBuy) : "Last: SELL " + f_ago(bsSell)
compBg = composite >= buyLevel ? color.new(color.green, 0) : composite <= sellLevel ? color.new(color.red, 0) : color.new(color.gray, 70)
 
// ── PLOTS ──
plot(composite, "Composite", color=color.new(color.aqua, 0), linewidth=3)
plot(trendScore, "Trend Score", color=color.new(color.orange, 0), linewidth=1)
plot(momentumScore, "Momentum Score", color=color.new(color.lime, 0), linewidth=1)
plot(structScore, "Structure Score", color=color.new(color.fuchsia, 0), linewidth=1)
plot(volScore, "Volume Score", color=color.new(color.yellow, 0), linewidth=1)
hline(25, "25", color=color.new(color.gray, 70))
hline(50, "50", color=color.new(color.gray, 70))
hline(75, "75", color=color.new(color.gray, 70))
 
bgcolor(colorBg ? (quietRegime ? color.new(color.gray, 85) : expanding ? (trendDir == 1 ? color.new(color.green, 90) : color.new(color.red, 90)) : color.new(color.blue, 90)) : na)
 
plot(upperLevel, "Resistance", color=color.red, style=plot.style_linebr, force_overlay=true)
plot(lowerLevel, "Support", color=color.green, style=plot.style_linebr, force_overlay=true)
plotshape(buySignal, title="BUY", style=shape.triangleup, location=location.belowbar, color=color.lime, text="BUY", size=size.small, force_overlay=true)
plotshape(sellSignal, title="SELL", style=shape.triangledown, location=location.abovebar, color=color.red, text="SELL", size=size.small, force_overlay=true)
plotshape(showClimax and sellingClimax, title="Selling Climax", style=shape.labelup, location=location.belowbar, color=color.new(color.teal, 0), text="CLX", textcolor=color.white, size=size.tiny, force_overlay=true)
plotshape(showClimax and buyingClimax, title="Buying Climax", style=shape.labeldown, location=location.abovebar, color=color.new(color.maroon, 0), text="CLX", textcolor=color.white, size=size.tiny, force_overlay=true)
 
// ── ALERTS ──
alertcondition(buySignal, title="BUY", message="BUY {{ticker}} {{interval}} {{close}}")
alertcondition(sellSignal, title="SELL", message="SELL {{ticker}} {{interval}} {{close}}")
// Fire once when full agreement starts, not on every bar while it lasts
alertcondition(fullBull and not fullBull[1], title="Full Bull MTF", message="FULL BULL MTF {{ticker}} {{interval}} {{close}}")
alertcondition(fullBear and not fullBear[1], title="Full Bear MTF", message="FULL BEAR MTF {{ticker}} {{interval}} {{close}}")
alertcondition(regimeFlipQuiet, title="Regime Flip QUIET", message="REGIME QUIET {{ticker}} {{interval}} {{close}}")
alertcondition(regimeFlipExpanding, title="Regime Flip EXPANDING", message="REGIME EXPANDING {{ticker}} {{interval}} {{close}}")
 
// ── DASHBOARD ──
var table dash = table.new(position.top_right, 3, 10, bgcolor=color.new(color.black, 82), frame_color=color.new(color.gray, 60), frame_width=1, border_color=color.new(color.gray, 70), border_width=1)
if barstate.islast
    table.cell(dash, 0, 0, syminfo.ticker + " " + timeframe.period, text_color=color.white, bgcolor=color.new(color.blue, 55), text_size=size.small)
    table.cell(dash, 1, 0, "VALUE", text_color=color.white, bgcolor=color.new(color.blue, 55), text_size=size.small)
    table.cell(dash, 2, 0, "NOTE", text_color=color.white, bgcolor=color.new(color.blue, 55), text_size=size.small)
 
    table.cell(dash, 0, 1, "TREND", text_color=color.white)
    table.cell(dash, 1, 1, f_safeStr(math.round(trendScore)) + " " + f_arrow(trendScore), text_color=color.white, bgcolor=f_scoreBg(trendScore))
    table.cell(dash, 2, 1, trendDir == 1 ? "BULL" : "BEAR", text_color=color.white)
 
    table.cell(dash, 0, 2, "MOMENTUM", text_color=color.white)
    table.cell(dash, 1, 2, f_safeStr(math.round(momentumScore)) + " " + f_arrow(momentumScore), text_color=color.white, bgcolor=f_scoreBg(momentumScore))
    table.cell(dash, 2, 2, momentumRising ? "RISING" : "FALLING", text_color=color.white)
 
    table.cell(dash, 0, 3, "STRUCTURE", text_color=color.white)
    table.cell(dash, 1, 3, f_safeStr(math.round(structScore)) + " " + f_arrow(structScore), text_color=color.white, bgcolor=f_scoreBg(structScore))
    table.cell(dash, 2, 3, "S=" + f_fmt(lowerLevel, format.mintick) + " R=" + f_fmt(upperLevel, format.mintick), text_color=color.white)
 
    table.cell(dash, 0, 4, "VOLUME", text_color=color.white)
    table.cell(dash, 1, 4, f_safeStr(math.round(volScore)) + " " + f_arrow(volScore), text_color=color.white, bgcolor=f_scoreBg(volScore))
    table.cell(dash, 2, 4, "RV=" + f_fmt(relVol, "#.##"), text_color=color.white)
 
    table.cell(dash, 0, 5, "REGIME", text_color=color.white)
    table.cell(dash, 1, 5, quietRegime ? "QUIET" : expanding ? "EXPANDING" : "CONTRACTING", text_color=color.white, bgcolor=quietRegime ? color.new(color.gray, 0) : expanding ? color.new(color.green, 0) : color.new(color.blue, 0))
    table.cell(dash, 2, 5, "ATR=" + f_fmt(gateAtr, "#.##") + (atrGateTf == "" ? "" : " (" + atrGateTf + ")"), text_color=color.white)
 
    table.cell(dash, 0, 6, "MTF " + mtfTf1 + "/" + mtfTf2 + "/" + mtfTf3, text_color=color.white)
    table.cell(dash, 1, 6, f_safeStr(mtfBull) + "/3 BULL", text_color=color.white, bgcolor=mtfBull >= mtfMinAgreement ? color.new(color.green, 20) : color.new(color.gray, 70))
    table.cell(dash, 2, 6, f_safeStr(mtfBear) + "/3 BEAR", text_color=color.white, bgcolor=mtfBear >= mtfMinAgreement ? color.new(color.red, 20) : color.new(color.gray, 70))
 
    table.cell(dash, 0, 7, "COMPOSITE", text_color=color.white)
    table.cell(dash, 1, 7, f_safeStr(math.round(composite)) + " " + f_arrow(composite), text_color=color.white, bgcolor=compBg)
    table.cell(dash, 2, 7, "BUY " + f_safeStr(buyLevel) + " / SELL " + f_safeStr(sellLevel), text_color=color.white)
 
    table.cell(dash, 0, 8, "STATE", text_color=color.white)
    table.cell(dash, 1, 8, stateText, text_color=color.white, bgcolor=buySetup ? color.new(color.green, 0) : sellSetup ? color.new(color.red, 0) : quietRegime ? color.new(color.gray, 0) : color.new(color.orange, 0))
    table.cell(dash, 2, 8, lastSigText, text_color=color.white)
 
    table.cell(dash, 0, 9, "CLX", text_color=color.white)
    table.cell(dash, 1, 9, buyingClimax ? "BUYING" : sellingClimax ? "SELLING" : "NONE", text_color=color.white)
    table.cell(dash, 2, 9, "Quiet < " + f_safeStr(quietAtrMin), text_color=color.white)

← 返回列表