ICT Confluence Signals

kyleol16 · study · 856 行 · 点赞 3 · TradingView 原页

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

Pine Script

//@version=6
indicator("ICT Confluence Signals", overlay = true, max_labels_count = 500, max_lines_count = 500, max_boxes_count = 500)
 
// v14 changes:
//  1. Stops sized from the market: structure (beyond the sweep / leg low) with an ATR floor, or ATR, or fixed $
//     Position size shown on every signal (contracts + micros for the same $ risk)
//  2. Targets in R (default 2R), capped at the next key level; min-R room filter
//  3. Filters each have their own mode (Block / Warn / Off). Default: liquidity = Block, bias and room = Warn
//     Stops wider than a max ATR multiple are always blocked. Blocked setups can be shown faintly.
//  4. Asia entries trade inside the Asia session itself, with their own loss limit (no longer eats the NY budget)
//  5. EARLY trigger tightened: the sweep low / high must hold and price must close beyond the sweep bar
//  6. 4H bias uses the last CLOSED 4H bar (no live repaint)
//  7. Context panel: results row by grade (A+ / B / EARLY) from the virtual trades
//  8. Clean-up: atr14 / h4Fast / h4Slow names, session stats table off by default
// v15: optional OTE entry zone (62-79% retracement of the sweep leg) instead of the single 50% line
// v15b: 62 / 70 / 79% lines with prices, shaded OTE zone
// v16h: no signal markers drawn (signals still drive the panel, alerts and stats)
// v16g: minimal look - triangles for signals, dots for sweeps, lighter boxes and EMAs
// v16f: no target/stop boxes or exit result labels on the chart
// v16e: compact signal tags (grade only, full details on hover)
// v16d: compact context panel (merged rows, coloured text, optional stats rows)
// v16c: orderflow approximation - signal-bar delta from 1m intrabars, results split by delta agree / against
// v16b: 'Draw on liquidity' row - nearest untaken level in the bias direction
// v16: OTE reaction entries - price trades into the zone, rejects (wick) and closes back beyond the 62% line; stop beyond the leg extreme
 
tz = "America/Chicago"
 
// ───────── Inputs ─────────
gR = "Risk"
riskUsd    = input.float(300, "Risk per trade ($)", minval = 1, group = gR)
stopMode   = input.string("Structure", "Stop", options = ["Structure", "ATR", "Fixed $"], group = gR, tooltip = "Structure: beyond the sweep / leg low (or high), never closer than the ATR floor. ATR: ATR multiple only. Fixed $: risk $ on 1 contract.")
atrMult    = input.float(1.0, "Min stop / ATR stop (x ATR)", minval = 0.1, step = 0.1, group = gR)
maxRiskAtr = input.float(3.0, "Max stop (x ATR) - wider is blocked", minval = 0.5, step = 0.5, group = gR)
rr         = input.float(2.0, "Target (R multiple)", minval = 0.5, step = 0.5, group = gR)
capNext    = input.bool(true, "Cap the target at the next key level", group = gR)
maxLosses  = input.int(2, "NY: max losing trades per day", minval = 1, group = gR)
dayLimit   = input.float(600, "Daily loss limit ($, per session group)", group = gR)
 
gS = "Setup"
entrySess = input.session("0730-1400", "Entry window (Chicago)", group = gS)
flatSess  = input.session("0730-1500", "Be flat outside (Chicago)", group = gS)
asiaEnt   = input.bool(true, "Asia entries (inside the Asia session, flat at its end)", group = gS)
asiaMaxL  = input.int(1, "Asia: max losing trades", minval = 1, group = gS)
minGap    = input.float(1.0, "Min FVG size (points)", minval = 0, group = gS)
minLeg    = input.float(10, "Min range for the 50% level (points)", minval = 0, group = gS)
dispMult  = input.float(1.2, "Displacement: FVG candle body vs avg body (0 = off)", minval = 0, step = 0.1, group = gS)
gradeMode = input.bool(true, "Grade signals: A+ = displacement, B = without (off = A+ only)", group = gS)
zoneMode  = input.string("OTE 62-79%", "FVG entry zone", options = ["OTE 62-79%", "Past 50%"], group = gS)
oteLo     = input.float(0.62, "OTE from (retracement)", minval = 0.5, maxval = 1, step = 0.005, group = gS)
oteHi     = input.float(0.79, "OTE to (retracement)", minval = 0.5, maxval = 1, step = 0.005, group = gS)
oteMid    = input.float(0.70, "OTE middle line (retracement)", minval = 0.5, maxval = 1, step = 0.005, group = gS)
earlyOn   = input.bool(true, "EARLY entries: sweep reversal before the 14 EMA", group = gS)
earlyBars = input.int(3, "EARLY: max bars after the sweep", minval = 1, group = gS)
oteSigOn  = input.bool(true, "OTE entries: rejection back out of the 62% line", group = gS)
rejWick   = input.float(0.4, "OTE: min rejection wick (share of the candle)", minval = 0, maxval = 1, step = 0.05, group = gS)
liveSw    = input.bool(true, "Live sweeps of the running Asia / London high-low", group = gS)
liveMin   = input.int(6, "Live sweeps: min bars into the session / between sweeps", minval = 1, group = gS)
 
gF = "Filters"
biasMode = input.string("Warn",  "Against 4H / daily-open bias", options = ["Block", "Warn", "Off"], group = gF)
roomMode = input.string("Warn",  "Next level closer than min R", options = ["Block", "Warn", "Off"], group = gF)
minRR    = input.float(1.5, "Min R before the next level", minval = 0.5, step = 0.25, group = gF)
liqMode  = input.string("Block", "Untaken liquidity close on the stop side", options = ["Block", "Warn", "Off"], group = gF)
liqMult  = input.float(2.5, "Untaken level within (x stop distance)", minval = 0, step = 0.5, group = gF)
ofTf     = input.timeframe("1", "Orderflow: intrabar timeframe for delta", group = gF)
ofMin    = input.float(0.10, "Orderflow: min delta share to count as agreeing", minval = 0, maxval = 1, step = 0.05, group = gF)
 
gT = "Sessions (Chicago time)"
asiaSess  = input.session("1900-2359", "Asia", group = gT)
lonSess   = input.session("0100-0400", "London", group = gT)
nyamSess  = input.session("0830-1100", "NY AM", group = gT)
nypmSess  = input.session("1230-1500", "NY PM", group = gT)
 
gD = "Display"
showBoxes = input.bool(true, "Session boxes", group = gD)
showLvls  = input.bool(true, "Key levels", group = gD)
showSweep = input.bool(false, "Sweep markers (dots)", group = gD)
showFvg   = input.bool(true, "Live fair value gaps", group = gD)
showHalf  = input.bool(true, "50% levels", group = gD)
showEma   = input.bool(true, "EMAs 14 / 50", group = gD)
showBlk   = input.bool(false, "Show blocked setups (faint)", group = gD)
showCtx   = input.bool(true, "Context panel", group = gD)
showTable = input.bool(false, "Session stats table", group = gD)
 
// ───────── Time ─────────
isIn(string s) => not na(time(timeframe.period, s, tz))
newDay   = timeframe.change("D")
inEntry  = isIn(entrySess)
inFlat   = isIn(flatSess)
inAsia   = isIn(asiaSess)
inLon    = isIn(lonSess)
inAm     = isIn(nyamSess)
inPm     = isIn(nypmSess)
inAnyFl  = inFlat or (asiaEnt and inAsia)
pv       = syminfo.pointvalue
 
var int dayBar = bar_index
if newDay
    dayBar := bar_index
 
// ───────── Session boxes ─────────
sess(bool on, color c, string name) =>
    var box   bx = na
    var float hi = na
    var float lo = na
    if on and not on[1]
        hi := high
        lo := low
        if showBoxes
            bx := box.new(bar_index, high, bar_index, low, border_color = color.new(c, 80), bgcolor = color.new(c, 96), text = name, text_color = color.new(c, 45), text_size = size.small, text_halign = text.align_left, text_valign = text.align_top)
    else if on
        hi := math.max(hi, high)
        lo := math.min(lo, low)
        if not na(bx)
            box.set_top(bx, hi)
            box.set_bottom(bx, lo)
            box.set_right(bx, bar_index)
    [hi, lo]
 
[aH, aL]   = sess(inAsia, color.blue,    "Asia")
[lH, lL]   = sess(inLon,  color.red,     "London")
[amH, amL] = sess(inAm,   color.green,   "NY AM")
[pmH, pmL] = sess(inPm,   color.fuchsia, "NY PM")
 
var bool aToday = false
var bool lToday = false
if newDay
    aToday := false
    lToday := false
if inAsia
    aToday := true
if inLon
    lToday := true
aRdy = aToday and not inAsia
lRdy = lToday and not inLon
 
// Bars into the current Asia / London session (for live sweeps)
var int aBars = 0
var int lBars = 0
aBars := inAsia ? (inAsia[1] ? aBars + 1 : 0) : 0
lBars := inLon ? (inLon[1] ? lBars + 1 : 0) : 0
 
// Price action after each session closes (has its high / low been taken?)
var float aPostHi = na
var float aPostLo = na
var float lPostHi = na
var float lPostLo = na
if newDay or inAsia
    aPostHi := na
    aPostLo := na
else if aToday
    aPostHi := na(aPostHi) ? high : math.max(aPostHi, high)
    aPostLo := na(aPostLo) ? low  : math.min(aPostLo, low)
if newDay or inLon
    lPostHi := na
    lPostLo := na
else if lToday
    lPostHi := na(lPostHi) ? high : math.max(lPostHi, high)
    lPostLo := na(lPostLo) ? low  : math.min(lPostLo, low)
aTakenH = aRdy and not na(aPostHi) and aPostHi > aH
aTakenL = aRdy and not na(aPostLo) and aPostLo < aL
lTakenH = lRdy and not na(lPostHi) and lPostHi > lH
lTakenL = lRdy and not na(lPostLo) and lPostLo < lL
 
// ───────── Key levels (current day only) ─────────
pdh = request.security(syminfo.tickerid, "D", high[1], lookahead = barmerge.lookahead_on)
pdl = request.security(syminfo.tickerid, "D", low[1],  lookahead = barmerge.lookahead_on)
pwh = request.security(syminfo.tickerid, "W", high[1], lookahead = barmerge.lookahead_on)
pwl = request.security(syminfo.tickerid, "W", low[1],  lookahead = barmerge.lookahead_on)
 
var float nyOpen = na
if newDay
    nyOpen := na
if inAm and not inAm[1]
    nyOpen := open
 
lvl(float y, string txt, color c) =>
    var line  ln = line.new(bar_index, y, bar_index, y, color = c, style = line.style_dotted, width = 1)
    var label lb = label.new(bar_index, y, txt, style = label.style_label_left, color = color.new(color.white, 100), textcolor = c, size = size.small)
    yy = showLvls ? y : na
    line.set_xy1(ln, dayBar, yy)
    line.set_xy2(ln, bar_index + 8, yy)
    label.set_xy(lb, bar_index + 8, yy)
 
lvl(pdh, "PDH", color.red)
lvl(pdl, "PDL", color.green)
lvl(pwh, "PWH", color.maroon)
lvl(pwl, "PWL", color.olive)
lvl(nyOpen, "NY Open", color.gray)
lvl(aToday ? aH : na, "Asia H", color.blue)
lvl(aToday ? aL : na, "Asia L", color.blue)
lvl(lToday ? lH : na, "London H", color.red)
lvl(lToday ? lL : na, "London L", color.red)
 
// ───────── Day range ─────────
var float dLo = na
var float dHi = na
if newDay
    dLo := na
    dHi := na
dLo := na(dLo) ? low  : math.min(dLo, low)
dHi := na(dHi) ? high : math.max(dHi, high)
 
// ───────── Sweeps: trade through a level, then close back inside ─────────
sweptBelow(float lvlY, bool ready) =>
    var bool traded = false
    var bool done   = false
    if newDay
        traded := false
        done   := false
    if ready and not na(lvlY)
        traded := traded or low < lvlY
        done   := done or (traded and close > lvlY)
    done
 
sweptAbove(float lvlY, bool ready) =>
    var bool traded = false
    var bool done   = false
    if newDay
        traded := false
        done   := false
    if ready and not na(lvlY)
        traded := traded or high > lvlY
        done   := done or (traded and close < lvlY)
    done
 
pdLow  = sweptBelow(pdl, true)
asLow  = sweptBelow(aL, aRdy)
lnLow  = sweptBelow(lL, lRdy)
pdHigh = sweptAbove(pdh, true)
asHigh = sweptAbove(aH, aRdy)
lnHigh = sweptAbove(lH, lRdy)
 
// Live sweeps: wick through the running session high / low and close back inside
var int lastLiveLo = na
var int lastLiveHi = na
liveCoolLo = na(lastLiveLo) or bar_index - lastLiveLo >= liveMin
liveCoolHi = na(lastLiveHi) or bar_index - lastLiveHi >= liveMin
liveALow  = liveSw and inAsia and aBars >= liveMin and liveCoolLo and low < aL[1] and close > aL[1]
liveAHigh = liveSw and inAsia and aBars >= liveMin and liveCoolHi and high > aH[1] and close < aH[1]
liveLLow  = liveSw and inLon and lBars >= liveMin and liveCoolLo and low < lL[1] and close > lL[1]
liveLHigh = liveSw and inLon and lBars >= liveMin and liveCoolHi and high > lH[1] and close < lH[1]
if liveALow or liveLLow
    lastLiveLo := bar_index
if liveAHigh or liveLHigh
    lastLiveHi := bar_index
 
var string lowSrc   = ""
var string highSrc  = ""
var float  lowLvlY  = na
var float  highLvlY = na
if newDay
    lowSrc   := ""
    highSrc  := ""
    lowLvlY  := na
    highLvlY := na
lowEvent  = (pdLow and not pdLow[1]) or (asLow and not asLow[1]) or (lnLow and not lnLow[1]) or liveALow or liveLLow
highEvent = (pdHigh and not pdHigh[1]) or (asHigh and not asHigh[1]) or (lnHigh and not lnHigh[1]) or liveAHigh or liveLHigh
if liveALow
    lowSrc  := "Asia L (live)"
    lowLvlY := aL[1]
if liveLLow
    lowSrc  := "London L (live)"
    lowLvlY := lL[1]
if pdLow and not pdLow[1]
    lowSrc  := "PDL"
    lowLvlY := pdl
if asLow and not asLow[1]
    lowSrc  := "Asia L"
    lowLvlY := aL
if lnLow and not lnLow[1]
    lowSrc  := "London L"
    lowLvlY := lL
if liveAHigh
    highSrc  := "Asia H (live)"
    highLvlY := aH[1]
if liveLHigh
    highSrc  := "London H (live)"
    highLvlY := lH[1]
if pdHigh and not pdHigh[1]
    highSrc  := "PDH"
    highLvlY := pdh
if asHigh and not asHigh[1]
    highSrc  := "Asia H"
    highLvlY := aH
if lnHigh and not lnHigh[1]
    highSrc  := "London H"
    highLvlY := lH
plotshape(lowEvent and showSweep, "Sweep low", shape.circle, location.belowbar, color.new(color.teal, 30), size = size.tiny)
plotshape(highEvent and showSweep, "Sweep high", shape.circle, location.abovebar, color.new(color.maroon, 30), size = size.tiny)
 
// Sweep bar extremes (for the EARLY trigger) and whether the sweep has held since
var float evLoL   = na
var float evHiL   = na
var bool  evHeldL = false
var float evHiS   = na
var float evLoS   = na
var bool  evHeldS = false
if newDay
    evHeldL := false
    evHeldS := false
if lowEvent
    evLoL   := low
    evHiL   := high
    evHeldL := true
else if evHeldL and low < evLoL
    evHeldL := false
if highEvent
    evHiS   := high
    evLoS   := low
    evHeldS := true
else if evHeldS and high > evHiS
    evHeldS := false
 
// ───────── 50% levels: move off the sweep, otherwise the day's range ─────────
var float bLegLo = na
var float bLegHi = na
var float sLegHi = na
var float sLegLo = na
if newDay
    bLegLo := na
    bLegHi := na
    sLegHi := na
    sLegLo := na
if lowEvent
    bLegLo := dLo
    bLegHi := high
else if not na(bLegLo)
    if low < bLegLo
        bLegLo := low
        bLegHi := high
    else
        bLegHi := math.max(bLegHi, high)
if highEvent
    sLegHi := dHi
    sLegLo := low
else if not na(sLegHi)
    if high > sLegHi
        sLegHi := high
        sLegLo := low
    else
        sLegLo := math.min(sLegLo, low)
 
bLo  = lowSrc  != "" and not na(bLegLo) ? bLegLo : dLo
bHi  = lowSrc  != "" and not na(bLegLo) ? bLegHi : dHi
sHi  = highSrc != "" and not na(sLegHi) ? sLegHi : dHi
sLo  = highSrc != "" and not na(sLegHi) ? sLegLo : dLo
bOk  = bHi - bLo >= minLeg
sOk  = sHi - sLo >= minLeg
b50  = bHi - 0.5 * (bHi - bLo)
s50  = sLo + 0.5 * (sHi - sLo)
isOte = zoneMode == "OTE 62-79%"
bZTop = bHi - oteLo * (bHi - bLo)
bZBot = bHi - oteHi * (bHi - bLo)
sZBot = sLo + oteLo * (sHi - sLo)
sZTop = sLo + oteHi * (sHi - sLo)
bZMid = bHi - oteMid * (bHi - bLo)
sZMid = sLo + oteMid * (sHi - sLo)
bLvl1 = isOte ? bZTop : b50
sLvl1 = isOte ? sZBot : s50
zoneL = isOte ? (close <= bZTop and close >= bZBot) : close <= b50
zoneS = isOte ? (close >= sZBot and close <= sZTop) : close >= s50
 
var int bStart = bar_index
var int sStart = bar_index
bStart := lowEvent or newDay ? bar_index : bStart
sStart := highEvent or newDay ? bar_index : sStart
var line bEq = line.new(bar_index, na, bar_index, na, color = color.green, width = 1)
var line sEq = line.new(bar_index, na, bar_index, na, color = color.red, width = 1)
bEq := newDay ? line.new(bar_index, na, bar_index, na, color = color.green, width = 1) : bEq
sEq := newDay ? line.new(bar_index, na, bar_index, na, color = color.red, width = 1) : sEq
bUpd = showHalf and inAnyFl and bOk
sUpd = showHalf and inAnyFl and sOk
line.set_xy1(bEq, bUpd ? bStart : line.get_x1(bEq), bUpd ? bLvl1 : line.get_y1(bEq))
line.set_xy2(bEq, bUpd ? bar_index : line.get_x2(bEq), bUpd ? bLvl1 : line.get_y2(bEq))
line.set_xy1(sEq, sUpd ? sStart : line.get_x1(sEq), sUpd ? sLvl1 : line.get_y1(sEq))
line.set_xy2(sEq, sUpd ? bar_index : line.get_x2(sEq), sUpd ? sLvl1 : line.get_y2(sEq))
// Second (79%) line of the OTE zone
var line bEq2 = line.new(bar_index, na, bar_index, na, color = color.green, width = 1, style = line.style_dashed)
var line sEq2 = line.new(bar_index, na, bar_index, na, color = color.red, width = 1, style = line.style_dashed)
bEq2 := newDay ? line.new(bar_index, na, bar_index, na, color = color.green, width = 1, style = line.style_dashed) : bEq2
sEq2 := newDay ? line.new(bar_index, na, bar_index, na, color = color.red, width = 1, style = line.style_dashed) : sEq2
b2Upd = bUpd and isOte
s2Upd = sUpd and isOte
line.set_xy1(bEq2, b2Upd ? bStart : line.get_x1(bEq2), b2Upd ? bZBot : line.get_y1(bEq2))
line.set_xy2(bEq2, b2Upd ? bar_index : line.get_x2(bEq2), b2Upd ? bZBot : line.get_y2(bEq2))
line.set_xy1(sEq2, s2Upd ? sStart : line.get_x1(sEq2), s2Upd ? sZTop : line.get_y1(sEq2))
line.set_xy2(sEq2, s2Upd ? bar_index : line.get_x2(sEq2), s2Upd ? sZTop : line.get_y2(sEq2))
// Middle (70%) line and shading of the OTE zone
var line bEq3 = line.new(bar_index, na, bar_index, na, color = color.new(color.green, 30), width = 1, style = line.style_dotted)
var line sEq3 = line.new(bar_index, na, bar_index, na, color = color.new(color.red, 30), width = 1, style = line.style_dotted)
var linefill bFill = linefill.new(bEq, bEq2, color.new(color.green, 100))
var linefill sFill = linefill.new(sEq, sEq2, color.new(color.red, 100))
if newDay
    bEq3  := line.new(bar_index, na, bar_index, na, color = color.new(color.green, 30), width = 1, style = line.style_dotted)
    sEq3  := line.new(bar_index, na, bar_index, na, color = color.new(color.red, 30), width = 1, style = line.style_dotted)
    bFill := linefill.new(bEq, bEq2, color.new(color.green, 100))
    sFill := linefill.new(sEq, sEq2, color.new(color.red, 100))
line.set_xy1(bEq3, b2Upd ? bStart : line.get_x1(bEq3), b2Upd ? bZMid : line.get_y1(bEq3))
line.set_xy2(bEq3, b2Upd ? bar_index : line.get_x2(bEq3), b2Upd ? bZMid : line.get_y2(bEq3))
line.set_xy1(sEq3, s2Upd ? sStart : line.get_x1(sEq3), s2Upd ? sZMid : line.get_y1(sEq3))
line.set_xy2(sEq3, s2Upd ? bar_index : line.get_x2(sEq3), s2Upd ? sZMid : line.get_y2(sEq3))
var label bLbl = label.new(bar_index, na, "Longs below 50%", style = label.style_label_left, color = color.new(color.white, 100), textcolor = color.green, size = size.small)
var label sLbl = label.new(bar_index, na, "Shorts above 50%", style = label.style_label_left, color = color.new(color.white, 100), textcolor = color.red, size = size.small)
label.set_xy(bLbl, bar_index + 2, showHalf ? line.get_y2(bEq) : na)
label.set_xy(sLbl, bar_index + 2, showHalf ? line.get_y2(sEq) : na)
pctTxt(float p) => str.tostring(p * 100, "#.#") + "%"
label.set_text(bLbl, isOte ? pctTxt(oteLo) + "  " + str.tostring(line.get_y2(bEq), format.mintick) : "Longs below 50%")
label.set_text(sLbl, isOte ? pctTxt(oteLo) + "  " + str.tostring(line.get_y2(sEq), format.mintick) : "Shorts above 50%")
var label bL70 = label.new(bar_index, na, "", style = label.style_label_left, color = color.new(color.white, 100), textcolor = color.green, size = size.small)
var label bL79 = label.new(bar_index, na, "", style = label.style_label_left, color = color.new(color.white, 100), textcolor = color.green, size = size.small)
var label sL70 = label.new(bar_index, na, "", style = label.style_label_left, color = color.new(color.white, 100), textcolor = color.red, size = size.small)
var label sL79 = label.new(bar_index, na, "", style = label.style_label_left, color = color.new(color.white, 100), textcolor = color.red, size = size.small)
showOteL = showHalf and isOte
label.set_xy(bL70, bar_index + 2, showOteL ? line.get_y2(bEq3) : na)
label.set_xy(bL79, bar_index + 2, showOteL ? line.get_y2(bEq2) : na)
label.set_xy(sL70, bar_index + 2, showOteL ? line.get_y2(sEq3) : na)
label.set_xy(sL79, bar_index + 2, showOteL ? line.get_y2(sEq2) : na)
label.set_text(bL70, pctTxt(oteMid) + "  " + str.tostring(line.get_y2(bEq3), format.mintick))
label.set_text(bL79, pctTxt(oteHi) + "  " + str.tostring(line.get_y2(bEq2), format.mintick))
label.set_text(sL70, pctTxt(oteMid) + "  " + str.tostring(line.get_y2(sEq3), format.mintick))
label.set_text(sL79, pctTxt(oteHi) + "  " + str.tostring(line.get_y2(sEq2), format.mintick))
avgBody = ta.sma(math.abs(close - open), 20)
dispOk  = dispMult <= 0 or math.abs(close[1] - open[1]) >= dispMult * avgBody[1]
 
// ───────── Fair value gaps (only the live one on each side is drawn) ─────────
var float bullTop = na
var float bullBot = na
var int   bullBar = na
var bool  bullDisp = false
var float bearTop = na
var float bearBot = na
var bool  bearDisp = false
var int   bearBar = na
var box   bullBx  = na
var box   bearBx  = na
if newDay
    bullTop := na
    bullBot := na
    bearTop := na
    bearBot := na
    if not na(bullBx)
        box.delete(bullBx)
    if not na(bearBx)
        box.delete(bearBx)
    bullBx := na
    bearBx := na
if low - high[2] >= minGap and (dispOk or gradeMode)
    if not na(bullBx)
        box.delete(bullBx)
    bullTop := low
    bullBot := high[2]
    bullDisp := dispOk
    bullBar := bar_index
    bullBx  := showFvg and inAnyFl ? box.new(bar_index - 2, low, bar_index + 3, high[2], border_color = na, bgcolor = color.new(color.green, 90)) : na
if low[2] - high >= minGap and (dispOk or gradeMode)
    if not na(bearBx)
        box.delete(bearBx)
    bearTop := low[2]
    bearBot := high
    bearDisp := dispOk
    bearBar := bar_index
    bearBx  := showFvg and inAnyFl ? box.new(bar_index - 2, low[2], bar_index + 3, high, border_color = na, bgcolor = color.new(color.red, 90)) : na
if not na(bullBot) and close < bullBot
    bullTop := na
    bullBot := na
    if not na(bullBx)
        box.delete(bullBx)
    bullBx := na
if not na(bearTop) and close > bearTop
    bearTop := na
    bearBot := na
    if not na(bearBx)
        box.delete(bearBx)
    bearBx := na
if not na(bullBx)
    box.set_right(bullBx, bar_index + 3)
if not na(bearBx)
    box.set_right(bearBx, bar_index + 3)
 
// ───────── EMAs ─────────
ema14 = ta.ema(close, 14)
ema50 = ta.ema(close, 50)
plot(showEma ? ema14 : na, "EMA 14", color = color.new(color.orange, 25), linewidth = 1)
plot(showEma ? ema50 : na, "EMA 50", color = color.new(color.teal, 25),   linewidth = 1)
 
// ───────── Context (used by the filters and the panel) ─────────
// 4H EMAs from the last CLOSED 4H bar, so the bias does not repaint intrabar
h4Fast = request.security(syminfo.tickerid, "240", ta.ema(close, 20)[1], lookahead = barmerge.lookahead_on)
h4Slow = request.security(syminfo.tickerid, "240", ta.ema(close, 50)[1], lookahead = barmerge.lookahead_on)
dOpen  = request.security(syminfo.tickerid, "D", open, lookahead = barmerge.lookahead_on)
bias   = close > dOpen and h4Fast > h4Slow ? 1 : close < dOpen and h4Fast < h4Slow ? -1 : 0
atr14  = ta.atr(14)
volR   = atr14 / ta.sma(atr14, 500)
adr    = request.security(syminfo.tickerid, "D", ta.sma(high - low, 10)[1], lookahead = barmerge.lookahead_on)
rngPct = (dHi - dLo) / adr * 100
lvlArr = array.from(pdh, pdl, pwh, pwl, nyOpen, aToday ? aH : na, aToday ? aL : na, lToday ? lH : na, lToday ? lL : na)
nmArr  = array.from("PDH", "PDL", "PWH", "PWL", "NY Open", "Asia H", "Asia L", "London H", "London L")
float upLvl = na
float dnLvl = na
string upNm = ""
string dnNm = ""
for i = 0 to 8
    v = array.get(lvlArr, i)
    if not na(v) and v > close and (na(upLvl) or v < upLvl)
        upLvl := v
        upNm := array.get(nmArr, i)
    if not na(v) and v < close and (na(dnLvl) or v > dnLvl)
        dnLvl := v
        dnNm := array.get(nmArr, i)
 
// Nearest UNTAKEN liquidity on each side (PDL/PDH and finished Asia / London sessions)
liqLoArr = array.from(dLo > pdl ? pdl : na, aRdy and not aTakenL ? aL : na, lRdy and not lTakenL ? lL : na)
liqHiArr = array.from(dHi < pdh ? pdh : na, aRdy and not aTakenH ? aH : na, lRdy and not lTakenH ? lH : na)
liqNm    = array.from("PDL/PDH", "Asia", "London")
float liqBelow = na
float liqAbove = na
string liqBNm = ""
string liqANm = ""
for i = 0 to 2
    lo = array.get(liqLoArr, i)
    hi = array.get(liqHiArr, i)
    if not na(lo) and lo < close and (na(liqBelow) or lo > liqBelow)
        liqBelow := lo
        liqBNm := i == 0 ? "PDL" : array.get(liqNm, i) + " L"
    if not na(hi) and hi > close and (na(liqAbove) or hi < liqAbove)
        liqAbove := hi
        liqANm := i == 0 ? "PDH" : array.get(liqNm, i) + " H"
 
// ───────── Orderflow approximation: candle delta from intrabar up/down volume ─────────
[ibO, ibC, ibV] = request.security_lower_tf(syminfo.tickerid, ofTf, [open, close, volume])
float dBuy  = 0.0
float dSell = 0.0
if array.size(ibV) > 0
    for j = 0 to array.size(ibV) - 1
        o1 = array.get(ibO, j)
        c1 = array.get(ibC, j)
        v1 = array.get(ibV, j)
        if c1 > o1
            dBuy += v1
        else if c1 < o1
            dSell += v1
dTot   = dBuy + dSell
dShare = dTot > 0 ? (dBuy - dSell) / dTot : na
ofL    = not na(dShare) and dShare >= ofMin
ofS    = not na(dShare) and dShare <= -ofMin
dTxt   = na(dShare) ? "n/a" : (dShare >= 0 ? "+" : "") + str.tostring(dShare * 100, "#") + "%"
 
// ───────── Virtual trades (daily limits + results by grade) ─────────
var int    dir      = 0
var float  ePx      = na
var float  sPx      = na
var float  tPx      = na
var float  eRisk    = na
var string tGrade   = ""
var bool   tAsia    = false
var int    dayLoss  = 0
var float  dayPnl   = 0.0
var int    asiaLoss = 0
var float  asiaPnl  = 0.0
var statN = array.new_int(4, 0)
var statW = array.new_int(4, 0)
var statR = array.new_float(4, 0.0)
var int tOF = 0
var ofN = array.new_int(3, 0)
var ofW = array.new_int(3, 0)
var ofR = array.new_float(3, 0.0)
if newDay
    dayLoss  := 0
    dayPnl   := 0.0
    asiaLoss := 0
    asiaPnl  := 0.0
 
float  exitR   = na
string exitTxt = ""
if dir != 0
    hitS = dir == 1 ? low  <= sPx : high >= sPx
    hitT = dir == 1 ? high >= tPx : low  <= tPx
    ownFl = tAsia ? inAsia : inFlat
    if hitS
        exitR   := -1.0
        exitTxt := "-1R"
    else if hitT
        exitR   := math.abs(tPx - ePx) / eRisk
        exitTxt := "+" + str.tostring(exitR, "#.0") + "R"
    else if not ownFl
        exitR   := (close - ePx) * dir / eRisk
        exitTxt := "Flat " + (exitR >= 0 ? "+" : "") + str.tostring(exitR, "#.0") + "R"
if not na(exitR)
    pnl = exitR * riskUsd
    if tAsia
        asiaPnl  += pnl
        asiaLoss += exitR < 0 ? 1 : 0
    else
        dayPnl  += pnl
        dayLoss += exitR < 0 ? 1 : 0
    gi = tGrade == "A+" ? 0 : tGrade == "B" ? 1 : tGrade == "OTE" ? 3 : 2
    array.set(statN, gi, array.get(statN, gi) + 1)
    array.set(statW, gi, array.get(statW, gi) + (exitR > 0 ? 1 : 0))
    array.set(statR, gi, array.get(statR, gi) + exitR)
    oi = tOF == 1 ? 0 : tOF == -1 ? 1 : 2
    array.set(ofN, oi, array.get(ofN, oi) + 1)
    array.set(ofW, oi, array.get(ofW, oi) + (exitR > 0 ? 1 : 0))
    array.set(ofR, oi, array.get(ofR, oi) + exitR)
    dir := 0
 
haltDay  = dayLoss >= maxLosses or dayPnl <= -dayLimit
haltAsia = asiaLoss >= asiaMaxL or asiaPnl <= -dayLimit
canDay   = inEntry and not haltDay
canAsia  = asiaEnt and inAsia and not haltAsia
canTrade = dir == 0 and (canDay or canAsia)
 
// ───────── Setups ─────────
inBull   = not na(bullTop) and bar_index > bullBar and low <= bullTop and close > bullBot
inBear   = not na(bearTop) and bar_index > bearBar and high >= bearBot and close < bearTop
fvgLong  = canTrade and inBull and bOk and zoneL
fvgShort = canTrade and inBear and sOk and zoneS
sinceLo  = nz(ta.barssince(lowEvent), 9999)
sinceHi  = nz(ta.barssince(highEvent), 9999)
earlyLong  = earlyOn and canTrade and not fvgLong and sinceLo >= 1 and sinceLo <= earlyBars and evHeldL and not na(lowLvlY) and close > lowLvlY and close > evHiL and close < ema14
earlyShort = earlyOn and canTrade and not fvgShort and sinceHi >= 1 and sinceHi <= earlyBars and evHeldS and not na(highLvlY) and close < highLvlY and close < evLoS and close > ema14
rng  = high - low
oteLong  = oteSigOn and canTrade and not fvgLong and not earlyLong and bOk and rng > 0 and low <= bZTop and low > bLo and close > bZTop and (math.min(open, close) - low) >= rejWick * rng and (close - low) >= 0.6 * rng
oteShort = oteSigOn and canTrade and not fvgShort and not earlyShort and sOk and rng > 0 and high >= sZBot and high < sHi and close < sZBot and (high - math.max(open, close)) >= rejWick * rng and (high - close) >= 0.6 * rng
rawL = fvgLong or earlyLong or oteLong
rawS = fvgShort or earlyShort or oteShort
 
// ───────── Stops, targets, size ─────────
buf    = syminfo.mintick * 4
atrD   = atrMult * atr14
fixD   = riskUsd / pv
lo5    = ta.lowest(low, 5)
hi5    = ta.highest(high, 5)
strucL = oteLong ? bLo : earlyLong ? evLoL : lowSrc  != "" and not na(bLegLo) ? bLegLo : lo5
strucS = oteShort ? sHi : earlyShort ? evHiS : highSrc != "" and not na(sLegHi) ? sLegHi : hi5
stopL  = stopMode == "Fixed $" ? close - fixD : stopMode == "ATR" ? close - atrD : math.min(strucL - buf, close - atrD)
stopS  = stopMode == "Fixed $" ? close + fixD : stopMode == "ATR" ? close + atrD : math.max(strucS + buf, close + atrD)
riskL  = close - stopL
riskS  = stopS - close
tgtRL  = close + rr * riskL
tgtRS  = close - rr * riskS
tgtL   = capNext and not na(upLvl) and upLvl - buf < tgtRL ? upLvl - buf : tgtRL
tgtS   = capNext and not na(dnLvl) and dnLvl + buf > tgtRS ? dnLvl + buf : tgtRS
rL     = (tgtL - close) / riskL
rS     = (close - tgtS) / riskS
hasMicro = syminfo.root == "NQ" or syminfo.root == "ES" or syminfo.root == "YM" or syminfo.root == "RTY"
sizeTxt(float riskPts) =>
    q  = math.floor(riskUsd / (riskPts * pv))
    mq = math.floor(riskUsd / (riskPts * pv / 10))
    hasMicro ? (q >= 1 ? str.tostring(q) + " " + syminfo.root + " / " : "") + str.tostring(mq) + " micro" : str.tostring(math.max(q, 1)) + " " + syminfo.root
 
// ───────── Filters (each: Block / Warn / Off) ─────────
fBiasL = biasMode != "Off" and bias == -1
fBiasS = biasMode != "Off" and bias == 1
fRoomL = roomMode != "Off" and rL < minRR
fRoomS = roomMode != "Off" and rS < minRR
fLiqL  = liqMode != "Off" and not na(liqBelow) and close - liqBelow <= liqMult * riskL
fLiqS  = liqMode != "Off" and not na(liqAbove) and liqAbove - close <= liqMult * riskS
fWideL = riskL > maxRiskAtr * atr14
fWideS = riskS > maxRiskAtr * atr14
blockL = fWideL or (fBiasL and biasMode == "Block") or (fRoomL and roomMode == "Block") or (fLiqL and liqMode == "Block")
blockS = fWideS or (fBiasS and biasMode == "Block") or (fRoomS and roomMode == "Block") or (fLiqS and liqMode == "Block")
warnL  = fBiasL or fRoomL or fLiqL
warnS  = fBiasS or fRoomS or fLiqS
wTxtL  = (fBiasL ? "\n! against bias" : "") + (fRoomL ? "\n! " + upNm + " at " + str.tostring(rL, "#.0") + "R" : "") + (fLiqL ? "\n! " + liqBNm + " untaken -" + str.tostring(close - liqBelow, "#") + " pts" : "") + (fWideL ? "\n! stop " + str.tostring(riskL / atr14, "#.0") + "x ATR" : "")
wTxtS  = (fBiasS ? "\n! against bias" : "") + (fRoomS ? "\n! " + dnNm + " at " + str.tostring(rS, "#.0") + "R" : "") + (fLiqS ? "\n! " + liqANm + " untaken +" + str.tostring(liqAbove - close, "#") + " pts" : "") + (fWideS ? "\n! stop " + str.tostring(riskS / atr14, "#.0") + "x ATR" : "")
 
longSig  = rawL and not blockL
shortSig = rawS and not blockS and not longSig
isEarlyL = earlyLong and not fvgLong
isEarlyS = earlyShort and not fvgShort
isOteL   = oteLong and not fvgLong and not earlyLong
isOteS   = oteShort and not fvgShort and not earlyShort
gradeL   = isOteL ? "OTE" : isEarlyL ? "EARLY" : bullDisp ? "A+" : "B"
gradeS   = isOteS ? "OTE" : isEarlyS ? "EARLY" : bearDisp ? "A+" : "B"
 
// Faint markers for setups the filters blocked (at most one per side every 6 bars)
var int lastBlkL = na
var int lastBlkS = na
if showBlk and rawL and blockL and (na(lastBlkL) or bar_index - lastBlkL >= 6)
    lastBlkL := bar_index
    label.new(bar_index, low, "x", style = label.style_label_up, color = color.new(color.gray, 60), textcolor = color.new(color.white, 30), size = size.tiny, tooltip = "x LONG " + gradeL + " blocked" + wTxtL)
if showBlk and rawS and blockS and not longSig and (na(lastBlkS) or bar_index - lastBlkS >= 6)
    lastBlkS := bar_index
    label.new(bar_index, high, "x", style = label.style_label_down, color = color.new(color.gray, 60), textcolor = color.new(color.white, 30), size = size.tiny, tooltip = "x SHORT " + gradeS + " blocked" + wTxtS)
 
if longSig
    dir    := 1
    ePx    := close
    sPx    := stopL
    tPx    := tgtL
    eRisk  := riskL
    tGrade := gradeL
    tAsia  := not canDay
    tOF    := na(dShare) ? 0 : ofL ? 1 : -1
    if not isEarlyL and not isOteL
        if not na(bullBx)
            box.set_right(bullBx, bar_index)
        bullBx  := na
        bullTop := na
        bullBot := na
if shortSig
    dir    := -1
    ePx    := close
    sPx    := stopS
    tPx    := tgtS
    eRisk  := riskS
    tGrade := gradeS
    tAsia  := not canDay
    tOF    := na(dShare) ? 0 : ofS ? 1 : -1
    if not isEarlyS and not isOteS
        if not na(bearBx)
            box.set_right(bearBx, bar_index)
        bearBx  := na
        bearTop := na
        bearBot := na
 
alertcondition(longSig,  "Long setup",  "ICT Confluence: LONG setup on {{ticker}} near {{close}}")
alertcondition(shortSig, "Short setup", "ICT Confluence: SHORT setup on {{ticker}} near {{close}}")
alertcondition((longSig and gradeL == "A+") or (shortSig and gradeS == "A+"), "A+ setup", "ICT Confluence: A+ setup on {{ticker}} near {{close}}")
alertcondition(isEarlyL or isEarlyS, "EARLY setup", "ICT Confluence: EARLY sweep-reversal setup on {{ticker}} near {{close}}")
alertcondition(isOteL or isOteS, "OTE setup", "ICT Confluence: OTE reaction setup on {{ticker}} near {{close}}")
alertcondition((longSig and not warnL) or (shortSig and not warnS), "Clean setup (no warnings)", "ICT Confluence: clean setup on {{ticker}} near {{close}}")
alertcondition(longSig or shortSig, "Any setup", "ICT Confluence: setup on {{ticker}} near {{close}}")
 
// ───────── 4H candle ─────────
newH4 = timeframe.change("240")
var float h4o = na
var float h4h = na
var float h4l = na
var int h4t = na
h4o := newH4 ? open : h4o
h4h := newH4 ? high : math.max(h4h, high)
h4l := newH4 ? low : math.min(h4l, low)
h4t := newH4 ? time : h4t
upWk = h4h - math.max(h4o, close)
dnWk = math.min(h4o, close) - h4l
wkMin = 2 * atr14
h4Lbl = na(h4t) ? "4H" : str.format_time(h4t, "HH:mm", "America/Chicago") + " 4H"
h4Txt = na(h4o) ? "n/a" : dnWk >= wkMin and close > h4o ? "Down-wick " + str.tostring(dnWk, "#") + " pts, back above open" : upWk >= wkMin and close < h4o ? "Up-wick " + str.tostring(upWk, "#") + " pts, back below open" : "No clear wick yet (" + str.tostring(dnWk, "#") + " dn / " + str.tostring(upWk, "#") + " up)"
h4Col = na(h4o) ? color.gray : dnWk >= wkMin and close > h4o ? color.green : upWk >= wkMin and close < h4o ? color.red : color.gray
plot(newH4 ? na : h4o, "4H open", color = color.new(color.yellow, 0), linewidth = 1, style = plot.style_linebr)
 
lunch  = isIn("1130-1300")
winTxt = inAm ? "NY AM (prime)" : lunch ? "Lunch (choppy)" : inEntry ? "In window" : asiaEnt and inAsia ? "Asia window" : "Outside window"
winTxt := winTxt + ((inEntry and haltDay) or (asiaEnt and inAsia and haltAsia) ? " (halted)" : "")
winCol = inAm ? color.green : lunch ? color.orange : inEntry ? color.teal : asiaEnt and inAsia ? color.blue : color.gray
var string lastSig = "None yet today"
var color lastCol = color.gray
lastSig := newDay ? "None yet today" : longSig ? gradeL + " LONG" + (bias == 1 ? " - with bias" : bias == -1 ? " - AGAINST bias" : "") + (warnL ? " (!)" : "") : shortSig ? gradeS + " SHORT" + (bias == -1 ? " - with bias" : bias == 1 ? " - AGAINST bias" : "") + (warnS ? " (!)" : "") : lastSig
lastCol := newDay ? color.gray : longSig ? (warnL or bias == -1 ? color.red : gradeL == "A+" and bias == 1 ? color.green : color.orange) : shortSig ? (warnS or bias == 1 ? color.red : gradeS == "A+" and bias == -1 ? color.green : color.orange) : lastCol
 
// ───────── Session stats: average range and how often the high / low gets taken later that day ─────────
sessStats(float hi, float lo, bool on) =>
    var bool  done = false
    var bool  hT   = false
    var bool  lT   = false
    var int   n    = 0
    var float sumR = 0.0
    var int   hC   = 0
    var int   lC   = 0
    if newDay
        if done
            n    += 1
            sumR += hi - lo
            hC   += hT ? 1 : 0
            lC   += lT ? 1 : 0
        done := false
        hT   := false
        lT   := false
    if not on and on[1]
        done := true
    else if done
        hT := hT or high > hi
        lT := lT or low < lo
    [n, sumR, hC, lC]
 
[aN, aR, aHc, aLc]     = sessStats(aH, aL, inAsia)
[lN, lR, lHc, lLc]     = sessStats(lH, lL, inLon)
[amN, amR, amHc, amLc] = sessStats(amH, amL, inAm)
[pmN, pmR, pmHc, pmLc] = sessStats(pmH, pmL, inPm)
 
// ───────── Context panel ─────────
asiaAvg = aN > 0 ? aR / aN : na
asiaRng = aH - aL
asiaTxt = not aToday or na(aH) ? "Not started" : "H " + str.tostring(aH, "#.00") + " / L " + str.tostring(aL, "#.00") + " | " + str.tostring(asiaRng, "#") + " pts" + (na(asiaAvg) ? "" : " (" + str.tostring(asiaRng / asiaAvg, "#.0") + "x avg)") + (inAsia ? " | live" : " | " + (aTakenH ? "H taken" : "H open") + ", " + (aTakenL ? "L taken" : "L open"))
asiaCol = not aToday ? color.gray : inAsia ? color.blue : aTakenH and aTakenL ? color.gray : color.teal
 
gradeStat(int i, string nm) =>
    n = array.get(statN, i)
    r = array.get(statR, i)
    n > 0 ? nm + " " + str.tostring(n) + " " + str.tostring(100.0 * array.get(statW, i) / n, "#") + "% " + (r >= 0 ? "+" : "") + str.tostring(r, "#.0") + "R" : nm + " -"
totN = array.sum(statN)
totR = array.sum(statR)
resTxt = gradeStat(0, "A+") + " | " + gradeStat(1, "B") + " | " + gradeStat(2, "EARLY") + " | " + gradeStat(3, "OTE")
resCol = totN == 0 ? color.gray : totR > 0 ? color.green : color.red
 
fmtP(float p) => str.tostring(p, format.mintick)
dolTxt = bias == 1 ? (na(liqAbove) ? "Up - no untaken high, next " + (na(upLvl) ? "none" : upNm + " " + fmtP(upLvl)) : "Up to " + liqANm + " " + fmtP(liqAbove) + " (+" + str.tostring(liqAbove - close, "#") + " pts)") : bias == -1 ? (na(liqBelow) ? "Down - no untaken low, next " + (na(dnLvl) ? "none" : dnNm + " " + fmtP(dnLvl)) : "Down to " + liqBNm + " " + fmtP(liqBelow) + " (-" + str.tostring(close - liqBelow, "#") + " pts)") : "Two-sided: " + (na(liqAbove) ? "no untaken high" : liqANm + " +" + str.tostring(liqAbove - close, "#")) + " / " + (na(liqBelow) ? "no untaken low" : liqBNm + " -" + str.tostring(close - liqBelow, "#"))
dolCol = bias == 1 ? color.green : bias == -1 ? color.red : color.gray
ofStat(int i, string nm) =>
    n = array.get(ofN, i)
    r = array.get(ofR, i)
    n > 0 ? nm + " " + str.tostring(n) + " " + str.tostring(100.0 * array.get(ofW, i) / n, "#") + "% " + (r >= 0 ? "+" : "") + str.tostring(r, "#.0") + "R" : nm + " -"
ofTxt = ofStat(0, "Agrees") + " | " + ofStat(1, "Against/flat") + " | " + ofStat(2, "No data")
ofCol = array.get(ofR, 0) > array.get(ofR, 1) ? color.green : color.gray
// Compact context panel: plain coloured text, no filled cells, merged rows
showStats = input.bool(false, "Panel: show results + orderflow rows", group = gD)
ctxSizeIn = input.string("Small", "Panel text size", options = ["Tiny", "Small", "Normal"], group = gD)
tSize = ctxSizeIn == "Tiny" ? size.tiny : ctxSizeIn == "Normal" ? size.normal : size.small
cGood = color.rgb(76, 211, 138)
cBad  = color.rgb(255, 107, 107)
cWarn = color.rgb(255, 183, 77)
cNeu  = color.rgb(201, 209, 217)
cInfo = color.rgb(110, 168, 255)
cKey  = color.rgb(140, 150, 165)
biasNm = bias == 1 ? "Bullish" : bias == -1 ? "Bearish" : "Neutral"
biasC  = bias == 1 ? cGood : bias == -1 ? cBad : cNeu
lvlTxt = (na(upLvl) ? "↑ none" : "↑ " + upNm + " +" + str.tostring(upLvl - close, "#")) + "    " + (na(dnLvl) ? "↓ none" : "↓ " + dnNm + " -" + str.tostring(close - dnLvl, "#"))
rngTxt = str.tostring(rngPct, "#") + "% of ADR  ·  ATR " + str.tostring(atr14, "#") + "  ·  " + (volR > 1.3 ? "High " : volR < 0.8 ? "Low " : "Normal ") + str.tostring(volR, "#.0") + "x"
rngC   = rngPct > 100 or volR > 1.3 ? cWarn : cNeu
sesTxt = winTxt + (aToday and not na(aH) ? "  ·  Asia " + (inAsia ? "live " + str.tostring(asiaRng, "#") + " pts" : (aTakenH ? "H taken" : "H open") + ", " + (aTakenL ? "L taken" : "L open")) : "")
sesC   = inAm ? cGood : lunch ? cWarn : inEntry or (asiaEnt and inAsia) ? cInfo : cNeu
h4C    = na(h4o) ? cNeu : dnWk >= wkMin and close > h4o ? cGood : upWk >= wkMin and close < h4o ? cBad : cNeu
lastC  = str.contains(lastSig, "None") ? cNeu : str.contains(lastSig, "AGAINST") or str.contains(lastSig, "(!)") ? cBad : str.contains(lastSig, "with bias") ? cGood : cWarn
var table ctx = table.new(position.bottom_right, 2, 8, bgcolor = color.new(color.black, 40), frame_color = color.new(color.gray, 75), frame_width = 1, border_width = 0)
ctxRow(int r, string k, string val, color c) =>
    table.cell(ctx, 0, r, k, text_color = cKey, text_size = tSize, text_halign = text.align_left)
    table.cell(ctx, 1, r, val, text_color = c, text_size = tSize, text_halign = text.align_left)
if showCtx and barstate.islast
    ctxRow(0, "Bias", biasNm + "  ·  " + dolTxt, biasC)
    ctxRow(1, "Levels", lvlTxt, cNeu)
    ctxRow(2, "Range", rngTxt, rngC)
    ctxRow(3, "Session", sesTxt, sesC)
    ctxRow(4, h4Lbl, h4Txt, h4C)
    ctxRow(5, "Last signal", lastSig, lastC)
    if showStats
        ctxRow(6, "Results", resTxt, totR > 0 ? cGood : totN == 0 ? cNeu : cBad)
        ctxRow(7, "Orderflow", ofTxt, cNeu)
 
var table tb = table.new(position.top_right, 4, 5, bgcolor = color.new(color.black, 25), border_width = 1, border_color = color.new(color.gray, 70))
row(int r, string name, int n, float sr, int hc, int lc, color c) =>
    table.cell(tb, 0, r, name, text_color = color.white, bgcolor = color.new(c, 40), text_size = size.small)
    table.cell(tb, 1, r, n > 0 ? str.tostring(sr / n, "#") : "-", text_color = color.white, text_size = size.small)
    table.cell(tb, 2, r, n > 0 ? str.tostring(100.0 * hc / n, "#") + "%" : "-", text_color = color.white, text_size = size.small)
    table.cell(tb, 3, r, n > 0 ? str.tostring(100.0 * lc / n, "#") + "%" : "-", text_color = color.white, text_size = size.small)
 
if showTable and barstate.islast
    table.cell(tb, 0, 0, "Session", text_color = color.white, text_size = size.small)
    table.cell(tb, 1, 0, "Avg range", text_color = color.white, text_size = size.small)
    table.cell(tb, 2, 0, "High taken", text_color = color.white, text_size = size.small)
    table.cell(tb, 3, 0, "Low taken", text_color = color.white, text_size = size.small)
    row(1, "Asia",   aN,  aR,  aHc,  aLc,  color.blue)
    row(2, "London", lN,  lR,  lHc,  lLc,  color.red)
    row(3, "NY AM",  amN, amR, amHc, amLc, color.green)
    row(4, "NY PM",  pmN, pmR, pmHc, pmLc, color.fuchsia)

← 返回列表