The Atlas: odds of a touch before the close

TheSimplifier7 · study · 542 行 · 点赞 51 · 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("The Atlas: odds of a touch before the close", shorttitle = "The Atlas", overlay = true, max_lines_count = 80, max_labels_count = 60, max_boxes_count = 100, max_bars_back = 1500)
 
//  THE ATLAS · version 1.0 · 19 October 2026
//  The levels that matter today, each lit by the odds that price touches it before 17:00 New York: last week's high and
//  low, yesterday's high and low, today's high and low so far, the day's open, the week's open and the round handles
//  around the price. A level under the price is a floor, in gold; over it, a ceiling, in blue. The odds come from a
//  random walk sized by yesterday's ATR and the hours left in the gold day, stamped three times a day and graded at the
//  close. The figure on each level is the walk's odds as graded on this chart: how often levels the walk rated alike
//  were touched here, from finished days only.
 
// ─────────────────────────────────────────────── inputs
string clockIn   = input.string("New York", "Clock", options = ["New York", "London", "Madrid"], tooltip = "The time zone the panel's time is written in. The gold day, the stamps and the close stay on New York's clock.")
float  stepIn    = input.float(50.0, "Round handle step", minval = 0.0001, tooltip = "The spacing of the round numbers drawn: the two at or under the price and the two over it.")
int    nearIn    = input.int(3, "Levels labelled in full", minval = 0, maxval = 12, tooltip = "How many levels, counted out from the price, carry their name, price and odds in words. The others carry a small faint name.")
int    thrIn     = input.int(15, "Lines from (in 100)", minval = 0, maxval = 99, tooltip = "A level gets its line once its odds of a touch before the close reach this. The levels under it are named in small type above the top line and under the bottom one, so the far ones never squeeze the chart. 0 draws every level.")
int    chgIn     = input.int(60, "Change over (minutes)", minval = 10, maxval = 240, step = 10, tooltip = "The panel's last column: how each level's odds have moved over this many minutes, with the level where it stands now. Time running out pulls every level's odds down; price travelling toward a level pulls them up.")
bool   tchIn     = input.bool(true, "Stamp levels touched today", tooltip = "A level price has already reached today keeps its line, dashed and dimmer, and its words say when it was first touched: TOUCHED 02:40. Its odds then read as the odds of touching it again before the close. Today's high, today's low and the day's open are touched by definition and are never stamped.")
bool   ladIn     = input.bool(true, "Ladder", tooltip = "A strip beside the last bar, shaded at every price by its odds of a touch before the close, graded on this chart, with rungs where the odds are 75, 50 and 25 in 100. It runs out to the outer lines.")
int    ladW      = input.int(5, "Ladder width (bars)", minval = 2, maxval = 20)
bool   showPanel = input.bool(true, "Panel")
string posIn     = input.string("Top right", "Panel position", options = ["Top right", "Bottom right", "Top left", "Bottom left"])
bool   levelsIn  = input.bool(false, "Levels table in the panel", tooltip = "Off: the levels speak on the chart and the panel keeps the time, one line of proof and the footer. On: the six levels in order of odds, with their change, under the time.")
bool   ledgerIn  = input.bool(false, "Full ledger in the panel", tooltip = "Off: one line of proof under the levels. On: every band of what the panel showed against what was touched on this chart, and the Brier against the walk's.")
 
// ─────────────────────────────────────────────── the house palette
color C_GOLD = #E0A93B
color C_BLUE = #6F9CF2
color C_MUTE = #8A94A6
color C_INK  = #F3EFE6
color C_BG   = #0B1730
color C_RED  = #E5625A
 
string NY  = "America/New_York"
string TZ  = clockIn == "New York" ? NY : clockIn == "London" ? "Europe/London" : "Europe/Madrid"
int    HR  = 3600000
 
int  tfSec = timeframe.in_seconds()
bool tfOk  = timeframe.isintraday and tfSec >= 60 and tfSec <= 1800 and 1800 % tfSec == 0
 
// 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)
 
// yesterday's ATR, from the daily context
float atrD = request.security(syminfo.tickerid, "D", ta.atr(14)[1], lookahead = barmerge.lookahead_on)
 
// the standard normal distribution, Abramowitz and Stegun 7.1.26
f_phi(float x) =>
    float z = math.abs(x) / math.sqrt(2.0)
    float t = 1.0 / (1.0 + 0.3275911 * z)
    float e = 1.0 - (((((1.061405429 * t - 1.453152027) * t) + 1.421413741) * t - 0.284496736) * t + 0.254829592) * t * math.exp(-z * z)
    x >= 0 ? 0.5 * (1.0 + e) : 0.5 * (1.0 - e)
 
// the walk's odds of a touch at distance d, with spread s; with no time left, only a level at the price counts
f_odds(float d, float s) =>
    s > 0 ? math.max(0.0, 2.0 * (1.0 - f_phi(d / s))) : (d == 0 ? 1.0 : 0.0)
 
// the odds in words
f_in100(float p) =>
    p >= 0.995 ? "OVER 99 IN 100" : p < 0.005 ? "UNDER 1 IN 100" : str.tostring(p * 100, "0") + " IN 100"
 
int    XE  = ladIn ? ladW + 5 : 4                   // where the lines end and their words begin, clear of the ladder
int    LADN = 24                                     // the ladder's slices on each side (the loop runs 0 to 23)
 
// ─────────────────────────────────────────────── the gold day and the gold week, tracked bar by bar
bool newD = timeframe.change("D")
bool newW = timeframe.change("W")
int  dEnd = time_close("D")
var bool  wholeD = false                            // the day was seen from its first bar; the chart's first day never counts
var float dOpen  = na
var float dHi    = na
var float dLo    = na
var int   dBar   = na
var float yHi    = na                               // yesterday's high and low, only from a day seen whole
var float yLo    = na
if newD or na(dHi)
    yHi    := wholeD and newD ? dHi : na
    yLo    := wholeD and newD ? dLo : na
    wholeD := newD and bar_index > 0
    dOpen  := open
    dHi    := high
    dLo    := low
    dBar   := bar_index
else
    dHi := math.max(dHi, high)
    dLo := math.min(dLo, low)
 
var bool  wholeW = false                            // the week was seen from its first bar; the chart's first week never counts
var float wOpen  = na
var float wHi    = na
var float wLo    = na
var float lwHi   = na                               // last week's high and low, only from a week seen whole
var float lwLo   = na
if newW or na(wHi)
    lwHi   := wholeW and newW ? wHi : na
    lwLo   := wholeW and newW ? wLo : na
    wholeW := newW and bar_index > 0
    wOpen  := open
    wHi    := high
    wLo    := low
else
    wHi := math.max(wHi, high)
    wLo := math.min(wLo, low)
 
// the walk, sized by yesterday's ATR and the hours left to 17:00 New York
float left = (dEnd - time_close) / float(HR)
float sdW  = atrD / 1.596 * math.sqrt(math.max(left, 0.0) / 23.0)
 
// ─────────────────────────────────────────────── the levels in force
var array<float>  lvP = array.new<float>()          // price
var array<string> lvN = array.new<string>()         // name
var array<bool>   lvR = array.new<bool>()           // a round handle, its price already in its name
f_put(float p, string n, bool r) =>
    array.push(lvP, p)
    array.push(lvN, n)
    array.push(lvR, r)
f_build(float px) =>
    array.clear(lvP)
    array.clear(lvN)
    array.clear(lvR)
    if not na(lwHi)
        f_put(lwHi, "LAST WEEK'S HIGH", false)
        f_put(lwLo, "LAST WEEK'S LOW", false)
    if not na(yHi)
        f_put(yHi, "YESTERDAY'S HIGH", false)
        f_put(yLo, "YESTERDAY'S LOW", false)
    if wholeD
        f_put(dHi, "TODAY'S HIGH", false)
        f_put(dLo, "TODAY'S LOW", false)
        f_put(dOpen, "DAY'S OPEN", false)
    if wholeW
        f_put(wOpen, "WEEK'S OPEN", false)
    // the two round handles at or under the price and the two over it
    float r0 = math.floor(px / stepIn) * stepIn
    for k = -1 to 2
        float rk = r0 + k * stepIn
        f_put(rk, "ROUND " + (rk == math.round(rk) ? str.tostring(rk, "#,##0") : f_px(rk)), true)
    array.size(lvP)
 
// ─────────────────────────────────────────────── the ledger: three stamps a day, graded at the close
var array<float> sLv   = array.new<float>()          // today's stamped levels
var array<bool>  sUp   = array.new<bool>()           // the level sat over the stamp's price
var array<float> sSaid = array.new<float>()
var array<bool>  sCame = array.new<bool>()
var array<float> sShow = array.new<float>()          // the figure the chart showed at the stamp: na before the curve had two points
var array<float> b10N  = array.new<float>(10, 0.0)   // ten bins of what was said, for the lookup beside each call
var array<float> b10C  = array.new<float>(10, 0.0)
var array<float> b10S  = array.new<float>(10, 0.0)
var array<float> cX    = array.new<float>()          // the graded curve: what the walk said, and what was touched here
var array<float> cY    = array.new<float>()
var array<float> b5N   = array.new<float>(5, 0.0)    // five bins for the panel
var array<float> b5S   = array.new<float>(5, 0.0)
var array<float> b5C   = array.new<float>(5, 0.0)
var array<float> acc   = array.new<float>(2, 0.0)    // the Brier sum and the number of calls
var array<float> g5N   = array.new<float>(5, 0.0)    // the figure shown, graded: five bins for the panel
var array<float> g5S   = array.new<float>(5, 0.0)
var array<float> g5C   = array.new<float>(5, 0.0)
var array<float> gAcc  = array.new<float>(3, 0.0)    // its Brier sum, the walk's Brier sum on the same calls, the number of calls
var int   nDays = 0
 
f_bin10(float p) =>
    math.min(int(math.floor(p * 10.0)), 9)
f_addShown(float h, float p, bool came) =>
    if not na(h)
        float y = came ? 1.0 : 0.0
        int i5 = math.min(int(math.floor(h * 5.0)), 4)
        array.set(g5N, i5, array.get(g5N, i5) + 1.0)
        array.set(g5S, i5, array.get(g5S, i5) + h)
        array.set(g5C, i5, array.get(g5C, i5) + y)
        array.set(gAcc, 0, array.get(gAcc, 0) + math.pow(h - y, 2))
        array.set(gAcc, 1, array.get(gAcc, 1) + math.pow(p - y, 2))
        array.set(gAcc, 2, array.get(gAcc, 2) + 1.0)
f_add(float p, bool came) =>
    int i10 = f_bin10(p)
    array.set(b10N, i10, array.get(b10N, i10) + 1.0)
    array.set(b10C, i10, array.get(b10C, i10) + (came ? 1.0 : 0.0))
    array.set(b10S, i10, array.get(b10S, i10) + p)
    int i5 = math.min(int(math.floor(p * 5.0)), 4)
    array.set(b5N, i5, array.get(b5N, i5) + 1.0)
    array.set(b5S, i5, array.get(b5S, i5) + p)
    array.set(b5C, i5, array.get(b5C, i5) + (came ? 1.0 : 0.0))
    array.set(acc, 0, array.get(acc, 0) + math.pow(p - (came ? 1.0 : 0.0), 2))
    array.set(acc, 1, array.get(acc, 1) + 1.0)
// the graded curve: each tenth of the walk's scale with 20 or more finished calls gives a point (what it said, what was
// touched); points are pooled until what was touched never falls as what was said rises, and read between by straight lines
f_curve() =>
    array.clear(cX)
    array.clear(cY)
    array<float> w = array.new<float>()
    for i = 0 to 9
        float n = array.get(b10N, i)
        if n >= 20
            array.push(cX, array.get(b10S, i) / n)
            array.push(cY, array.get(b10C, i) / n)
            array.push(w, n)
    bool moved = true
    while moved
        moved := false
        int m = array.size(cY)
        if m > 1
            for i = 0 to m - 2
                if not moved and array.get(cY, i) > array.get(cY, i + 1)
                    float wa = array.get(w, i)
                    float wb = array.get(w, i + 1)
                    array.set(cY, i, (array.get(cY, i) * wa + array.get(cY, i + 1) * wb) / (wa + wb))
                    array.set(cX, i, (array.get(cX, i) * wa + array.get(cX, i + 1) * wb) / (wa + wb))
                    array.set(w, i, wa + wb)
                    array.remove(cY, i + 1)
                    array.remove(cX, i + 1)
                    array.remove(w, i + 1)
                    moved := true
                    break
    array.size(cX)
// the odds as graded on this chart for a walk's figure p; na until the curve has two points
f_here(float p) =>
    int   m = array.size(cX)
    float h = na
    if m >= 2
        float x0 = array.get(cX, 0)
        float xL = array.get(cX, m - 1)
        float yL = array.get(cY, m - 1)
        if p <= x0
            h := x0 > 0 ? p / x0 * array.get(cY, 0) : array.get(cY, 0)
        else if p >= xL
            h := xL < 1 ? yL + (p - xL) / (1 - xL) * (1 - yL) : yL
        else
            for i = 0 to m - 2
                float xa = array.get(cX, i)
                float xb = array.get(cX, i + 1)
                if na(h) and p >= xa and p <= xb
                    h := xb > xa ? array.get(cY, i) + (p - xa) / (xb - xa) * (array.get(cY, i + 1) - array.get(cY, i)) : array.get(cY, i)
    h
 
// the odds shown at distance d: the walk's, graded on the chart once the curve can grade it
f_shown(float d) =>
    float pw = f_odds(d, sdW)
    float ph = f_here(pw)
    na(ph) ? pw : ph
// the distance at which the odds shown fall to q, by halving; na when they do not reach q inside dMax
f_dAt(float q, float dMax) =>
    float r = na
    if dMax > 0 and f_shown(0.0) >= q and f_shown(dMax) <= q
        float lo = 0.0
        float hi = dMax
        for it = 1 to 24
            float md = (lo + hi) / 2.0
            if f_shown(md) > q
                lo := md
            else
                hi := md
        r := (lo + hi) / 2.0
    r
 
// a day that never reached its close (a short session) drops its stamps ungraded
if newD
    array.clear(sLv)
    array.clear(sUp)
    array.clear(sSaid)
    array.clear(sCame)
    array.clear(sShow)
 
if tfOk and barstate.isconfirmed and not na(atrD)
    // a stamped level came when a later bar reached it: a later high for a level over the stamp's price, a later low for one under it
    int nS = array.size(sLv)
    if nS > 0
        for jC = 0 to nS - 1
            float lvC = array.get(sLv, jC)
            bool  hit = array.get(sUp, jC) ? high >= lvC : low <= lvC
            if hit
                array.set(sCame, jC, true)
    // a stamp at 03:00, 10:00 and 14:00 New York: every level in force, with the walk's odds
    int hNY = hour(time_close, NY)
    if wholeD and minute(time_close, NY) == 0 and (hNY == 3 or hNY == 10 or hNY == 14) and left > 0
        int nB = f_build(close)
        int nCs = f_curve()
        if nB > 0
            for kS = 0 to nB - 1
                float lvS = array.get(lvP, kS)
                array.push(sLv, lvS)
                array.push(sUp, lvS > close)
                float pS = f_odds(math.abs(lvS - close), sdW)
                array.push(sSaid, pS)
                array.push(sCame, false)
                array.push(sShow, f_here(pS))
    // the gold day's last bar grades the day's stamps
    if time_close >= dEnd and wholeD
        int nG = array.size(sLv)
        if nG > 0
            for jG = 0 to nG - 1
                f_addShown(array.get(sShow, jG), array.get(sSaid, jG), array.get(sCame, jG))
                f_add(array.get(sSaid, jG), array.get(sCame, jG))
        nDays += 1
        array.clear(sLv)
        array.clear(sUp)
        array.clear(sSaid)
        array.clear(sCame)
        array.clear(sShow)
 
// ─────────────────────────────────────────────── the panel
string POS = posIn == "Top right" ? position.top_right : posIn == "Bottom right" ? position.bottom_right : posIn == "Top left" ? position.top_left : position.bottom_left
int    RP  = levelsIn ? 9 : 2                       // the proof row, or the ledger's head
int    FA  = ledgerIn ? RP + 7 : RP + 1             // the footer row
var table tb = table.new(POS, 4, FA + 1, bgcolor = color.new(C_BG, 6), border_color = color.new(C_INK, 88), border_width = 1,
     frame_color = color.new(C_INK, 80), frame_width = 1)
f_row(int r, string a, color ca, string b, color cb, string c, color cc, 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 = cb, text_size = size.small, text_halign = text.align_right)
    table.cell(tb, 2, r, c, text_color = cc, text_size = size.small, text_halign = text.align_right)
    table.cell(tb, 3, r, d, text_color = C_MUTE, text_size = size.small, text_halign = text.align_right)
f_sec(int r, string a) =>
    table.cell(tb, 0, r, a, text_color = C_GOLD, text_size = size.tiny, text_halign = text.align_left)
if barstate.isfirst and showPanel
    table.merge_cells(tb, 0, 0, 2, 0)
    for r in array.from(1, RP, FA)
        table.merge_cells(tb, 0, r, 3, r)
 
// ─────────────────────────────────────────────── today's levels, drawn at the last bar
var array<line>  oLn = array.new<line>()
var array<label> oLb = array.new<label>()
var array<box>   oBx = array.new<box>()
if barstate.islast
    for ln in oLn
        line.delete(ln)
    for lb in oLb
        label.delete(lb)
    for bx in oBx
        box.delete(bx)
    array.clear(oLn)
    array.clear(oLb)
    array.clear(oBx)
    if showPanel
        table.cell(tb, 0, 0, "THE ATLAS", 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
        if showPanel
            f_sec(2, "OPEN A 1, 5, 10, 15 OR 30 MINUTE CHART")
    else
        if showPanel
            table.cell(tb, 0, 1, str.format_time(time_close, "HH:mm", TZ) + " " + str.upper(clockIn) + " · " + str.tostring(math.max(left, 0.0), "0.0") + " HOURS TO THE CLOSE", text_color = C_INK, text_size = size.small, text_halign = text.align_left)
        if showPanel and levelsIn
            // column heads: what each column of the six rows holds
            string chH = chgIn == 60 ? "LAST HOUR" : chgIn % 60 == 0 ? "LAST " + str.tostring(int(chgIn / 60)) + " HOURS" : "LAST " + str.tostring(chgIn) + " MIN"
            table.cell(tb, 0, 2, "LEVEL, BY ODDS OF A TOUCH BEFORE THE CLOSE", text_color = C_GOLD, text_size = size.tiny, text_halign = text.align_left)
            table.cell(tb, 1, 2, "PRICE", text_color = C_GOLD, text_size = size.tiny, text_halign = text.align_right)
            table.cell(tb, 2, 2, "ODDS", text_color = C_GOLD, text_size = size.tiny, text_halign = text.align_center, tooltip = "The odds of a touch before 17:00 New York: a random walk's, graded on this chart's finished days. Hover a figure for the walk's own.")
            table.cell(tb, 3, 2, chH, text_color = C_GOLD, text_size = size.tiny, text_halign = text.align_right, tooltip = "Time running out pulls every level's odds down; price travelling toward a level pulls them up.")
        if not na(atrD) and not na(dBar)
            // levels within 0.03 of the day's ATR of each other (a few dollars on gold) share one line and one label; every one
            // stays in the ledger
            int nL = f_build(close)
            array<float>  gP = array.new<float>()
            array<string> gN = array.new<string>()
            array<bool>   gR = array.new<bool>()
            if nL > 0
                for kL = 0 to nL - 1
                    float pL  = array.get(lvP, kL)
                    int   hitG = -1
                    int   nG0  = array.size(gP)
                    if nG0 > 0
                        for qG = 0 to nG0 - 1
                            if hitG < 0 and math.abs(pL - array.get(gP, qG)) <= math.max(syminfo.mintick, 0.03 * atrD)
                                hitG := qG
                    if hitG >= 0
                        array.set(gN, hitG, array.get(gN, hitG) + " / " + array.get(lvN, kL))
                        array.set(gR, hitG, false)
                    else
                        array.push(gP, pL)
                        array.push(gN, array.get(lvN, kL))
                        array.push(gR, array.get(lvR, kL))
            // ranked by distance from the price; drawn from the far end in, so the near ones sit on top
            int nCv = f_curve()
            int nGr = array.size(gP)
            array<float> gD = array.new<float>()
            if nGr > 0
                for qD = 0 to nGr - 1
                    array.push(gD, math.abs(array.get(gP, qD) - close))
            array<int> byD = array.sort_indices(gD, order.ascending)
            // a level under the odds set in the inputs gets no line: it is named above the top line or under the bottom one
            float  topD = na
            float  botD = na
            float ladTop = na                                // the ladder's ends, so the far lists sit beyond it
            float ladBot = na
            array<string> upA = array.new<string>()       // the far levels, collected far to near
            array<string> dnA = array.new<string>()
            // the ladder, drawn first so the level lines cross it as ticks; it reaches the outer drawn line on each side,
            // or the even-odds price where a side has none, so it never stretches the chart
            if ladIn and sdW > 0
                float upX = na
                float dnX = na
                if nGr > 0
                    for qX = 0 to nGr - 1
                        float pX = array.get(gP, qX)
                        if thrIn == 0 or f_shown(math.abs(pX - close)) * 100 >= thrIn
                            if pX > close
                                upX := na(upX) ? pX - close : math.max(upX, pX - close)
                            else
                                dnX := na(dnX) ? close - pX : math.max(dnX, close - pX)
                float dHalf = f_dAt(0.5, 4.0 * atrD)
                float dFill = na(dHalf) ? 0.15 * atrD : dHalf
                int   xa    = bar_index + 2
                int   xb    = bar_index + 2 + ladW
                for sd = 0 to 1
                    // out to the outer drawn line, and never short of the even-odds rung
                    float ext = math.max(sd == 0 ? nz(upX) : nz(dnX), dFill)
                    if sd == 0
                        ladTop := close + ext
                    else
                        ladBot := close - ext
                    float sg  = sd == 0 ? 1.0 : -1.0
                    color cl  = sd == 0 ? C_BLUE : C_GOLD
                    for k = 0 to 23
                        float d0 = ext * k / LADN
                        float d1 = ext * (k + 1) / LADN
                        float pk = f_shown((d0 + d1) / 2.0)
                        array.push(oBx, box.new(xa, math.max(close + sg * d0, close + sg * d1), xb, math.min(close + sg * d0, close + sg * d1), border_color = color(na), border_width = 0, bgcolor = color.new(cl, 94 - int(math.round(78 * pk * pk)))))
                    // the rungs: the prices this side where the odds shown are 75, 50 and 25 in 100
                    for q in array.from(0.75, 0.5, 0.25)
                        float dq = f_dAt(q, ext)
                        if not na(dq)
                            float yq = close + sg * dq
                            array.push(oLn, line.new(xa, yq, xb, yq, color = color.new(C_INK, 30), width = 1))
                            array.push(oLb, label.new(xa + int(math.floor(ladW / 2.0)), yq, str.tostring(q * 100, "0"), style = label.style_label_center, color = C_BG, textcolor = C_INK, size = size.tiny, tooltip = str.tostring(q * 100, "0") + " in 100 odds of a touch before the close at " + f_px(yq) + ", graded on this chart"))
            if nGr > 0
                for kD = 0 to nGr - 1
                    int    rk  = nGr - 1 - kD
                    int    j   = array.get(byD, rk)
                    float  gpj = array.get(gP, j)
                    float  pwj = f_odds(array.get(gD, j), sdW)
                    float  phj = f_here(pwj)
                    bool   grj = not na(phj)
                    float  prj = grj ? phj : pwj      // the figure shown: graded on this chart once it can be, the walk's until then
                    color  cj  = gpj > close ? C_BLUE : C_GOLD
                    string nmj = array.get(gN, j)
                    string tpj = grj ? "The walk alone: " + str.tostring(pwj * 100, "0") + " in 100. Graded on this chart's finished days: " + str.tostring(prj * 100, "0") + " in 100." : "Not graded on this chart yet: the walk's own figure."
                    // touched today: the first bar of today's gold day whose range reached the level; today's high and low and the
                    // day's open are touched by definition and never stamped
                    int tT = na
                    if tchIn and not str.contains(nmj, "TODAY'S") and not str.contains(nmj, "DAY'S OPEN")
                        int nK = bar_index - dBar
                        for k = nK to 0
                            if low[k] <= gpj and high[k] >= gpj
                                tT := time[k]
                                break
                    bool   tch = not na(tT)
                    string tSt = tch ? " · TOUCHED " + str.format_time(tT, "HH:mm", TZ) : ""
                    string pxj = nmj + (array.get(gR, j) ? "" : " " + f_px(gpj)) + tSt + " · " + f_in100(prj) + (tch ? " AGAIN" : "") + (grj ? "" : " (THE WALK)")
                    // the trajectory: the same level's odds as they stood the set minutes ago, from that bar's price and hours left
                    string chj = "·"
                    color  chc = C_MUTE
                    string cht = "The change needs that many minutes of today's gold day on the chart."
                    int    nBk = math.max(1, int(math.round(chgIn * 60.0 / tfSec)))
                    if bar_index - nBk >= dBar
                        float lt0 = (dEnd - time_close[nBk]) / float(HR)
                        float pw0 = f_odds(math.abs(gpj - close[nBk]), atrD / 1.596 * math.sqrt(math.max(lt0, 0.0) / 23.0))
                        float ph0 = f_here(pw0)
                        float p0  = grj and not na(ph0) ? ph0 : pw0
                        int   dlt = int(math.round(prj * 100)) - int(math.round(p0 * 100))
                        chj := dlt > 0 ? "up " + str.tostring(dlt) : dlt < 0 ? "down " + str.tostring(-dlt) : "no change"
                        chc := dlt > 0 ? C_INK : C_MUTE
                        cht := str.tostring(chgIn) + " minutes ago, with the price at " + f_px(close[nBk]) + " and " + str.tostring(lt0, "0.0") + " hours left, this level showed " + str.tostring(p0 * 100, "0") + " in 100. Time running out pulls every level's odds down; price travelling toward a level pulls them up."
                    string spn = chgIn == 60 ? " IN THE HOUR" : chgIn % 60 == 0 ? " IN " + str.tostring(int(chgIn / 60)) + " HOURS" : " IN " + str.tostring(chgIn) + " MIN"
                    string chL = chj == "·" ? "" : " · " + str.upper(chj) + spn
                    if thrIn == 0 or prj * 100 >= thrIn
                        // brighter and thicker as the odds rise
                        // a touched level: no glow, a dashed and dimmer line
                        if prj >= 0.05 and not tch
                            array.push(oLn, line.new(dBar, gpj, bar_index + XE, gpj, color = color.new(cj, 92 - 12 * prj), width = 2 + int(math.round(8 * prj))))
                        array.push(oLn, line.new(dBar, gpj, bar_index + XE, gpj, color = color.new(cj, tch ? 55 : 78 - 68 * prj), width = tch ? 1 : 1 + int(math.round(2 * prj)), style = tch ? line.style_dashed : line.style_solid))
                        if rk < nearIn
                            array.push(oLb, label.new(bar_index + XE, gpj, pxj + (tch ? "" : chL), style = label.style_label_left, color = color.new(C_BG, 15), textcolor = tch ? color.new(cj, 30) : cj, size = size.small, tooltip = tpj + " " + cht))
                        else
                            array.push(oLb, label.new(bar_index + XE, gpj, nmj + tSt + " · " + str.tostring(prj * 100, "0") + (tch ? " AGAIN" : chj == "·" ? "" : " · " + str.upper(chj)), style = label.style_label_left, color = color(na), textcolor = color.new(cj, 15), size = size.tiny, tooltip = tpj + " " + cht))
                        if gpj > close
                            topD := na(topD) ? gpj : math.max(topD, gpj)
                        else
                            botD := na(botD) ? gpj : math.min(botD, gpj)
                    else if gpj > close
                        if prj >= 0.01
                            array.push(upA, pxj)
                    else
                        if prj >= 0.01
                            array.push(dnA, pxj)
                    if showPanel and levelsIn and rk < 6
                        // the odds cell is shaded in the level's colour, deeper as the odds rise, as the lines glow
                        f_row(3 + rk, nmj, cj, f_px(gpj), C_INK, str.lower(f_in100(prj)), C_INK, chj)
                        table.cell_set_text_color(tb, 3, 3 + rk, chc)
                        table.cell_set_tooltip(tb, 3, 3 + rk, cht)
                        table.cell_set_bgcolor(tb, 2, 3 + rk, color.new(cj, 92 - int(math.round(52 * prj))))
                        table.cell_set_tooltip(tb, 2, 3 + rk, tpj)
            // the far levels, nearest to the lines; a blank line keeps them clear of the outer line's own name
            // the three far levels nearest the lines on each side, 1 in 100 or more; the rest are left off the chart
            string upT = ""
            string dnT = ""
            int    nUA = array.size(upA)
            int    nDA = array.size(dnA)
            if nUA > 0
                for kU = math.max(0, nUA - 3) to nUA - 1
                    upT := upT + array.get(upA, kU) + "\n"
            if nDA > 0
                for kV = nDA - 1 to math.max(0, nDA - 3)
                    dnT := dnT + "\n" + array.get(dnA, kV)
            float yUp = na(topD) ? close + 0.15 * atrD : topD
            float yDn = na(botD) ? close - 0.15 * atrD : botD
            if not na(ladTop)
                yUp := math.max(yUp, ladTop)
            if not na(ladBot)
                yDn := math.min(yDn, ladBot)
            if str.length(upT) > 0
                array.push(oLb, label.new(bar_index + (ladIn ? 2 : XE), yUp, upT + "FURTHER UP\n ", style = label.style_label_lower_left, color = color(na), textcolor = color.new(C_BLUE, 35), size = size.tiny, textalign = text.align_left))
            if str.length(dnT) > 0
                array.push(oLb, label.new(bar_index + (ladIn ? 2 : XE), yDn, " \nFURTHER DOWN" + dnT, style = label.style_label_upper_left, color = color(na), textcolor = color.new(C_GOLD, 35), size = size.tiny, textalign = text.align_left))
            if showPanel and levelsIn and nGr < 6
                for kE = nGr to 5
                    f_row(3 + kE, "", C_MUTE, "", C_INK, "", C_INK, "")
        if showPanel and ledgerIn
            f_sec(RP, "WHAT IT SHOWED, AND WHAT WAS TOUCHED ON THIS CHART · " + str.tostring(nDays) + " DAYS")
            for i = 0 to 4
                float n = array.get(g5N, i)
                f_row(RP + 1 + i, "showed " + str.tostring(i * 20) + " to " + str.tostring((i + 1) * 20), C_MUTE,
                     n > 0 ? "showed " + str.tostring(100 * array.get(g5S, i) / n, "0") : "·", C_INK,
                     n > 0 ? "touched " + str.tostring(100 * array.get(g5C, i) / n, "0") : "", C_INK,
                     n > 0 ? str.tostring(n, "#,##0") + " calls" : "")
            float nGc = array.get(gAcc, 2)
            f_row(RP + 6, "BRIER", C_MUTE, nGc > 0 ? str.tostring(array.get(gAcc, 0) / nGc, "0.000") : "·", C_INK,
                 nGc > 0 ? "walk " + str.tostring(array.get(gAcc, 1) / nGc, "0.000") : "", C_MUTE, str.tostring(nGc, "#,##0") + " calls")
        if showPanel
            // one line of proof: of the bands from 20 to 80, the one holding more calls than the other two
            if not ledgerIn
                int   bB = -1
                float bN = 19.0
                for i = 1 to 3
                    if array.get(g5N, i) > bN
                        bN := array.get(g5N, i)
                        bB := i
                string prf = "ON THIS CHART: TOO FEW GRADED CALLS YET"
                if bB >= 0
                    prf := "ON THIS CHART: SHOWED " + str.tostring(100 * array.get(g5S, bB) / bN, "0") + " IN 100, TOUCHED " + str.tostring(100 * array.get(g5C, bB) / bN, "0") + " · " + str.tostring(bN, "#,##0") + " CALLS"
                table.cell(tb, 0, RP, prf, text_color = C_INK, text_size = size.small, text_halign = text.align_left, tooltip = "Every stamp at 03:00, 10:00 and 14:00 New York is graded at the close. Switch on the full ledger for every band and the Brier.")
            f_sec(FA, "GRADED ON FINISHED DAYS ONLY · CALLS NO DIRECTION")

← 返回列表