Multi-Setup Intraday Signals - Sess VP ORB, VWAP, Liq+Strat

Acalvillo20 · study · 472 行 · 点赞 924 · TradingView 原页

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

Pine Script

// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ MPL-2.0
//@version=6
indicator("Multi-Setup Intraday Signals", shorttitle = "MS Signals", overlay = true, max_labels_count = 200, max_boxes_count = 100)
 
// --- Constants ---
int BULL = 1
int BEAR = -1
 
// --- Inputs ---
string biasTfInput = input.string("60", "Bias timeframe", options = ["60", "240"], tooltip = "Choose the higher timeframe whose EMA trend defines signal direction.", group = "Higher-Timeframe Bias")
int fastEmaInput = input.int(20, "Fast EMA", minval = 1, tooltip = "Fast EMA length used to determine higher-timeframe direction.", group = "Higher-Timeframe Bias")
int slowEmaInput = input.int(50, "Slow EMA", minval = 2, tooltip = "Slow EMA length used to determine higher-timeframe direction.", group = "Higher-Timeframe Bias")
int pivotLenInput = input.int(3, "Zone pivot strength", minval = 1, maxval = 20, tooltip = "Bars on each side required to confirm 30-minute and 1-hour supply/demand pivots.", group = "Supply and Demand Zones")
float zoneAtrInput = input.float(0.25, "Zone thickness (ATR fraction)", minval = 0.05, maxval = 2.0, step = 0.05, tooltip = "Zone thickness uses the ATR from that zone's own timeframe.", group = "Supply and Demand Zones")
color htfColorInput = input.color(#5b9cf6, "HTF bias color", tooltip = "Color for the higher-timeframe bias status.", group = "Style")
int zonesToKeepInput = input.int(6, "Zones to keep per type/timeframe", minval = 1, maxval = 12, tooltip = "Retain this many separate historical boxes for each timeframe and supply/demand type.", group = "Supply and Demand Zones")
color demand30ColorInput = input.color(color.new(#089981, 84), "30m Demand fill / transparency", tooltip = "Independent 30-minute demand fill color and transparency.", group = "Style")
color supply30ColorInput = input.color(color.new(#f23645, 84), "30m Supply fill / transparency", tooltip = "Independent 30-minute supply fill color and transparency.", group = "Style")
color demand60ColorInput = input.color(color.new(#089981, 76), "1hr Demand fill / transparency", tooltip = "Independent 1-hour demand fill color and transparency.", group = "Style")
color supply60ColorInput = input.color(color.new(#f23645, 76), "1hr Supply fill / transparency", tooltip = "Independent 1-hour supply fill color and transparency.", group = "Style")
 
// --- Higher-timeframe bias ---
int bias1h = request.security(syminfo.tickerid, "60", ta.ema(close, fastEmaInput)[1] > ta.ema(close, slowEmaInput)[1] ? BULL : ta.ema(close, fastEmaInput)[1] < ta.ema(close, slowEmaInput)[1] ? BEAR : 0, gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_off)
int bias4h = request.security(syminfo.tickerid, "240", ta.ema(close, fastEmaInput)[1] > ta.ema(close, slowEmaInput)[1] ? BULL : ta.ema(close, fastEmaInput)[1] < ta.ema(close, slowEmaInput)[1] ? BEAR : 0, gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_off)
int htfBias = biasTfInput == "60" ? bias1h : bias4h
bool bullishBias = htfBias == BULL
bool bearishBias = htfBias == BEAR
 
// --- 30-minute and 1-hour pivot zones, retained as separate boxes ---
[ph30, pl30, atr30] = request.security(syminfo.tickerid, "30", [ta.pivothigh(high, pivotLenInput, pivotLenInput), ta.pivotlow(low, pivotLenInput, pivotLenInput), ta.atr(14)], gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_off)
[ph60, pl60, atr60] = request.security(syminfo.tickerid, "60", [ta.pivothigh(high, pivotLenInput, pivotLenInput), ta.pivotlow(low, pivotLenInput, pivotLenInput), ta.atr(14)], gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_off)
[closed30Close, closed30Time] = request.security(syminfo.tickerid, "30", [close[1], time_close[1]], gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_on)
[closed60Close, closed60Time] = request.security(syminfo.tickerid, "60", [close[1], time_close[1]], gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_on)
bool newClosed30 = not na(closed30Time) and (na(closed30Time[1]) or closed30Time != closed30Time[1])
bool newClosed60 = not na(closed60Time) and (na(closed60Time[1]) or closed60Time != closed60Time[1])
var float supply30 = na
var float demand30 = na
var float supply60 = na
var float demand60 = na
var float supply30Width = na
var float demand30Width = na
var float supply60Width = na
var float demand60Width = na
var array<box> demand30Boxes = array.new_box()
var array<box> supply30Boxes = array.new_box()
var array<box> demand60Boxes = array.new_box()
var array<box> supply60Boxes = array.new_box()
var array<label> demand30Labels = array.new_label()
var array<label> supply30Labels = array.new_label()
var array<label> demand60Labels = array.new_label()
var array<label> supply60Labels = array.new_label()
f_pruneInvalidated(array<box> zoneBoxes, array<label> zoneLabels, float sourceClose, bool demandZone) =>
    int zoneIndex = 0
    while zoneIndex < array.size(zoneBoxes)
        box zoneBox = array.get(zoneBoxes, zoneIndex)
        bool broken = demandZone ? sourceClose < box.get_bottom(zoneBox) : sourceClose > box.get_top(zoneBox)
        if broken
            box.delete(zoneBox)
            label.delete(array.get(zoneLabels, zoneIndex))
            array.remove(zoneBoxes, zoneIndex)
            array.remove(zoneLabels, zoneIndex)
            zoneIndex := zoneIndex
        else
            zoneIndex += 1
bool newSupply30 = not na(ph30) and (na(ph30[1]) or ph30 != ph30[1])
bool newDemand30 = not na(pl30) and (na(pl30[1]) or pl30 != pl30[1])
bool newSupply60 = not na(ph60) and (na(ph60[1]) or ph60 != ph60[1])
bool newDemand60 = not na(pl60) and (na(pl60[1]) or pl60 != pl60[1])
if newSupply30
    supply30 := ph30
    supply30Width := atr30 * zoneAtrInput
if newDemand30
    demand30 := pl30
    demand30Width := atr30 * zoneAtrInput
if newSupply60
    supply60 := ph60
    supply60Width := atr60 * zoneAtrInput
if newDemand60
    demand60 := pl60
    demand60Width := atr60 * zoneAtrInput
float d30Upper = demand30 + demand30Width
float d30Lower = demand30 - demand30Width
float s30Upper = supply30 + supply30Width
float s30Lower = supply30 - supply30Width
float d60Upper = demand60 + demand60Width
float d60Lower = demand60 - demand60Width
float s60Upper = supply60 + supply60Width
float s60Lower = supply60 - supply60Width
if newDemand30 and not na(d30Upper) and not na(d30Lower)
    box newDemand30Box = box.new(left = bar_index, top = d30Upper, right = bar_index + 1, bottom = d30Lower, border_color = color.new(demand30ColorInput, 0), bgcolor = demand30ColorInput, xloc = xloc.bar_index)
    label newDemand30Label = label.new(bar_index, math.avg(d30Upper, d30Lower), "30m Demand", xloc = xloc.bar_index, style = label.style_label_center, color = color.new(demand30ColorInput, 10), textcolor = color.white, size = size.small)
    array.push(demand30Boxes, newDemand30Box)
    array.push(demand30Labels, newDemand30Label)
    if array.size(demand30Boxes) > zonesToKeepInput
        box.delete(array.shift(demand30Boxes))
        label.delete(array.shift(demand30Labels))
if newSupply30 and not na(s30Upper) and not na(s30Lower)
    box newSupply30Box = box.new(left = bar_index, top = s30Upper, right = bar_index + 1, bottom = s30Lower, border_color = color.new(supply30ColorInput, 0), bgcolor = supply30ColorInput, xloc = xloc.bar_index)
    label newSupply30Label = label.new(bar_index, math.avg(s30Upper, s30Lower), "30m Supply", xloc = xloc.bar_index, style = label.style_label_center, color = color.new(supply30ColorInput, 10), textcolor = color.white, size = size.small)
    array.push(supply30Boxes, newSupply30Box)
    array.push(supply30Labels, newSupply30Label)
    if array.size(supply30Boxes) > zonesToKeepInput
        box.delete(array.shift(supply30Boxes))
        label.delete(array.shift(supply30Labels))
if newDemand60 and not na(d60Upper) and not na(d60Lower)
    box newDemand60Box = box.new(left = bar_index, top = d60Upper, right = bar_index + 1, bottom = d60Lower, border_color = color.new(demand60ColorInput, 0), bgcolor = demand60ColorInput, xloc = xloc.bar_index)
    label newDemand60Label = label.new(bar_index, math.avg(d60Upper, d60Lower), "1hr Demand", xloc = xloc.bar_index, style = label.style_label_center, color = color.new(demand60ColorInput, 10), textcolor = color.white, size = size.small)
    array.push(demand60Boxes, newDemand60Box)
    array.push(demand60Labels, newDemand60Label)
    if array.size(demand60Boxes) > zonesToKeepInput
        box.delete(array.shift(demand60Boxes))
        label.delete(array.shift(demand60Labels))
if newSupply60 and not na(s60Upper) and not na(s60Lower)
    box newSupply60Box = box.new(left = bar_index, top = s60Upper, right = bar_index + 1, bottom = s60Lower, border_color = color.new(supply60ColorInput, 0), bgcolor = supply60ColorInput, xloc = xloc.bar_index)
    label newSupply60Label = label.new(bar_index, math.avg(s60Upper, s60Lower), "1hr Supply", xloc = xloc.bar_index, style = label.style_label_center, color = color.new(supply60ColorInput, 10), textcolor = color.white, size = size.small)
    array.push(supply60Boxes, newSupply60Box)
    array.push(supply60Labels, newSupply60Label)
    if array.size(supply60Boxes) > zonesToKeepInput
        box.delete(array.shift(supply60Boxes))
        label.delete(array.shift(supply60Labels))
if newClosed30
    f_pruneInvalidated(demand30Boxes, demand30Labels, closed30Close, true)
    f_pruneInvalidated(supply30Boxes, supply30Labels, closed30Close, false)
if newClosed60
    f_pruneInvalidated(demand60Boxes, demand60Labels, closed60Close, true)
    f_pruneInvalidated(supply60Boxes, supply60Labels, closed60Close, false)
if array.size(demand30Boxes) > 0
    for zoneIndex = 0 to array.size(demand30Boxes) - 1
        box demand30Box = array.get(demand30Boxes, zoneIndex)
        label demand30Tag = array.get(demand30Labels, zoneIndex)
        box.set_right(demand30Box, bar_index + 1)
        label.set_xy(demand30Tag, int(math.round(math.avg(box.get_left(demand30Box), bar_index))), math.avg(box.get_top(demand30Box), box.get_bottom(demand30Box)))
if array.size(supply30Boxes) > 0
    for zoneIndex = 0 to array.size(supply30Boxes) - 1
        box supply30Box = array.get(supply30Boxes, zoneIndex)
        label supply30Tag = array.get(supply30Labels, zoneIndex)
        box.set_right(supply30Box, bar_index + 1)
        label.set_xy(supply30Tag, int(math.round(math.avg(box.get_left(supply30Box), bar_index))), math.avg(box.get_top(supply30Box), box.get_bottom(supply30Box)))
if array.size(demand60Boxes) > 0
    for zoneIndex = 0 to array.size(demand60Boxes) - 1
        box demand60Box = array.get(demand60Boxes, zoneIndex)
        label demand60Tag = array.get(demand60Labels, zoneIndex)
        box.set_right(demand60Box, bar_index + 1)
        label.set_xy(demand60Tag, int(math.round(math.avg(box.get_left(demand60Box), bar_index))), math.avg(box.get_top(demand60Box), box.get_bottom(demand60Box)))
if array.size(supply60Boxes) > 0
    for zoneIndex = 0 to array.size(supply60Boxes) - 1
        box supply60Box = array.get(supply60Boxes, zoneIndex)
        label supply60Tag = array.get(supply60Labels, zoneIndex)
        box.set_right(supply60Box, bar_index + 1)
        label.set_xy(supply60Tag, int(math.round(math.avg(box.get_left(supply60Box), bar_index))), math.avg(box.get_top(supply60Box), box.get_bottom(supply60Box)))
 
// --- 1H and 4H bias panel; fixed-position tables remain visible on any chart timeframe ---
var table biasTable = table.new(position.top_right, 2, 3, frame_color = color.new(htfColorInput, 35), frame_width = 1)
if barstate.islast
    color bias1hBg = bias1h == BULL ? color.new(#089981, 15) : bias1h == BEAR ? color.new(#f23645, 15) : color.new(color.gray, 25)
    color bias4hBg = bias4h == BULL ? color.new(#089981, 15) : bias4h == BEAR ? color.new(#f23645, 15) : color.new(color.gray, 25)
    string bias1hText = bias1h == BULL ? "BULLISH" : bias1h == BEAR ? "BEARISH" : "NEUTRAL"
    string bias4hText = bias4h == BULL ? "BULLISH" : bias4h == BEAR ? "BEARISH" : "NEUTRAL"
    table.cell(biasTable, 0, 0, "HTF", text_color = color.white, bgcolor = color.new(htfColorInput, 55), text_size = size.small)
    table.cell(biasTable, 1, 0, "BIAS", text_color = color.white, bgcolor = color.new(htfColorInput, 55), text_size = size.small)
    table.cell(biasTable, 0, 1, "1H", text_color = color.white, bgcolor = color.new(htfColorInput, 75), text_size = size.small)
    table.cell(biasTable, 1, 1, bias1hText, text_color = color.white, bgcolor = bias1hBg, text_size = size.small)
    table.cell(biasTable, 0, 2, "4H", text_color = color.white, bgcolor = color.new(htfColorInput, 75), text_size = size.small)
    table.cell(biasTable, 1, 2, bias4hText, text_color = color.white, bgcolor = bias4hBg, text_size = size.small)
 
// --- Sweep, break-of-structure and retest inputs ---
int ltfPivotInput = input.int(3, "Signal-chart structure pivot strength", minval = 1, maxval = 15, tooltip = "Swing bars on each side used to identify structure and liquidity levels on a 1-minute or 5-minute chart.", group = "Zone Sweep + BOS Retest")
int setupExpiryInput = input.int(24, "Setup expiry (chart bars)", minval = 3, maxval = 100, tooltip = "Maximum number of chart bars to wait between the sweep, BOS and retest stages.", group = "Zone Sweep + BOS Retest")
bool showBosSignalsInput = input.bool(true, "Show zone sweep + BOS retest signals", tooltip = "Plot buy/sell signals after a zone-area liquidity sweep, aligned BOS, and retest.", group = "Zone Sweep + BOS Retest")
 
// --- 5-minute structure and zone-touch state ---
float ltfPivotHigh = ta.pivothigh(high, ltfPivotInput, ltfPivotInput)
float ltfPivotLow = ta.pivotlow(low, ltfPivotInput, ltfPivotInput)
var float lastSwingHigh = na
var float lastSwingLow = na
if not na(ltfPivotHigh)
    lastSwingHigh := ltfPivotHigh
if not na(ltfPivotLow)
    lastSwingLow := ltfPivotLow
 
bool inDemandZone = (not na(d30Upper) and low <= d30Upper and high >= d30Lower) or (not na(d60Upper) and low <= d60Upper and high >= d60Lower)
bool inSupplyZone = (not na(s30Upper) and high >= s30Lower and low <= s30Upper) or (not na(s60Upper) and high >= s60Lower and low <= s60Upper)
bool bullishLiquiditySweep = bullishBias and inDemandZone and not na(lastSwingLow) and low < lastSwingLow and close > lastSwingLow
bool bearishLiquiditySweep = bearishBias and inSupplyZone and not na(lastSwingHigh) and high > lastSwingHigh and close < lastSwingHigh
 
var bool bullSweepArmed = false
var bool bearSweepArmed = false
var int bullSweepBar = na
var int bearSweepBar = na
var bool bullBosWaitingRetest = false
var bool bearBosWaitingRetest = false
var float bullBosLevel = na
var float bearBosLevel = na
var int bullBosBar = na
var int bearBosBar = na
 
if bullishLiquiditySweep
    bullSweepArmed := true
    bullSweepBar := bar_index
    bullBosWaitingRetest := false
if bearishLiquiditySweep
    bearSweepArmed := true
    bearSweepBar := bar_index
    bearBosWaitingRetest := false
if bullSweepArmed and not na(bullSweepBar) and bar_index - bullSweepBar > setupExpiryInput
    bullSweepArmed := false
if bearSweepArmed and not na(bearSweepBar) and bar_index - bearSweepBar > setupExpiryInput
    bearSweepArmed := false
 
bool bullishBos = bullSweepArmed and bullishBias and not na(lastSwingHigh) and close > lastSwingHigh and close[1] <= lastSwingHigh
bool bearishBos = bearSweepArmed and bearishBias and not na(lastSwingLow) and close < lastSwingLow and close[1] >= lastSwingLow
if bullishBos
    bullBosLevel := lastSwingHigh
    bullBosBar := bar_index
    bullBosWaitingRetest := true
    bullSweepArmed := false
if bearishBos
    bearBosLevel := lastSwingLow
    bearBosBar := bar_index
    bearBosWaitingRetest := true
    bearSweepArmed := false
 
if bullBosWaitingRetest and not na(bullBosBar) and bar_index - bullBosBar > setupExpiryInput
    bullBosWaitingRetest := false
if bearBosWaitingRetest and not na(bearBosBar) and bar_index - bearBosBar > setupExpiryInput
    bearBosWaitingRetest := false
bool supportedSignalTimeframe = timeframe.isminutes and (timeframe.multiplier == 1 or timeframe.multiplier == 5)
bool zoneBosBuy = showBosSignalsInput and supportedSignalTimeframe and barstate.isconfirmed and bullBosWaitingRetest and bar_index > bullBosBar and low <= bullBosLevel and close > bullBosLevel and bullishBias
bool zoneBosSell = showBosSignalsInput and supportedSignalTimeframe and barstate.isconfirmed and bearBosWaitingRetest and bar_index > bearBosBar and high >= bearBosLevel and close < bearBosLevel and bearishBias
if zoneBosBuy
    bullBosWaitingRetest := false
if zoneBosSell
    bearBosWaitingRetest := false
 
plotshape(zoneBosBuy, title = "Zone Sweep BOS Retest Buy", text = "Z-BUY", style = shape.labelup, location = location.belowbar, color = #089981, textcolor = color.white, size = size.tiny)
plotshape(zoneBosSell, title = "Zone Sweep BOS Retest Sell", text = "Z-SELL", style = shape.labeldown, location = location.abovebar, color = #f23645, textcolor = color.white, size = size.tiny)
alertcondition(zoneBosBuy, title = "Zone sweep + bullish BOS retest", message = "Bullish HTF-aligned zone sweep, BOS and retest on {{ticker}} {{interval}}")
alertcondition(zoneBosSell, title = "Zone sweep + bearish BOS retest", message = "Bearish HTF-aligned zone sweep, BOS and retest on {{ticker}} {{interval}}")
 
// --- Session profile inputs ---
string profileTimezoneInput = input.string("America/Chicago", "Session timezone", tooltip = "IANA timezone for session windows. Central time defaults to America/Chicago and follows daylight saving time.", group = "Session Opening Profile")
int profileBinsInput = input.int(60, "Profile price bins", minval = 8, maxval = 60, tooltip = "Number of price bins used to estimate the opening profile from the first six 5-minute candles. More bins refine the VAH/VAL/POC estimate.", group = "Session Opening Profile")
float valueAreaPctInput = input.float(70.0, "Value area (%)", minval = 50.0, maxval = 90.0, step = 1.0, tooltip = "Percent of estimated opening volume included around the point of control.", group = "Session Opening Profile")
bool showProfileSignalsInput = input.bool(true, "Show VAH/VAL break-retest signals", tooltip = "Signals on five-minute value-area breaks, retests, and breakout re-entry.", group = "Session Opening Profile")
color openingBoxColorInput = input.color(color.new(color.white, 88), "VAH–VAL box fill", tooltip = "Fill color for the session value-area box from VAL to VAH; it extends through the session.", group = "Style")
color pocColorInput = input.color(color.yellow, "POC color", tooltip = "Color for the estimated point of control; yellow by default.", group = "Style")
 
// --- Three Central-time sessions: NY 08:30-15:30, Asia 17:00-02:00, London 02:00-08:30 ---
bool inNySession = not na(time(timeframe.period, "0830-1530:1234567", profileTimezoneInput))
bool inAsiaSession = not na(time(timeframe.period, "1700-0200:1234567", profileTimezoneInput))
bool inLondonSession = not na(time(timeframe.period, "0200-0830:1234567", profileTimezoneInput))
int sessionKey = inNySession ? 1 : inAsiaSession ? 2 : inLondonSession ? 3 : 0
bool profileSignalTimeframe = timeframe.isminutes and timeframe.multiplier == 5
int profileOpeningBars = 6
bool newProfileSession = profileSignalTimeframe and sessionKey != 0 and sessionKey != nz(sessionKey[1], 0)
 
var int activeSessionKey = 0
var int profileBarsSeen = 0
var float openingHigh = na
var float openingLow = na
var float profileVah = na
var float profileVal = na
var float profilePoc = na
var float activeProfileTarget = na
var box openingBox = na
var array<float> openingHighs = array.new_float()
var array<float> openingLows = array.new_float()
var array<float> openingVolumes = array.new_float()
var bool vahRetestArmed = false
var bool valRetestArmed = false
var bool vahReentryArmed = false
var bool valReentryArmed = false
 
if newProfileSession
    activeSessionKey := sessionKey
    profileBarsSeen := 0
    openingHigh := na
    openingLow := na
    profileVah := na
    profileVal := na
    profilePoc := na
    activeProfileTarget := na
    openingBox := na
    vahRetestArmed := false
    valRetestArmed := false
    vahReentryArmed := false
    valReentryArmed := false
    array.clear(openingHighs)
    array.clear(openingLows)
    array.clear(openingVolumes)
 
if profileSignalTimeframe and sessionKey != 0 and activeSessionKey == sessionKey
    if not na(openingBox)
        box.set_right(openingBox, bar_index)
    if profileBarsSeen < profileOpeningBars
        profileBarsSeen += 1
        openingHigh := na(openingHigh) ? high : math.max(openingHigh, high)
        openingLow := na(openingLow) ? low : math.min(openingLow, low)
        array.push(openingHighs, high)
        array.push(openingLows, low)
        array.push(openingVolumes, math.max(nz(volume), 0.0))
 
        if profileBarsSeen == profileOpeningBars
            float span = math.max(openingHigh - openingLow, syminfo.mintick)
            float binSize = span / profileBinsInput
            array<float> binVolumes = array.new_float(profileBinsInput, 0.0)
            for candleIndex = 0 to array.size(openingHighs) - 1
                float candleHigh = array.get(openingHighs, candleIndex)
                float candleLow = array.get(openingLows, candleIndex)
                float candleVolume = array.get(openingVolumes, candleIndex)
                float candleRange = math.max(candleHigh - candleLow, syminfo.mintick)
                for binIndex = 0 to profileBinsInput - 1
                    float binLow = openingLow + binIndex * binSize
                    float binHigh = binLow + binSize
                    float overlap = math.max(0.0, math.min(candleHigh, binHigh) - math.max(candleLow, binLow))
                    float allocatedVolume = candleHigh == candleLow ? (binIndex == math.min(profileBinsInput - 1, int(math.floor((candleLow - openingLow) / binSize))) ? candleVolume : 0.0) : candleVolume * overlap / candleRange
                    array.set(binVolumes, binIndex, array.get(binVolumes, binIndex) + allocatedVolume)
 
            float totalProfileVolume = 0.0
            float maxBinVolume = -1.0
            int pocIndex = 0
            for binIndex = 0 to profileBinsInput - 1
                float thisBinVolume = array.get(binVolumes, binIndex)
                totalProfileVolume += thisBinVolume
                if thisBinVolume > maxBinVolume
                    maxBinVolume := thisBinVolume
                    pocIndex := binIndex
            int lowerValueIndex = pocIndex
            int upperValueIndex = pocIndex
            float accumulatedValueVolume = array.get(binVolumes, pocIndex)
            float targetValueVolume = totalProfileVolume * valueAreaPctInput / 100.0
            while accumulatedValueVolume < targetValueVolume and (lowerValueIndex > 0 or upperValueIndex < profileBinsInput - 1)
                float nextLowerVolume = lowerValueIndex > 0 ? array.get(binVolumes, lowerValueIndex - 1) : -1.0
                float nextUpperVolume = upperValueIndex < profileBinsInput - 1 ? array.get(binVolumes, upperValueIndex + 1) : -1.0
                if nextUpperVolume > nextLowerVolume
                    upperValueIndex += 1
                    accumulatedValueVolume += math.max(nextUpperVolume, 0.0)
                else
                    lowerValueIndex -= 1
                    accumulatedValueVolume += math.max(nextLowerVolume, 0.0)
            profileVal := openingLow + lowerValueIndex * binSize
            profileVah := openingLow + (upperValueIndex + 1) * binSize
            profilePoc := openingLow + (pocIndex + 0.5) * binSize
            openingBox := box.new(left = bar_index - profileOpeningBars + 1, top = profileVah, right = bar_index, bottom = profileVal, border_color = color.white, border_width = 1, bgcolor = openingBoxColorInput)
 
plot(sessionKey != 0 and activeSessionKey == sessionKey ? profilePoc : na, "Session POC (estimated)", color = pocColorInput, linewidth = 2, style = plot.style_linebr)
 
// --- Value-area break/retest and breakout re-entry signals ---
bool profileReady = sessionKey != 0 and activeSessionKey == sessionKey and profileBarsSeen >= profileOpeningBars and not na(profileVah) and not na(profileVal)
bool profileBuyRetest = profileReady and vahRetestArmed and close[1] > profileVah and low <= profileVah and close > profileVah
bool profileSellRetest = profileReady and valRetestArmed and close[1] < profileVal and high >= profileVal and close < profileVal
bool profileBuyReentry = profileReady and valReentryArmed and close >= profileVal and close <= profileVah
bool profileSellReentry = profileReady and vahReentryArmed and close <= profileVah and close >= profileVal
 
if profileReady
    if close > profileVah and close[1] <= profileVah
        vahRetestArmed := true
        vahReentryArmed := true
    if close < profileVal and close[1] >= profileVal
        valRetestArmed := true
        valReentryArmed := true
    if profileBuyRetest or profileSellRetest
        vahRetestArmed := false
        valRetestArmed := false
    if profileBuyReentry
        valReentryArmed := false
    if profileSellReentry
        vahReentryArmed := false
 
bool fiveMinuteChart = timeframe.isminutes and timeframe.multiplier == 5
bool vahValBuy = showProfileSignalsInput and fiveMinuteChart and barstate.isconfirmed and bullishBias and (profileBuyRetest or profileBuyReentry)
bool vahValSell = showProfileSignalsInput and fiveMinuteChart and barstate.isconfirmed and bearishBias and (profileSellRetest or profileSellReentry)
if vahValBuy and profileBuyReentry
    activeProfileTarget := profileVah
if vahValSell and profileSellReentry
    activeProfileTarget := profileVal
plot(sessionKey != 0 and activeSessionKey == sessionKey ? activeProfileTarget : na, "Breakout re-entry target", color = color.new(#5b9cf6, 0), linewidth = 2, style = plot.style_linebr)
plotshape(vahValBuy, title = "VAH/VAL Buy", text = "VA BUY", style = shape.labelup, location = location.belowbar, color = #089981, textcolor = color.white, size = size.tiny)
plotshape(vahValSell, title = "VAH/VAL Sell", text = "VA SELL", style = shape.labeldown, location = location.abovebar, color = #f23645, textcolor = color.white, size = size.tiny)
alertcondition(vahValBuy, title = "Session value-area bullish signal", message = "Five-minute bullish value-area retest/re-entry on {{ticker}}")
alertcondition(vahValSell, title = "Session value-area bearish signal", message = "Five-minute bearish value-area retest/re-entry on {{ticker}}")
 
// --- Liquidity-grab + The Strat confluence inputs ---
int stratGrabWindowInput = input.int(8, "Liquidity grab validity (chart bars)", minval = 1, maxval = 30, tooltip = "A Strat formation is eligible only within this many signal-chart bars after a swing-liquidity sweep.", group = "Liquidity + Strat Confluence")
bool showStratSignalsInput = input.bool(true, "Show liquidity + Strat signals", tooltip = "Plot 2-1-2, 3-1-2, and failed 2-up/down continuation signals after a liquidity grab.", group = "Liquidity + Strat Confluence")
bool showSweepMarksInput = input.bool(true, "Show liquidity grab markers", tooltip = "Mark confirmed sweeps of the latest 5-minute swing high or low.", group = "Liquidity + Strat Confluence")
 
// --- The Strat candle classifications ---
f_isInside(int offset) => high[offset] < high[offset + 1] and low[offset] > low[offset + 1]
f_isTwoUp(int offset) => high[offset] > high[offset + 1] and low[offset] >= low[offset + 1]
f_isTwoDown(int offset) => low[offset] < low[offset + 1] and high[offset] <= high[offset + 1]
f_isOutside(int offset) => high[offset] > high[offset + 1] and low[offset] < low[offset + 1]
 
bool rawBullGrab = not na(lastSwingLow) and low < lastSwingLow and close > lastSwingLow
bool rawBearGrab = not na(lastSwingHigh) and high > lastSwingHigh and close < lastSwingHigh
int barsSinceBullGrab = ta.barssince(rawBullGrab)
int barsSinceBearGrab = ta.barssince(rawBearGrab)
bool recentBullGrab = not na(barsSinceBullGrab) and barsSinceBullGrab <= stratGrabWindowInput
bool recentBearGrab = not na(barsSinceBearGrab) and barsSinceBearGrab <= stratGrabWindowInput
bool supportedStratTimeframe = supportedSignalTimeframe
 
// 2-1-2 continuation/reversal variants: directional bar, inside bar, then a same-direction 2 bar.
bool strat212Bull = f_isTwoUp(2) and f_isInside(1) and f_isTwoUp(0)
bool strat212Bear = f_isTwoDown(2) and f_isInside(1) and f_isTwoDown(0)
// 3-1-2: outside bar, inside bar, then directional break.
bool strat312Bull = f_isOutside(2) and f_isInside(1) and f_isTwoUp(0)
bool strat312Bear = f_isOutside(2) and f_isInside(1) and f_isTwoDown(0)
// Failed 2-up/down: the next directional bar breaks and closes beyond the failed bar's opposite side.
bool failedTwoUpBear = f_isTwoUp(1) and low < low[1] and close < low[1]
bool failedTwoDownBull = f_isTwoDown(1) and high > high[1] and close > high[1]
 
bool stratBuy212 = showStratSignalsInput and supportedStratTimeframe and barstate.isconfirmed and bullishBias and recentBullGrab and strat212Bull
bool stratSell212 = showStratSignalsInput and supportedStratTimeframe and barstate.isconfirmed and bearishBias and recentBearGrab and strat212Bear
bool stratBuy312 = showStratSignalsInput and supportedStratTimeframe and barstate.isconfirmed and bullishBias and recentBullGrab and strat312Bull
bool stratSell312 = showStratSignalsInput and supportedStratTimeframe and barstate.isconfirmed and bearishBias and recentBearGrab and strat312Bear
bool stratBuyFailed2 = showStratSignalsInput and supportedStratTimeframe and barstate.isconfirmed and bullishBias and recentBullGrab and failedTwoDownBull
bool stratSellFailed2 = showStratSignalsInput and supportedStratTimeframe and barstate.isconfirmed and bearishBias and recentBearGrab and failedTwoUpBear
bool stratBuy = stratBuy212 or stratBuy312 or stratBuyFailed2
bool stratSell = stratSell212 or stratSell312 or stratSellFailed2
 
plotshape(showSweepMarksInput and rawBullGrab, title = "Bullish liquidity grab", text = "LQ", style = shape.circle, location = location.belowbar, color = color.new(#089981, 10), textcolor = color.white, size = size.tiny)
plotshape(showSweepMarksInput and rawBearGrab, title = "Bearish liquidity grab", text = "LQ", style = shape.circle, location = location.abovebar, color = color.new(#f23645, 10), textcolor = color.white, size = size.tiny)
plotshape(stratBuy212, title = "Liquidity + 2-1-2 Buy", text = "LQ 212", style = shape.labelup, location = location.belowbar, color = #089981, textcolor = color.white, size = size.tiny)
plotshape(stratSell212, title = "Liquidity + 2-1-2 Sell", text = "LQ 212", style = shape.labeldown, location = location.abovebar, color = #f23645, textcolor = color.white, size = size.tiny)
plotshape(stratBuy312, title = "Liquidity + 3-1-2 Buy", text = "LQ 312", style = shape.labelup, location = location.belowbar, color = #089981, textcolor = color.white, size = size.tiny)
plotshape(stratSell312, title = "Liquidity + 3-1-2 Sell", text = "LQ 312", style = shape.labeldown, location = location.abovebar, color = #f23645, textcolor = color.white, size = size.tiny)
plotshape(stratBuyFailed2, title = "Failed 2-down continuation Buy", text = "FAIL 2D", style = shape.labelup, location = location.belowbar, color = #089981, textcolor = color.white, size = size.tiny)
plotshape(stratSellFailed2, title = "Failed 2-up continuation Sell", text = "FAIL 2U", style = shape.labeldown, location = location.abovebar, color = #f23645, textcolor = color.white, size = size.tiny)
alertcondition(stratBuy, title = "Liquidity grab + bullish Strat setup", message = "Bullish liquidity-grab and Strat confluence on {{ticker}} {{interval}}")
alertcondition(stratSell, title = "Liquidity grab + bearish Strat setup", message = "Bearish liquidity-grab and Strat confluence on {{ticker}} {{interval}}")
 
// --- Session VWAP with 5-minute break/retest and rejection signals ---
int vwapSetupExpiryInput = input.int(12, "VWAP retest expiry (5m bars)", minval = 1, maxval = 50, tooltip = "Maximum five-minute bars allowed after a VWAP break for its retest signal.", group = "VWAP Signals")
bool showVwapSignalsInput = input.bool(true, "Show 5m VWAP signals", tooltip = "Show HTF-bias-aligned five-minute VWAP break/retest and rejection signals.", group = "VWAP Signals")
color vwapLineColorInput = input.color(#a855f7, "VWAP line color", tooltip = "VWAP plot color; purple by default.", group = "Style")
float sessionVwap = ta.vwap(hlc3)
plot(sessionVwap, "Session VWAP", color = vwapLineColorInput, linewidth = 2)
bool vwapSignalTimeframe = timeframe.isminutes and timeframe.multiplier == 5
bool vwapCrossUp = ta.crossover(close, sessionVwap)
bool vwapCrossDown = ta.crossunder(close, sessionVwap)
var bool vwapBullRetestArmed = false
var bool vwapBearRetestArmed = false
var int vwapBullCrossBar = na
var int vwapBearCrossBar = na
if vwapCrossUp
    vwapBullRetestArmed := true
    vwapBullCrossBar := bar_index
    vwapBearRetestArmed := false
if vwapCrossDown
    vwapBearRetestArmed := true
    vwapBearCrossBar := bar_index
    vwapBullRetestArmed := false
if vwapBullRetestArmed and not na(vwapBullCrossBar) and bar_index - vwapBullCrossBar > vwapSetupExpiryInput
    vwapBullRetestArmed := false
if vwapBearRetestArmed and not na(vwapBearCrossBar) and bar_index - vwapBearCrossBar > vwapSetupExpiryInput
    vwapBearRetestArmed := false
bool vwapBreakRetestBuy = showVwapSignalsInput and vwapSignalTimeframe and barstate.isconfirmed and bullishBias and vwapBullRetestArmed and bar_index > vwapBullCrossBar and low <= sessionVwap and close > sessionVwap
bool vwapBreakRetestSell = showVwapSignalsInput and vwapSignalTimeframe and barstate.isconfirmed and bearishBias and vwapBearRetestArmed and bar_index > vwapBearCrossBar and high >= sessionVwap and close < sessionVwap
if vwapBreakRetestBuy
    vwapBullRetestArmed := false
if vwapBreakRetestSell
    vwapBearRetestArmed := false
bool vwapBullRejection = showVwapSignalsInput and vwapSignalTimeframe and barstate.isconfirmed and bullishBias and close[1] > sessionVwap[1] and low <= sessionVwap and close > sessionVwap and close > open
bool vwapBearRejection = showVwapSignalsInput and vwapSignalTimeframe and barstate.isconfirmed and bearishBias and close[1] < sessionVwap[1] and high >= sessionVwap and close < sessionVwap and close < open
plotshape(vwapBreakRetestBuy, title = "5m VWAP Break Retest Buy", text = "VWAP RT+", style = shape.labelup, location = location.belowbar, color = #089981, textcolor = color.white, size = size.tiny)
plotshape(vwapBreakRetestSell, title = "5m VWAP Break Retest Sell", text = "VWAP RT-", style = shape.labeldown, location = location.abovebar, color = #f23645, textcolor = color.white, size = size.tiny)
plotshape(vwapBullRejection, title = "5m Bullish VWAP Rejection", text = "VWAP REJ+", style = shape.labelup, location = location.belowbar, color = #089981, textcolor = color.white, size = size.tiny)
plotshape(vwapBearRejection, title = "5m Bearish VWAP Rejection", text = "VWAP REJ-", style = shape.labeldown, location = location.abovebar, color = #f23645, textcolor = color.white, size = size.tiny)
alertcondition(vwapBreakRetestBuy, title = "5m VWAP bullish break and retest", message = "Bullish five-minute VWAP break and retest, aligned with HTF bias, on {{ticker}}")
alertcondition(vwapBreakRetestSell, title = "5m VWAP bearish break and retest", message = "Bearish five-minute VWAP break and retest, aligned with HTF bias, on {{ticker}}")
alertcondition(vwapBullRejection, title = "5m bullish VWAP rejection", message = "Bullish five-minute rejection of VWAP, aligned with HTF bias, on {{ticker}}")
alertcondition(vwapBearRejection, title = "5m bearish VWAP rejection", message = "Bearish five-minute rejection of VWAP, aligned with HTF bias, on {{ticker}}")

← 返回列表