Minervini VCP and Overhead Supply Multi Timeframe

austriancapitaltheory · study · 315 行 · 点赞 27 · TradingView 原页

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

Pine Script

//@version=6
indicator("VCP & Overhead Supply (Minervini) – Multi-TF", shorttitle="VCP Supply MTF", overlay=true,
     max_lines_count=500, max_labels_count=500, max_boxes_count=50, max_bars_back=1000)
 
// ───────────────────────── Inputs ─────────────────────────
grpA = "Timeframe adaptation"
autoMode    = input.bool(true, "Auto-adapt to chart timeframe", group=grpA,
     tooltip="ON: swing strength, lookbacks, depth thresholds and trend MAs scale to the chart timeframe. OFF: uses the manual values below.")
sessionMins = input.int(390, "Session minutes per day (390 stocks, 1440 crypto/FX)", minval=60, maxval=1440, group=grpA)
atrLen      = input.int(20, "ATR length for depth scaling", minval=5, group=grpA)
 
grpS = "Swing detection (manual)"
swingLenIn = input.int(5,  "Swing strength (bars left/right)", minval=2, maxval=20, group=grpS)
maxPivots  = input.int(30, "Max swings stored", minval=10, maxval=60, group=grpS)
maxStaleIn = input.int(60, "Pattern freshness (bars since last swing low)", minval=5, group=grpS)
 
grpV = "VCP contractions"
minT        = input.int(2,     "Min contractions (T) for valid VCP", minval=2, maxval=6, group=grpV)
tightRatio  = input.float(0.75,"Each contraction must be ≤ prior ×", minval=0.3, maxval=1.0, step=0.05, group=grpV,
     tooltip="0.50 = textbook halving (31→17→8→3). 0.75 is looser.")
maxDepth1In = input.float(50.0,"Max first contraction depth % (manual)", minval=1, maxval=80, group=grpV)
minDepthFIn = input.float(1.0, "Min final contraction depth % (manual)", minval=0.05, maxval=10, step=0.05, group=grpV)
 
grpVol = "Volume"
volLenIn = input.int(50,    "Volume average length (manual)", minval=10, group=grpVol)
vduMult  = input.float(0.60,"Dry-up: bar volume ≤ avg ×", minval=0.1, maxval=1.0, step=0.05, group=grpVol)
boMult   = input.float(1.40,"Breakout: volume ≥ avg ×", minval=1.0, maxval=5.0, step=0.1, group=grpVol)
vduLook  = input.int(5,     "Recent-volume lookback (bars)", minval=2, maxval=20, group=grpVol)
 
grpSup = "Overhead supply"
showSupply = input.bool(true, "Show supply zone (old high)", group=grpSup)
supDepthIn = input.float(5.0, "Supply zone depth below old high % (manual)", minval=0.1, maxval=20, step=0.1, group=grpSup)
showBF     = input.bool(true, "Show bottom-fisher gain label", group=grpSup)
 
grpT = "Trend filter"
useTrend = input.bool(true, "Require Stage-2 trend for BUY", group=grpT)
ma1LenIn = input.int(50,  "Fast MA (manual)", group=grpT)
ma2LenIn = input.int(150, "Mid MA (manual)",  group=grpT)
ma3LenIn = input.int(200, "Slow MA (manual)", group=grpT)
showMAs  = input.bool(false, "Plot trend MAs", group=grpT)
 
grpD = "Display"
cZig    = input.color(color.new(color.gray, 0),   "Zigzag / labels", group=grpD)
cPivot  = input.color(color.new(color.teal, 0),   "Pivot line",      group=grpD)
cSupply = input.color(color.new(color.red, 80),   "Supply zone fill",group=grpD)
cVDU    = input.color(color.new(color.orange, 0), "Dry-up marker",   group=grpD)
cBO     = input.color(color.new(color.green, 0),  "Breakout",        group=grpD)
lineW   = input.int(2, "Line width", minval=1, maxval=4, group=grpD)
showTable = input.bool(true, "Show status table", group=grpD)
tblPos    = input.string("Top right", "Table position",
     options=["Top left", "Top right", "Bottom left", "Bottom right"], group=grpD)
 
// ───────────────── Timeframe-adaptive parameters ─────────────────
tfSec = timeframe.in_seconds()
float barsPerDay = timeframe.isintraday ? sessionMins * 60.0 / tfSec :
     timeframe.isdaily ? 1.0 : timeframe.isweekly ? 0.2 : 1.0 / 21.0
 
int autoSwing = timeframe.ismonthly ? 2 : timeframe.isweekly ? 3 : timeframe.isdaily ? 5 :
     tfSec >= 3600 ? 5 : tfSec >= 900 ? 6 : 8
 
int swingLen = autoMode ? autoSwing : swingLenIn
int volLen   = autoMode ? int(math.min(500, math.max(10, math.round(50 * barsPerDay)))) : volLenIn
int maxStale = autoMode ? int(math.min(400, math.max(8,  math.round(60 * barsPerDay)))) : maxStaleIn
 
atrPct = ta.atr(atrLen) / close * 100
float minDepthF = autoMode ? math.max(0.1, 0.5 * atrPct)                : minDepthFIn
float maxDepth1 = autoMode ? math.min(80.0, math.max(5.0, 25 * atrPct)) : maxDepth1In
float supDepth  = autoMode ? math.min(10.0, math.max(0.3, 2 * atrPct))  : supDepthIn
 
// Trend MAs: intraday → daily 50/150/200 via security; daily/weekly/monthly → scaled on chart
useDailyMAs = autoMode and timeframe.isintraday
int L1 = autoMode ? int(math.max(2, math.round(50  * barsPerDay))) : ma1LenIn
int L2 = autoMode ? int(math.max(3, math.round(150 * barsPerDay))) : ma2LenIn
int L3 = autoMode ? int(math.max(4, math.round(200 * barsPerDay))) : ma3LenIn
int L3slope = int(math.max(1, math.round(20 * barsPerDay)))
 
[d50, d150, d200, d200p] = request.security(syminfo.tickerid, "D",
     [ta.sma(close, 50)[1], ta.sma(close, 150)[1], ta.sma(close, 200)[1], ta.sma(close, 200)[21]],
     lookahead=barmerge.lookahead_on)
c1 = ta.sma(close, L1)
c2 = ta.sma(close, L2)
c3 = ta.sma(close, L3)
c3p = c3[L3slope]
ma1  = useDailyMAs ? d50  : c1
ma2  = useDailyMAs ? d150 : c2
ma3  = useDailyMAs ? d200 : c3
ma3p = useDailyMAs ? d200p : c3p
 
// ───────────────────── Global-scope series ─────────────────────
volAvg = ta.sma(volume, volLen)
volRec = ta.sma(volume, vduLook)
stage2  = close > ma1 and ma1 > ma2 and ma2 > ma3 and ma3 > ma3p
trendOK = not useTrend or stage2
ph = ta.pivothigh(high, swingLen, swingLen)
pl = ta.pivotlow(low,  swingLen, swingLen)
 
plot(showMAs ? ma1 : na, "Fast MA", color.new(color.blue,   0))
plot(showMAs ? ma2 : na, "Mid MA",  color.new(color.purple, 0))
plot(showMAs ? ma3 : na, "Slow MA", color.new(color.black,  0))
 
// ───────────────── Alternating swing-point storage ─────────────────
var array<float> pvP = array.new<float>()
var array<int>   pvB = array.new<int>()
var array<bool>  pvH = array.new<bool>()
 
addPivot(float p, int b, bool isHigh) =>
    sz = array.size(pvP)
    if sz > 0 and array.get(pvH, sz - 1) == isHigh
        last = array.get(pvP, sz - 1)
        if (isHigh and p > last) or (not isHigh and p < last)
            array.set(pvP, sz - 1, p)
            array.set(pvB, sz - 1, b)
    else
        array.push(pvP, p)
        array.push(pvB, b)
        array.push(pvH, isHigh)
    if array.size(pvP) > maxPivots
        array.shift(pvP)
        array.shift(pvB)
        array.shift(pvH)
 
if not na(ph)
    addPivot(ph, bar_index - swingLen, true)
if not na(pl)
    addPivot(pl, bar_index - swingLen, false)
 
// ───────────────── Contraction measurement ─────────────────
n = array.size(pvP)
array<float> cDepth = array.new<float>()
array<int>   cHiIdx = array.new<int>()
if n >= 2
    for i = 0 to n - 2
        if array.get(pvH, i) and not array.get(pvH, i + 1)
            hi = array.get(pvP, i)
            lo = array.get(pvP, i + 1)
            array.push(cDepth, (hi - lo) / hi * 100)
            array.push(cHiIdx, i)
 
nc = array.size(cDepth)
int tCount = 0
int startC = -1
if nc > 0
    tCount := 1
    startC := nc - 1
    if nc >= 2
        for k = nc - 1 to 1
            if array.get(cDepth, k) <= array.get(cDepth, k - 1) * tightRatio and array.get(cDepth, k - 1) <= maxDepth1
                tCount += 1
                startC := k - 1
            else
                break
 
float baseHigh    = na
int   baseHighBar = na
int   baseHighIdx = na
float pivot       = na
int   pivotBar    = na
float lastLow     = na
int   lastLowBar  = na
float baseLow     = na
int   baseLowBar  = na
float finalDepth  = na
if nc > 0
    baseHighIdx := array.get(cHiIdx, startC)
    baseHigh    := array.get(pvP, baseHighIdx)
    baseHighBar := array.get(pvB, baseHighIdx)
    lastHiIdx    = array.get(cHiIdx, nc - 1)
    pivot       := array.get(pvP, lastHiIdx)
    pivotBar    := array.get(pvB, lastHiIdx)
    lastLow     := array.get(pvP, lastHiIdx + 1)
    lastLowBar  := array.get(pvB, lastHiIdx + 1)
    finalDepth  := array.get(cDepth, nc - 1)
    baseLow     := lastLow
    baseLowBar  := lastLowBar
    for i = baseHighIdx to n - 1
        if not array.get(pvH, i) and array.get(pvP, i) < baseLow
            baseLow    := array.get(pvP, i)
            baseLowBar := array.get(pvB, i)
 
// ───────────────── Pattern state & signals ─────────────────
fresh       = nc > 0 and bar_index - lastLowBar <= maxStale
notBroken   = nc > 0 and close >= lastLow
validVCP    = nc > 0 and tCount >= minT and finalDepth >= minDepthF and fresh and notBroken
abovePivot  = nc > 0 and close > pivot
distToPivot = nc > 0 ? (pivot - close) / close * 100 : na
zoneBot     = baseHigh * (1 - supDepth / 100)
 
breakout      = validVCP and close > pivot and close[1] <= pivot
boStrong      = breakout and volume >= volAvg * boMult and trendOK
boWeak        = breakout and not boStrong
inZone        = nc > 0 and fresh and high >= zoneBot and close <= baseHigh
supplyHit     = inZone and close < open and volume > volAvg
supplyCleared = nc > 0 and fresh and close > baseHigh and close[1] <= baseHigh
vduMark       = validVCP and bar_index > pivotBar and volume <= volAvg * vduMult
nearPct       = autoMode ? math.max(0.2, 2 * atrPct) : 5.0
setupReady    = validVCP and not abovePivot and distToPivot <= nearPct and volRec <= volAvg * vduMult and trendOK
 
plotshape(vduMark,       "Volume dry-up",           shape.circle,       location.belowbar, cVDU, size=size.tiny)
plotshape(boStrong,      "Pivot breakout + volume", shape.triangleup,   location.belowbar, cBO,  text="BUY",     textcolor=cBO, size=size.small)
plotshape(boWeak,        "Pivot breakout weak vol", shape.triangleup,   location.belowbar, color.orange, text="low vol", textcolor=color.orange, size=size.small)
plotshape(supplyHit,     "Supply at old high",      shape.triangledown, location.abovebar, color.red, text="Supply", textcolor=color.red, size=size.small)
plotshape(supplyCleared, "Old high cleared",        shape.diamond,      location.abovebar, cBO,  size=size.tiny)
 
// ───────────────── Drawings (current pattern) ─────────────────
var array<line>  dLines  = array.new<line>()
var array<label> dLabels = array.new<label>()
var box  supBox  = na
var line pivLine = na
 
if barstate.islast
    while array.size(dLines) > 0
        line.delete(array.pop(dLines))
    while array.size(dLabels) > 0
        label.delete(array.pop(dLabels))
    box.delete(supBox)
    line.delete(pivLine)
 
    if nc > 0 and fresh and notBroken
        for i = baseHighIdx to n - 2
            array.push(dLines, line.new(array.get(pvB, i), array.get(pvP, i),
                 array.get(pvB, i + 1), array.get(pvP, i + 1), color=cZig, width=lineW))
 
        tNum = 0
        for k = startC to nc - 1
            tNum += 1
            hiIdx = array.get(cHiIdx, k)
            hb = array.get(pvB, hiIdx)
            lb = array.get(pvB, hiIdx + 1)
            float vsum = 0.0
            int   cnt  = 0
            for b = hb to lb
                off = bar_index - b
                if off >= 0 and off < 1000
                    vsum += volume[off]
                    cnt  += 1
            offLo = bar_index - lb
            float vr = cnt > 0 and offLo < 1000 ? (vsum / cnt) / volAvg[offLo] : na
            txt = "T" + str.tostring(tNum) + "  " + str.tostring(array.get(cDepth, k), "#.##") + "%"
                 + (na(vr) ? "" : "\nvol " + str.tostring(vr, "#.##") + "x")
            array.push(dLabels, label.new(lb, array.get(pvP, hiIdx + 1), txt,
                 style=label.style_label_up, color=color.new(cZig, 60), textcolor=color.white, size=size.small))
 
        pivLine := line.new(pivotBar, pivot, bar_index + 5, pivot, color=cPivot, style=line.style_dashed, width=lineW)
        pivTxt = "Pivot " + str.tostring(pivot, format.mintick) +
             (abovePivot ? "  (cleared)" : "  (" + str.tostring(distToPivot, "#.##") + "% away)")
        array.push(dLabels, label.new(bar_index + 5, pivot, pivTxt,
             style=label.style_label_left, color=cPivot, textcolor=color.white, size=size.small))
 
        if showSupply
            cleared = close > baseHigh
            supTxt  = cleared ? "Old high cleared – trapped-buyer supply absorbed" :
                 "OVERHEAD SUPPLY – trapped buyers get back to even"
            supBox := box.new(baseHighBar, baseHigh, bar_index + 5, zoneBot,
                 border_color=color.new(color.red, 40), bgcolor=cleared ? color.new(color.green, 85) : cSupply,
                 text=supTxt, text_size=size.small, text_color=cleared ? color.new(color.green, 0) : color.new(color.red, 0),
                 text_halign=text.align_right, text_valign=text.align_top)
 
        if showBF
            bfGain = (pivot - baseLow) / baseLow * 100
            array.push(dLabels, label.new(baseLowBar, baseLow,
                 "Base low " + str.tostring(baseLow, format.mintick) + "\nBottom-fishers +" + str.tostring(bfGain, "#.#") + "% at pivot",
                 style=label.style_label_up, color=color.new(color.blue, 70), textcolor=color.white, size=size.small))
 
// ───────────────────────── Status table ─────────────────────────
pos = switch tblPos
    "Top left"     => position.top_left
    "Top right"    => position.top_right
    "Bottom left"  => position.bottom_left
    => position.bottom_right
 
var table tbl = table.new(pos, 2, 10, bgcolor=color.new(color.black, 15), border_width=1, border_color=color.new(color.gray, 60))
 
cellRow(table t, int r, string k, string v, color vc) =>
    table.cell(t, 0, r, k, text_color=color.new(color.gray, 0), text_halign=text.align_left,  text_size=size.small)
    table.cell(t, 1, r, v, text_color=vc,                        text_halign=text.align_right, text_size=size.small)
 
if showTable and barstate.islast
    string status = nc == 0 ? "No swings yet" :
         not fresh     ? "Stale – no recent swing low" :
         not notBroken ? "FAILED – below last low" :
         abovePivot    ? "Above pivot" :
         tCount < minT ? "Forming (" + str.tostring(tCount) + "T)" :
         setupReady    ? "READY – tight & dry" : "Setting up (" + str.tostring(tCount) + "T)"
    color sc = setupReady ? color.lime : (not notBroken ? color.red : color.white)
 
    string seq = ""
    if nc > 0
        for k = startC to nc - 1
            seq += (k > startC ? " › " : "") + str.tostring(array.get(cDepth, k), "#.#") + "%"
 
    float volRatio = volRec / volAvg
    string volTxt = str.tostring(volRatio, "#.##") + "x avg" + (volRatio <= vduMult ? "  DRY" : "")
    string supTxt = nc > 0 ? str.tostring(baseHigh, format.mintick) + (close > baseHigh ? "  cleared" : "  overhead") : "—"
    string bfTxt  = nc > 0 ? "+" + str.tostring((pivot - baseLow) / baseLow * 100, "#.#") + "% at pivot" : "—"
    string maTxt  = useDailyMAs ? "daily 50/150/200" : str.tostring(L1) + "/" + str.tostring(L2) + "/" + str.tostring(L3)
    string parTxt = (autoMode ? "Auto · " : "Manual · ") + timeframe.period + " · swing " + str.tostring(swingLen) +
         " · vol " + str.tostring(volLen) + " · min " + str.tostring(minDepthF, "#.##") + "%"
 
    cellRow(tbl, 0, "VCP status",            status, sc)
    cellRow(tbl, 1, "Contractions",          nc > 0 ? str.tostring(tCount) + "T" : "—", color.white)
    cellRow(tbl, 2, "Sequence",              nc > 0 ? seq : "—", color.white)
    cellRow(tbl, 3, "Final contraction",     nc > 0 ? str.tostring(finalDepth, "#.##") + "%" : "—", color.white)
    cellRow(tbl, 4, "Pivot / distance",      nc > 0 ? str.tostring(pivot, format.mintick) + " / " + str.tostring(distToPivot, "#.##") + "%" : "—", cPivot)
    cellRow(tbl, 5, "Volume (" + str.tostring(vduLook) + "b vs " + str.tostring(volLen) + "b)", volTxt, volRatio <= vduMult ? cVDU : color.white)
    cellRow(tbl, 6, "Overhead supply (old high)", supTxt, nc > 0 and close > baseHigh ? cBO : color.red)
    cellRow(tbl, 7, "Bottom-fisher gain",    bfTxt, color.white)
    cellRow(tbl, 8, "Stage-2 trend (" + maTxt + ")", stage2 ? "Yes" : "No", stage2 ? cBO : color.red)
    cellRow(tbl, 9, "Parameters",            parTxt, color.new(color.gray, 0))
 
// ───────────────────────── Alerts ─────────────────────────
alertcondition(boStrong,      "VCP breakout (volume confirmed)", "VCP: close above pivot on expanding volume")
alertcondition(boWeak,        "VCP breakout (weak volume)",      "VCP: pivot cleared on light volume – caution")
alertcondition(setupReady,    "VCP ready (tight + dry-up)",      "VCP: near pivot with volume dry-up")
alertcondition(supplyHit,     "Supply at old high",              "Price reversed from overhead supply zone on volume")
alertcondition(supplyCleared, "Old high cleared",                "Close above old high – trapped-buyer supply absorbed")

← 返回列表