IPDA + Liquidity + Breaker + Signals v3

Chz_wiz · study · 211 行 · 点赞 24 · TradingView 原页

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

Pine Script

//@version=6
// Fixed, non-repainting version of ipda_raymond_v6.pine (balanced v3 candidate). The original file is left untouched.
// Fixes: (1) prior-day IPDA via [1] + lookahead_on (no intraday repaint), so sweeps compare against completed levels only
//        (2) FVG colours: high[2] < low = bullish BISI (green), low[2] > high = bearish SIBI (red)
//        (3) breakers use the PRIOR swing (ta.lowest(...)[1]) so they can fire; ta.* calls moved out of conditional blocks
//        (4) explicit stop (sweep extreme ± 1 tick) and target (20 EQ, or opposite IPDA 20 H/L if price is already past the EQ; or fixed R multiple)
//        signals only on confirmed bars; one active trade is tracked for the stop/target lines
indicator("IPDA + Liquidity + Breaker + Signals v3", "IPDA v3", overlay=true, max_boxes_count=100, max_lines_count=100, max_labels_count=100)
 
grpIPDA = "IPDA Ranges"
showIPDA = input.bool(true, "Show IPDA 20/40/60", group=grpIPDA, display=display.none)
showEQ   = input.bool(true, "Show Equilibriums", group=grpIPDA, display=display.none)
grpLevels = "Key Levels"
showPWH = input.bool(true, "Show PWH / PWL", group=grpLevels, display=display.none)
useManualWeekly = input.bool(false, "Use Manual Weekly Close", group=grpLevels, display=display.none)
manualWeeklyClose = input.float(0.0, "Manual Weekly Close Price", step=0.25, group=grpLevels, display=display.none)
lineLookback = input.int(8, "Line Lookback Bars", minval=1, maxval=2000, group=grpLevels, display=display.none)
lineForward  = input.int(8, "Line Forward Bars", minval=1, maxval=50, group=grpLevels, display=display.none)
grpFVG = "FVG / SIBI"
showFVG = input.bool(true, "Show FVGs", group=grpFVG, display=display.none)
fvgMax  = input.int(15, "Max FVG boxes", minval=5, maxval=30, group=grpFVG, display=display.none)
useDailyBoxes = input.bool(false, "Pull FVG/Breaker from Daily (prior completed days)", group=grpFVG, display=display.none)
grpBreaker = "Breaker (Discretionary)"
showBreaker = input.bool(true, "Show Potential Breakers", group=grpBreaker, display=display.none)
breakerLookback = input.int(50, "Breaker Lookback", minval=20, group=grpBreaker, display=display.none)
 
grpSignals = "Signal Layer"
enableSignals = input.bool(false, "Show Signal Markers", group=grpSignals, display=display.none)
enableAlerts  = input.bool(false, "Enable Alerts", group=grpSignals, tooltip="Off = no alert() fires. Alert conditions are also gated by this toggle.", display=display.none)
showStops     = input.bool(true, "Show Stop Lines", group=grpSignals, display=display.none)
showTargets   = input.bool(true, "Show Target Lines", group=grpSignals, display=display.none)
signalOnSweep = input.bool(true, "Require Liquidity Sweep", group=grpSignals, display=display.none)
requireEQ     = input.bool(true, "Require close beyond 20 EQ (original rule – almost never fires)", group=grpSignals, display=display.none)
targetMode    = input.string("20 EQ / opposite IPDA", "Target", options=["20 EQ / opposite IPDA", "R multiple"], group=grpSignals, display=display.none)
rMult         = input.float(2.0, "R multiple", minval=0.5, step=0.5, group=grpSignals, display=display.none)
 
grpUI = "UI"
showInfo   = input.bool(true, "Show Info Table", group=grpUI, display=display.none)
showLegend = input.bool(true, "Show Legend", group=grpUI, display=display.none)
instructional = input.bool(false, "Instructional Mode (tooltips on boxes/labels)", group=grpUI, display=display.none)
infoPos   = input.string("top_right", "Info Table Position", options=["top_right", "top_left", "bottom_right", "bottom_left"], group=grpUI, display=display.none)
infoSize  = input.string("small", "Info Table Text Size", options=["tiny", "small", "normal", "large"], group=grpUI, display=display.none)
legendPos = input.string("bottom_left", "Legend Position", options=["top_right", "top_left", "bottom_right", "bottom_left"], group=grpUI, display=display.none)
legendSize = input.string("tiny", "Legend Text Size", options=["tiny", "small", "normal", "large"], group=grpUI, display=display.none)
 
// ── IPDA from COMPLETED daily bars only
f_ipda(n) => [ta.highest(high, n)[1], ta.lowest(low, n)[1]]
[ipda20H, ipda20L] = request.security(syminfo.tickerid, "D", f_ipda(20), lookahead=barmerge.lookahead_on)
[ipda40H, ipda40L] = request.security(syminfo.tickerid, "D", f_ipda(40), lookahead=barmerge.lookahead_on)
[ipda60H, ipda60L] = request.security(syminfo.tickerid, "D", f_ipda(60), lookahead=barmerge.lookahead_on)
ipda20EQ = (ipda20H + ipda20L) / 2
ipda40EQ = (ipda40H + ipda40L) / 2
ipda60EQ = (ipda60H + ipda60L) / 2
[autoPWH, autoPWL] = request.security(syminfo.tickerid, "W", [high[1], low[1]], lookahead=barmerge.lookahead_on)
pwh = useManualWeekly and manualWeeklyClose > 0 ? manualWeeklyClose : autoPWH
pwl = useManualWeekly and manualWeeklyClose > 0 ? manualWeeklyClose : autoPWL
 
// ── Level lines (redrawn on last bar)
var line[] lvl = array.new_line()
var label[] lvlLb = array.new_label()
f_ln(y, c, st, w) => line.new(bar_index - lineLookback, y, bar_index + lineForward, y, color=c, style=st, width=w)
if barstate.islast
    while array.size(lvl) > 0
        line.delete(array.pop(lvl))
    while array.size(lvlLb) > 0
        label.delete(array.pop(lvlLb))
    if showIPDA
        array.push(lvl, f_ln(ipda20H, #2962FF, line.style_solid, 1)), array.push(lvl, f_ln(ipda20L, #2962FF, line.style_solid, 1))
        array.push(lvl, f_ln(ipda40H, #FF6D00, line.style_solid, 1)), array.push(lvl, f_ln(ipda40L, #FF6D00, line.style_solid, 1))
        array.push(lvl, f_ln(ipda60H, #D50000, line.style_solid, 1)), array.push(lvl, f_ln(ipda60L, #D50000, line.style_solid, 1))
    if showEQ
        array.push(lvl, f_ln(ipda20EQ, color.new(#2962FF, 40), line.style_dashed, 1))
        array.push(lvl, f_ln(ipda40EQ, color.new(#FF6D00, 40), line.style_dashed, 1))
        array.push(lvl, f_ln(ipda60EQ, color.new(#D50000, 40), line.style_dashed, 1))
    if showPWH
        array.push(lvl, f_ln(pwh, #AA00FF, line.style_solid, 2)), array.push(lvl, f_ln(pwl, #AA00FF, line.style_solid, 2))
        array.push(lvlLb, label.new(bar_index, pwh, useManualWeekly ? "Manual Wk" : "PWH", color=#AA00FF, textcolor=color.white, style=label.style_label_down, size=size.small, tooltip=instructional ? "Previous week high: buy-side liquidity. A sweep above and close back below = short idea." : ""))
        array.push(lvlLb, label.new(bar_index, pwl, useManualWeekly ? "Manual Wk" : "PWL", color=#AA00FF, textcolor=color.white, style=label.style_label_up, size=size.small, tooltip=instructional ? "Previous week low: sell-side liquidity. A sweep below and close back above = long idea." : ""))
 
// ── FVG / breaker source (daily = completed days only)
[dH, dL, dC, dO] = request.security(syminfo.tickerid, "D", [high[1], low[1], close[1], open[1]], lookahead=barmerge.lookahead_on)
srcHigh  = useDailyBoxes ? dH : high
srcLow   = useDailyBoxes ? dL : low
srcClose = useDailyBoxes ? dC : close
srcOpen  = useDailyBoxes ? dO : open
newSrc   = useDailyBoxes ? ta.change(time("D")) != 0 : true
 
// ── FVGs (colours fixed)
var box[] fvgBoxes = array.new_box()
bullFVG = srcHigh[2] < srcLow      // BISI
bearFVG = srcLow[2] > srcHigh      // SIBI
if showFVG and barstate.isconfirmed and newSrc
    if bullFVG
        array.push(fvgBoxes, box.new(bar_index[2], srcLow, bar_index, srcHigh[2], bgcolor=color.new(color.green, 88), border_color=color.green, text=instructional ? "BISI" : "", text_size=size.tiny, text_color=color.green))
    if bearFVG
        array.push(fvgBoxes, box.new(bar_index[2], srcLow[2], bar_index, srcHigh, bgcolor=color.new(color.red, 88), border_color=color.red, text=instructional ? "SIBI" : "", text_size=size.tiny, text_color=color.red))
    while array.size(fvgBoxes) > fvgMax
        box.delete(array.shift(fvgBoxes))
 
// ── Breakers (prior swing, all ta.* evaluated every bar)
priorSwingLow  = ta.lowest(srcLow, breakerLookback)[1]
priorSwingHigh = ta.highest(srcHigh, breakerLookback)[1]
hh10 = ta.highest(srcHigh, 10)[1]
ll10 = ta.lowest(srcLow, 10)[1]
brTop = ta.highest(srcHigh, 5)[1]
brBot = ta.lowest(srcLow, 5)[1]
bearBr = srcHigh > hh10 and srcClose < priorSwingLow and srcClose < srcOpen
bullBr = srcLow < ll10 and srcClose > priorSwingHigh and srcClose > srcOpen
var box[] brBoxes = array.new_box()
var label[] brLabels = array.new_label()
if showBreaker and barstate.isconfirmed and newSrc
    if bearBr
        array.push(brBoxes, box.new(bar_index[3], brTop, bar_index, brBot, bgcolor=srcClose > (brTop + brBot) / 2 ? color.new(#FF5252, 75) : color.new(#B71C1C, 75), border_color=#FF5252))
        bearNote = srcClose > brTop ? "Above" : srcClose < brBot ? "Below" : "Inside"
        array.push(brLabels, label.new(bar_index, brTop, "Bearish Breaker\n" + bearNote, color=#FF5252, textcolor=color.white, style=label.style_label_down, size=size.tiny, tooltip=instructional ? "Candidate only (discretionary). Two paths: price rejects the box from below (short), or breaks back through it (bearish idea invalid)." : ""))
    if bullBr
        array.push(brBoxes, box.new(bar_index[3], brTop, bar_index, brBot, bgcolor=srcClose < (brTop + brBot) / 2 ? color.new(#69F0AE, 75) : color.new(#00C853, 75), border_color=#00C853))
        bullNote = srcClose < brBot ? "Below" : srcClose > brTop ? "Above" : "Inside"
        array.push(brLabels, label.new(bar_index, brBot, "Bullish Breaker\n" + bullNote, color=#00C853, textcolor=color.white, style=label.style_label_up, size=size.tiny, tooltip=instructional ? "Candidate only (discretionary). Two paths: price holds the box from above (long), or breaks back down through it (bullish idea invalid)." : ""))
    while array.size(brBoxes) > 8
        box.delete(array.shift(brBoxes))
    while array.size(brLabels) > 8
        label.delete(array.shift(brLabels))
 
// ── Signals (confirmed bars, completed levels)
hiLiq = math.max(ipda20H, pwh)
loLiq = math.min(ipda20L, pwl)
sweptHigh = high >= hiLiq and close < hiLiq
sweptLow  = low  <= loLiq and close > loLiq
longRaw  = (not signalOnSweep or sweptLow)  and close > open and (not requireEQ or close > ipda20EQ)
shortRaw = (not signalOnSweep or sweptHigh) and close < open and (not requireEQ or close < ipda20EQ)
longSignal  = barstate.isconfirmed and longRaw
shortSignal = barstate.isconfirmed and shortRaw and not longSignal
 
tick = syminfo.mintick
var int   tDir = 0
var float tStop = na
var float tTgt = na
var line  stopLn = na
var line  tgtLn = na
if tDir != 0   // manage the active trade
    hitS = tDir > 0 ? low <= tStop : high >= tStop
    hitT = tDir > 0 ? high >= tTgt : low <= tTgt
    if hitS or hitT
        tDir := 0
    else
        if not na(stopLn)
            line.set_x2(stopLn, bar_index)
        if not na(tgtLn)
            line.set_x2(tgtLn, bar_index)
newLong  = longSignal and tDir == 0
newShort = shortSignal and tDir == 0
if newLong or newShort
    d = newLong ? 1 : -1
    stp = newLong ? low - tick : high + tick
    r = (close - stp) * d
    eqAhead = (ipda20EQ - close) * d > 0
    tgt = targetMode == "R multiple" ? close + rMult * r * d : eqAhead ? ipda20EQ : (d > 0 ? ipda20H : ipda20L)
    tDir := d, tStop := stp, tTgt := tgt
    stopLn := showStops   ? line.new(bar_index, stp, bar_index + 1, stp, color=color.red, style=line.style_dotted, width=2) : na
    tgtLn  := showTargets ? line.new(bar_index, tgt, bar_index + 1, tgt, color=color.lime, style=line.style_dotted, width=2) : na
    if enableAlerts
        alert((d > 0 ? "LONG " : "SHORT ") + syminfo.ticker + " @" + str.tostring(close, format.mintick) + " stop " + str.tostring(stp, format.mintick) + " target " + str.tostring(tgt, format.mintick), alert.freq_once_per_bar_close)
 
plotshape(enableSignals and newLong,  title="Long Signal",  style=shape.triangleup,   location=location.belowbar, color=color.green, size=size.small, display=display.all - display.status_line)
plotshape(enableSignals and newShort, title="Short Signal", style=shape.triangledown, location=location.abovebar, color=color.red,   size=size.small, display=display.all - display.status_line)
alertcondition(enableAlerts and newLong,  title="Long Signal",  message="IPDA long {{ticker}} {{close}}")
alertcondition(enableAlerts and newShort, title="Short Signal", message="IPDA short {{ticker}} {{close}}")
 
// ── Info table + Legend (independent)
inPremium = close > ipda20EQ
f_pos(p) => p == "top_left" ? position.top_left : p == "bottom_right" ? position.bottom_right : p == "bottom_left" ? position.bottom_left : position.top_right
f_sz(z) => z == "tiny" ? size.tiny : z == "normal" ? size.normal : z == "large" ? size.large : size.small
iSz = f_sz(infoSize)
lSz = f_sz(legendSize)
var table info = table.new(f_pos(infoPos), 2, 7, bgcolor=color.new(color.black, 85))
var table legend = table.new(f_pos(legendPos), 2, 9, bgcolor=color.new(color.black, 85))
if barstate.islast and showInfo
    table.cell(info, 0, 0, "IPDA 20 EQ", text_color=color.white, text_size=iSz)
    table.cell(info, 1, 0, str.tostring(ipda20EQ, format.mintick), text_color=#2962FF, text_size=iSz)
    table.cell(info, 0, 1, "Bias", text_color=color.white, text_size=iSz)
    table.cell(info, 1, 1, inPremium ? "Premium" : "Discount", text_color=inPremium ? color.red : color.green, text_size=iSz)
    table.cell(info, 0, 2, useManualWeekly ? "Manual Wk" : "PWH", text_color=color.white, text_size=iSz)
    table.cell(info, 1, 2, str.tostring(pwh, format.mintick), text_color=#AA00FF, text_size=iSz)
    table.cell(info, 0, 3, useManualWeekly ? "Manual Wk" : "PWL", text_color=color.white, text_size=iSz)
    table.cell(info, 1, 3, str.tostring(pwl, format.mintick), text_color=#AA00FF, text_size=iSz)
    table.cell(info, 0, 4, "Markers / Alerts", text_color=color.white, text_size=iSz)
    table.cell(info, 1, 4, (enableSignals ? "ON" : "OFF") + " / " + (enableAlerts ? "ON" : "OFF"), text_color=enableSignals or enableAlerts ? color.green : color.gray, text_size=iSz)
    table.cell(info, 0, 5, "Boxes", text_color=color.white, text_size=iSz)
    table.cell(info, 1, 5, useDailyBoxes ? "Daily" : "Chart", text_color=useDailyBoxes ? color.orange : color.gray, text_size=iSz)
    table.cell(info, 0, 6, "Active trade", text_color=color.white, text_size=iSz)
    table.cell(info, 1, 6, tDir == 0 ? "flat" : (tDir > 0 ? "long" : "short") + " S " + str.tostring(tStop, format.mintick) + " T " + str.tostring(tTgt, format.mintick), text_color=color.white, text_size=iSz)
if barstate.islast and showLegend
    table.cell(legend, 0, 0, "— Legend —", text_color=color.yellow, text_size=lSz)
    table.cell(legend, 1, 0, "", text_size=lSz)
    table.cell(legend, 0, 1, "Solid Blue", text_color=#2962FF, text_size=lSz)
    table.cell(legend, 1, 1, "IPDA 20 H/L", text_color=color.white, text_size=lSz)
    table.cell(legend, 0, 2, "Solid Orange", text_color=#FF6D00, text_size=lSz)
    table.cell(legend, 1, 2, "IPDA 40 H/L", text_color=color.white, text_size=lSz)
    table.cell(legend, 0, 3, "Solid Red", text_color=#D50000, text_size=lSz)
    table.cell(legend, 1, 3, "IPDA 60 H/L", text_color=color.white, text_size=lSz)
    table.cell(legend, 0, 4, "Dashed", text_color=color.white, text_size=lSz)
    table.cell(legend, 1, 4, "Equilibriums", text_color=color.white, text_size=lSz)
    table.cell(legend, 0, 5, "Purple", text_color=#AA00FF, text_size=lSz)
    table.cell(legend, 1, 5, "PWH / PWL", text_color=color.white, text_size=lSz)
    table.cell(legend, 0, 6, "Green / Red box", text_color=color.white, text_size=lSz)
    table.cell(legend, 1, 6, "BISI / SIBI (FVG)", text_color=color.white, text_size=lSz)
    table.cell(legend, 0, 7, "Breaker box", text_color=color.white, text_size=lSz)
    table.cell(legend, 1, 7, "Candidate (discretionary)", text_color=color.white, text_size=lSz)
    table.cell(legend, 0, 8, "Dotted red / lime", text_color=color.white, text_size=lSz)
    table.cell(legend, 1, 8, "Stop / Target", text_color=color.white, text_size=lSz)

← 返回列表