The Tether [TheSimplifier7]

TheSimplifier7 · study · 672 行 · 点赞 36 · 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 Tether [TheSimplifier7]", shorttitle = "The Tether", overlay = true, max_lines_count = 20, max_labels_count = 60)
 
//  THE TETHER · version 2.2 · 30 October 2026
//  One line that says where the move is proven wrong. Under price it is a floor, over price a ceiling. It hangs from the
//  furthest 2 hour close since it last turned, moves only on a 2 hour close and only toward price, and a 2 hour close
//  through it turns it. A gold knot marks each turn, and the line it broke stays on, dotted, until the next turn.
//  Its room comes from the market's own days at this time of day, each in its own ATR, not from a dial.
//  The tide: the same rule on daily closes, 1.53 ATR from the furthest daily close, drawn as a soft band behind, blue
//  when it runs up, red when it runs down. The line is gold when its leg runs with the tide, pale and fine against it.
//  Two tiers, where they were proven: on metals and FX a leg with the tide gets a wide room and a leg against it a close
//  room, so a counter move is let go sooner. On crypto, indices and oil the two tiers lost to the plain line in the
//  test, so there every leg gets the same room.
//  The light: a glow on the price side of the line that deepens and warms as price comes back from its furthest close
//  toward the line. It is distance, drawn; it is not odds.
//  When the last day's 2 hour closes travelled at least four times as far as they got, the day reads as ranging and the
//  line and its light dim. It describes; it moves nothing. It calls no side.
//  On charts of 2 hours or less, on metals, crypto, FX, indices and oil, the panel prints the odds a stop at the line is
//  touched before this 2 hour block closes, and this chart's own record of them: said against came, over every call
//  graded. The rule was written down before the data was read and graded once per market, from 1 July to 27 October
//  2026 on 30 minute bars: gold, BTCUSD, EURUSD, NAS100, SPX500 and XTIUSD; every band of 200 calls or more held within
//  5 in 100. The rungs' odds did not hold on gold or BTCUSD, so the rungs carry none.
//  Tests: PREREG_TETHER_CALM_27OCT (135c96bd) and PREREG_TETHER_GAUNTLET_28OCT (132bd748).
//  The face of 29 October 2026, as The Fan's draft 6: drawn as the record's own page is drawn. Nothing it reads, counts,
//  grades or alerts moved, and no setting. The panel: the name in plain ink on a raised band, the words small and quiet,
//  the line's price in the figure face and in gold, the only gold in it, and a breath between the parts. On the chart:
//  the line's name and price sit in a faint tag of the line's own colour, pointing at it; the line carries a fine bright
//  core, so it reads as polished gold; a turn's words are in the figure face. The navy is the record's (#0B1A2E).
 
// ─────────────────────────────────────────────── inputs
string gT      = "THE LINE"
string modeIn  = input.string("A day's room, from the clock", "The line's distance", options = ["A day's room, from the clock", "A fixed ATR distance"], group = gT,
     tooltip = "A day's room: at each 2 hour close the line is set where a stop from the furthest close was reached within a day on the chosen share of the earlier days at this time of day, and it only ever moves toward price. A fixed ATR distance: the same distance at every hour.")
string roomIn  = input.string("Normal", "Room", options = ["Close", "Normal", "Wide"], group = gT,
     tooltip = "Used when the tide is off. Close: reached within a day on 30 in 100 of the earlier days. Normal: 20 in 100. Wide: 10 in 100. These are counts from past days that set the distance, not odds the line prints.")
float  kIn     = input.float(0.45, "Fixed distance, in ATR", minval = 0.25, maxval = 3.0, step = 0.05, group = gT,
     tooltip = "The distance when it is set as fixed, above 2 hours, and until a time of day holds 60 earlier day pairs: from the 2 hour close price has run to, in the 14 day ATR of the day before. Above 2 hours it is scaled to the chart's clock: times the square root of the bar's trading hours over 2.")
bool   anchorIn = input.bool(true, "The tide", group = gT,
     tooltip = "The same rule on daily closes, 1.53 ATR from the furthest daily close, moving only on a daily close and only toward price. It sets which way the larger move runs.")
string tierIn  = input.string("Where proven", "Two tiers", options = ["Where proven", "Always", "Never"], group = gT,
     tooltip = "With two tiers, legs that run with the tide get a wide room (reached within a day on 10 in 100 of the earlier days) and legs against it a close room (30 in 100). Where proven: metals and FX, where they beat the plain line in the test. Elsewhere every leg gets the Room above.")
string clockIn = input.string("New York", "Clock", options = ["New York", "London", "Madrid"], group = gT,
     tooltip = "The time zone the times are written in. The day is gold's, 18:00 to 17:00 New York, whatever the clock.")
string gL      = "LOOK"
int    lwIn    = input.int(3, "Line width", minval = 1, maxval = 4, group = gL)
bool   tideIn  = input.bool(true, "Draw the tide", group = gL, tooltip = "The tide's own line, as a soft band behind everything: blue when it runs up, red when it runs down.")
bool   lightIn = input.bool(true, "The light", group = gL, tooltip = "A narrow glow on the price side of the line. It grows, and the line warms, as price comes back from its furthest close toward the line.")
bool   brokeIn = input.bool(true, "The broken line", group = gL,
     tooltip = "At each turn, the level the last leg's line stood at when a 2 hour close went through it, drawn dotted across the new leg until the next turn. Gold if that leg ran with the tide, pale if against it.")
bool   rangeIn = input.bool(true, "Dim on a ranging day", group = gL,
     tooltip = "When the last day's 2 hour closes travelled at least four times as far as they got, the line and its light dim and the panel says so. It describes; it moves nothing.")
bool   rungIn  = input.bool(false, "Rungs", group = gL,
     tooltip = "Five short marks at the right edge dividing the room between the line and the furthest 2 hour close into six. They carry no odds: on gold and BTCUSD their odds did not hold to within 5 in 100.")
int    turnsIn = input.int(0, "Turns marked with words", minval = 0, maxval = 30, group = gL, tooltip = "How many of the last turns carry a short label. Every turn keeps its gold knot.")
string panelIn = input.string("Top right", "Panel", options = ["Top right", "Bottom right", "Bottom left", "Off"], group = gL)
string gO      = "ODDS"
bool   oddsIn  = input.bool(true, "Show the odds", group = gO,
     tooltip = "On metals, crypto, FX, indices and oil, on charts of 2 hours or less: the odds a stop at the line is touched before this 2 hour block closes, counted at each 30 minute close, and this chart's own record of them. Graded once per market on 30 minute bars, 1 July to 27 October 2026: gold, BTCUSD, EURUSD, NAS100, SPX500 and XTIUSD; and on silver's 6 minute bars (24 October 2026).")
int    alIn    = input.int(25, "Alert when a stop at the line reaches, in 100", minval = 1, maxval = 99, group = gO,
     tooltip = "With the odds shown: an alert when the odds a stop at the line is touched before the block closes rise to this many in 100. Price touching the line and a 2 hour close turning it always alert. Create one alert on the script with Any alert() function call.")
int    histIn  = input.int(10000, "30 minute bars to count from", minval = 3000, maxval = 20000, step = 1000, group = gO,
     tooltip = "How many 30 minute bars of history the counts are read from. 10,000 is about 217 days. Each time of day needs 60 earlier days before it prints.")
 
// ─────────────────────────────────────────────── the house palette
color C_GOLD = #E0A93B
color C_BLUE = #8DB0F5                                   // a floor, under price
color C_MUTE = #8A94A6
color C_INK  = #F3EDE2
color C_INK2 = #A8B3C4                                   // the panel's notes, a step under the ink
color C_BG   = #0B1A2E
color C_RAISE = #11233C                                  // the panel's name band
color C_RULE  = #2A4468                                  // the panel's frame
color C_SHEEN = #FFF1C8                                  // the line's bright core
color C_RED  = #E2574C                                   // a ceiling, over price; lines
color C_REDT = #EE7B70                                   // its words, a step lighter so they read on the navy
color C_IVORY = #F6E7C1                                  // the mirror, in the gold look
 
string NY   = "America/New_York"
bool   useRoom = modeIn == "A day's room, from the clock"
float  p24     = roomIn == "Close" ? 0.30 : roomIn == "Wide" ? 0.10 : 0.20
// the market's family, from the symbol. The odds print on the families they were graded on; the two tiers run where they won.
string TKU   = str.upper(syminfo.ticker)
bool   isMetal  = str.contains(TKU, "GOLD") or str.contains(TKU, "SILVER") or str.contains(TKU, "XAU") or str.contains(TKU, "XAG") or str.contains(TKU, "XPT") or str.contains(TKU, "XPD") or str.contains(TKU, "PLATINUM") or str.contains(TKU, "PALLADIUM") or (syminfo.type == "futures" and (syminfo.root == "GC" or syminfo.root == "MGC" or syminfo.root == "SI" or syminfo.root == "SIL" or syminfo.root == "PL" or syminfo.root == "PA"))
bool   isCrypto = not isMetal and (syminfo.type == "crypto" or str.startswith(TKU, "BTC") or str.startswith(TKU, "ETH"))
bool   isFx     = not isMetal and not isCrypto and syminfo.type == "forex"
bool   isIndex  = not isMetal and not isCrypto and not isFx and (syminfo.type == "index" or str.contains(TKU, "SPX") or str.contains(TKU, "US500") or str.contains(TKU, "NAS100") or str.contains(TKU, "NDX") or str.contains(TKU, "US100") or str.contains(TKU, "US30") or str.contains(TKU, "DJI") or str.contains(TKU, "GER40") or str.contains(TKU, "DE40") or str.contains(TKU, "UK100") or str.contains(TKU, "JP225") or (syminfo.type == "futures" and (syminfo.root == "ES" or syminfo.root == "MES" or syminfo.root == "NQ" or syminfo.root == "MNQ" or syminfo.root == "YM" or syminfo.root == "MYM" or syminfo.root == "RTY")))
bool   isOil    = not isMetal and not isCrypto and not isFx and not isIndex and (str.contains(TKU, "XTI") or str.contains(TKU, "WTI") or str.contains(TKU, "USOIL") or str.contains(TKU, "UKOIL") or str.contains(TKU, "XBR") or str.contains(TKU, "BRENT") or (syminfo.type == "futures" and (syminfo.root == "CL" or syminfo.root == "MCL" or syminfo.root == "BZ")))
bool   oddsOn   = isMetal or isCrypto or isFx or isIndex or isOil
string FAM      = isMetal ? "metals" : isCrypto ? "crypto" : isFx ? "FX" : isIndex ? "indices" : isOil ? "oil" : "this market"
bool   tierOk   = tierIn == "Always" or (tierIn == "Where proven" and (isMetal or isFx))
string TZ   = clockIn == "New York" ? NY : clockIn == "London" ? "Europe/London" : "Europe/Madrid"
string CK   = clockIn == "New York" ? "NY" : str.upper(clockIn)
int    NR   = 48                                        // 30 minute rows, 18:00 to 17:30 New York; metals trade to 16:30, so their last two stay empty
int    WIN  = 250                                       // earlier days counted
int    MINP = 60                                        // none printed from fewer
bool   tfLow = timeframe.isintraday and timeframe.in_seconds() <= 1800    // the line set at the close before each bar
bool   tfMid = timeframe.isintraday and timeframe.in_seconds() > 1800 and timeframe.in_seconds() <= 7200   // 1 and 2 hour: the line at each bar's close
bool   tfHi  = not tfLow and not tfMid                                     // 4 hours to monthly, the line on the chart's own closes
bool   tfOk  = tfLow or tfMid or tfHi
// a bar's trading hours (gold trades 23 a day, 115 a week, about 483 a month), and the distance scaled to them
float  hBar  = timeframe.isintraday ? timeframe.in_seconds() / 3600.0 : timeframe.isdaily ? 23.0 * timeframe.multiplier : timeframe.isweekly ? 115.0 * timeframe.multiplier : 483.0 * timeframe.multiplier
float  kEff  = tfHi ? kIn * math.sqrt(hBar / 2.0) : kIn
string CLK   = not tfHi ? "2 HOUR" : timeframe.isintraday ? str.tostring(math.round(hBar)) + " HOUR" : timeframe.isdaily ? (timeframe.multiplier == 1 ? "DAILY" : str.tostring(timeframe.multiplier) + " DAY") : timeframe.isweekly ? (timeframe.multiplier == 1 ? "WEEKLY" : str.tostring(timeframe.multiplier) + " WEEK") : (timeframe.multiplier == 1 ? "MONTHLY" : str.tostring(timeframe.multiplier) + " MONTH")
 
f_px(float p) =>
    math.abs(p) >= 100 ? str.tostring(p, "#,##0.00") : str.tostring(p, format.mintick)
f_d(float x) =>
    str.tostring(x, "#,##0.00")
// the odds as shown are corrected by band, on the chart's own record. Each band of the raw count keeps the sum of
// what it said and how many came, and a call is shown as its raw count times came over said in its band, from calls already
// graded only. The sums start worth 50 of said in each band at the ratio the gold 6 minute pit gave at 0.45 ATR, April 2023 to
// June 2026, so the correction stands from the first bar and the chart's own record takes over as it grows.
f_band(float p) =>
    p < 0.02 ? 0 : p < 0.05 ? 1 : p < 0.10 ? 2 : p < 0.20 ? 3 : p < 0.30 ? 4 : p < 0.50 ? 5 : p < 0.70 ? 6 : 7
f_kTx() =>
    kEff == 1.0 ? "one ATR" : str.tostring(kEff, "0.00") + " ATR"
f_in100(float p) =>
    p < 0.005 ? "UNDER 1 IN 100" : p >= 0.995 ? "OVER 99 IN 100" : str.tostring(math.round(p * 100)) + " IN 100"
 
// ─────────────────────────────────────────────── the engine, run on 30 minute bars
// Each 30 minute bar's row is its open's half hour since 18:00 New York; a 2 hour block ends on rows 3, 7 ... 43, and the
// 16:00 hour on row 45. The line moves only on a block's last close. At every 30 minute close, a call for the block the next
// bar is in: of the earlier days stored, each measured in its own ATR from its close at this row, the share whose low (high)
// went the line's distance before the block's last close, and the share whose close at the block's end was past it. Each
// call is graded at that close. The ATR is the 14 day Wilder ATR of gold's day before, built from these bars. Everything
// returned is the last closed bar's on charts of 30 minutes or less (off 1, with lookahead), so nothing repaints; on the
// 1 and 2 hour charts (off 0, no lookahead) it is the last 30 minute bar inside each chart bar, as it stood at that close.
f_eng(int off) =>
    int   t6  = time + 6 * 3600000
    int   dk  = year(t6, NY) * 10000 + month(t6, NY) * 100 + dayofmonth(t6, NY)
    int   mn  = ((hour(time, NY) - 18 + 24) % 24) * 60 + minute(time, NY)
    int   row = int(mn / 30)
    bool  onG = mn % 30 == 0 and row < NR
    // a market that trades the 17:00 hour (crypto) ends its day on row 47; gold, silver and the rest on row 45, as sealed
    var bool late = false
    if onG and row >= 46
        late := true
    int   lastR = late ? 47 : 45
    // the store: the last 250 days, row by row, high, low and close in the day's own ATR; and today's path
    var array<float> sH  = array.new<float>(NR * WIN, na)
    var array<float> sL  = array.new<float>(NR * WIN, na)
    var array<float> sC  = array.new<float>(NR * WIN, na)
    var array<float> pH  = array.new<float>(NR, na)
    var array<float> pL  = array.new<float>(NR, na)
    var array<float> pC  = array.new<float>(NR, na)
    var int          sHd = 0
    var int          sN  = 0
    var array<float> sA  = array.new<float>(WIN, na)       // the day's ATR, the ruler its rows are stored in
    var array<int>   sQ  = array.new<int>(WIN, -10)        // the day's place in the run of stored days
    var array<bool>  sF  = array.new<bool>(WIN, false)     // every row of the day present
    var array<float> uL  = array.new<float>(NR * WIN, na)  // the lowest low from each row to the day's last
    var array<float> uH  = array.new<float>(NR * WIN, na)  // the highest high from each row to the day's last
    var array<float> vL  = array.new<float>(NR * WIN, na)  // the lowest low from the day's first row to each row
    var array<float> vH  = array.new<float>(NR * WIN, na)
    var int          nQ  = 0
    // the ATR of the day before
    var float dH   = na
    var float dL   = na
    var float dC   = na
    var float pCl  = na
    var int   dKey = -1
    var int   nD   = 0
    var float trS  = 0.0
    var float atrW = na
    var float aPv  = na
    if dk != dKey
        if dKey != -1
            // yesterday's path joins the store, when it had a ruler
            if not na(aPv)
                for q = 0 to NR - 1
                    array.set(sH, sHd * NR + q, array.get(pH, q))
                    array.set(sL, sHd * NR + q, array.get(pL, q))
                    array.set(sC, sHd * NR + q, array.get(pC, q))
                bool whole = true
                for q = 0 to lastR
                    if na(array.get(pL, q)) or na(array.get(pH, q))
                        whole := false
                        break
                array.set(sA, sHd, aPv)
                array.set(sQ, sHd, nQ)
                array.set(sF, sHd, whole)
                nQ += 1
                if whole
                    float mL = na
                    float mH = na
                    for q = lastR to 0
                        mL := na(mL) ? array.get(pL, q) : math.min(mL, array.get(pL, q))
                        mH := na(mH) ? array.get(pH, q) : math.max(mH, array.get(pH, q))
                        array.set(uL, sHd * NR + q, mL)
                        array.set(uH, sHd * NR + q, mH)
                    mL := na
                    mH := na
                    for q = 0 to lastR
                        mL := na(mL) ? array.get(pL, q) : math.min(mL, array.get(pL, q))
                        mH := na(mH) ? array.get(pH, q) : math.max(mH, array.get(pH, q))
                        array.set(vL, sHd * NR + q, mL)
                        array.set(vH, sHd * NR + q, mH)
                sHd := (sHd + 1) % WIN
                sN  := math.min(sN + 1, WIN)
            array.fill(pH, na)
            array.fill(pL, na)
            array.fill(pC, na)
            float tr = na(pCl) ? dH - dL : math.max(dH - dL, math.max(math.abs(dH - pCl), math.abs(dL - pCl)))
            nD += 1
            if nD <= 14
                trS += tr
                if nD == 14
                    atrW := trS / 14.0
            else
                atrW := (atrW * 13.0 + tr) / 14.0
            pCl := dC
        dKey := dk
        dH   := high
        dL   := low
        dC   := close
        aPv  := atrW
    else
        dH := math.max(dH, high)
        dL := math.min(dL, low)
        dC := close
    // the line: side 1 a floor, -1 a ceiling; E the 2 hour close price has run to since it last turned
    var int   side  = 1
    var float E     = na
    var float lineV = na
    var float room  = kIn                                // the line's distance from E, in the ATR of the day before
    var array<float> ends = array.new<float>()           // the last 13 two hour closes: the last day, for the range read
    var float trv   = na                                 // how far those closes travelled, close to close, in ATR
    var int   aSd   = 0                                  // the anchor: the same rule on daily closes; 1 up, -1 down
    var float aEx   = na
    var float aLn   = na
    var int   aT    = na
    var float netv  = na                                 // how far they got, first to last, in ATR
    var int   tTurn = na
    var float pTurn = na
    // the calls waiting for their block's last close: the line, its side, what they said, the low or high since, the close they end on
    var array<float> qLn = array.new<float>()
    var array<float> qSd = array.new<float>()
    var array<float> qPh = array.new<float>()
    var array<float> qPt = array.new<float>()
    var array<float> qMx = array.new<float>()
    var array<int>   qKy = array.new<int>()
    var array<float> qPs = array.new<float>()           // the touch odds as shown, for the record on the panel
    // the record: the touch odds as shown and the close odds raw, what they said and what came
    var float hN = 0.0
    var float hS = 0.0
    var float hY = 0.0
    var float uN = 0.0
    var float uS = 0.0
    var float uY = 0.0
    // the correction's sums by band: said and came, for the touch and for the close
    var array<float> kS = array.new<float>(8, 50.0)
    var array<float> kY = array.from(57.1, 36.95, 40.15, 40.1, 43.9, 45.1, 47.6, 49.8)
    var array<float> tS = array.new<float>(8, 50.0)
    var array<float> tY = array.from(45.2, 40.35, 42.8, 43.2, 48.5, 46.0, 48.0, 47.0)
    float ph  = na
    float pt  = na
    int   be  = na
    float g1  = na                                       // the rungs' odds, 1 nearest the line; -1 when price is past the rung
    float g2  = na
    float g3  = na
    float g4  = na
    float g5  = na
    if onG and not na(aPv) and aPv > 0
        array.set(pH, row, high / aPv)
        array.set(pL, row, low / aPv)
        array.set(pC, row, close / aPv)
        int  key   = dk * 100 + row
        bool isEnd = row % 4 == 3 or row == 45
        // the waiting calls: their low or high since, then graded on their block's last close; any left from a gap dropped
        int m = array.size(qLn)
        if m > 0
            for j = m - 1 to 0
                float sd = array.get(qSd, j)
                float mx = array.get(qMx, j)
                array.set(qMx, j, na(mx) ? (sd > 0 ? low : high) : sd > 0 ? math.min(mx, low) : math.max(mx, high))
                int ky = array.get(qKy, j)
                if ky <= key
                    if ky == key
                        float ln = array.get(qLn, j)
                        float x  = array.get(qMx, j)
                        float r1 = array.get(qPh, j)
                        float r2 = array.get(qPt, j)
                        float y1 = (sd > 0 ? x <= ln : x >= ln) ? 1.0 : 0.0
                        float y2 = (sd > 0 ? close < ln : close > ln) ? 1.0 : 0.0
                        hN += 1.0
                        hS += array.get(qPs, j)
                        hY += y1
                        uN += 1.0
                        uS += r2
                        uY += y2
                        int b1 = f_band(r1)
                        int b2 = f_band(r2)
                        array.set(kS, b1, array.get(kS, b1) + r1)
                        array.set(kY, b1, array.get(kY, b1) + y1)
                        array.set(tS, b2, array.get(tS, b2) + r2)
                        array.set(tY, b2, array.get(tY, b2) + y2)
                    array.remove(qLn, j)
                    array.remove(qSd, j)
                    array.remove(qPh, j)
                    array.remove(qPt, j)
                    array.remove(qMx, j)
                    array.remove(qKy, j)
                    array.remove(qPs, j)
        // the line moves on a block's last close only
        if na(E)
            E := open
            lineV := E - kIn * aPv
        float lnNow = useRoom ? lineV : side == 1 ? E - kIn * aPv : E + kIn * aPv
        bool  turned = false
        if isEnd
            if side == 1 and close < lnNow
                side  := -1
                E     := close
                tTurn := time_close
                pTurn := close
                turned := true
            else if side == -1 and close > lnNow
                side  := 1
                E     := close
                tTurn := time_close
                pTurn := close
                turned := true
            else
                E := side == 1 ? math.max(E, close) : math.min(E, close)
            // version 2: the room for a day. For each stored day followed by the next, both whole: the furthest a stop
            // from this row's close was reached from here to the day's last row, then on the next day to this row, in the
            // first day's ATR. The room is that excursion's (1 - p) point; the line moves to it only if that is toward price.
            if useRoom
                array<float> xs = array.new<float>()
                for k = 0 to WIN - 1
                    int k2 = (k + 1) % WIN
                    if array.get(sF, k) and array.get(sF, k2) and array.get(sQ, k) >= 0 and array.get(sQ, k2) == array.get(sQ, k) + 1
                        float xRf = array.get(sC, k * NR + row)
                        if not na(xRf)
                            float ra = array.get(sA, k2) / array.get(sA, k)
                            float a1 = row + 1 <= lastR ? (side == 1 ? array.get(uL, k * NR + row + 1) : array.get(uH, k * NR + row + 1)) : na
                            float a2 = (side == 1 ? array.get(vL, k2 * NR + row) : array.get(vH, k2 * NR + row)) * ra
                            float ex = na(a1) ? a2 : side == 1 ? math.min(a1, a2) : math.max(a1, a2)
                            if not na(ex)
                                array.push(xs, side == 1 ? xRf - ex : ex - xRf)
                int nX = array.size(xs)
                if nX >= MINP
                    array.sort(xs, order.ascending)
                    float pu  = anchorIn and tierOk and aSd != 0 ? (side == aSd ? 0.10 : 0.30) : p24
                    float pos = (1.0 - pu) * (nX - 1)
                    int   iLo = math.floor(pos)
                    int   iHi = math.min(iLo + 1, nX - 1)
                    room := math.max(0.05, array.get(xs, iLo) + (pos - iLo) * (array.get(xs, iHi) - array.get(xs, iLo)))
                else
                    room := kIn
                float cand = side == 1 ? E - room * aPv : E + room * aPv
                lineV := turned ? cand : side == 1 ? math.max(lineV, cand) : math.min(lineV, cand)
        if not useRoom
            lineV := side == 1 ? E - kIn * aPv : E + kIn * aPv
        // the range read: a day of 2 hour closes that travelled far and got nowhere is a ranging day
        if isEnd
            array.push(ends, close)
            if array.size(ends) > 13
                array.shift(ends)
            if array.size(ends) == 13
                float tv = 0.0
                for q = 1 to 12
                    tv += math.abs(array.get(ends, q) - array.get(ends, q - 1))
                trv  := tv / aPv
                netv := math.abs(array.get(ends, 12) - array.get(ends, 0)) / aPv
        // the anchor moves on the day's last close only, toward price only, 1.53 ATR from the furthest daily close
        if anchorIn and row == lastR
            if aSd == 0
                aSd := 1
                aEx := close
                aLn := aEx - 1.53 * aPv
                aT  := time_close
            else if (aSd == 1 and close < aLn) or (aSd == -1 and close > aLn)
                aSd := -aSd
                aEx := close
                aLn := aSd == 1 ? aEx - 1.53 * aPv : aEx + 1.53 * aPv
                aT  := time_close
            else
                aEx := aSd == 1 ? math.max(aEx, close) : math.min(aEx, close)
                float cA = aSd == 1 ? aEx - 1.53 * aPv : aEx + 1.53 * aPv
                aLn := aSd == 1 ? math.max(aLn, cA) : math.min(aLn, cA)
        // at the day's last close, the line takes tomorrow's ATR, so it does not step at the next 18:30 close
        if row == lastR
            float trT = na(pCl) ? dH - dL : math.max(dH - dL, math.max(math.abs(dH - pCl), math.abs(dL - pCl)))
            float aNx = nD + 1 < 14 ? na : nD + 1 == 14 ? (trS + trT) / 14.0 : (atrW * 13.0 + trT) / 14.0
            if not na(aNx) and aNx > 0
                if useRoom
                    float cN = side == 1 ? E - room * aNx : E + room * aNx
                    lineV := side == 1 ? math.max(lineV, cN) : math.min(lineV, cN)
                else
                    lineV := side == 1 ? E - kIn * aNx : E + kIn * aNx
        // this close's call, for the block the next bar is in
        if row < lastR
            int   nb   = math.min(int((row + 1) / 4), 11)
            int   bEnd = row >= 45 ? 47 : nb == 11 ? 45 : nb * 4 + 3
            float d    = side == 1 ? (close - lineV) / aPv : (lineV - close) / aPv
            int   nU   = 0
            int   kH   = 0
            int   kT   = 0
            array<float> xsC = array.new<float>()
            if sN > 0
                for j = 0 to sN - 1
                    int   b0  = j * NR
                    float ref = array.get(sC, b0 + row)
                    float ce  = array.get(sC, b0 + bEnd)
                    if not na(ref) and not na(ce)
                        float ext = na
                        bool  ok  = true
                        for q = row + 1 to bEnd
                            float v = side == 1 ? array.get(sL, b0 + q) : array.get(sH, b0 + q)
                            if na(v)
                                ok := false
                                break
                            ext := na(ext) ? v : side == 1 ? math.min(ext, v) : math.max(ext, v)
                        if ok
                            nU += 1
                            array.push(xsC, side == 1 ? ref - ext : ext - ref)
                            kH += (side == 1 ? ref - ext : ext - ref) >= d ? 1 : 0
                            kT += (side == 1 ? ref - ce : ce - ref) >= d ? 1 : 0
            be := time_close + (bEnd - row) * 1800000
            if nU >= MINP
                float rH = float(kH) / nU                                 // the raw counts
                float rT = float(kT) / nU
                int   c1 = f_band(rH)
                int   c2 = f_band(rT)
                ph := math.min(0.99, rH * array.get(kY, c1) / array.get(kS, c1))   // as shown: corrected by its band's record
                pt := math.min(0.99, rT * array.get(tY, c2) / array.get(tS, c2))
                array.push(qLn, lineV)
                array.push(qSd, float(side))
                array.push(qPh, rH)
                array.push(qPt, rT)
                array.push(qMx, na)
                array.push(qKy, dk * 100 + bEnd)
                array.push(qPs, ph)
                // the rungs: k sixths of the way from the line to the furthest close, counted at their own distance
                for kk = 1 to 5
                    float rp = lineV + kk / 6.0 * (E - lineV)
                    float dR = side == 1 ? (close - rp) / aPv : (rp - close) / aPv
                    float gv = -1.0
                    if dR > 0
                        int cnt = 0
                        for xv in xsC
                            cnt += xv >= dR ? 1 : 0
                        float rr = float(cnt) / nU
                        int   cb = f_band(rr)
                        gv := math.min(0.99, rr * array.get(kY, cb) / array.get(kS, cb))
                    if kk == 1
                        g1 := gv
                    else if kk == 2
                        g2 := gv
                    else if kk == 3
                        g3 := gv
                    else if kk == 4
                        g4 := gv
                    else
                        g5 := gv
    float ln_ = onG ? lineV : na
    int   sd_ = onG ? side : na
    float cl_ = onG ? close : na
    int   tc_ = onG ? time_close : na
    float a_  = onG ? aPv : na
    float e_  = onG ? E : na                             // the close the line hangs from, for its envelope; nothing in the engine reads it
    float rm_ = onG ? room : na
    [ln_[off], sd_[off], tTurn[off], pTurn[off], cl_[off], tc_[off], a_[off], ph[off], pt[off], be[off], hN[off], hS[off], hY[off], uN[off], uS[off], uY[off], e_[off], rm_[off], g1[off], g2[off], g3[off], g4[off], g5[off], trv[off], netv[off], aLn[off], aSd[off], aT[off]]
 
int OFF = tfLow ? 1 : 0
[ln0r, sd0r, tt0r, pt0r, c0r, t0r, a0r, ph0r, pu0, be0r, hN0, hS0, hY0, uN0, uS0, uY0, e0r, rm0r, g10, g20, g30, g40, g50, tv0r, nt0r, aL0r, aS0r, aT0r] = request.security(syminfo.tickerid, "30", f_eng(OFF), lookahead = tfLow ? barmerge.lookahead_on : barmerge.lookahead_off, calc_bars_count = histIn)
 
// above 2 hours, the same rule on the chart's own closes. The ATR(14) of the day before; the distance scaled to the
// chart's clock. It moves only on a closed bar, so the open bar shows the line as the last close left it.
float aD  = request.security(syminfo.tickerid, "D", ta.atr(14)[1], lookahead = barmerge.lookahead_on)
var int   hSd = 1
var float hE  = na
var float hLn = na
var int   hTt = na
var float hPt = na
if tfHi and barstate.isconfirmed and not na(aD) and aD > 0
    if na(hE)
        hE := open
    float lnN = hSd == 1 ? hE - kEff * aD : hE + kEff * aD
    if hSd == 1 and close < lnN
        hSd := -1
        hE  := close
        hTt := time_close
        hPt := close
    else if hSd == -1 and close > lnN
        hSd := 1
        hE  := close
        hTt := time_close
        hPt := close
    else
        hE := hSd == 1 ? math.max(hE, close) : math.min(hE, close)
    hLn := hSd == 1 ? hE - kEff * aD : hE + kEff * aD
float ln0 = tfHi ? hLn : ln0r
int   sd0 = tfHi ? (na(hLn) ? na : hSd) : sd0r
int   tt0 = tfHi ? hTt : tt0r
float pt0 = tfHi ? hPt : pt0r
float c0  = tfHi ? close : c0r
int   t0  = tfHi ? time_close : t0r
float a0  = tfHi ? aD : a0r
float ph0 = tfHi or not oddsOn ? na : ph0r
int   be0 = tfHi ? time_close : be0r
float e0  = tfHi ? hE : e0r
float rm0 = tfHi or not useRoom ? kEff : rm0r
f_dTx() =>
    na(rm0) ? "" : rm0 == 1.0 ? "one ATR" : str.tostring(rm0, "0.00") + " ATR"
 
 
f_hm(int t) =>
    str.format_time(t, "HH:mm", TZ) + " " + CK
f_dm(int t) =>
    str.format_time(t, "d MMM", TZ)
 
bool live   = tfOk and not na(ln0) and not na(c0)
bool oddsSh = oddsIn and oddsOn and not tfHi
 
// ─────────────────────────────────────────────── the face
// One line, gold with the tide and pale against it; a narrow light on its price side that grows as price leans back toward
// it; the tide as a soft band behind; a gold knot at each turn. Nothing is filled over the candles.
color C_GOLDHI = #FFE29A                                 // the line at its warmest, price leaning on it
color C_PALE   = #E6D7B4                                 // a leg against the tide
bool  flip = tfOk and not na(sd0) and not na(sd0[1]) and sd0 != sd0[1]
float lnP  = live ? ln0 : na
float tv0  = tfHi ? na : tv0r
float nt0  = tfHi ? na : nt0r
bool  rng  = rangeIn and not na(tv0) and tv0 > 0 and nt0 / tv0 < 0.25
float aL0  = tfHi or not anchorIn ? na : aL0r
int   aS0  = tfHi or not anchorIn or na(aS0r) or aS0r == 0 ? na : aS0r
bool  ctr  = not na(aS0) and not na(sd0) and sd0 != aS0          // a leg against the tide
bool  aFl  = not na(aS0) and not na(aS0[1]) and aS0 != aS0[1]
// the pull: how far price has come back from the furthest close toward the line. 0 at that close, 1 at the line or past it
float spanV = live and not na(e0) ? math.abs(e0 - ln0) : na
float distV = live ? (sd0 == 1 ? close - ln0 : ln0 - close) : na
float pRaw  = na(spanV) or spanV <= 0 or na(distV) ? 0.0 : math.max(0.0, math.min(1.0, 1.0 - distV / spanV))
float pull  = ta.ema(pRaw, 4)
float dimF  = rng ? 0.55 : 1.0
 
// the tide, behind everything
color cTide = aS0 == 1 ? C_BLUE : C_RED
float tideV = tideIn and live and not na(aL0) ? aL0 : na
plot(tideV, "The tide, haze", color = aFl ? color(na) : color.new(cTide, 93), linewidth = 12, style = plot.style_steplinebr, display = display.pane, editable = false)
plot(tideV, "The tide", color = aFl ? color(na) : color.new(cTide, 80), linewidth = 3, style = plot.style_steplinebr, display = display.pane)
 
// the broken line: at each turn, the level the last leg's line stood at when a 2 hour close went through it, carried dotted
// across the new leg until the next turn
var float brk  = na
var bool  brkC = false
if flip and not na(lnP[1])
    brk  := lnP[1]
    brkC := ctr[1]
plot(brokeIn and live and not flip ? brk : na, "The broken line", color = color.new(brkC ? C_PALE : C_GOLD, rng ? 60 : 40), linewidth = 1,
     style = plot.style_steplinebr, linestyle = plot.linestyle_dotted, display = display.pane)
 
// the light: from the line toward price, a quarter of a day's ATR deep, brighter as the pull grows
float deep  = live and not na(a0) ? (0.15 + 0.45 * pull) * a0 : na
float hEdge = lightIn and live and not na(deep) ? ln0 + sd0 * deep : na
pEd = plot(hEdge, "The light's edge", color = color.new(C_INK, 100), style = plot.style_stepline, display = display.none, editable = false)
pLh = plot(lightIn ? lnP : na, "The line, for the light", color = color.new(C_INK, 100), style = plot.style_stepline, display = display.none, editable = false)
float hAmp = (0.06 + 0.62 * math.pow(pull, 1.3)) * dimF * (ctr ? 0.5 : 1.0)
color cOn  = flip ? color(na) : color.new(C_GOLD, 100.0 - 100.0 * hAmp)
color cOff = flip ? color(na) : color.new(C_GOLD, 100)
fill(pLh, pEd, math.max(lnP, hEdge), math.min(lnP, hEdge), sd0 == 1 ? cOff : cOn, sd0 == 1 ? cOn : cOff, title = "The light", fillgaps = false)
 
// the line
color cHot = color.from_gradient(pull, 0.0, 1.0, C_GOLD, C_GOLDHI)
float gTr  = 100.0 - 100.0 * math.min(1.0, (0.12 + 0.33 * math.pow(pull, 1.5)) * dimF)
plot(not ctr ? lnP : na, "The Tether, glow", color = flip ? color(na) : color.new(cHot, gTr), linewidth = lwIn + 6, style = plot.style_steplinebr, display = display.pane, editable = false)
plot(not ctr ? lnP : na, "The Tether", color = flip ? color(na) : color.new(cHot, rng ? 35 : 0), linewidth = lwIn, style = plot.style_steplinebr)
plot(ctr ? lnP : na, "The Tether, glow against the tide", color = flip ? color(na) : color.new(C_PALE, 92), linewidth = lwIn + 3, style = plot.style_steplinebr, display = display.pane, editable = false)
plot(ctr ? lnP : na, "The Tether, against the tide", color = flip ? color(na) : color.new(C_PALE, rng ? 45 : 10), linewidth = lwIn > 1 ? lwIn - 1 : 1, style = plot.style_steplinebr)
plotshape(flip and not na(pt0) ? pt0 : na, "A knot, its halo", shape.circle, location.absolute, color = color.new(C_GOLDHI, rng ? 88 : 78), size = size.small, display = display.pane, editable = false)
plotshape(flip and not na(pt0) ? pt0 : na, "A knot: a 2 hour close turned the line", shape.circle, location.absolute, color = color.new(C_GOLDHI, rng ? 45 : 0), size = size.tiny)
// the face of 29 October: a fine bright core along the line, so it reads as polished gold. Declared after every earlier
// plot, so the styles already saved on a chart keep their places.
plot(not ctr ? lnP : na, "The Tether, its core", color = flip ? color(na) : color.new(C_SHEEN, rng ? 75 : 45), linewidth = 1, style = plot.style_steplinebr, display = display.pane, editable = false)
 
// turns, in words, when asked for
var array<label> tl = array.new<label>()
if flip and not na(pt0)
    if turnsIn > 0
        array.push(tl, label.new(time, pt0, "TURNED " + f_hm(tt0) + " · " + f_px(pt0), xloc = xloc.bar_time,
             style = sd0[1] == 1 ? label.style_label_up : label.style_label_down, color = color.new(C_BG, 100), textcolor = color.new(C_MUTE, 15), size = size.tiny, text_font_family = font.family_monospace,
             tooltip = "A " + str.lower(CLK) + " close through the line turned it here."))
        if array.size(tl) > turnsIn
            label.delete(array.shift(tl))
    if barstate.isrealtime
        alert("THE TETHER · THE LINE TURNED ON THE " + f_hm(tt0) + " CLOSE · " + f_px(pt0) + " · IT NOW SITS " + (sd0 == 1 ? "UNDER" : "OVER") + " PRICE AT " + f_px(ln0), alert.freq_once_per_bar)
 
// ─────────────────────────────────────────────── touched since the last 30 minute close
var int   tTch = na
var float lPrv = na
var int   bPrv = na
if tfOk and not na(be0) and (na(bPrv) or na(lPrv) or be0 != bPrv or ln0 != lPrv)
    bPrv := be0
    lPrv := ln0
    tTch := na
if live and not tfHi and not na(t0) and time >= t0 and na(tTch) and (sd0 == 1 ? low <= ln0 : high >= ln0)
    tTch := time
    if barstate.isrealtime
        alert("THE TETHER · PRICE TOUCHED THE " + (sd0 == 1 ? "FLOOR " : "CEILING ") + f_px(ln0) + " · " + f_hm(time), alert.freq_once_per_bar)
var float phPv = na
if oddsSh and tfOk and not na(t0) and t0 != t0[1]
    if barstate.isrealtime and not na(ph0) and ph0 * 100.0 >= alIn and (na(phPv) or phPv * 100.0 < alIn)
        alert("THE TETHER · A STOP AT THE " + (sd0 == 1 ? "FLOOR " : "CEILING ") + f_px(ln0) + " · " + f_in100(ph0) + " IT IS REACHED BY " + f_hm(be0), alert.freq_once_per_bar)
    phPv := ph0
 
// ─────────────────────────────────────────────── drawn at the last bar: one level, one word
string nm = sd0 == 1 ? "FLOOR" : "CEILING"
var line  xl = na
var label xb = na
var array<line> rgx = array.new<line>()
if barstate.islast
    line.delete(xl)
    label.delete(xb)
    for l_ in rgx
        line.delete(l_)
    array.clear(rgx)
    if live
        int xA = tfHi or na(t0) ? time : t0
        int xB = tfHi or na(be0) ? time + 2 * timeframe.in_seconds() * 1000 : be0
        color lc = ctr ? C_PALE : cHot
        xl := line.new(xA, ln0, xB, ln0, xloc = xloc.bar_time, color = color.new(lc, 30), style = line.style_dotted, width = 2)
        xb := label.new(xB, ln0, " " + nm + "  " + f_px(ln0) + " ", xloc = xloc.bar_time, style = label.style_label_left, color = color.new(ctr ? C_PALE : C_GOLD, 84),
             textcolor = ctr ? C_PALE : C_GOLDHI, size = size.normal, text_font_family = font.family_monospace,
             tooltip = "Where this move is proven wrong: a " + str.lower(CLK) + " close through this line turns it. It moves only on a " + str.lower(CLK) + " close and only toward price.")
        if rungIn and not na(e0)
            for kk = 1 to 5
                float rp = ln0 + kk / 6.0 * (e0 - ln0)
                array.push(rgx, line.new(xA, rp, xB, rp, xloc = xloc.bar_time, color = color.new(C_GOLD, 70), style = line.style_solid, width = 1))
 
// ─────────────────────────────────────────────── the panel: the line, the tide, the odds and counsel's first line
// The face of 29 October 2026: the name on a raised band, a breath after it, the line and the tide, a breath, the odds and
// their record, a breath, counsel. The rows, their words and their colours of meaning are as they were.
var table tb = table.new(panelIn == "Top right" ? position.top_right : panelIn == "Bottom right" ? position.bottom_right : position.bottom_left, 3, 10,
     bgcolor = color.new(C_BG, 4), frame_color = C_RULE, frame_width = 1, border_color = color.new(C_BG, 4), border_width = 2)
f_row(int r, string a, string b, color cb, string c, string fb) =>
    table.cell(tb, 0, r, a, text_color = C_MUTE, text_size = size.small, text_halign = text.align_left)
    table.cell(tb, 1, r, "  " + b, text_color = cb, text_size = size.normal, text_halign = text.align_left, text_font_family = fb)
    table.cell(tb, 2, r, "  " + c, text_color = C_INK2, text_size = size.small, text_halign = text.align_right)
f_gap(int r) =>
    table.cell(tb, 0, r, " ", text_size = size.tiny)
if barstate.isfirst
    table.merge_cells(tb, 0, 9, 2, 9)
if panelIn != "Off" and barstate.islast
    table.clear(tb, 0, 1, 2, 8)
    table.cell(tb, 0, 0, "THE TETHER", text_color = C_INK, bgcolor = C_RAISE, text_size = size.normal, text_halign = text.align_left)
    table.cell(tb, 1, 0, "", bgcolor = C_RAISE, text_size = size.tiny)
    table.cell(tb, 2, 0, syminfo.ticker + " · " + str.lower(CLK) + " line" + (tierOk and anchorIn ? " · two tiers" : ""), text_color = C_MUTE, bgcolor = C_RAISE, text_size = size.tiny, text_halign = text.align_right, text_font_family = font.family_monospace)
    f_gap(1)
    if not live
        table.cell(tb, 0, 2, "Counting: the line needs 14 days for its ATR.", text_color = C_MUTE, text_size = size.small, text_halign = text.align_left)
    else
        float dNow = math.abs(close - ln0)
        f_row(2, nm, f_px(ln0), ctr ? C_PALE : C_GOLDHI, not na(tTch) ? "touched " + f_hm(tTch) : f_d(dNow) + (sd0 == 1 ? " under price" : " above price"), font.family_monospace)
        if not na(aS0)
            f_row(3, "THE TIDE", (aS0 == 1 ? "Up" : "Down") + (na(aT0r) ? "" : " since " + f_dm(aT0r)), aS0 == 1 ? C_BLUE : C_REDT, ctr ? "this leg runs against it" : "this leg runs with it", font.family_default)
        if oddsIn and not oddsOn and not tfHi
            f_gap(4)
            f_row(5, "A STOP AT THE LINE", "no odds here", C_MUTE, "graded on metals, crypto, FX, indices and oil", font.family_default)
        if oddsSh
            f_gap(4)
            bool noCall = na(be0) or na(ph0)
            f_row(5, "A STOP AT THE LINE", noCall ? "counted again at the next close" : str.lower(f_in100(ph0)) + " by " + f_hm(be0), noCall ? C_MUTE : C_INK, "", font.family_default)
            bool noRec = na(hN0) or hN0 < 1
            f_row(6, "ITS RECORD", noRec ? "graded when its block closes" : str.tostring(100.0 * hS0 / hN0, "0.0") + " said, " + str.tostring(100.0 * hY0 / hN0, "0.0") + " came", noRec ? C_MUTE : C_INK,
                 noRec ? "" : "across " + str.tostring(hN0, "#,###") + " calls graded here", font.family_default)
        if rng
            f_row(7, "THE LAST DAY", "Ranging", C_MUTE, "the line dims until it moves", font.family_default)
    f_gap(8)
    table.cell(tb, 0, 9, "This is structural analysis. It is not a recommendation to buy or sell.", text_color = color.new(C_MUTE, 25), text_size = size.tiny, text_halign = text.align_left)

← 返回列表