Squeeze Momentum + Signal Layer [SQZMOM_SL]

durgaprasadch16 · study · 210 行 · 点赞 25 · TradingView 原页

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

Pine Script

//@version=6
indicator("Squeeze Momentum + Signal Layer [SQZMOM_SL]", shorttitle="SQZMOM_SL", overlay=false)
 
// =====================================================================
// SQUEEZE MOMENTUM + SIGNAL LAYER
// Pine Script v6
// Original signal and momentum logic preserved.
// Dashboard simplified to six requested metrics.
// =====================================================================
 
// ------------------------------ INPUTS --------------------------------
grpSq = "Squeeze / Momentum"
bbLen        = input.int(20, "BB Length", minval=1, group=grpSq)
bbMult       = input.float(2.0, "BB MultFactor", step=0.1, group=grpSq)
legacyBB     = input.bool(false, "Legacy mode: use KC mult for BB", group=grpSq)
kcLen        = input.int(20, "KC Length", minval=1, group=grpSq)
kcMult       = input.float(1.5, "KC MultFactor", step=0.1, group=grpSq)
useTR        = input.bool(true, "Use TrueRange (KC)", group=grpSq)
 
grpSig = "Signal Rules"
relWin        = input.int(3, "Squeeze release valid for N bars", minval=1, group=grpSig)
brkLen        = input.int(20, "Breakout lookback (prior high/low)", minval=2, group=grpSig)
brkWin        = input.int(3, "Breakout valid for N bars", minval=1, group=grpSig)
confMode      = input.string("Release OR Breakout", "Confirmation required", options=["Release OR Breakout", "Release AND Breakout", "Release only", "Breakout only"], group=grpSig)
confirmedOnly = input.bool(true, "Signals on confirmed bar close only", group=grpSig)
 
grpFlt = "Optional Independent Filters"
useTrend   = input.bool(false, "EMA trend filter", group=grpFlt)
emaFastLen = input.int(20, "EMA fast", minval=1, group=grpFlt)
emaSlowLen = input.int(50, "EMA slow", minval=1, group=grpFlt)
useADX     = input.bool(false, "ADX strength filter", group=grpFlt)
adxLen     = input.int(14, "ADX length", minval=1, group=grpFlt)
adxMin     = input.float(20.0, "ADX minimum", step=0.5, group=grpFlt)
useRelVol  = input.bool(false, "Relative volume filter", group=grpFlt)
rvLen      = input.int(20, "RelVol average length", minval=1, group=grpFlt)
rvMin      = input.float(1.2, "RelVol minimum", step=0.1, group=grpFlt)
 
grpUI = "Display"
showSignals = input.bool(true, "Show signal markers", group=grpUI)
showBg      = input.bool(true, "Tint background by regime", group=grpUI)
showTable   = input.bool(true, "Show six-metric dashboard", group=grpUI)
 
grpVol = "Dashboard Volatility"
atrLen       = input.int(14, "ATR Length", minval=1, group=grpVol)
atrAvgLen    = input.int(20, "ATR Average Length", minval=1, group=grpVol)
 
// ------------------------- BASE CALCULATIONS --------------------------
src     = close
basis   = ta.sma(src, bbLen)
bbDev   = (legacyBB ? kcMult : bbMult) * ta.stdev(src, bbLen)
upperBB = basis + bbDev
lowerBB = basis - bbDev
 
kcMa  = ta.sma(src, kcLen)
rng   = useTR ? ta.tr : (high - low)
rngMa = ta.sma(rng, kcLen)
 
upperKC = kcMa + rngMa * kcMult
lowerKC = kcMa - rngMa * kcMult
 
sqzOn  = lowerBB > lowerKC and upperBB < upperKC
sqzOff = lowerBB < lowerKC and upperBB > upperKC
noSqz  = not sqzOn and not sqzOff
 
val = ta.linreg(src - math.avg(math.avg(ta.highest(high, kcLen), ta.lowest(low, kcLen)), ta.sma(close, kcLen)), kcLen, 0)
valPrev = nz(val[1])
 
bcolor = val > 0 ? (val > valPrev ? color.lime : color.green) : (val < 0 ? (val < valPrev ? color.red : color.maroon) : color.gray)
scolor = noSqz ? color.blue : sqzOn ? color.black : color.gray
 
// ---------------------------- EVENT LAYER -----------------------------
sqzRelease = sqzOn[1] and not sqzOn
relRecent  = nz(ta.barssince(sqzRelease), 9999) <= relWin
 
bullBreak = close > ta.highest(high, brkLen)[1]
bearBreak = close < ta.lowest(low, brkLen)[1]
 
bullBrkRec = nz(ta.barssince(bullBreak), 9999) <= brkWin
bearBrkRec = nz(ta.barssince(bearBreak), 9999) <= brkWin
 
bullConf = switch confMode
    "Release OR Breakout"  => relRecent or bullBrkRec
    "Release AND Breakout" => relRecent and bullBrkRec
    "Release only"         => relRecent
    => bullBrkRec
 
bearConf = switch confMode
    "Release OR Breakout"  => relRecent or bearBrkRec
    "Release AND Breakout" => relRecent and bearBrkRec
    "Release only"         => relRecent
    => bearBrkRec
 
// ----------------------- OPTIONAL FILTERS -----------------------------
emaF = ta.ema(close, emaFastLen)
emaS = ta.ema(close, emaSlowLen)
 
trendBull = not useTrend or (emaF > emaS and close > emaS)
trendBear = not useTrend or (emaF < emaS and close < emaS)
 
trendConflictBull = useTrend and not trendBull
trendConflictBear = useTrend and not trendBear
 
[diP, diM, adxVal] = ta.dmi(adxLen, adxLen)
adxOk = not useADX or adxVal >= adxMin
 
rvVal = volume / ta.sma(volume, rvLen)
rvOk  = not useRelVol or (not na(rvVal) and rvVal >= rvMin)
 
filtersOk = adxOk and rvOk
 
// ----------------------------- REGIME LOGIC ---------------------------
momBull = val > 0 and val > valPrev
momBear = val < 0 and val < valPrev
 
bullCand = momBull and bullConf and trendBull and filtersOk
bearCand = momBear and bearConf and trendBear and filtersOk
 
// Original regime retained for signal and background logic.
regime = bullCand ? 1 : bearCand ? -1 : 0
 
neutralReason = regime != 0 ? "" :
     ((momBull and bullConf and trendConflictBull) or (momBear and bearConf and trendConflictBear)) ? "Trend filter conflict" :
     ((momBull and bullConf) or (momBear and bearConf)) and not filtersOk ? "ADX/RelVol filter failed" :
     sqzOn and not (relRecent or bullBrkRec or bearBrkRec) ? "Persistent compression, no confirmation" :
     ((val > 0 and val <= valPrev) or (val < 0 and val >= valPrev)) ? "Momentum fading / mixed" :
     (momBull or momBear) ? "Momentum without confirmation" : "Mixed / no setup"
 
sigOk     = not confirmedOnly or barstate.isconfirmed
bullEvent = regime == 1 and regime[1] != 1 and sigOk
bearEvent = regime == -1 and regime[1] != -1 and sigOk
 
// ----------------------- DASHBOARD METRICS ----------------------------
 
// 1. Momentum direction and value.
momentumText = val > 0 ? "Positive (" + str.tostring(val, "#.##") + ")" :
     val < 0 ? "Negative (" + str.tostring(val, "#.##") + ")" :
     "Zero (0.00)"
 
momentumColor = val > 0 ? color.green : val < 0 ? color.red : color.gray
 
// 2. Current momentum change versus previous bar.
momentumChange = val - valPrev
changeColor = momentumChange > 0 ? color.green : momentumChange < 0 ? color.red : color.gray
 
// 3. Squeeze status.
squeezeText = sqzOn ? "ON (Compression)" : sqzOff ? "OFF (Released)" : "Intermediate"
squeezeColor = sqzOn ? color.orange : sqzOff ? color.green : color.gray
 
// 4. ATR and volatility state.
// High/Low/Normal compares current ATR with its moving average.
// This is a dashboard-only calculation and does not alter signals.
atrValue = ta.atr(atrLen)
atrAverage = ta.sma(atrValue, atrAvgLen)
 
volatilityText = na(atrAverage) ? "Calculating" :
     atrValue > atrAverage ? "High" :
     atrValue < atrAverage ? "Low" : "Normal"
 
volatilityColor = na(atrAverage) ? color.gray :
     atrValue > atrAverage ? color.orange :
     atrValue < atrAverage ? color.blue : color.gray
 
// 5. Relative volume, using the original RVOL calculation.
rvolText = na(rvVal) ? "N/A" : str.tostring(rvVal, "#.##") + "x"
rvolColor = na(rvVal) ? color.gray : rvVal >= 1 ? color.green : color.orange
 
// ------------------------------- PLOTS --------------------------------
plot(val, "Momentum", color=bcolor, style=plot.style_histogram, linewidth=4)
plot(0, "Squeeze state", color=scolor, style=plot.style_cross, linewidth=2)
 
plotshape(showSignals and bullEvent, title="Bullish candidate", style=shape.triangleup, location=location.bottom, color=color.lime, size=size.small, text="BULL")
plotshape(showSignals and bearEvent, title="Bearish candidate", style=shape.triangledown, location=location.top, color=color.red, size=size.small, text="BEAR")
plotshape(showSignals and sqzRelease, title="Squeeze release", style=shape.diamond, location=location.absolute, color=color.orange, size=size.tiny)
 
bgcolor(showBg ? (regime == 1 ? color.new(color.lime, 88) : regime == -1 ? color.new(color.red, 88) : na) : na)
 
// ------------------------------ ALERTS --------------------------------
alertcondition(bullEvent, "Bullish candidate", "SQZMOM_SL: Bullish candidate on {{ticker}} ({{interval}})")
alertcondition(bearEvent, "Bearish candidate", "SQZMOM_SL: Bearish candidate on {{ticker}} ({{interval}})")
alertcondition(sqzRelease, "Squeeze release", "SQZMOM_SL: Squeeze released on {{ticker}} ({{interval}})")
alertcondition(ta.crossover(val, 0), "Momentum cross up", "SQZMOM_SL: val crossed above 0 on {{ticker}}")
alertcondition(ta.crossunder(val, 0), "Momentum cross down", "SQZMOM_SL: val crossed below 0 on {{ticker}}")
 
// ----------------------- SIX-METRIC DASHBOARD -------------------------
var table dash = table.new(position.top_right, 2, 6, border_width=1)
 
if showTable and barstate.islast
    headerBg = color.new(color.gray, 30)
    valueBg  = color.new(color.gray, 75)
 
    table.cell(dash, 0, 0, "Momentum", text_color=color.white, bgcolor=headerBg)
    table.cell(dash, 1, 0, momentumText, text_color=color.white, bgcolor=momentumColor)
 
    table.cell(dash, 0, 1, "Momentum Change", text_color=color.white, bgcolor=headerBg)
    table.cell(dash, 1, 1, str.tostring(momentumChange, "#.####"), text_color=color.white, bgcolor=changeColor)
 
    table.cell(dash, 0, 2, "Squeeze Status", text_color=color.white, bgcolor=headerBg)
    table.cell(dash, 1, 2, squeezeText, text_color=color.white, bgcolor=squeezeColor)
 
    table.cell(dash, 0, 3, "Volatility State", text_color=color.white, bgcolor=headerBg)
    table.cell(dash, 1, 3, volatilityText, text_color=color.white, bgcolor=volatilityColor)
 
    table.cell(dash, 0, 4, "RVOL", text_color=color.white, bgcolor=headerBg)
    table.cell(dash, 1, 4, rvolText, text_color=color.white, bgcolor=rvolColor)
 
    table.cell(dash, 0, 5, "ATR (" + str.tostring(atrLen) + ")", text_color=color.white, bgcolor=headerBg)
    table.cell(dash, 1, 5, str.tostring(atrValue, "#.####"), text_color=color.white, bgcolor=valueBg)
 
if not showTable and barstate.islast
    table.clear(dash, 0, 0, 1, 5)

← 返回列表