Gold's Sessions: each session against its usual range

TheSimplifier7 · study · 519 行 · 点赞 16 · 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/
// © TheSimplifier7
//@version=6
indicator("Gold's Sessions: each session against its usual range", shorttitle = "Gold's Sessions", overlay = true, max_boxes_count = 60, max_lines_count = 200, max_labels_count = 150)
 
//  GOLD'S SESSIONS · version 1.0 · 21 October 2026
//  Gold's three sessions on its own clock, in New York time: Asia 18:00 to 03:00, London 03:00 to 08:00 and New York
//  08:00 to 17:00. Each session is a soft column of light from its high to its low across its hours, the running one
//  brighter; no colour for which way it closed, as it calls no direction. Its usual is the median high minus low of the
//  last 60 whole sessions of its kind on this chart. Today's sessions each carry a dotted frame one usual tall, set on the
//  open and never moved: its top half the usual over the open, its bottom half under it. A band inside its frame is a
//  smaller session than usual; one through it, a bigger one. The top lights blue and the bottom red from the bar that
//  reaches it. The panel sets today's sessions against their usual. The desk's hours are marked and named.
//  The frame pass, 20 October 2026, on Bradley's word: the two lines became the frame's top and bottom, ink until reached;
//  the frame's sides, its caption and the words TOP and BOTTOM; the desk's hours named under their bars; the panel in
//  capitals with a two line key.
//  21 October 2026: the live counts at the running frame's top and bottom taken off before release. Read walk-forward
//  on gold's 10 minute bars, they ran low with price near a line and high with price far from it, so they wait for a
//  count that holds there. An earlier session's label reads its ratio alone, LONDON 0.83×, so a short session's label
//  clears the next one's; the running session's label keeps USUAL.
 
// ─────────────────────────────────────────────── inputs
string clockIn   = input.string("New York", "Clock", options = ["New York", "London", "Madrid"], tooltip = "The time zone the hours in the panel and the desk's marks are written in. The sessions keep gold's own New York hours.")
int    nDays     = input.int(3, "Days drawn", minval = 1, maxval = 10, tooltip = "How many gold days of sessions stay on the chart, today's included. The usual and the ranks do not depend on it.")
int    nWin      = input.int(60, "Usual window", minval = 20, maxval = 500, tooltip = "How many whole sessions of each kind the usual and the rank are taken from. The usual shows once 20 have closed on the chart.")
bool   showDesk  = input.bool(true, "Desk hours", tooltip = "The two desk hours gold's bars run wide at, as dotted marks, each named under its bar: New York's data hour (08:30 New York), DATA, and the LBMA PM auction (15:00 London), LBMA PM. On gold's 6 minute bars from 2023 the morning auction and the COMEX settle hardly stood out from the hour around them, so they are left off. The Clockface marks these hours too: turn these off if it is on the same chart.")
bool   showPanel = input.bool(true, "Panel")
 
// ─────────────────────────────────────────────── the house palette
color C_GOLD = #E0A93B
color C_BLUE = #8DB0F5                                   // the up side, as in The Fan (R23, 20 October)
color C_MUTE = #8A94A6
color C_INK  = #F3EFE6
color C_BG   = #0B1730
color C_RED  = #E2574C                                   // the down side, as in The Fan; lines and lights
color C_REDT = #EE7B70                                   // its words, a step lighter so they read on the navy (6.08 to 1)
 
string NY   = "America/New_York"
string LDN  = "Europe/London"
string TZ   = clockIn == "New York" ? NY : clockIn == "London" ? LDN : "Europe/Madrid"
int    HR   = 3600000
string XS   = "×"
int    NMIN = 20                                       // the usual shows once this many whole sessions of its kind have closed
 
int  tfSec  = timeframe.in_seconds()
bool tfOk   = timeframe.isintraday and tfSec >= 60 and tfSec <= 3600 and 3600 % tfSec == 0     // 1 to 60 minutes, dividing the hour
bool tfHalf = tfOk and 1800 % tfSec == 0                                                       // and the half hour, for the :30 marks
int  SLOT   = math.max(600, tfSec) * 1000              // the by-now marks: every 10 minutes, or every bar on longer charts
 
// prices of 100 or more print to two decimals
f_px(float p) =>
    math.abs(p) >= 100 ? str.tostring(p, "#,##0.00") : str.tostring(p, format.mintick)
// ranges of 1 or more print to two decimals
f_rg(float r) =>
    math.abs(r) >= 1 ? str.tostring(r, "#,##0.00") : str.tostring(r, format.mintick)
f_name(int k) =>
    k == 0 ? "ASIA" : k == 1 ? "LONDON" : "NEW YORK"
f_title(int k) =>
    k == 0 ? "Asia" : k == 1 ? "London" : "New York"
 
// ─────────────────────────────────────────────── gold's clock
// The gold day runs 18:00 to 17:00 New York and is dated by the day it closes on. It is read off the New York clock, so
// the day and its sessions agree on any chart; on gold it is the daily bar. Asia opens at 18:00 the evening before (the
// day's midnight less six hours), London at 03:00, New York at 08:00, and the day closes at 17:00.
int t6   = time + 6 * HR
int gY   = year(t6, NY)
int gM   = month(t6, NY)
int gD   = dayofmonth(t6, NY)
int gKey = gY * 10000 + gM * 100 + gD
int aS   = timestamp(NY, gY, gM, gD, 0, 0, 0) - 6 * HR
int lS   = timestamp(NY, gY, gM, gD, 3, 0, 0)
int nS   = timestamp(NY, gY, gM, gD, 8, 0, 0)
int nE   = timestamp(NY, gY, gM, gD, 17, 0, 0)
int sId  = time >= aS and time < lS ? 0 : time >= lS and time < nS ? 1 : time >= nS and time < nE ? 2 : -1
int sSt  = sId == 0 ? aS : sId == 1 ? lS : nS
int sEn  = sId == 0 ? lS : sId == 1 ? nS : nE
 
// ─────────────────────────────────────────────── the session in hand, and the whole sessions behind it
var int   dayNo = 0                                    // gold days seen
var int   gPrev = na
var int   cK    = -1                                   // the session in hand: 0 Asia, 1 London, 2 New York
var int   cS    = na                                   // its start and end on the clock
var int   cE    = na
var int   cT0   = na                                   // its first bar's open and its last bar's close
var int   cT1   = na
var bool  cRun  = false                                // still running
var float cO    = na
var float cH    = na
var float cL    = na
var float cC    = na
var float cU    = na                                   // its usual, from the whole sessions of its kind before it
var int   cN    = 0                                    // how many whole sessions that usual is taken from
var box   cBand = na
var line  cGhT  = na                                   // the frame's top and bottom, set at the open: half the usual over it and under it
var line  cGhB  = na
var line  cVl   = na                                   // the frame's two sides, at its open and its end
var line  cVr   = na
var label cLab  = na
var line  cLtU  = na                                   // each line's lit part, from the bar that reached it
var line  cLtD  = na
var float cUp   = na
var float cDn   = na
var int   cRU   = na                                   // the open time of the first bar to reach each line
var int   cRD   = na
var int   cKU   = 0                                    // of the last whole sessions of its kind with a usual, how many reached
var int   cKD   = 0                                    // half their usual over and under their open by their close
var int   cNR   = 0
// the running session's words at its top and bottom, out past its end, and its frame's caption, over its top left
var label lbU = label.new(time, close, "", xloc = xloc.bar_time, style = label.style_label_left, color = color(na), textcolor = C_BLUE, size = size.small)
var label lbD = label.new(time, close, "", xloc = xloc.bar_time, style = label.style_label_left, color = color(na), textcolor = C_REDT, size = size.small)
var label lbF = label.new(time, close, "", xloc = xloc.bar_time, style = label.style_label_lower_left, color = color(na), textcolor = color.new(C_INK, 30), size = size.small)
// each whole session with a usual: did it reach half its usual over its open, and under it, by its close (1 or 0)
var array<int> uA = array.new<int>()
var array<int> uL = array.new<int>()
var array<int> uN = array.new<int>()
var array<int> dA = array.new<int>()
var array<int> dL = array.new<int>()
var array<int> dN = array.new<int>()
f_ru(int k) =>
    k == 0 ? uA : k == 1 ? uL : uN
f_rd(int k) =>
    k == 0 ? dA : k == 1 ? dL : dN
var array<float> hA = array.new<float>()              // the ranges of the last whole sessions: Asia, London, New York
var array<float> hL = array.new<float>()
var array<float> hN = array.new<float>()
f_hist(int k) =>
    k == 0 ? hA : k == 1 ? hL : hN
// by now: each whole session's range so far at every mark of its hours, the last sessions of each kind end to end
var array<float> mA  = array.new<float>()
var array<float> mL  = array.new<float>()
var array<float> mN  = array.new<float>()
var array<float> cMk = array.new<float>()               // the session in hand's marks
f_mhist(int k) =>
    k == 0 ? mA : k == 1 ? mL : mN
f_marks(int k) =>
    int((k == 1 ? 5 : 9) * HR / SLOT)
// the mark a bar closing at tc falls in: the range so far at the end of that mark's ten minutes
f_mark(int tc) =>
    int(math.ceil(float(tc - cS) / SLOT)) - 1
 
// today's rows: 0 not open yet, 1 running, 2 closed whole, 3 closed as a part session
var array<int>   tSt  = array.new<int>(3, 0)
var array<float> tR   = array.new<float>(3, na)
var array<float> tU   = array.new<float>(3, na)
var array<int>   tN   = array.new<int>(3, 0)
var array<int>   tRk  = array.new<int>(3, 0)
var array<int>   tDir = array.new<int>(3, 0)
var array<float> tUN  = array.new<float>(3, na)       // the running session against its usual by now
var array<int>   tKN  = array.new<int>(3, 0)
var array<int>   tNN  = array.new<int>(3, 0)
 
// what is drawn, a session at a time, each with the gold day it belongs to; then the desk's marks
var array<box>   dBand = array.new<box>()
var array<line>  dGhT  = array.new<line>()
var array<line>  dGhB  = array.new<line>()
var array<line>  dLtU  = array.new<line>()
var array<line>  dLtD  = array.new<line>()
var array<line>  dVl   = array.new<line>()
var array<line>  dVr   = array.new<line>()
var array<label> dLab  = array.new<label>()
var array<int>   dDay  = array.new<int>()
var array<line>  kLine = array.new<line>()
var array<label> kLab  = array.new<label>()
var array<int>   kDay  = array.new<int>()
 
// the usual: the middle of the sorted ranges, or the mean of the middle two
f_median(array<float> h) =>
    int   n = array.size(h)
    float m = na
    if n > 0
        array<float> s = array.copy(h)
        array.sort(s, order.ascending)
        int i = int(math.floor(n / 2.0))
        m := n % 2 == 1 ? array.get(s, i) : (array.get(s, i - 1) + array.get(s, i)) / 2.0
    m
 
// the rank: how many of those whole sessions a range is bigger than
f_rank(array<float> h, float r) =>
    int c = 0
    for v in h
        if v < r
            c += 1
    c
 
// by now: the median range of the stored sessions of kind k at mark m, a range's rank among them, and their number
f_now(int k, int m, float r) =>
    array<float> mh = f_mhist(k)
    int nM = f_marks(k)
    int ns = nM > 0 ? int(array.size(mh) / nM) : 0
    array<float> v = array.new<float>()
    if ns > 0 and m >= 0 and m < nM
        for q = 0 to ns - 1
            float x = array.get(mh, q * nM + m)
            if not na(x)
                array.push(v, x)
    int   n   = array.size(v)
    float med = n >= NMIN ? f_median(v) : na
    [med, f_rank(v, r), n]
 
// the words at the frame's top and bottom: TOP or BOTTOM before it is reached, the time it was reached after. Kept short
// so they clear the price scale with a usual right margin (20 October: the long form ran under it); the key and the
// tooltip hold the rest.
f_lineTx(bool over, int tR) =>
    (over ? "TOP" : "BOTTOM") + (na(tR) ? "" : " · REACHED " + str.format_time(tR, "HH:mm", TZ))
f_lineTip(bool over, float y, int kR, int tR) =>
    string side = over ? "over" : "under"
    string head = over ? "The top" : "The bottom"
    head + " of the usual frame: half the usual " + f_title(cK) + " range " + side + " the open, " + f_px(y) + ", set at the open and fixed." +
     (na(tR) ? (cRun ? " Not reached yet." : " Not reached.") : " Reached " + str.format_time(tR, "HH:mm", TZ) + ".") +
     (cNR >= NMIN ? "\nAt the open: " + str.tostring(kR) + " of the last " + str.tostring(cNR) + " reached half their usual " + side + " their open by their close." : "")
// the band, its frame and its label, for the session in hand
f_draw() =>
    float r    = cH - cL
    bool  hasU = cN >= NMIN and cU > 0
    bool  part = not cRun and not (cT0 == cS and cT1 == cE)
    int   x1   = cRun ? cE : cT1
    // one neutral light, never a colour for the way it closed: the session so far, the running one brighter; no border,
    // so the frame is the only outline
    // 21 October: the shaded band is the session so far, so it runs to the last bar's close and fills the frame as the hours pass
    box.set_lefttop(cBand, cT0, cH)
    box.set_rightbottom(cBand, cT1, cL)
    box.set_bgcolor(cBand, color.new(C_INK, cRun ? 93 : 96))
    box.set_border_color(cBand, color.new(C_INK, 100))
    float yLab = cL                                    // the label sits right under its band
    if hasU
        // the usual frame: dotted ink until its top or bottom is reached, then that side lit from the bar that reached it, blue
        // at the top and red at the bottom, as across the range; the sides fainter; the running session's frame bright, the rest faint
        line.set_x2(cGhT, na(cRU) ? x1 : cRU)
        line.set_x2(cGhB, na(cRD) ? x1 : cRD)
        line.set_color(cGhT, color.new(C_INK, cRun ? 45 : 80))
        line.set_color(cGhB, color.new(C_INK, cRun ? 45 : 80))
        line.set_xy1(cVr, x1, cDn)
        line.set_xy2(cVr, x1, cUp)
        line.set_color(cVl, color.new(C_INK, cRun ? 65 : 88))
        line.set_color(cVr, color.new(C_INK, cRun ? 65 : 88))
        if not na(cLtU)
            line.set_x2(cLtU, x1)
            line.set_color(cLtU, color.new(C_BLUE, cRun ? 5 : 50))
        if not na(cLtD)
            line.set_x2(cLtD, x1)
            line.set_color(cLtD, color.new(C_RED, cRun ? 5 : 50))
        // when the frame's bottom runs just under the band, the label goes under it, so neither hides the other
        if cDn < cL and cL - cDn < 0.12 * cU
            yLab := cDn
    if cRun and hasU
        label.set_xy(lbU, cE, cUp)
        label.set_text(lbU, f_lineTx(true, cRU))
        label.set_tooltip(lbU, f_lineTip(true, cUp, cKU, cRU))
        label.set_xy(lbD, cE, cDn)
        label.set_text(lbD, f_lineTx(false, cRD))
        label.set_tooltip(lbD, f_lineTip(false, cDn, cKD, cRD))
        label.set_xy(lbF, cT0, cUp)
        label.set_text(lbF, "A USUAL " + f_name(cK) + ", SET ON ITS OPEN · " + f_rg(cU))
        label.set_tooltip(lbF, "The dotted frame is one usual " + f_title(cK) + " range tall, the middle of the last " + str.tostring(cN) + " whole " + f_title(cK) + " sessions' high minus low, set on this session's open: its top half the usual over the open, its bottom half under it. The shaded band is this session so far. Inside the frame, a smaller session than usual; through it, a bigger one. It calls no direction.")
    else
        label.set_text(lbU, "")
        label.set_text(lbD, "")
        label.set_text(lbF, "")
    // while it runs, the session is set against its usual by now, the same point of the same hours
    float uN = na
    int   kN = 0
    int   nN = 0
    if cRun
        [a1, a2, a3] = f_now(cK, f_mark(cT1), r)
        uN := a1
        kN := a2
        nN := a3
    bool   hasN = cRun and not na(uN) and uN > 0
    string txt = f_name(cK) + (hasN ? " " + str.tostring(r / uN, "0.00") + XS + " USUAL BY NOW" : hasU ? " " + str.tostring(r / cU, "0.00") + XS + (cRun ? " USUAL" : "") : " RANGE " + f_rg(r)) + (cRun and not hasN ? " SO FAR" : part ? " · PART SESSION" : "")
    string tip = (hasN ? "By now the usual " + f_title(cK) + " had covered " + f_rg(uN) + "; this one is bigger than " + str.tostring(kN) + " of " + str.tostring(nN) + " at the same point\n" : "") +
         "High " + f_px(cH) + " · low " + f_px(cL) + "\nRange " + f_rg(r) + (hasU ? " · usual for the whole session " + f_rg(cU) : "") + "\n" +
         (cN >= NMIN ? "Bigger than " + str.tostring(f_rank(f_hist(cK), r)) + " of the " + str.tostring(cN) + " whole " + f_title(cK) + " sessions before it" + (cRun ? ", so far" : "") : "The usual shows once " + str.tostring(NMIN) + " whole " + f_title(cK) + " sessions have closed") +
         (part ? "\nPart session: left out of the usual" : "") +
         (hasU ? "\nThe usual frame's top, half the usual over the open, " + f_px(cUp) + ": " + (na(cRU) ? (cRun ? "not yet" : "not reached") : "reached " + str.format_time(cRU, "HH:mm", TZ)) +
         "\nIts bottom, half the usual under the open, " + f_px(cDn) + ": " + (na(cRD) ? (cRun ? "not yet" : "not reached") : "reached " + str.format_time(cRD, "HH:mm", TZ)) : "")
    label.set_xy(cLab, cT0, yLab)
    label.set_text(cLab, txt)
    label.set_textcolor(cLab, cRun ? color.new(C_INK, 10) : color.new(C_MUTE, 10))
    label.set_tooltip(cLab, tip)
 
// today's row for the session in hand
f_today() =>
    float r = cH - cL
    array.set(tSt, cK, cRun ? 1 : (cT0 == cS and cT1 == cE) ? 2 : 3)
    array.set(tR, cK, r)
    array.set(tU, cK, cU)
    array.set(tN, cK, cN)
    array.set(tRk, cK, f_rank(f_hist(cK), r))
    array.set(tDir, cK, cC > cO ? 1 : cC < cO ? -1 : 0)
    array.set(tUN, cK, na)
    if cRun
        [b1, b2, b3] = f_now(cK, f_mark(cT1), r)
        array.set(tUN, cK, b1)
        array.set(tKN, cK, b2)
        array.set(tNN, cK, b3)
 
// keep the last few gold days drawn, today's included, and delete the rest
f_prune(int keep) =>
    while array.size(dDay) > 0
        if array.get(dDay, 0) >= keep
            break
        array.shift(dDay)
        box.delete(array.shift(dBand))
        line.delete(array.shift(dGhT))
        line.delete(array.shift(dGhB))
        line.delete(array.shift(dLtU))
        line.delete(array.shift(dLtD))
        line.delete(array.shift(dVl))
        line.delete(array.shift(dVr))
        label.delete(array.shift(dLab))
    while array.size(kDay) > 0
        if array.get(kDay, 0) >= keep
            break
        array.shift(kDay)
        line.delete(array.shift(kLine))
        label.delete(array.shift(kLab))
 
// a desk's mark: a dotted line through the bar that opens on it, its name in small mute words just under that bar
f_deskMark(string nme) =>
    array.push(kLine, line.new(time, high, time, low, xloc = xloc.bar_time, extend = extend.both, color = color.new(C_MUTE, 55), style = line.style_dotted))
    array.push(kLab, label.new(time, low, nme, xloc = xloc.bar_time, yloc = yloc.belowbar, style = label.style_none, textcolor = color.new(C_MUTE, 25), size = size.tiny))
    array.push(kDay, dayNo)
 
// ─────────────────────────────────────────────── the sessions, bar by bar
if tfOk
    bool newDay  = na(gPrev) or gKey != gPrev
    bool newSess = sId >= 0 and (cK < 0 or sSt != cS)
    // a session cut short (a holiday's early close) is closed when the next one opens
    if cRun and (newSess or newDay)
        cRun := false
        f_draw()
        f_today()
    if newDay
        dayNo += 1
        f_prune(dayNo - nDays + 1)
        array.fill(tSt, 0)
        // an earlier day's sessions keep their light and their words; their frames go, so nothing old reads as a level
        int nd = array.size(dDay)
        if nd > 0
            for i = 0 to nd - 1
                line.delete(array.get(dGhT, i))
                line.delete(array.get(dGhB, i))
                line.delete(array.get(dLtU, i))
                line.delete(array.get(dLtD, i))
                line.delete(array.get(dVl, i))
                line.delete(array.get(dVr, i))
    if newSess
        array<float> hS = f_hist(sId)
        cK    := sId
        cS    := sSt
        cE    := sEn
        cT0   := time
        cT1   := time_close
        cRun  := true
        cO    := open
        cH    := high
        cL    := low
        cC    := close
        cN    := array.size(hS)
        cU    := cN >= NMIN ? f_median(hS) : na
        array.clear(cMk)
        int nMk = f_marks(sId)
        if nMk > 0
            for q = 1 to nMk
                array.push(cMk, na)
        cBand := box.new(time, high, sEn, low, xloc = xloc.bar_time, border_width = 1)
        cGhT  := na
        cGhB  := na
        cUp   := na
        cDn   := na
        cRU   := na
        cRD   := na
        cLtU  := na
        cLtD  := na
        cVl   := na
        cVr   := na
        array<int> ru = f_ru(sId)
        array<int> rd = f_rd(sId)
        cNR   := array.size(ru)
        cKU   := int(array.sum(ru))
        cKD   := int(array.sum(rd))
        // the usual frame is set here, at the open, and never moves: its top and bottom, then its two sides
        if cN >= NMIN and cU > 0
            cUp  := open + cU / 2.0
            cDn  := open - cU / 2.0
            cGhT := line.new(time, cUp, sEn, cUp, xloc = xloc.bar_time, color = color.new(C_INK, 45), style = line.style_dotted)
            cGhB := line.new(time, cDn, sEn, cDn, xloc = xloc.bar_time, color = color.new(C_INK, 45), style = line.style_dotted)
            cVl  := line.new(time, cDn, time, cUp, xloc = xloc.bar_time, color = color.new(C_INK, 65), style = line.style_dotted)
            cVr  := line.new(sEn, cDn, sEn, cUp, xloc = xloc.bar_time, color = color.new(C_INK, 65), style = line.style_dotted)
        cLab  := label.new(time, low, "", xloc = xloc.bar_time, style = label.style_label_upper_left, color = color.new(C_BG, 10), size = size.small)
        array.push(dBand, cBand)
        array.push(dGhT, cGhT)
        array.push(dGhB, cGhB)
        array.push(dLtU, cLtU)
        array.push(dLtD, cLtD)
        array.push(dVl, cVl)
        array.push(dVr, cVr)
        array.push(dLab, cLab)
        array.push(dDay, dayNo)
        f_draw()
        f_today()
    else if cRun and sId == cK and sSt == cS
        cH  := math.max(cH, high)
        cL  := math.min(cL, low)
        cC  := close
        cT1 := time_close
    if cRun
        // the top or bottom reached lights from this bar to the session's end
        if not na(cUp) and na(cRU) and high >= cUp
            cRU  := time
            cLtU := line.new(time, cUp, cE, cUp, xloc = xloc.bar_time, color = color.new(C_BLUE, 5), style = line.style_solid)
            array.set(dLtU, array.size(dLtU) - 1, cLtU)
        if not na(cDn) and na(cRD) and low <= cDn
            cRD  := time
            cLtD := line.new(time, cDn, cE, cDn, xloc = xloc.bar_time, color = color.new(C_RED, 5), style = line.style_solid)
            array.set(dLtD, array.size(dLtD) - 1, cLtD)
        int mk = f_mark(time_close)
        if mk >= 0 and mk < array.size(cMk)
            array.set(cMk, mk, cH - cL)
    // the session's last bar has closed: whole if it also opened on time, and only a whole session joins the usual
    if cRun and barstate.isconfirmed and time_close >= cE
        cRun := false
        f_draw()
        f_today()
        if cT0 == cS and cT1 == cE
            array<float> hC = f_hist(cK)
            array.push(hC, cH - cL)
            while array.size(hC) > nWin
                array.shift(hC)
            // with a usual, whether it reached half of it over and under its open joins the counts
            if not na(cUp)
                array<int> ru = f_ru(cK)
                array<int> rd = f_rd(cK)
                array.push(ru, na(cRU) ? 0 : 1)
                array.push(rd, na(cRD) ? 0 : 1)
                while array.size(ru) > nWin
                    array.shift(ru)
                    array.shift(rd)
            // its marks join the by-now store, gaps carried forward from the mark before
            array<float> mh = f_mhist(cK)
            int   nM = array.size(cMk)
            float lv = na
            if nM > 0
                for q = 0 to nM - 1
                    float v = array.get(cMk, q)
                    if na(v)
                        array.set(cMk, q, lv)
                    else
                        lv := v
                    array.push(mh, array.get(cMk, q))
                while array.size(mh) > nWin * nM
                    for q = 1 to nM
                        array.shift(mh)
    // the session still running, at the last bar
    if cRun and barstate.islast
        f_draw()
        f_today()
gPrev := gKey
 
// ─────────────────────────────────────────────── the desk's hours
if tfOk and showDesk and barstate.isconfirmed
    int lh = hour(time, LDN)
    int lm = minute(time, LDN)
    int nh = hour(time, NY)
    int nm = minute(time, NY)
    // 21 October: the two hours gold's bars run wide at. On his 6 minute exports, 2023 to June 2026, the 08:30 bar ran at
    // a median 1.34 of the hour around it and the 15:00 London auction at 1.20; the 10:30 auction (1.06) and the settle
    // (1.10) hardly stood out, so they are not drawn.
    if tfHalf and nh == 8 and nm == 30
        f_deskMark("DATA")
    if lh == 15 and lm == 0
        f_deskMark("LBMA PM")
 
// ─────────────────────────────────────────────── the panel
// no grid; each session in one row: its hours, today's range, its usual, and the two set against each other in words.
// The running session's name is gold, the house's mark for now.
int PR = 7
var table tb = table.new(position.top_right, 4, PR, bgcolor = color.new(C_BG, 4), border_color = color.new(C_INK, 100), border_width = 0,
     frame_color = color.new(C_INK, 82), frame_width = 1)
f_row(int r, string a, color ca, string b, string c, string d) =>
    table.cell(tb, 0, r, a, text_color = ca, text_size = size.small, text_halign = text.align_left)
    table.cell(tb, 1, r, "   " + b, text_color = b == "·" ? C_MUTE : C_INK, text_size = size.small, text_halign = text.align_right)
    table.cell(tb, 2, r, "   " + c, text_color = c == "·" ? C_MUTE : C_INK, text_size = size.small, text_halign = text.align_right)
    table.cell(tb, 3, r, "   " + d, text_color = d == "·" ? C_MUTE : C_INK, text_size = size.small, text_halign = text.align_right)
f_head(int r, string a, string b, string c, string d) =>
    table.cell(tb, 0, r, a, text_color = C_MUTE, text_size = size.tiny, text_halign = text.align_left)
    table.cell(tb, 1, r, b, text_color = C_MUTE, text_size = size.tiny, text_halign = text.align_right)
    table.cell(tb, 2, r, c, text_color = C_MUTE, text_size = size.tiny, text_halign = text.align_right)
    table.cell(tb, 3, r, d, text_color = C_MUTE, text_size = size.tiny, text_halign = text.align_right)
f_sec(int r, string a) =>
    table.cell(tb, 0, r, a, text_color = C_MUTE, text_size = size.tiny, text_halign = text.align_left)
if barstate.isfirst
    for r = 5 to PR - 1
        table.merge_cells(tb, 0, r, 3, r)
if showPanel and barstate.islast
    table.cell(tb, 0, 0, "GOLD'S SESSIONS", text_color = C_GOLD, text_size = size.small, text_halign = text.align_left)
    table.cell(tb, 3, 0, syminfo.ticker, text_color = C_MUTE, text_size = size.small, text_halign = text.align_right)
    if not tfOk
        f_sec(5, "OPEN A 1 TO 60 MINUTE CHART")
    else
        f_head(1, str.upper(clockIn) + " TIME", "TODAY", "USUAL", "AGAINST THE LAST " + str.tostring(nWin))
        array<int> bnd = array.from(aS, lS, nS, nE)
        for k = 0 to 2
            int          st   = array.get(tSt, k)
            array<float> h    = f_hist(k)
            int          n    = st == 0 ? array.size(h) : array.get(tN, k)
            float        u    = st == 0 ? (n >= NMIN ? f_median(h) : na) : array.get(tU, k)
            float        r    = array.get(tR, k)
            bool         hasU = st > 0 and n >= NMIN and u > 0
            string hrs = f_name(k) + " " + str.format_time(array.get(bnd, k), "HH:mm", TZ) + " TO " + str.format_time(array.get(bnd, k + 1), "HH:mm", TZ)
            string rTx = st == 0 ? "·" : f_rg(r) + (st == 1 ? " SO FAR" : st == 3 ? " PART SESSION" : "")
            float  uN  = array.get(tUN, k)
            bool   byN = st == 1 and not na(uN) and uN > 0
            string uTx = byN ? f_rg(uN) + " BY NOW" : n >= NMIN ? f_rg(u) : "·"
            string aTx = byN ? str.tostring(r / uN, "0.00") + XS + " · BIGGER THAN " + str.tostring(array.get(tKN, k)) + " OF " + str.tostring(array.get(tNN, k)) + " BY NOW" :
                 hasU ? str.tostring(r / u, "0.00") + XS + " · BIGGER THAN " + str.tostring(array.get(tRk, k)) + " OF " + str.tostring(n) : "·"
            f_row(2 + k, hrs, st == 1 ? C_GOLD : st == 0 ? C_MUTE : C_INK, rTx, uTx, aTx)
        f_sec(5, "SHADED: THE SESSION SO FAR · DOTTED FRAME: A USUAL RANGE, SET ON ITS OPEN · CALLS NO DIRECTION")
        f_sec(6, "USUAL: MIDDLE OF THE LAST " + str.tostring(nWin) + " WHOLE SESSIONS · BY NOW: SAME POINT OF THEIR HOURS")

← 返回列表