Squeeze Breakout & Retest Targets [PhenLabs]

PhenLabs · study · 518 行 · 点赞 329 · TradingView 原页

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

Pine Script

//@version=6
indicator("Squeeze Breakout & Retest Targets [PhenLabs]", shorttitle = "SBRT [PhenLabs]", overlay = true, max_lines_count = 200, max_labels_count = 300, max_boxes_count = 200)
 
// ══════════════════════════════════════════════════════════════════
// Enums & Inputs
// ══════════════════════════════════════════════════════════════════
enum Preset
    auto     = "Auto (by timeframe)"
    scalp    = "Scalp (1m–5m)"
    intraday = "Intraday (15m–1H)"
    swing    = "Swing (4H+)"
 
enum EntryMode
    retest     = "Retest (confirmed)"
    aggressive = "Aggressive (breakout close)"
 
enum StopMode
    atr   = "ATR (1.5×)"
    tight = "Retest Extreme (tight)"
    wide  = "Box Midline (wide)"
 
enum TblSize
    tiny   = "Tiny"
    small  = "Small"
    normal = "Normal"
 
const string G1 = "Setup"
const string G2 = "Filters"
const string G3 = "Display"
 
Preset    presetIn  = input.enum(Preset.auto, "Preset", group = G1, tooltip = "Auto picks Scalp on 5m and below, Intraday up to 1H, and Swing above 1H.")
EntryMode modeIn    = input.enum(EntryMode.retest, "Entry Mode", group = G1, tooltip = "Retest waits for price to come back to the box edge and hold. Aggressive enters on the breakout candle close.")
StopMode  stopIn    = input.enum(StopMode.atr, "Stop Placement", group = G1, tooltip = "ATR: 1.5× ATR from entry (best result in testing across NQ, ES, gold and BTC). Tight: just beyond the retest wick. Wide: beyond the squeeze box midline. Scale-out is 50% at TP1, 30% at TP2, 20% at TP3 and the stop stays put.")
bool      trendIn   = input.bool(true, "Trend Filter (EMA)", group = G2, inline = "tr", tooltip = "Only takes breakouts in the direction of the trend EMA. In testing, this filter plus the ATR stop turned a negative result into a small positive one.")
int       trendLen  = input.int(200, "", minval = 20, maxval = 400, group = G2, inline = "tr")
bool      useVolIn  = input.bool(true, "Volume / Range Expansion Filter", group = G2, tooltip = "Breakout candle needs above-average volume. Symbols without volume use range expansion instead.")
bool      useOddsIn = input.bool(false, "TAKE / SKIP Odds Gate", group = G2, tooltip = "Learns how often similar squeeze breakouts reached TP1 on this chart. Setups below 50% are marked SKIP. Off by default because it did not improve results in testing.")
bool      showBoxIn = input.bool(true, "Squeeze Boxes", group = G3)
bool      showPlnIn = input.bool(true, "Trade Plan (Entry / SL / TP1–TP3)", group = G3)
bool      showFkIn  = input.bool(true, "Fakeout Markers", group = G3)
int       histIn    = input.int(4, "Past Plans Shown", minval = 1, maxval = 12, group = G3)
bool      showTblIn = input.bool(true, "Dashboard", group = G3, inline = "tb")
TblSize   tblSizeIn = input.enum(TblSize.small, "", group = G3, inline = "tb")
color     bullC     = input.color(#00E5A8, "Bull / Bear / Squeeze", group = G3, inline = "cl")
color     bearC     = input.color(#FF4D6D, "", group = G3, inline = "cl")
color     sqC       = input.color(#F5C542, "", group = G3, inline = "cl")
 
// ══════════════════════════════════════════════════════════════════
// Preset parameters
// ══════════════════════════════════════════════════════════════════
const int   LEN      = 20
const int   MIN_SAMP = 8
const int   BOX_KEEP = 6
const float SL_ATR   = 1.5
int    tfSec  = timeframe.in_seconds()
Preset pr     = presetIn != Preset.auto ? presetIn : tfSec <= 300 ? Preset.scalp : tfSec <= 3600 ? Preset.intraday : Preset.swing
float  bbMult = 2.0
float  kcMult = pr == Preset.swing ? 1.4 : 1.5
int    minSq  = pr == Preset.scalp ? 6 : pr == Preset.intraday ? 8 : 10
int    retWin = pr == Preset.scalp ? 8 : pr == Preset.intraday ? 10 : 12
float  brkAtr = pr == Preset.scalp ? 0.10 : pr == Preset.intraday ? 0.15 : 0.20
float  tolAtr = 0.25
float  slBuf  = 0.10
bool   aggro  = modeIn == EntryMode.aggressive
int    barMs  = tfSec * 1000
string prName = pr == Preset.scalp ? "Scalp" : pr == Preset.intraday ? "Intraday" : "Swing"
 
// ══════════════════════════════════════════════════════════════════
// Core series
// ══════════════════════════════════════════════════════════════════
float atr    = ta.atr(14)
float trEma  = ta.ema(close, trendLen)
float basis  = ta.sma(close, LEN)
float dev    = ta.stdev(close, LEN)
float kcMid  = ta.ema(close, LEN)
float kcRng  = ta.atr(LEN)
float bbU    = basis + bbMult * dev
float bbL    = basis - bbMult * dev
float kcU    = kcMid + kcMult * kcRng
float kcL    = kcMid - kcMult * kcRng
bool  sqOn   = not na(kcRng) and not na(dev) and bbU < kcU and bbL > kcL
 
float volAvg  = ta.sma(volume, 20)
bool  hasVol  = not na(volume) and not na(volAvg) and volAvg > 0
float barRng  = high - low
float rngAvg  = ta.sma(barRng, 20)
float effort  = hasVol ? volume / volAvg : (rngAvg > 0 ? barRng / rngAvg : 1.0)
bool  effOk   = not useVolIn or effort >= 1.2
float bodyPct = barRng > 0 ? math.abs(close - open) / barRng : 0.0
 
// ══════════════════════════════════════════════════════════════════
// Types
// ══════════════════════════════════════════════════════════════════
type PlanDraw
    box   riskB
    box   rewB
    line  eL
    line  sL
    line  t1L
    line  t2L
    line  t3L
    label eLb
    label sLb
    label t1Lb
    label t2Lb
    label t3Lb
    label resLb
 
method del(PlanDraw this) =>
    box.delete(this.riskB)
    box.delete(this.rewB)
    line.delete(this.eL)
    line.delete(this.sL)
    line.delete(this.t1L)
    line.delete(this.t2L)
    line.delete(this.t3L)
    label.delete(this.eLb)
    label.delete(this.sLb)
    label.delete(this.t1Lb)
    label.delete(this.t2Lb)
    label.delete(this.t3Lb)
    label.delete(this.resLb)
    true
 
// ══════════════════════════════════════════════════════════════════
// State
// ══════════════════════════════════════════════════════════════════
var bool  inSq     = false
var int   sqCount  = 0
var float sqHi     = na
var float sqLo     = na
var int   sqStartT = na
var bool  sqUsed   = false
var bool  armed    = false
var int   armedAge = 0
var box   curBox   = na
 
var int   st       = 0      // 0 idle, 1 retest wait, 2 in trade
var int   dir      = 0
var float bHi      = na
var float bLo      = na
var float bMid     = na
var float bH       = na
var int   bSqN     = 0
var int   bucket   = -1
var int   waitBars = 0
var bool  touched  = false
var float retExt   = na
 
var float tEn    = na
var float tSl    = na
var float tTp1   = na
var float tTp2   = na
var float tTp3   = na
var float tRisk  = na
var int   tStage = 0
var bool  tTaken = true
var float tR     = 0.0
var int   tRight = na
 
var PlanDraw        cur     = na
var array<PlanDraw> plans   = array.new<PlanDraw>()
var array<box>      sqBoxes = array.new<box>()
var array<line>     rzLines = array.new<line>()
var line            rzL     = na
 
var array<int> bWins = array.new<int>(6, 0)
var array<int> bN    = array.new<int>(6, 0)
 
var int    nTrades  = 0
var int    nWins    = 0
var float  netR     = 0.0
var int    nTp1     = 0
var int    nTp2     = 0
var int    nTp3     = 0
var int    nBreak   = 0
var int    nFake    = 0
var string lastSig  = "—"
var float  lastOdds = na
 
bool evSqStart = false
bool evBullBrk = false
bool evBearBrk = false
bool evBuy     = false
bool evSell    = false
bool evFake    = false
bool evTp1     = false
bool evSlHit   = false
int  sigVal    = 0
 
// ══════════════════════════════════════════════════════════════════
// Helpers (pure)
// ══════════════════════════════════════════════════════════════════
f_odds(int w, int n) => (w + 1.0) / (n + 2.0)
 
f_bucket(int sqLen, float expRatio, int minS) =>
    int d = sqLen <= math.round(minS * 1.5) ? 0 : sqLen <= minS * 3 ? 1 : 2
    int e = expRatio >= 1.5 ? 1 : 0
    d * 2 + e
 
// ══════════════════════════════════════════════════════════════════
// Confirmed-bar logic
// ══════════════════════════════════════════════════════════════════
if barstate.isconfirmed and not na(atr)
    // ── 1. Squeeze tracker ──
    if sqOn
        if not inSq
            inSq      := true
            sqCount   := 1
            sqHi      := high
            sqLo      := low
            sqStartT  := time
            sqUsed    := false
            armed     := false
            curBox    := na
            evSqStart := true
        else
            sqCount := sqCount + 1
            sqHi    := math.max(sqHi, high)
            sqLo    := math.min(sqLo, low)
        if showBoxIn and sqCount >= minSq and not sqUsed and st == 0
            if na(curBox)
                curBox := box.new(sqStartT, sqHi, time, sqLo, xloc = xloc.bar_time, border_color = color.new(sqC, 55), bgcolor = color.new(sqC, 92), border_width = 1, text = "", text_size = size.tiny, text_color = color.new(sqC, 15), text_halign = text.align_left, text_valign = text.align_top)
                array.push(sqBoxes, curBox)
                if array.size(sqBoxes) > BOX_KEEP
                    box.delete(array.shift(sqBoxes))
            else
                box.set_top(curBox, sqHi)
                box.set_bottom(curBox, sqLo)
                box.set_right(curBox, time)
    else
        if inSq
            inSq := false
            if sqCount >= minSq and not sqUsed
                armed    := true
                armedAge := 0
        else if armed
            armedAge := armedAge + 1
            if armedAge > retWin * 2
                armed := false
        if not na(curBox) and armed and st == 0
            box.set_right(curBox, time)
 
    bool boxReady = not sqUsed and ((inSq and sqCount >= minSq) or armed)
 
    // ── 2. Trade management (state 2) ──
    if st == 2
        bool  isLong = dir == 1
        bool  slHit  = isLong ? low <= tSl : high >= tSl
        float rr2    = math.abs(tTp2 - tEn) / tRisk
        float rr3    = math.abs(tTp3 - tEn) / tRisk
        bool  closed = false
        bool  won    = tStage >= 1
        string tag   = "STOP"
        if slHit
            if tStage == 0
                tR := -1.0
                evSlHit := true
            else
                float remW = tStage == 1 ? 0.5 : 0.2
                tR := tR + remW * (isLong ? tSl - tEn : tEn - tSl) / tRisk
            if not na(cur)
                string rs = (tR >= 0 ? "+" : "") + str.tostring(tR, "#.#") + "R"
                label.set_text(cur.sLb, tStage == 0 ? "SL ✕" : "SL")
                box.set_bgcolor(cur.riskB, color.new(bearC, 75))
            closed := true
        else
            if tStage == 0 and (isLong ? high >= tTp1 : low <= tTp1)
                tStage := 1
                tR     := tR + 0.5
                evTp1  := true
                won    := true
                if tTaken
                    nTp1 := nTp1 + 1
                if not na(cur)
                    line.set_color(cur.t1L, bullC)
                    line.set_width(cur.t1L, 2)
                    label.set_text(cur.t1Lb, "TP1 ✓")
            if tStage == 1 and (isLong ? high >= tTp2 : low <= tTp2)
                tStage := 2
                tR     := tR + 0.3 * rr2
                if tTaken
                    nTp2 := nTp2 + 1
                if not na(cur)
                    line.set_color(cur.t2L, bullC)
                    line.set_width(cur.t2L, 2)
                    label.set_text(cur.t2Lb, "TP2 ✓")
            if tStage == 2 and (isLong ? high >= tTp3 : low <= tTp3)
                tStage := 3
                tR     := tR + 0.2 * rr3
                tag    := "TP3"
                closed := true
                if tTaken
                    nTp3 := nTp3 + 1
                if not na(cur)
                    line.set_color(cur.t3L, bullC)
                    line.set_width(cur.t3L, 2)
                    label.set_text(cur.t3Lb, "TP3 ✓")
        // extend plan while live, snap to the exit bar when closed
        tRight := closed ? time : math.max(tRight, time + 3 * barMs)
        if not na(cur)
            box.set_right(cur.riskB, tRight)
            box.set_right(cur.rewB, tRight)
            line.set_x2(cur.eL, tRight)
            line.set_x2(cur.sL, tRight)
            line.set_x2(cur.t1L, tRight)
            line.set_x2(cur.t2L, tRight)
            line.set_x2(cur.t3L, tRight)
            label.set_x(cur.eLb, tRight)
            label.set_x(cur.sLb, tRight)
            label.set_x(cur.t1Lb, tRight)
            label.set_x(cur.t2Lb, tRight)
            label.set_x(cur.t3Lb, tRight)
        if closed and not na(cur)
            string rsT = (tR >= 0 ? "+" : "") + str.tostring(tR, "#.#") + "R · " + (tag == "TP3" ? "TP3" : tStage == 0 ? "STOP" : "TRAIL")
            color  rsC = tR > 0 ? bullC : bearC
            bool   up  = (dir == 1) == (tR > 0)
            cur.resLb := label.new(time - 2 * barMs, up ? high + 0.6 * atr : low - 0.6 * atr, rsT, xloc = xloc.bar_time, yloc = yloc.price, style = up ? label.style_label_down : label.style_label_up, color = color.new(rsC, 10), textcolor = color.black, size = size.small)
        if closed
            if bucket >= 0
                bN.set(bucket, bN.get(bucket) + 1)
                if won
                    bWins.set(bucket, bWins.get(bucket) + 1)
            if tTaken
                nTrades := nTrades + 1
                nWins   := nWins + (won ? 1 : 0)
                netR    := netR + tR
 
            cur := na
            st  := 0
            dir := 0
 
    // ── 3. Retest wait (state 1) ──
    bool fireSignal = false
    if st == 1
        waitBars := waitBars + 1
        bool isLong = dir == 1
        if isLong ? close < bMid : close > bMid
            evFake := true
            nFake  := nFake + 1
            if bucket >= 0
                bN.set(bucket, bN.get(bucket) + 1)
            if showFkIn
                label.new(time, isLong ? low : high, "FAKEOUT", xloc = xloc.bar_time, yloc = yloc.price, style = isLong ? label.style_label_up : label.style_label_down, color = color.new(chart.fg_color, 85), textcolor = color.new(chart.fg_color, 20), size = size.tiny)
            if not na(curBox)
                box.set_text(curBox, "FAKEOUT")
                box.set_text_color(curBox, color.new(chart.fg_color, 30))
                box.set_border_style(curBox, line.style_dashed)
                box.set_border_color(curBox, color.new(chart.fg_color, 60))
                box.set_bgcolor(curBox, color.new(chart.fg_color, 93))
            line.set_x2(rzL, time)
            st  := 0
            dir := 0
        else
            if isLong ? low <= bHi + tolAtr * atr : high >= bLo - tolAtr * atr
                touched := true
                retExt  := na(retExt) ? (isLong ? low : high) : (isLong ? math.min(retExt, low) : math.max(retExt, high))
            if touched and (isLong ? close > bHi and close > open : close < bLo and close < open)
                fireSignal := true
                line.set_x2(rzL, time)
            else if waitBars >= retWin
                st  := 0
                dir := 0
 
    // ── 4. Breakout detection (state 0) ──
    if st == 0 and not fireSignal and boxReady
        float h = sqHi - sqLo
        bool bodyOk = bodyPct >= 0.5
        bool upBrk  = close > sqHi + brkAtr * atr and close > open and bodyOk and effOk and (not trendIn or close > trEma)
        bool dnBrk  = close < sqLo - brkAtr * atr and close < open and bodyOk and effOk and (not trendIn or close < trEma)
        if (upBrk or dnBrk) and h > 0
            dir    := upBrk ? 1 : -1
            bHi    := sqHi
            bLo    := sqLo
            bMid   := (sqHi + sqLo) / 2
            bH     := h
            bSqN   := sqCount
            bucket := f_bucket(sqCount, barRng / atr, minSq)
            sqUsed := true
            armed  := false
            nBreak := nBreak + 1
            evBullBrk := upBrk
            evBearBrk := dnBrk
            color bc = upBrk ? bullC : bearC
            if not na(curBox)
                box.set_right(curBox, time)
                box.set_border_color(curBox, color.new(bc, 35))
                box.set_border_width(curBox, 1)
                box.set_bgcolor(curBox, color.new(bc, 88))
                box.set_text_size(curBox, size.tiny)
                box.set_text(curBox, "SQUEEZE " + str.tostring(sqCount) + " BARS")
                box.set_text_valign(curBox, upBrk ? text.align_bottom : text.align_top)
                box.set_text_color(curBox, color.new(bc, 10))
            waitBars := 0
            touched  := false
            retExt   := na
            if aggro
                retExt     := upBrk ? low : high
                fireSignal := true
            else
                st := 1
                float edge = upBrk ? bHi : bLo
                rzL := line.new(time, edge, time + retWin * barMs, edge, xloc = xloc.bar_time, color = color.new(bc, 30), style = line.style_dashed, width = 1)
                array.push(rzLines, rzL)
                if array.size(rzLines) > BOX_KEEP
                    line.delete(array.shift(rzLines))
 
    // ── 5. Signal → trade plan ──
    if fireSignal
        bool  isLong = dir == 1
        float odds   = f_odds(bucket >= 0 ? bWins.get(bucket) : 0, bucket >= 0 ? bN.get(bucket) : 0)
        int   nb     = bucket >= 0 ? bN.get(bucket) : 0
        bool  skip   = useOddsIn and nb >= MIN_SAMP and odds < 0.5
        lastOdds := nb >= MIN_SAMP ? odds : na
        float ext  = na(retExt) ? (isLong ? low : high) : retExt
        float slLv = stopIn == StopMode.atr ? (isLong ? close - SL_ATR * atr : close + SL_ATR * atr) : stopIn == StopMode.wide ? (isLong ? math.min(ext, bMid) - slBuf * atr : math.max(ext, bMid) + slBuf * atr) : (isLong ? ext - slBuf * atr : ext + slBuf * atr)
        float risk = math.abs(close - slLv)
        if risk > 0
            tEn    := close
            tSl    := slLv
            tRisk  := risk
            tTp1   := isLong ? close + risk : close - risk
            tTp2   := isLong ? math.max(bHi + bH, close + 1.5 * risk) : math.min(bLo - bH, close - 1.5 * risk)
            tTp3   := isLong ? math.max(bHi + 2 * bH, close + 2.5 * risk) : math.min(bLo - 2 * bH, close - 2.5 * risk)
            tStage := 0
            tR     := 0.0
            tTaken := not skip
            tRight := time + 20 * barMs
            st     := 2
            evBuy  := isLong and not skip
            evSell := not isLong and not skip
            sigVal := skip ? 0 : dir
            lastSig := skip ? "SKIP" : (isLong ? "BUY" : "SELL")
            color dc = isLong ? bullC : bearC
            string how = aggro ? "BREAKOUT" : "RETEST"
            string txt = (skip ? "SKIP · " : "") + (isLong ? "BUY" : "SELL") + "\n" + how + (nb >= MIN_SAMP and useOddsIn ? " " + str.tostring(math.round(odds * 100)) + "%" : "")
            label.new(time, isLong ? low - 0.2 * atr : high + 0.2 * atr, txt, xloc = xloc.bar_time, yloc = yloc.price, style = isLong ? label.style_label_up : label.style_label_down, color = skip ? color.new(chart.fg_color, 60) : dc, textcolor = skip ? chart.bg_color : color.black, size = size.small)
            cur := na
            if not skip and showPlnIn
                int t0 = time
                PlanDraw pd = PlanDraw.new()
                pd.riskB := box.new(t0, math.max(tEn, tSl), tRight, math.min(tEn, tSl), xloc = xloc.bar_time, bgcolor = color.new(bearC, 86), border_color = color.new(bearC, 100))
                pd.rewB  := box.new(t0, math.max(tEn, tTp3), tRight, math.min(tEn, tTp3), xloc = xloc.bar_time, bgcolor = color.new(bullC, 90), border_color = color.new(bullC, 100))
                pd.eL    := line.new(t0, tEn, tRight, tEn, xloc = xloc.bar_time, color = chart.fg_color, width = 2)
                pd.sL    := line.new(t0, tSl, tRight, tSl, xloc = xloc.bar_time, color = bearC, width = 2)
                pd.t1L   := line.new(t0, tTp1, tRight, tTp1, xloc = xloc.bar_time, color = color.new(bullC, 40), width = 1)
                pd.t2L   := line.new(t0, tTp2, tRight, tTp2, xloc = xloc.bar_time, color = color.new(bullC, 40), width = 1)
                pd.t3L   := line.new(t0, tTp3, tRight, tTp3, xloc = xloc.bar_time, color = color.new(bullC, 40), width = 1)
                pd.eLb   := label.new(tRight, tEn, "ENTRY " + str.tostring(tEn, format.mintick), xloc = xloc.bar_time, yloc = yloc.price, style = label.style_label_left, color = color.new(color.black, 100), textcolor = chart.fg_color, size = size.small)
                pd.sLb   := label.new(tRight, tSl, "SL " + str.tostring(tSl, format.mintick), xloc = xloc.bar_time, yloc = yloc.price, style = label.style_label_left, color = color.new(color.black, 100), textcolor = bearC, size = size.small)
                pd.t1Lb  := label.new(tRight, tTp1, "TP1 " + str.tostring(math.abs(tTp1 - tEn) / tRisk, "#.#") + "R", xloc = xloc.bar_time, yloc = yloc.price, style = label.style_label_left, color = color.new(color.black, 100), textcolor = bullC, size = size.small)
                pd.t2Lb  := label.new(tRight, tTp2, "TP2 " + str.tostring(math.abs(tTp2 - tEn) / tRisk, "#.#") + "R", xloc = xloc.bar_time, yloc = yloc.price, style = label.style_label_left, color = color.new(color.black, 100), textcolor = bullC, size = size.small)
                pd.t3Lb  := label.new(tRight, tTp3, "TP3 " + str.tostring(math.abs(tTp3 - tEn) / tRisk, "#.#") + "R", xloc = xloc.bar_time, yloc = yloc.price, style = label.style_label_left, color = color.new(color.black, 100), textcolor = bullC, size = size.small)
                cur := pd
                array.push(plans, pd)
                if array.size(plans) > histIn
                    PlanDraw old = array.shift(plans)
                    old.del()
        else
            st  := 0
            dir := 0
 
// ══════════════════════════════════════════════════════════════════
// Pine Screener / Data Window (first plots)
// ══════════════════════════════════════════════════════════════════
plot(sigVal, "Signal (+1 Buy / -1 Sell)", display = display.data_window + display.pine_screener)
plot(sqOn ? 1 : 0, "Squeeze On", display = display.data_window + display.pine_screener)
plot(st, "State (0 Idle / 1 Retest / 2 Trade)", display = display.data_window + display.pine_screener)
plot(na(lastOdds) ? na : lastOdds * 100, "Setup Odds %", display = display.data_window + display.pine_screener)
plot(evFake ? 1 : 0, "Fakeout", display = display.data_window + display.pine_screener)
 
plot(high, "Anchor H", color = color.new(color.white, 100), display = display.pane, editable = false)
plot(low, "Anchor L", color = color.new(color.white, 100), display = display.pane, editable = false)
 
// ══════════════════════════════════════════════════════════════════
// Alerts
// ══════════════════════════════════════════════════════════════════
alertcondition(evSqStart, "Squeeze Started", "SBRT: squeeze started on {{ticker}} {{interval}}")
alertcondition(evBullBrk, "Bullish Breakout", "SBRT: bullish squeeze breakout on {{ticker}} {{interval}}")
alertcondition(evBearBrk, "Bearish Breakout", "SBRT: bearish squeeze breakout on {{ticker}} {{interval}}")
alertcondition(evBuy, "BUY Signal", "SBRT: BUY confirmed on {{ticker}} {{interval}} @ {{close}}")
alertcondition(evSell, "SELL Signal", "SBRT: SELL confirmed on {{ticker}} {{interval}} @ {{close}}")
alertcondition(evFake, "Fakeout", "SBRT: breakout failed back inside the box on {{ticker}} {{interval}}")
alertcondition(evTp1, "TP1 Hit", "SBRT: TP1 hit on {{ticker}}")
alertcondition(evSlHit, "Stop Hit", "SBRT: stop loss hit on {{ticker}}")
 
// ══════════════════════════════════════════════════════════════════
// Dashboard
// ══════════════════════════════════════════════════════════════════
string tblSz = tblSizeIn == TblSize.tiny ? size.tiny : tblSizeIn == TblSize.small ? size.small : size.normal
var table dash = table.new(position.top_right, 2, 10, bgcolor = color.new(chart.bg_color, 10), border_color = color.new(chart.fg_color, 80), border_width = 1, frame_color = color.new(chart.fg_color, 70), frame_width = 1)
 
f_row(table tb, int r, string k, string v, color vc, string sz) =>
    table.cell(tb, 0, r, k, text_color = color.new(chart.fg_color, 30), text_size = sz, text_halign = text.align_left)
    table.cell(tb, 1, r, v, text_color = vc, text_size = sz, text_halign = text.align_left)
    true
 
if barstate.islast and showTblIn
    color fgC = chart.fg_color
    string stTxt = st == 2 ? (dir == 1 ? "LONG" : "SHORT") + (tTaken ? "" : " (skipped)") + " @ " + str.tostring(tEn, format.mintick) + (tStage >= 1 ? " · TP" + str.tostring(tStage) + " ✓" : "") : st == 1 ? "WAITING RETEST " + str.tostring(waitBars) + "/" + str.tostring(retWin) : inSq and sqCount >= minSq ? "SQUEEZE · " + str.tostring(sqCount) + " bars" : armed ? "ARMED" : "SCANNING"
    color stCol = st == 2 ? (dir == 1 ? bullC : bearC) : st == 1 ? sqC : (inSq and sqCount >= minSq) or armed ? sqC : color.new(fgC, 40)
    string trTxt = not trendIn ? "OFF" : close > trEma ? "ABOVE EMA " + str.tostring(trendLen) + " · longs" : "BELOW EMA " + str.tostring(trendLen) + " · shorts"
    color trCol = not trendIn ? color.new(fgC, 40) : close > trEma ? bullC : bearC
    string wrTxt = nTrades > 0 ? str.tostring(100.0 * nWins / nTrades, "#.#") + "% (" + str.tostring(nWins) + "W/" + str.tostring(nTrades - nWins) + "L)" : "—"
    string nrTxt = nTrades > 0 ? (netR >= 0 ? "+" : "") + str.tostring(netR, "#.#") + "R" : "—"
    string fkTxt = nBreak > 0 ? str.tostring(100.0 * nFake / nBreak, "#") + "% (" + str.tostring(nFake) + " of " + str.tostring(nBreak) + ")" : "—"
    string sgTxt = lastSig + (useOddsIn and not na(lastOdds) ? " · " + str.tostring(math.round(lastOdds * 100)) + "%" : "")
    table.cell(dash, 0, 0, "SQUEEZE BREAKOUT & RETEST", text_color = sqC, text_size = tblSz, text_formatting = text.format_bold)
    table.merge_cells(dash, 0, 0, 1, 0)
    f_row(dash, 1, "Preset", prName + " · " + (aggro ? "Aggressive" : "Retest"), fgC, tblSz)
    f_row(dash, 2, "Trend", trTxt, trCol, tblSz)
    f_row(dash, 3, "State", stTxt, stCol, tblSz)
    f_row(dash, 4, "Last Signal", sgTxt, lastSig == "BUY" ? bullC : lastSig == "SELL" ? bearC : color.new(fgC, 40), tblSz)
    f_row(dash, 5, "Win Rate (TP1+)", wrTxt, fgC, tblSz)
    f_row(dash, 6, "Net R", nrTxt, netR >= 0 ? bullC : bearC, tblSz)
    f_row(dash, 7, "Target Hits", "TP1: " + str.tostring(nTp1) + " · TP2: " + str.tostring(nTp2) + " · TP3: " + str.tostring(nTp3), fgC, tblSz)
    f_row(dash, 8, "Fakeouts Caught", fkTxt, fgC, tblSz)
    f_row(dash, 9, "Data", hasVol ? "VOLUME" : "RANGE PROXY", color.new(fgC, 40), tblSz)

← 返回列表