Previous Day / Week / Month High Low + Sweeps

ORBMap · study · 476 行 · 点赞 20 · 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/
//@version=6
// ═══════════════════════════════════════════════════════════════════════════════
//  Previous Day / Week / Month High Low + Sweeps  (v1.1)
//  Publish as: PUBLIC · OPEN-SOURCE (free)
//
//  Levels: prior day high/low/close (PDH, PDL, PDC), prior week high/low (PWH,
//  PWL), prior month high/low (PMoH, PMoL). All from completed periods.
//  Stocks/ETFs use regular-session daily bars, so pre/post-market is excluded.
//
//  Status of each level, confirmed bars only:
//    untested · swept (wick through, close back) · broken (close beyond) ·
//    failed break (closed back after a break)
//
//  Bias & targets (intraday charts), from the latest event at PDH/PDL:
//    close above PDH  → BULLISH   · SL 0.25 daily ATR below PDH
//    close below PDL  → BEARISH   · SL 0.25 daily ATR above PDL
//    PDL swept or failed break, price back above → BULLISH LEAN · SL under today's low
//    PDH swept or failed break, price back below → BEARISH LEAN · SL over today's high
//    otherwise        → NEUTRAL (inside the prior day's range)
//  PT1 / PT2 = the next two levels in the bias direction (PDC, PDH/PDL, week and
//  month levels, and ½× / 1× prior-day-range extensions), with R from the SL.
// ═══════════════════════════════════════════════════════════════════════════════
indicator("Previous Day / Week / Month High Low + Sweeps", shorttitle = "PDH PDL Sweeps", overlay = true,
     max_labels_count = 300)
 
// ───────────────────────────── Inputs ─────────────────────────────
grpL = "Levels"
showD  = input.bool(true, "Prior day high / low", group = grpL, inline = "d")
showDC = input.bool(true, "close", group = grpL, inline = "d")
showW  = input.bool(true, "Prior week high / low", group = grpL)
showM  = input.bool(true, "Prior month high / low", group = grpL)
scope  = input.string("Current period only", "Show lines for", options = ["Current period only", "All history"], group = grpL)
 
grpS = "Prior day source"
dSrc   = input.string("Auto", "Levels from", options = ["Auto", "Exchange daily bars", "Custom session from chart bars"], group = grpS,
     tooltip = "Auto: stocks/ETFs on intraday charts build day/week/month levels from the chart's regular-market bars (pre/post-market excluded, correct in pre-market); everything else uses the exchange's daily/weekly/monthly bars. Custom session: build them from the session below — e.g. 09:30–16:00 RTH levels on ES/NQ.")
sessIn = input.session("0930-1600", "Custom session", group = grpS)
tzIn   = input.string("America/New_York", "Session timezone", group = grpS)
 
grpB = "Bias & profit targets"
showBias = input.bool(true, "Show bias, SL and profit targets", group = grpB)
brkBuf   = input.float(0.25, "Breakout SL: distance back inside × daily ATR", minval = 0.05, step = 0.05, group = grpB,
     tooltip = "After a close above PDH (or below PDL) the SL sits this far back inside the prior day's range.")
swpBuf   = input.float(0.10, "Sweep SL: buffer beyond today's extreme × daily ATR", minval = 0.0, step = 0.05, group = grpB)
minPT    = input.float(0.10, "Ignore targets closer than × daily ATR", minval = 0.0, step = 0.05, group = grpB)
 
grpW = "Chart marks"
markSweep = input.bool(true, "Mark sweeps", group = grpW, inline = "mk")
markBreak = input.bool(true, "Mark breaks / failed breaks", group = grpW, inline = "mk")
 
grpD = "Display"
showLbl  = input.bool(true, "Right-edge labels", group = grpD)
lblOff   = input.int(6, "Label offset (bars)", minval = 2, maxval = 50, group = grpD)
lblGap   = input.float(4.5, "Label spacing (% of visible price range)", minval = 1.0, maxval = 12.0, step = 0.5, group = grpD)
showTbl  = input.bool(true, "Bias panel", group = grpD, inline = "t1")
biasPos  = input.string("Top right", "", options = ["Top right", "Top left", "Bottom right", "Bottom left"], group = grpD, inline = "t1")
showLvT  = input.bool(true, "Level status table", group = grpD, inline = "t2")
lvPos    = input.string("Bottom right", "", options = ["Top right", "Top left", "Bottom right", "Bottom left"], group = grpD, inline = "t2")
tblSz    = input.string("Small", "Table text", options = ["Tiny", "Small", "Normal"], group = grpD)
 
grpC = "Label colors (line colors: Style tab)"
lcD  = input.color(#0284c7, "Day", group = grpC, inline = "c")
lcW  = input.color(#9333ea, "Week", group = grpC, inline = "c")
lcM  = input.color(#ea580c, "Month", group = grpC, inline = "c")
cSw  = input.color(#f59e0b, "Sweep", group = grpC, inline = "c2")
cBk  = input.color(#64748b, "Break", group = grpC, inline = "c2")
cBul = input.color(#16a34a, "Bullish", group = grpC, inline = "c3")
cBer = input.color(#dc2626, "Bearish", group = grpC, inline = "c3")
cPT1 = input.color(#d97706, "PT1", group = grpC, inline = "c4")
cPT2 = input.color(#059669, "PT2", group = grpC, inline = "c4")
cSL  = input.color(#b91c1c, "SL", group = grpC, inline = "c4")
 
C_D = #0284c7
C_W = #9333ea
C_M = #ea580c
 
// ───────────────────────────── Data ─────────────────────────────
// Stocks/ETFs: request regular-session bars so daily highs/lows match the official session.
isEq = syminfo.type == "stock" or syminfo.type == "fund" or syminfo.type == "dr"
tk   = isEq ? ticker.new(syminfo.prefix, syminfo.ticker, session.regular) : syminfo.tickerid
 
tfSec = timeframe.in_seconds()
intra = timeframe.isintraday
useD  = showD and tfSec < 86400
useW  = showW and tfSec < 604800
useM  = showM and not timeframe.ismonthly
 
[dH, dL, dC] = request.security(tk, "D", [high[1], low[1], close[1]], lookahead = barmerge.lookahead_on)
[wHx, wLx]   = request.security(tk, "W", [high[1], low[1]], lookahead = barmerge.lookahead_on)
[mHx, mLx]   = request.security(tk, "M", [high[1], low[1]], lookahead = barmerge.lookahead_on)
dATR  = request.security(tk, "D", ta.atr(14)[1], lookahead = barmerge.lookahead_on)
dOpen = request.security(tk, "D", time, lookahead = barmerge.lookahead_on)
wOpen = request.security(tk, "W", time, lookahead = barmerge.lookahead_on)
 
// ── Levels built from chart bars (stocks: regular market hours; or a custom session)
useCustom    = dSrc == "Custom session from chart bars" and intra
useChartSess = intra and (useCustom or (dSrc == "Auto" and isEq))
inSess  = useCustom ? not na(time(timeframe.period, sessIn, tzIn)) : session.ismarket
tzS     = useCustom ? tzIn : syminfo.timezone
sKey    = year(time, tzS) * 10000 + month(time, tzS) * 100 + dayofmonth(time, tzS)
dow     = dayofweek(time, tzS)
mKey    = year(time, tzS) * 100 + month(time, tzS)
isFirst = inSess and (not inSess[1] or sKey != sKey[1])
var float curSH = na
var float curSL = na
var float curSC = na
var float prvSH = na
var float prvSL = na
var float prvSC = na
var float cwH = na
var float cwL = na
var float pwH = na
var float pwL = na
var int   lastDow = na
var int   lastStart = na
var float cmH = na
var float cmL = na
var float pmH = na
var float pmL = na
var int   cmK = na
if isFirst
    prvSH := curSH
    prvSL := curSL
    prvSC := curSC
    curSH := high
    curSL := low
    // new week: weekday went backwards (e.g. Fri → Mon) or 7+ days passed
    if na(lastStart) or dow < lastDow or time - lastStart >= 7 * 86400000
        pwH := cwH
        pwL := cwL
        cwH := high
        cwL := low
    lastDow := dow
    lastStart := time
    if mKey != cmK
        pmH := cmH
        pmL := cmL
        cmH := high
        cmL := low
        cmK := mKey
else if inSess
    curSH := math.max(curSH, high)
    curSL := math.min(curSL, low)
    cwH := math.max(cwH, high)
    cwL := math.min(cwL, low)
    cmH := math.max(cmH, high)
    cmL := math.min(cmL, low)
if inSess
    curSC := close
 
// Outside the session the just-closed session becomes "prior day" (ready for the next open);
// before the first session of a new week/month, the just-finished week/month becomes "prior".
newWkPending = not inSess and not na(lastStart) and (dow < lastDow or time - lastStart >= 7 * 86400000)
newMoPending = not inSess and mKey != cmK
pdH = useChartSess ? (inSess ? prvSH : curSH) : dH
pdL = useChartSess ? (inSess ? prvSL : curSL) : dL
pdC = useChartSess ? (inSess ? prvSC : curSC) : dC
wH  = useChartSess ? (newWkPending ? cwH : pwH) : wHx
wL  = useChartSess ? (newWkPending ? cwL : pwL) : wLx
mH  = useChartSess ? (newMoPending ? cmH : pmH) : mHx
mL  = useChartSess ? (newMoPending ? cmL : pmL) : mLx
 
// ───────────────────────────── Current period (for "Current period only") ─────────────────────────────
tz     = syminfo.timezone
sLast  = year(last_bar_time, tzS) * 10000 + month(last_bar_time, tzS) * 100 + dayofmonth(last_bar_time, tzS)
curD   = useChartSess ? sKey == sLast : dOpen > last_bar_time - 86400000
curW   = wOpen > last_bar_time - 7 * 86400000
curM   = year(time, tz) == year(last_bar_time, tz) and month(time, tz) == month(last_bar_time, tz)
allH   = scope == "All history"
 
f_line(float x, bool show, bool cur) => show and (allH or cur) and x == x[1] ? x : na
 
plot(f_line(pdH, useD, curD), "Prior day high (PDH)", C_D, 2, plot.style_linebr)
plot(f_line(pdL, useD, curD), "Prior day low (PDL)", C_D, 2, plot.style_linebr)
plot(f_line(pdC, useD and showDC, curD), "Prior day close (PDC)", color.new(C_D, 55), 1, plot.style_linebr)
plot(f_line(wH, useW, curW), "Prior week high (PWH)", C_W, 2, plot.style_linebr)
plot(f_line(wL, useW, curW), "Prior week low (PWL)", C_W, 2, plot.style_linebr)
plot(f_line(mH, useM, curM), "Prior month high (PMoH)", C_M, 2, plot.style_linebr)
plot(f_line(mL, useM, curM), "Prior month low (PMoL)", C_M, 2, plot.style_linebr)
 
// ───────────────────────────── Level status ─────────────────────────────
// st: 0 untested · 1 swept · 2 broken · 3 failed break. Confirmed bars only.
f_state(float lvl, bool isHigh) =>
    var int st = 0
    bool sw = false
    bool bk = false
    bool fl = false
    if lvl != lvl[1]
        st := 0
    if barstate.isconfirmed and not na(lvl)
        beyond = isHigh ? close > lvl : close < lvl
        traded = isHigh ? high > lvl : low < lvl
        if st == 2 and not beyond
            st := 3
            fl := true
        else if beyond and st != 2
            st := 2
            bk := true
        else if traded and not beyond and st == 0
            st := 1
            sw := true
    [st, sw, bk, fl]
 
[stDH, swDH, bkDH, flDH] = f_state(pdH, true)
[stDL, swDL, bkDL, flDL] = f_state(pdL, false)
[stWH, swWH, bkWH, flWH] = f_state(wH, true)
[stWL, swWL, bkWL, flWL] = f_state(wL, false)
[stMH, swMH, bkMH, flMH] = f_state(mH, true)
[stML, swML, bkML, flML] = f_state(mL, false)
 
f_stTxt(int st) => st == 0 ? "untested" : st == 1 ? "swept" : st == 2 ? "broken" : "failed break"
f_px(float x) => str.tostring(x, format.mintick)
 
f_mark(bool on, bool sw, bool bk, bool fl, string name, bool isHigh, bool cur) =>
    if on and (allH or cur)
        sty = isHigh ? label.style_label_down : label.style_label_up
        y   = isHigh ? high : low
        if sw and markSweep
            label.new(bar_index, y, name + " sweep", xloc.bar_index, yloc.price, cSw, sty, color.black, size.tiny,
                 tooltip = "Price traded through " + name + " and closed back " + (isHigh ? "below" : "above") + " it.")
        if bk and markBreak
            label.new(bar_index, y, name + " break", xloc.bar_index, yloc.price, cBk, sty, color.white, size.tiny,
                 tooltip = "A candle closed " + (isHigh ? "above " : "below ") + name + ".")
        if fl and markBreak
            label.new(bar_index, y, name + " failed", xloc.bar_index, yloc.price, cBk, sty, color.white, size.tiny,
                 tooltip = "After closing beyond " + name + ", a candle closed back " + (isHigh ? "below" : "above") + " it.")
 
f_mark(useD, swDH, bkDH, flDH, "PDH", true, curD)
f_mark(useD, swDL, bkDL, flDL, "PDL", false, curD)
f_mark(useW, swWH, bkWH, flWH, "PWH", true, curW)
f_mark(useW, swWL, bkWL, flWL, "PWL", false, curW)
f_mark(useM, swMH, bkMH, flMH, "PMoH", true, curM)
f_mark(useM, swML, bkML, flML, "PMoL", false, curM)
 
f_al(bool on, bool sw, bool bk, bool fl, string name, float lvl) =>
    if on and (sw or bk or fl)
        alert(syminfo.ticker + " " + name + " " + (sw ? "swept" : bk ? "broken" : "failed break") + " at " + f_px(lvl), alert.freq_once_per_bar_close)
f_al(useD, swDH, bkDH, flDH, "PDH", pdH)
f_al(useD, swDL, bkDL, flDL, "PDL", pdL)
f_al(useW, swWH, bkWH, flWH, "PWH", wH)
f_al(useW, swWL, bkWL, flWL, "PWL", wL)
f_al(useM, swMH, bkMH, flMH, "PMoH", mH)
f_al(useM, swML, bkML, flML, "PMoL", mL)
 
// ───────────────────────────── Today's extremes & last event ─────────────────────────────
newDay = pdH != pdH[1] or pdL != pdL[1]
var float tdHi = na
var float tdLo = na
if newDay or na(tdHi)
    tdHi := high
    tdLo := low
else
    tdHi := math.max(tdHi, high)
    tdLo := math.min(tdLo, low)
 
// evt: 1 close above PDH · -1 close below PDL · 2 PDL swept · 3 PDL failed break · -2 PDH swept · -3 PDH failed break
var int evt = 0
if newDay
    evt := 0
if useD
    if swDH
        evt := -2
    if flDH
        evt := -3
    if swDL
        evt := 2
    if flDL
        evt := 3
    if bkDH
        evt := 1
    if bkDL
        evt := -1
 
// ───────────────────────────── Bias, SL, PT1 / PT2 (computed on the last bar) ─────────────────────────────
f_pick(array<float> ps, array<string> ns, int dir, float ref, float minD) =>
    float d1 = na
    string n1 = ""
    for [i, lv] in ps
        if not na(lv)
            d = (lv - ref) * dir
            if d >= minD and (na(d1) or d < d1)
                d1 := d
                n1 := ns.get(i)
    float d2 = na
    string n2 = ""
    if not na(d1)
        for [i, lv] in ps
            if not na(lv)
                d = (lv - ref) * dir
                if d >= d1 + minD and (na(d2) or d < d2)
                    d2 := d
                    n2 := ns.get(i)
    [na(d1) ? na : ref + d1 * dir, n1, na(d2) ? na : ref + d2 * dir, n2]
 
int    bDir  = 0
string bTxt  = "NEUTRAL"
string bWhy  = ""
string bPlan = ""
string bPT   = ""
float  bSL   = na
float  pt1   = na
float  pt2   = na
string pt1N  = ""
string pt2N  = ""
string bBig  = ""
if barstate.islast and showBias and useD and intra and not na(pdH) and not na(dATR)
    if evt == 1 and close > pdH
        bDir := 1
        bTxt := "BULLISH"
        bWhy := "A candle closed above yesterday's high (PDH " + f_px(pdH) + ") and price is still above it."
        bSL  := pdH - brkBuf * dATR
    else if evt == -1 and close < pdL
        bDir := -1
        bTxt := "BEARISH"
        bWhy := "A candle closed below yesterday's low (PDL " + f_px(pdL) + ") and price is still below it."
        bSL  := pdL + brkBuf * dATR
    else if (evt == 2 or evt == 3) and close > pdL
        bDir := 1
        bTxt := "BULLISH LEAN"
        bWhy := evt == 2 ? "Price dipped below yesterday's low (PDL " + f_px(pdL) + ") and closed back above it — sellers could not hold it lower." :
             "Price closed below yesterday's low, then closed back above it — the breakdown failed."
        bSL  := tdLo - swpBuf * dATR
    else if (evt == -2 or evt == -3) and close < pdH
        bDir := -1
        bTxt := "BEARISH LEAN"
        bWhy := evt == -2 ? "Price poked above yesterday's high (PDH " + f_px(pdH) + ") and closed back below it — buyers could not hold it higher." :
             "Price closed above yesterday's high, then closed back below it — the breakout failed."
        bSL  := tdHi + swpBuf * dATR
    else
        bWhy := close <= pdH and close >= pdL ? "Price is inside yesterday's range (" + f_px(pdL) + " – " + f_px(pdH) + "). Wait for a close beyond PDH/PDL or a sweep of one." :
             "Price is outside yesterday's range but no candle has closed there yet this session."
 
    if bDir != 0
        pr = pdH - pdL
        ps = array.from(pdC, pdH, pdL, wH, wL, mH, mL, pdH + 0.5 * pr, pdH + pr, pdL - 0.5 * pr, pdL - pr)
        ns = array.from("PDC", "PDH", "PDL", "PWH", "PWL", "PMoH", "PMoL", "PDH + ½ day range", "PDH + 1× day range", "PDL − ½ day range", "PDL − 1× day range")
        [p1, n1, p2, n2] = f_pick(ps, ns, bDir, close, minPT * dATR)
        pt1 := p1
        pt2 := p2
        pt1N := n1
        pt2N := n2
        risk = math.abs(close - bSL)
        bPlan := (bDir == 1 ? "Long ideas while price stays above SL " : "Short ideas while price stays below SL ") + f_px(bSL) + " (risk " + f_px(risk) + " from last price)."
        bPT := na(pt1) ? "No level ahead within range — trail the SL." :
             "PT1 " + f_px(pt1) + " " + pt1N + " (" + str.tostring(math.abs(pt1 - close) / risk, "0.0") + "R)" +
             (na(pt2) ? "" : " · PT2 " + f_px(pt2) + " " + pt2N + " (" + str.tostring(math.abs(pt2 - close) / risk, "0.0") + "R)")
    else
        bPlan := "No trade idea yet. Bullish above PDH " + f_px(pdH) + ", bearish below PDL " + f_px(pdL) + "."
        bPT := "—"
    wTxt = na(wH) ? "—" : close > wH ? "above last week's high" : close < wL ? "below last week's low" : "inside last week's range"
    mTxt = na(mH) ? "—" : close > mH ? "above last month's high" : close < mL ? "below last month's low" : "inside last month's range"
    bBig := "Week: " + wTxt + " · Month: " + mTxt
 
// ───────────────────────────── Right-edge labels ─────────────────────────────
var float visHi = na
var float visLo = na
if time >= chart.left_visible_bar_time and time <= chart.right_visible_bar_time
    visHi := na(visHi) ? high : math.max(visHi, high)
    visLo := na(visLo) ? low : math.min(visLo, low)
 
f_add(array<float> ps, array<string> ts, array<color> cs, bool on, float p, string txt, color col) =>
    if on and not na(p)
        ps.push(p)
        ts.push(txt)
        cs.push(col)
 
// Put a role (PT1 / PT2 / SL) on the label at that price, or add it as its own label
f_tag(array<float> ps, array<string> ts, array<color> cs, float p, string role, color col) =>
    if not na(p)
        int hit = -1
        for [i, q] in ps
            if hit < 0 and math.abs(q - p) <= syminfo.mintick * 0.5
                hit := i
        if hit >= 0
            ts.set(hit, role + "  ·  " + ts.get(hit))
            cs.set(hit, col)
        else
            ps.push(p)
            ts.push(role + "  " + str.tostring(p, format.mintick))
            cs.push(col)
 
var array<label> lblArr = array.new<label>()
if barstate.islast
    for lb in lblArr
        label.delete(lb)
    lblArr.clear()
    if showLbl
        ps = array.new<float>()
        ts = array.new<string>()
        cs = array.new<color>()
        f_add(ps, ts, cs, useD, pdH, "PDH  " + f_px(pdH) + "  · " + f_stTxt(stDH), lcD)
        f_add(ps, ts, cs, useD, pdL, "PDL  " + f_px(pdL) + "  · " + f_stTxt(stDL), lcD)
        f_add(ps, ts, cs, useD and showDC, pdC, "PDC  " + f_px(pdC), color.new(lcD, 40))
        f_add(ps, ts, cs, useW, wH, "PWH  " + f_px(wH) + "  · " + f_stTxt(stWH), lcW)
        f_add(ps, ts, cs, useW, wL, "PWL  " + f_px(wL) + "  · " + f_stTxt(stWL), lcW)
        f_add(ps, ts, cs, useM, mH, "PMoH  " + f_px(mH) + "  · " + f_stTxt(stMH), lcM)
        f_add(ps, ts, cs, useM, mL, "PMoL  " + f_px(mL) + "  · " + f_stTxt(stML), lcM)
        if bDir != 0
            f_tag(ps, ts, cs, pt1, "PT1 (" + str.tostring(math.abs(pt1 - close) / math.abs(close - bSL), "0.0") + "R)", cPT1)
            f_tag(ps, ts, cs, pt2, "PT2 (" + str.tostring(math.abs(pt2 - close) / math.abs(close - bSL), "0.0") + "R)", cPT2)
            f_tag(ps, ts, cs, bSL, "SL (" + (bDir == 1 ? "long" : "short") + " idea)", cSL)
        if ps.size() > 0
            idx = ps.sort_indices(order.descending)
            top = math.max(ps.max(), nz(visHi, ps.max()))
            bot = math.min(ps.min(), nz(visLo, ps.min()))
            gap = (top - bot) * lblGap / 100
            float prevY = na
            for k in idx
                p = ps.get(k)
                y = na(prevY) ? p : math.min(p, prevY - gap)
                prevY := y
                lblArr.push(label.new(bar_index + lblOff, y, ts.get(k), xloc.bar_index, yloc.price, color.new(cs.get(k), 10),
                     label.style_label_left, color.white, size.small))
 
// ───────────────────────────── Panels ─────────────────────────────
f_pos(string s) => s == "Top right" ? position.top_right : s == "Top left" ? position.top_left : s == "Bottom right" ? position.bottom_right : position.bottom_left
tsz  = tblSz == "Tiny" ? size.tiny : tblSz == "Normal" ? size.normal : size.small
cKey = color.new(#94a3b8, 0)
cVal = color.new(#f1f5f9, 0)
cHd  = color.new(#1e293b, 0)
 
var table tbB = table.new(f_pos(biasPos), 2, 6, color.new(#0f172a, 8), color.new(#334155, 0), 1, color.new(#334155, 0), 0)
var table tbL = table.new(f_pos(lvPos), 4, 7, color.new(#0f172a, 8), color.new(#334155, 0), 1, color.new(#334155, 0), 0)
 
if barstate.islast and showTbl and showBias
    color bc = bDir == 1 ? cBul : bDir == -1 ? cBer : cKey
    table.cell(tbB, 0, 0, "PRIOR LEVELS", text_color = cVal, text_size = tsz, text_halign = text.align_left, bgcolor = cHd)
    table.cell(tbB, 1, 0, syminfo.ticker + " · " + timeframe.period, text_color = cKey, text_size = tsz, text_halign = text.align_left, bgcolor = cHd)
    if not (useD and intra)
        table.cell(tbB, 0, 1, "Bias", text_color = cKey, text_size = tsz, text_halign = text.align_left)
        table.cell(tbB, 1, 1, "Use an intraday chart for bias and targets", text_color = cKey, text_size = tsz, text_halign = text.align_left)
    else
        table.cell(tbB, 0, 1, "Bias", text_color = cKey, text_size = tsz, text_halign = text.align_left)
        table.cell(tbB, 1, 1, bTxt, text_color = color.white, bgcolor = color.new(bc, 15), text_size = tsz, text_halign = text.align_left,
             tooltip = "Based on the latest close or sweep at yesterday's high/low. A lean is weaker than a full bias. Not a prediction.")
        table.cell(tbB, 0, 2, "What happened", text_color = cKey, text_size = tsz, text_halign = text.align_left)
        table.cell(tbB, 1, 2, bWhy, text_color = cVal, text_size = tsz, text_halign = text.align_left)
        table.cell(tbB, 0, 3, "Plan", text_color = cKey, text_size = tsz, text_halign = text.align_left)
        table.cell(tbB, 1, 3, bPlan, text_color = bDir == 0 ? cKey : bc, text_size = tsz, text_halign = text.align_left,
             tooltip = "SL = the price where the idea is wrong. R = reward ÷ risk measured from the last price to the SL.")
        table.cell(tbB, 0, 4, "Targets", text_color = cKey, text_size = tsz, text_halign = text.align_left)
        table.cell(tbB, 1, 4, bPT, text_color = bDir == 0 ? cKey : cPT1, text_size = tsz, text_halign = text.align_left,
             tooltip = "PT1/PT2 = the next two levels in the bias direction: prior day close/high/low, week and month levels, or ½× / 1× of yesterday's range beyond PDH/PDL.")
        table.cell(tbB, 0, 5, "Bigger picture", text_color = cKey, text_size = tsz, text_halign = text.align_left)
        table.cell(tbB, 1, 5, bBig, text_color = cVal, text_size = tsz, text_halign = text.align_left)
 
f_lrow(int r, bool on, string name, float lvl, int st, color col) =>
    if on and not na(lvl)
        d = lvl - close
        stc = st == 0 ? cKey : st == 1 ? cSw : st == 2 ? cVal : color.new(#f87171, 0)
        table.cell(tbL, 0, r, name, text_color = col, text_size = tsz, text_halign = text.align_left)
        table.cell(tbL, 1, r, f_px(lvl), text_color = cVal, text_size = tsz, text_halign = text.align_right)
        table.cell(tbL, 2, r, (d >= 0 ? "+" : "") + f_px(d) + " (" + str.tostring(d / close * 100, "+0.00;-0.00") + "%)", text_color = cKey, text_size = tsz, text_halign = text.align_right)
        table.cell(tbL, 3, r, f_stTxt(st), text_color = stc, text_size = tsz, text_halign = text.align_left)
 
if barstate.islast and showLvT
    table.cell(tbL, 0, 0, "LEVEL", text_color = cKey, text_size = tsz, bgcolor = cHd, text_halign = text.align_left)
    table.cell(tbL, 1, 0, "PRICE", text_color = cKey, text_size = tsz, bgcolor = cHd, text_halign = text.align_right)
    table.cell(tbL, 2, 0, "FROM LAST", text_color = cKey, text_size = tsz, bgcolor = cHd, text_halign = text.align_right)
    table.cell(tbL, 3, 0, "STATUS", text_color = cKey, text_size = tsz, bgcolor = cHd, text_halign = text.align_left)
    f_lrow(1, useD, "PDH", pdH, stDH, lcD)
    f_lrow(2, useD, "PDL", pdL, stDL, lcD)
    f_lrow(3, useW, "PWH", wH, stWH, lcW)
    f_lrow(4, useW, "PWL", wL, stWL, lcW)
    f_lrow(5, useM, "PMoH", mH, stMH, lcM)
    f_lrow(6, useM, "PMoL", mL, stML, lcM)
 
// ───────────────────────────── Alert conditions ─────────────────────────────
alertcondition(useD and swDH, "PDH swept", "{{ticker}} swept the prior day high")
alertcondition(useD and swDL, "PDL swept", "{{ticker}} swept the prior day low")
alertcondition(useD and bkDH, "PDH broken", "{{ticker}} closed above the prior day high")
alertcondition(useD and bkDL, "PDL broken", "{{ticker}} closed below the prior day low")
alertcondition(useD and (flDH or flDL), "PDH/PDL failed break", "{{ticker}} closed back inside after breaking PDH/PDL")
alertcondition(useW and (swWH or swWL or bkWH or bkWL), "Prior week level swept/broken", "{{ticker}} swept or broke a prior week level")
alertcondition(useM and (swMH or swML or bkMH or bkML), "Prior month level swept/broken", "{{ticker}} swept or broke a prior month level")

← 返回列表