Pivot Numbers+ (M1D)

mayb1dayy · study · 651 行 · 点赞 86 · 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/
// © M1D
//@version=6
indicator("Pivot Numbers+ (M1D)", "Pivots+", overlay = true, max_lines_count = 400, max_labels_count = 400)
 
// Filling the numbers: price tends to fill about four levels a day. The main ruler is the floor trader
// pivot ladder on the 0 GMT day with the M levels between; the CBDR, Asian range and flout steps are the
// other three rulers. Majority off once four fill, a runner for a large range day.
 
// ─── Constants ───────────────────────────────────────────────────────────────
const string TZ     = "America/New_York"
// The brand pair: violet on the CPP and a bullish read, dark grey on a bearish one.
const color  BULL   = #7246CE
const color  BEAR   = #4A4A4A
const int    HR     = 3600000
 
//@variable True on a dark chart background
bool  DARK   = color.r(chart.bg_color) + color.g(chart.bg_color) + color.b(chart.bg_color) < 384
color INK    = DARK ? #FFFFFF : #000000
color MUTE   = DARK ? #B2B5BE : #4A4A4A
color PILL   = color.new(#787B86, 75)
color NOFILL = color.new(#FFFFFF, 100)
 
// ─── Inputs ──────────────────────────────────────────────────────────────────
const string G_P = "Pivots"
const string G_R = "Other rulers"
const string G_B = "Bias"
const string G_T = "Tables"
 
string pivDay    = input.string("0 GMT (ICT)", "Pivot day", options = ["0 GMT (ICT)", "18:00 NY (futures day)"], group = G_P, tooltip = "Floor trader pivots on the 0 GMT day (8pm NY in summer, 7pm in winter), as ICT teaches. The futures day starts at 18:00 NY instead: try both on your market.")
bool   showM     = input.bool(true, "M levels (half way between)", group = G_P, tooltip = "M3 = 50% CPP-R1, M4 = 50% R1-R2, M5 = 50% R2-R3; M2, M1, M0 the same below.")
bool   showR3    = input.bool(true, "R3 / S3 (and M5 / M0)", group = G_P)
bool   showPd    = input.bool(true, "Previous day high / low", group = G_P, tooltip = "The first reference. One of them is usually retested or traded through, unless the day's range is too small.")
bool   showPrice = input.bool(false, "Prices in the names", group = G_P)
int    daysKept  = input.int(2, "Days kept on chart", minval = 1, maxval = 5, group = G_P)
 
bool   rCb       = input.bool(false, "CBDR steps", group = G_R, tooltip = "14:00-20:00 NY. Long: CBDR high = #1, then one CBDR range per number above. Short: CBDR low = #1, one range per number below.")
bool   rAs       = input.bool(false, "Asian range steps", group = G_R, tooltip = "20:00-00:00 NY. Asian high = #1 for longs, Asian low = #1 for shorts, one Asian range per number.")
bool   rFl       = input.bool(false, "Flout steps", group = G_R, tooltip = "15:00-00:00 NY. Flout high / low = #1, then 50% of the flout range per number, never the whole range. On futures the 17:00-18:00 break sits inside it.")
string rMeas     = input.string("Wicks", "Rulers measured from", options = ["Wicks", "Bodies"], group = G_R, tooltip = "Check both: the feed differs by broker.")
 
string biasIn    = input.string("Not set", "Daily bias", options = ["Not set", "Bullish", "Bearish"], group = G_B, tooltip = "Bias from order flow and the PD array matrix first. Long = the four numbers above, short = the four below. Not set reads both sides.")
 
bool   showTbl   = input.bool(true, "Read table", group = G_T)
string tblPos    = input.string("Bottom Centre", "Position", options = ["Bottom Centre", "Top Right", "Middle Right", "Bottom Right", "Top Left", "Bottom Left"], group = G_T, tooltip = "Bottom Centre: the read on one short line under the candles. Any other spot: the read as rows.")
string layoutIn  = input.string("Desktop", "Layout", options = ["Desktop", "Phone"], group = G_T, tooltip = "Phone: two short rows in one narrow column (filled counts and the next level each way), no statistics table or header strip. Put it Top Right or Bottom Right.")
bool   showStat  = input.bool(false, "Statistics table", group = G_T, tooltip = "Bottom centre, or bottom right while the read sits on the bottom centre line. Every completed day on the chart: how many pivot numbers the day filled, counted from its open on the side it moved further.")
int    TXT       = input.int(10, "Text size", minval = 6, maxval = 14, group = G_T)
bool   hStripOn  = input.bool(false, "Header strip", group = G_T, tooltip = "Top centre: the chart timeframe and pivot day, the numbers filled and the next one, and the ticker with its CPP and your bias.")
 
const string G_S = "Session pivots (TBR)"
bool   tLon      = input.bool(false, "London range", group = G_S, inline = "lon", tooltip = "The same pivot ladder, built from the London time based range (high, low and its last close) instead of a whole day. Drawn from the range end, filled from the range close, names at the left. With NY AM on, it hands over when the NY AM range closes.")
string sesLon    = input.session("0100-0500", "", group = G_S, inline = "lon")
bool   tNy       = input.bool(false, "NY AM range", group = G_S, inline = "ny", tooltip = "The same pivot ladder, built from the NY AM time based range. Drawn from the range end, filled from the range close.")
string sesNy     = input.session("0700-0900", "", group = G_S, inline = "ny")
int    tUntil    = input.int(16, "Lines run until (NY hour)", minval = 1, maxval = 23, group = G_S)
color  cLon      = input.color(BULL, "London", group = G_S, inline = "col")
color  cNy       = input.color(BEAR, "NY AM", group = G_S, inline = "col")
bool   showForm  = input.bool(false, "Show the ladder while the range forms", group = G_S, tooltip = "Dashed, names ending in ?, from the range start to just past price. It moves with every candle until the range closes, then the solid ladder takes over.")
string drawIn    = input.string("Nearest", "Levels drawn", options = ["Nearest", "All"], group = G_P, tooltip = "Nearest: today's CPP, the one level just above price and the one just below, and PDH / PDL. A filled level is drawn faded. Past days are not drawn. All: every level of every kept day.")
 
// ─── Derived ─────────────────────────────────────────────────────────────────
bool PHONE   = layoutIn == "Phone"
bool STRIP   = tblPos == "Bottom Centre" and not PHONE
bool GMT     = pivDay == "0 GMT (ICT)"
bool BODIES  = rMeas == "Bodies"
bool WANTUP  = biasIn != "Bearish"
bool WANTDN  = biasIn != "Bullish"
bool intra   = timeframe.isintraday and timeframe.in_seconds() <= 3600
int  DAYLEN  = GMT ? 24 * HR : 23 * HR
 
// ─── Helpers ─────────────────────────────────────────────────────────────────
//@function A table position from its name.
f_pos(string p) =>
    switch p
        "Bottom Centre" => position.bottom_center
        "Middle Right" => position.middle_right
        "Bottom Right" => position.bottom_right
        "Top Left"     => position.top_left
        "Bottom Left"  => position.bottom_left
        =>                position.top_right
 
//@function A price at the symbol's tick.
f_pts(float v) =>
    na(v) ? "-" : str.tostring(math.round_to_mintick(v))
 
//@function A level's name, centred on its line: past the right end, or before the left end when left is true.
f_lab(int x, float y, string txt, color c, string tip, bool left = false) =>
    label lb = label.new(x, y, txt, xloc = xloc.bar_time, style = left ? label.style_label_right : label.style_label_left, color = NOFILL, textcolor = c, size = TXT, tooltip = tip)
    label.set_text_font_family(lb, font.family_monospace)
    lb
 
//@function The floor trader ladder with the M levels between, low to high.
//@returns ([array<float>, array<string>]) The prices and their names.
f_ladder(float H, float L, float C) =>
    float P  = (H + L + C) / 3
    float R1 = 2 * P - L
    float S1 = 2 * P - H
    float R2 = P + (H - L)
    float S2 = P - (H - L)
    float R3 = H + 2 * (P - L)
    float S3 = L - 2 * (H - P)
    array<float>  v = array.from(S3, (S2 + S3) / 2, S2, (S1 + S2) / 2, S1, (P + S1) / 2, P, (P + R1) / 2, R1, (R1 + R2) / 2, R2, (R2 + R3) / 2, R3)
    array<string> n = array.from("S3", "M0", "S2", "M1", "S1", "M2", "CPP", "M3", "R1", "M4", "R2", "M5", "R3")
    [v, n]
 
//@function True when the settings draw this level.
f_keep(string nm) =>
    bool isM = str.startswith(nm, "M")
    bool far = nm == "S3" or nm == "R3" or nm == "M0" or nm == "M5"
    (showM or not isM) and (showR3 or not far)
 
//@function True when none of the 8 bars before this one cover the price: a name parked there sits clear of candles.
f_clear(float y) =>
    bool ok = true
    for k = 1 to 8
        if high[k] >= y and low[k] <= y
            ok := false
    ok
 
//@function Parks a past ladder's names at its left start, dropping any a candle would cover.
f_park(array<label> lbs, array<bool> clr, int x) =>
    for [i, lb] in lbs
        if clr.get(i)
            label.set_x(lb, x)
            label.set_style(lb, label.style_label_right)
        else
            label.delete(lb)
    true
 
// ─── One pivot day ───────────────────────────────────────────────────────────
type PDay
    int           t0
    int           tLast
    float         o
    float         h
    float         l
    float         c
    array<float>  lv
    array<string> nm
    array<bool>   hit
    array<line>   ln
    array<label>  lb
    array<bool>   clr
 
method full(PDay d) =>
    d.tLast - d.t0 >= 6 * HR
 
method clear(PDay d) =>
    for x in d.ln
        line.delete(x)
    for x in d.lb
        label.delete(x)
 
//@function Levels the day has traded to on one side of its open.
//@param up (bool) Above the open (true) or below.
//@returns (int) The count.
method filled(PDay d, bool up) =>
    int n = 0
    for [i, v] in d.lv
        if d.hit.get(i) and (up ? v > d.o : v < d.o)
            n += 1
    n
 
//@function The names filled on one side, nearest the open first.
method names(PDay d, bool up) =>
    string s = ""
    int n = d.lv.size()
    for k = 0 to n - 1
        int i = up ? k : n - 1 - k
        float v = d.lv.get(i)
        if d.hit.get(i) and (up ? v > d.o : v < d.o)
            s += (s == "" ? "" : " ") + d.nm.get(i)
    s == "" ? "none" : s
 
//@function The next level not yet traded to on one side.
//@returns ([string, float]) Its name and price.
method nextLv(PDay d, bool up) =>
    string nm = "-"
    float  px = na
    int n = d.lv.size()
    for k = 0 to n - 1
        int i = up ? k : n - 1 - k
        float v = d.lv.get(i)
        if na(px) and not d.hit.get(i) and (up ? v > d.o : v < d.o)
            nm := d.nm.get(i)
            px := v
    [nm, px]
 
// ─── Clock and days ──────────────────────────────────────────────────────────
int  mins   = hour(time, TZ) * 60 + minute(time, TZ)
bool gap    = bar_index > 0 and time - time[1] > 3 * HR
bool newUtc = bar_index > 0 and dayofmonth(time, "UTC") != dayofmonth(time[1], "UTC")
bool newNy  = bar_index > 0 and mins >= 1080 and (mins[1] < 1080 or gap)
bool newDay = GMT ? newUtc : newNy
 
var array<PDay> days = array.new<PDay>()
var PDay cur  = na
var PDay prev = na
 
// Statistics, completed days only.
type Stats
    int days = 0
    int sum  = 0
    int four = 0
    int six  = 0
 
var Stats S = Stats.new()
 
if intra and (newDay or na(cur))
    // A finished full day: count its numbers and keep it as the pivot source.
    if not na(cur) and cur.full()
        if cur.lv.size() > 0
            int up = cur.filled(true)
            int dn = cur.filled(false)
            int mv = biasIn == "Bullish" ? up : biasIn == "Bearish" ? dn : math.max(up, dn)
            S.days += 1
            S.sum  += mv
            S.four += mv >= 4 ? 1 : 0
            S.six  += mv >= 6 ? 1 : 0
        // The finished day's names move to its left start, where the candles are known.
        f_park(cur.lb, cur.clr, cur.t0)
        prev := cur
    // A Sunday stub drew the same levels the next day will: remove it.
    else if not na(cur) and days.size() > 0
        days.pop().clear()
    PDay d = PDay.new(t0 = time, tLast = time, o = open, h = high, l = low, c = close, lv = array.new<float>(), nm = array.new<string>(), hit = array.new<bool>(), ln = array.new<line>(), lb = array.new<label>(), clr = array.new<bool>())
    // A Sunday stub (under 6 hours) never becomes the source: Monday reads Friday, as ICT does.
    if not na(prev)
        float H = prev.h
        float L = prev.l
        [v, n] = f_ladder(H, L, prev.c)
        for [i, x] in v
            string nmI = n.get(i)
            bool   isM = str.startswith(nmI, "M")
            if f_keep(nmI)
                d.lv.push(x)
                d.nm.push(nmI)
                d.hit.push(false)
                color c = nmI == "CPP" ? BULL : isM ? MUTE : INK
                d.ln.push(line.new(time, x, time + DAYLEN, x, xloc = xloc.bar_time, color = c, width = 1))
                d.clr.push(f_clear(x))
                d.lb.push(f_lab(time + DAYLEN, x, nmI + (showPrice ? " " + f_pts(x) : ""), c, nmI + " " + f_pts(x) + (nmI == "CPP" ? ": the central pivot, (H + L + C) / 3 of the last full day." : isM ? ": half way between its two pivots." : "")))
        if showPd
            d.ln.push(line.new(time, H, time + DAYLEN, H, xloc = xloc.bar_time, color = MUTE, width = 1))
            d.clr.push(f_clear(H))
            d.lb.push(f_lab(time + DAYLEN, H, "PDH" + (showPrice ? " " + f_pts(H) : ""), MUTE, "Previous day high " + f_pts(H) + ". The first reference, usually retested or traded through."))
            d.ln.push(line.new(time, L, time + DAYLEN, L, xloc = xloc.bar_time, color = MUTE, width = 1))
            d.clr.push(f_clear(L))
            d.lb.push(f_lab(time + DAYLEN, L, "PDL" + (showPrice ? " " + f_pts(L) : ""), MUTE, "Previous day low " + f_pts(L) + ". The first reference, usually retested or traded through."))
    days.push(d)
    cur := d
    if days.size() > daysKept
        days.shift().clear()
else if intra and not na(cur)
    cur.h     := math.max(cur.h, high)
    cur.l     := math.min(cur.l, low)
    cur.c     := close
    cur.tLast := time
 
// Today's lines stop, and their names sit, 10 bars right of price (or at the day's end if sooner).
// With a session ladder live its names take that spot, and the day's move 20 bars further out.
if barstate.islast and intra and not na(cur)
    int xR = math.min(cur.t0 + DAYLEN, time + (tLon or tNy ? 30 : 10) * timeframe.in_seconds() * 1000)
    for x in cur.ln
        line.set_x2(x, xR)
    for x in cur.lb
        label.set_x(x, xR)
 
// A level is filled once the day trades to it from its open; a filled level turns dotted.
if intra and not na(cur)
    for [i, v] in cur.lv
        if not cur.hit.get(i) and ((v > cur.o and cur.h >= v) or (v < cur.o and cur.l <= v) or v == cur.o)
            cur.hit.set(i, true)
            line.set_style(cur.ln.get(i), line.style_dotted)
 
// Nearest: past days hidden; today keeps the CPP and the level either side of price, a filled one faded.
if barstate.islast and intra and not na(cur) and drawIn == "Nearest"
    for d in days
        if d.t0 != cur.t0
            for x in d.ln
                line.set_color(x, NOFILL)
            for x in d.lb
                label.set_textcolor(x, NOFILL)
    int above = -1
    int below = -1
    for [i, v] in cur.lv
        if v > close and (above < 0 or v < cur.lv.get(above))
            above := i
        if v <= close and (below < 0 or v > cur.lv.get(below))
            below := i
    for [i, v] in cur.lv
        string nmI  = cur.nm.get(i)
        color  base = nmI == "CPP" ? BULL : str.startswith(nmI, "M") ? MUTE : INK
        color  c    = nmI == "CPP" or i == above or i == below ? (cur.hit.get(i) ? color.new(base, 60) : base) : NOFILL
        line.set_color(cur.ln.get(i), c)
        label.set_textcolor(cur.lb.get(i), c)
 
// ─── The other rulers: CBDR, Asia and flout steps from midnight NY ──────────
var float cbH = na
var float cbL = na
var float asH = na
var float asL = na
var float flH = na
var float flL = na
var array<line>  rLn = array.new<line>()
var array<label> rLb = array.new<label>()
var float rCbSz = na
var float rAsSz = na
var float rFlSz = na
 
float bHi  = BODIES ? math.max(open, close) : high
float bLo  = BODIES ? math.min(open, close) : low
bool  inCb = mins >= 840 and mins < 1200
bool  inFl = mins >= 900
bool  inAs = mins >= 1200
 
if intra
    if inCb
        if not inCb[1] or gap or na(cbH)
            cbH := bHi
            cbL := bLo
        else
            cbH := math.max(cbH, bHi)
            cbL := math.min(cbL, bLo)
    if inFl
        if not inFl[1] or gap or na(flH)
            flH := bHi
            flL := bLo
        else
            flH := math.max(flH, bHi)
            flL := math.min(flL, bLo)
    if inAs
        if not inAs[1] or gap or na(asH)
            asH := bHi
            asL := bLo
        else
            asH := math.max(asH, bHi)
            asL := math.min(asL, bLo)
 
//@function Draws one ruler's four numbers each side from midnight to 17:00 NY.
f_ruler(float hi, float lo, float step, string tag, color c, string nameFull) =>
    if not na(hi) and step > 0
        for k = 1 to 4
            if WANTUP
                float y = hi + (k - 1) * step
                rLn.push(line.new(time, y, time + 17 * HR, y, xloc = xloc.bar_time, color = c, width = 1, style = line.style_dotted))
                rLb.push(f_lab(time + 17 * HR, y, tag + str.tostring(k), c, nameFull + " #" + str.tostring(k) + " above, " + f_pts(y) + ". Long: buy below the range, the high is #1, one step " + f_pts(step) + " per number."))
            if WANTDN
                float y = lo - (k - 1) * step
                rLn.push(line.new(time, y, time + 17 * HR, y, xloc = xloc.bar_time, color = c, width = 1, style = line.style_dotted))
                rLb.push(f_lab(time + 17 * HR, y, tag + str.tostring(k), c, nameFull + " #" + str.tostring(k) + " below, " + f_pts(y) + ". Short: sell above the range, the low is #1, one step " + f_pts(step) + " per number."))
    true
 
// At midnight NY the three ranges are complete. Monday has no CBDR or flout on futures.
if intra and mins < 900 and not na(mins[1]) and mins[1] >= 1200
    bool mon = dayofweek(time, TZ) == dayofweek.monday
    rCbSz := mon ? na : cbH - cbL
    rAsSz := asH - asL
    rFlSz := mon ? na : flH - flL
    if rCb and not mon
        f_ruler(cbH, cbL, cbH - cbL, "C", MUTE, "CBDR")
    if rAs
        f_ruler(asH, asL, asH - asL, "A", MUTE, "Asian range")
    if rFl and not mon
        f_ruler(flH, flL, (flH - flL) / 2, "F", MUTE, "Flout")
    int cap = daysKept * 24
    while rLn.size() > cap
        line.delete(rLn.shift())
    while rLb.size() > cap
        label.delete(rLb.shift())
 
// ─── Session pivots: the same ladder from a time based range ─────────────────
type TRange
    float h  = na
    float l  = na
    float c  = na
    int   t0 = na
 
type TSes
    float         o
    int           t0
    int           tEnd
    bool          left
    bool          past = false
    array<bool>   clr
    array<float>  lv
    array<string> nm
    array<bool>   hit
    array<line>   ln
    array<label>  lb
 
method wipe(TSes s) =>
    for x in s.ln
        line.delete(x)
    for x in s.lb
        label.delete(x)
 
//@function A past day's set: names to the left start, any a candle would cover dropped.
method toPast(TSes s) =>
    if not s.past
        f_park(s.lb, s.clr, s.t0)
        s.past := true
 
//@function Levels traded to since the range closed, on one side of its close.
method cnt(TSes s, bool up) =>
    int n = 0
    for [i, v] in s.lv
        if s.hit.get(i) and (up ? v > s.o : v < s.o)
            n += 1
    n
 
//@function Follows a range bar by bar while its window is open.
method track(TRange r, bool first) =>
    if first or na(r.h)
        r.h  := high
        r.l  := low
        r.t0 := time
    else
        r.h := math.max(r.h, high)
        r.l := math.min(r.l, low)
    r.c := close
 
//@function The line end: the NY hour set, or six hours on when that has already passed.
f_until() =>
    int t = timestamp(TZ, year(time, TZ), month(time, TZ), dayofmonth(time, TZ), tUntil, 0)
    t > time ? t : time + 6 * HR
 
//@function Draws one range's ladder from the bar after the range closes.
//@returns (TSes) The drawn set.
f_tbr(TRange r, string tag, color c, string nameFull, string ses, bool left) =>
    int  tE = f_until()
    TSes s  = TSes.new(o = r.c, t0 = time, tEnd = tE, left = left, clr = array.new<bool>(), lv = array.new<float>(), nm = array.new<string>(), hit = array.new<bool>(), ln = array.new<line>(), lb = array.new<label>())
    [v, n] = f_ladder(r.h, r.l, r.c)
    for [i, x] in v
        string nmI = n.get(i)
        if f_keep(nmI)
            s.lv.push(x)
            s.nm.push(nmI)
            s.hit.push(false)
            s.ln.push(line.new(time, x, tE, x, xloc = xloc.bar_time, color = c, width = 1))
            s.clr.push(f_clear(x))
            s.lb.push(f_lab(left ? time : tE, x, tag + " " + nmI + (showPrice ? " " + f_pts(x) : ""), c, nameFull + " " + nmI + " " + f_pts(x) + (nmI == "CPP" ? ": (H + L + C) / 3 of the " + ses + " NY range." : str.startswith(nmI, "M") ? ": half way between its two pivots." : ""), left))
    s
 
//@function Marks a set's levels traded to since its range closed; a filled level turns dotted.
method mark(TSes s) =>
    if time <= s.tEnd
        for [i, v] in s.lv
            if not s.hit.get(i) and ((v > s.o and high >= v) or (v < s.o and low <= v))
                s.hit.set(i, true)
                line.set_style(s.ln.get(i), line.style_dotted)
 
//@function Stops a set's lines at x when x is sooner than its end; right-hand names move with them.
method trim(TSes s, int x) =>
    if x < s.tEnd
        for ln in s.ln
            line.set_x2(ln, x)
        if not s.left
            for lb in s.lb
                label.set_x(lb, x)
 
var TRange rLon = TRange.new()
var TRange rNy  = TRange.new()
var array<TSes> tSets = array.new<TSes>()
var TSes curLon = na
var TSes curNy  = na
 
bool inLon = intra and tLon and not na(time(timeframe.period, sesLon, TZ))
bool inNy  = intra and tNy and not na(time(timeframe.period, sesNy, TZ))
 
if intra
    // A new pivot day: the earlier days' names move to their left start.
    if newDay
        for o in tSets
            o.toPast()
    if not na(curLon)
        curLon.mark()
    if not na(curNy)
        curNy.mark()
    if inLon
        rLon.track(not inLon[1] or gap)
    else if inLon[1] and not na(rLon.h)
        curLon := f_tbr(rLon, "L", cLon, "London", sesLon, true)
        tSets.push(curLon)
        rLon.h := na
    if inNy
        rNy.track(not inNy[1] or gap)
    else if inNy[1] and not na(rNy.h)
        // London hands over here, so the two ladders never share a stretch of chart.
        if not na(curLon) and curLon.tEnd > time
            curLon.trim(time)
            curLon.tEnd := time
        curNy := f_tbr(rNy, "NY", cNy, "NY AM", sesNy, false)
        tSets.push(curNy)
        rNy.h := na
    while tSets.size() > daysKept * 2
        tSets.shift().wipe()
 
// While a range forms: its ladder so far, dashed, redrawn on the last bar only.
var array<line>  fLn = array.new<line>()
var array<label> fLb = array.new<label>()
 
//@function Draws a range's ladder while it forms, from its start to 10 bars past price, names ending in ?.
f_forming(TRange r, string tag, color c, string nameFull) =>
    int xT = time + 10 * timeframe.in_seconds() * 1000
    [v, n] = f_ladder(r.h, r.l, r.c)
    for [i, x] in v
        string nmI = n.get(i)
        if f_keep(nmI)
            fLn.push(line.new(r.t0, x, xT, x, xloc = xloc.bar_time, color = c, width = 1, style = line.style_dashed))
            fLb.push(f_lab(xT, x, tag + " " + nmI + "?", c, nameFull + " " + nmI + " " + f_pts(x) + " so far: it moves with every candle until the range closes."))
    true
 
if barstate.islast and intra and showForm
    while fLn.size() > 0
        line.delete(fLn.pop())
    while fLb.size() > 0
        label.delete(fLb.pop())
    if inLon and not na(rLon.h)
        f_forming(rLon, "L", cLon, "London")
    if inNy and not na(rNy.h)
        f_forming(rNy, "NY", cNy, "NY AM")
 
// The live sets stop, and their names sit, 10 bars right of price, as the day's do.
if barstate.islast and intra
    int xT = time + 10 * timeframe.in_seconds() * 1000
    if not na(curLon)
        curLon.trim(xT)
    if not na(curNy)
        curNy.trim(xT)
 
// ─── Read ────────────────────────────────────────────────────────────────────
//@function The side the day is read on: the bias, else the side that has filled more.
//@returns (bool) True for above the open.
f_upSide() =>
    biasIn == "Bullish" ? true : biasIn == "Bearish" ? false : na(cur) ? true : cur.filled(true) >= cur.filled(false)
 
//@function The read in a few words, for the panel; f_read has the full line.
f_readShort(int up, int dn) =>
    int mv = biasIn == "Bullish" ? up : biasIn == "Bearish" ? dn : math.max(up, dn)
    mv >= 6 ? "Large range: runner" : mv >= 4 ? "Four filled: scale out" : biasIn == "Not set" ? "Set the bias" : "Day not done"
 
//@function The read line from the scaling rule: majority off at four, a runner past six.
f_read(int up, int dn) =>
    int mv = biasIn == "Bullish" ? up : biasIn == "Bearish" ? dn : math.max(up, dn)
    string side = f_upSide() ? "above" : "below"
    mv >= 6 ? "Large range day: " + str.tostring(mv) + " filled " + side + ", runner only" : mv >= 4 ? "Four filled " + side + ": 75-80% off, leave a runner" : biasIn == "Not set" ? "Set the bias: long = four above, short = four below" : str.tostring(mv) + " of 4 filled " + side + ": the day isn't done"
 
// The numbers filled today on the bias side (either side when no bias), for the alert.
int mvNow = 0
if intra and not na(cur)
    int u = cur.filled(true)
    int w = cur.filled(false)
    mvNow := biasIn == "Bullish" ? u : biasIn == "Bearish" ? w : math.max(u, w)
 
alertcondition(mvNow >= 4 and nz(mvNow[1]) < 4 and not newDay, "Four numbers filled", "Pivot Numbers+: four pivot numbers filled on {{ticker}}")
 
// ─── Read table ──────────────────────────────────────────────────────────────
var table tb = showTbl ? table.new(f_pos(tblPos), PHONE ? 1 : STRIP ? 6 : 3, PHONE ? 2 : STRIP ? 1 : 8, bgcolor = chart.bg_color, border_width = 0, frame_width = 0) : na
 
//@function One read-panel row: label, value, verdict. The verdict is the row's one filled cell.
f_row(int r, string name, string val, string pill, string tip) =>
    table.cell(tb, 0, r, name, text_color = INK, text_size = TXT, text_halign = text.align_left, text_font_family = font.family_monospace)
    table.cell(tb, 1, r, val, text_color = INK, text_size = TXT, text_halign = text.align_left, text_font_family = font.family_monospace, tooltip = tip)
    table.cell(tb, 2, r, pill, text_color = INK, bgcolor = pill == "" ? na : PILL, text_size = TXT, text_halign = text.align_center, text_font_family = font.family_monospace, tooltip = tip)
    true
 
if barstate.islast and showTbl
    table.clear(tb, 0, 0, PHONE ? 0 : STRIP ? 5 : 2, PHONE ? 1 : STRIP ? 0 : 7)
    bool ok = intra and not na(cur) and cur.lv.size() > 0
    int  up = ok ? cur.filled(true) : 0
    int  dn = ok ? cur.filled(false) : 0
    if PHONE
        // Phone: one narrow column, two short rows. Filled counts and the CPP side, then the next level each way.
        string r1 = not intra ? "PivN+ 1H or lower" : not ok ? "PivN+ waiting" : "▲" + str.tostring(up) + " ▼" + str.tostring(dn) + " · " + (close >= cur.lv.get(cur.nm.indexof("CPP")) ? "above" : "below") + " CPP"
        table.cell(tb, 0, 0, r1, text_color = INK, text_size = TXT, text_halign = text.align_left, text_font_family = font.family_monospace)
        if ok
            [pnU, ppU] = cur.nextLv(true)
            [pnD, ppD] = cur.nextLv(false)
            table.cell(tb, 0, 1, "▲" + pnU + " " + f_pts(ppU) + "\n▼" + pnD + " " + f_pts(ppD), text_color = INK, text_size = TXT, text_halign = text.align_left, text_font_family = font.family_monospace)
    else if STRIP
        // Bottom centre: one short line. The filled names and the full read sit in the tooltips.
        table.cell(tb, 0, 0, "PivN+" + (not intra ? " 1H or lower" : ok ? "" : " waiting"), text_color = INK, text_size = TXT, text_font_family = font.family_monospace)
        if ok
            float cpp = cur.lv.get(cur.nm.indexof("CPP"))
            [snU, spU] = cur.nextLv(true)
            [snD, spD] = cur.nextLv(false)
            table.cell(tb, 1, 0, "CPP " + f_pts(cpp) + (close >= cpp ? " ▲" : " ▼"), text_color = INK, text_size = TXT, text_font_family = font.family_monospace, tooltip = "The central pivot. Day " + (GMT ? "0 GMT" : "18:00 NY") + ", open " + f_pts(cur.o) + ". ▲ = price above it, ▼ = below.")
            table.cell(tb, 2, 0, "▲" + str.tostring(up) + "/4", text_color = INK, bgcolor = up >= 4 ? PILL : na, text_size = TXT, text_font_family = font.family_monospace, tooltip = "Filled above the open, nearest first: " + cur.names(true))
            table.cell(tb, 3, 0, "▼" + str.tostring(dn) + "/4", text_color = INK, bgcolor = dn >= 4 ? PILL : na, text_size = TXT, text_font_family = font.family_monospace, tooltip = "Filled below the open, nearest first: " + cur.names(false))
            table.cell(tb, 4, 0, "Next ▲" + snU + " " + f_pts(spU) + " ▼" + snD + " " + f_pts(spD), text_color = INK, text_size = TXT, text_font_family = font.family_monospace, tooltip = "The next level not yet traded to on each side.")
            table.cell(tb, 5, 0, f_readShort(up, dn), text_color = INK, text_size = TXT, text_font_family = font.family_monospace, tooltip = f_read(up, dn) + ". Take the majority off (75-80%) once four fill, leave a piece for a large range day, time permitting.")
    else
        table.cell(tb, 0, 0, "Pivot Numbers+", text_color = INK, text_size = TXT, text_halign = text.align_left, text_font_family = font.family_monospace)
        table.cell(tb, 2, 0, not intra ? "1H or lower" : ok ? "▲" + str.tostring(up) + " ▼" + str.tostring(dn) : "Waiting", text_color = INK, text_size = TXT, text_halign = text.align_right, text_font_family = font.family_monospace)
        if ok
            int r = 1
            float cpp = cur.lv.get(cur.nm.indexof("CPP"))
            if not PHONE
                f_row(r, "CPP", f_pts(cpp), close >= cpp ? "Above" : "Below", "The central pivot. Day " + (GMT ? "0 GMT" : "18:00 NY") + ", open " + f_pts(cur.o) + ": numbers are counted from the open; ICT counts from your entry. Below the CPP is often read as a buy; price trades to the levels either way for the orders resting there.")
                r += 1
            f_row(r, "Filled ▲", cur.names(true), up >= 4 ? "4+" : str.tostring(up) + "/4", "Levels above the open the day has traded to, nearest first.")
            r += 1
            f_row(r, "Filled ▼", cur.names(false), dn >= 4 ? "4+" : str.tostring(dn) + "/4", "Levels below the open the day has traded to, nearest first.")
            r += 1
            [nU, pU] = cur.nextLv(true)
            [nD, pD] = cur.nextLv(false)
            f_row(r, "Next", "▲" + nU + " " + f_pts(pU) + " ▼" + nD + " " + f_pts(pD), "", "The next level not yet traded to on each side. Exit a little ahead of the level.")
            r += 1
            if not PHONE and (rCb or rAs or rFl)
                f_row(r, "Rulers", (rCb ? "C " + f_pts(rCbSz) + " " : "") + (rAs ? "A " + f_pts(rAsSz) + " " : "") + (rFl ? "F " + f_pts(rFlSz / 2) : ""), "", "C = CBDR range, A = Asian range, F = flout step (half the flout range). Use all four rulers. Confluence between two or more near a PD array, at the right time of day, marks the likely high or low. NY generally shows which one price is using.")
                r += 1
            if not PHONE and (tLon or tNy)
                string tv = (tLon and not na(curLon) ? "L ▲" + str.tostring(curLon.cnt(true)) + " ▼" + str.tostring(curLon.cnt(false)) : "") + (tLon and tNy and not na(curLon) and not na(curNy) ? " · " : "") + (tNy and not na(curNy) ? "NY ▲" + str.tostring(curNy.cnt(true)) + " ▼" + str.tostring(curNy.cnt(false)) : "")
                if tv != ""
                    f_row(r, "TBR", tv, "", "Session pivot levels traded to since each range closed, above and below its close. L = London, NY = NY AM.")
                    r += 1
            f_row(r, "Read", f_readShort(up, dn), "", f_read(up, dn) + ". Take the majority off (75-80%) once four fill, leave a piece for a large range day, time permitting. No volatility, no displacement = no precision.")
 
// ─── Statistics table, bottom centre ─────────────────────────────────────────
// With the read on the bottom centre line, the statistics move to the bottom right.
var table st = showStat and not PHONE ? table.new(STRIP ? position.bottom_right : position.bottom_center, 4, 2, bgcolor = chart.bg_color, border_width = 0, frame_width = 0) : na
 
//@function One statistics cell.
f_sc(int c, int r, string txt, string tip) =>
    table.cell(st, c, r, txt, text_color = INK, text_size = TXT, text_font_family = font.family_monospace, tooltip = tip)
    true
 
if barstate.islast and showStat and not PHONE and intra
    string over = "Over " + str.tostring(S.days) + " completed days on this chart."
    f_sc(0, 0, "Days", "Completed full days on this chart.")
    f_sc(1, 0, "Avg filled", "Pivot levels traded to from the open, on the side the day moved further (or your bias side). " + over)
    f_sc(2, 0, "4+ filled", "Price tends to fill about four numbers a day. " + over)
    f_sc(3, 0, "6+ filled", "Large range days: what the runner is for. " + over)
    f_sc(0, 1, str.tostring(S.days), "")
    f_sc(1, 1, S.days > 0 ? str.tostring(S.sum / S.days, "0.0") : "-", over)
    f_sc(2, 1, S.days > 0 ? str.tostring(math.round(S.four / S.days * 100)) + "%" : "-", over)
    f_sc(3, 1, S.days > 0 ? str.tostring(math.round(S.six / S.days * 100)) + "%" : "-", over)
 
// ─── Header strip, top centre ────────────────────────────────────────────────
var table hs = hStripOn and not PHONE ? table.new(position.top_center, 1, 3, bgcolor = chart.bg_color, border_width = 0, frame_width = 0) : na
if barstate.islast and hStripOn and not PHONE and intra and not na(cur) and cur.lv.size() > 0
    string tf  = timeframe.isminutes ? timeframe.period + "m" : timeframe.period
    bool   upS = f_upSide()
    int    n   = cur.filled(upS)
    [nN, pN]   = cur.nextLv(upS)
    color  sc  = biasIn == "Bullish" ? BULL : biasIn == "Bearish" ? BEAR : INK
    table.cell(hs, 0, 0, "Pivot Numbers+    " + tf + " ▸ " + (GMT ? "0 GMT" : "18:00 NY"), text_color = INK, text_size = TXT, text_font_family = font.family_monospace)
    table.cell(hs, 0, 1, "● " + (upS ? "▲ " : "▼ ") + str.tostring(n) + " of 4 filled · next " + nN + " " + f_pts(pN), text_color = sc, text_size = TXT, text_font_family = font.family_monospace)
    table.cell(hs, 0, 2, "◎ " + syminfo.ticker + " · CPP " + f_pts(cur.lv.get(cur.nm.indexof("CPP"))) + " · bias " + str.lower(biasIn), text_color = INK, text_size = TXT, text_font_family = font.family_monospace)

← 返回列表