ORPanels

martinmr777_ · library · 383 行 · 点赞 0 · TradingView 原页

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

Pine Script

//@version=6
 
// @description Panel/table renderers for the Opening Range indicator family, factored out of the
//              host to relieve its compiled-token budget (CE10117). The host owns all state and
//              inputs; it marshals them into the config/state UDTs below and calls the exported
//              renderers. This library is pure presentation — it reads no host globals (Pine
//              forbids that), creates/mutates the table passed to it, and returns nothing.
//
//   Scope (2026-07): the DEBUG table only. The status table stays in the host (it is the client's
//   primary panel and Phase 6 modifies it). `writeRow` is duplicated here on purpose — the host
//   keeps its own copy for the still-in-host status table.
//
//   Usage (host):
//     import Mr_NSFT/ORPanels/1 as orp
//     var orp.DebugConfig dbgCfg = orp.DebugConfig.new(...)          // from inputs, once
//     var orp.DbgLayout   dbgL   = makeDbgLayout()                   // host builds it (feeds table.new rows)
//     var table dbgTable = makeDebugTable(dbgL.total)                // host owns table.new
//     if i_showDebugTable and not na(dbgTable) and barstate.isconfirmed
//         orp.renderDebug(dbgTable, dbgL, dbgCfg, buildDebugState())  // buildDebugState() -> DebugState
library("ORPanels")
 
// ═══════════════════════════════════════════════════════════════════════════════════════════
// Types
// ═══════════════════════════════════════════════════════════════════════════════════════════
 
// @type Debug-table layout: total row count + per-section presence flags and row offsets.
//       Built host-side (its `total` feeds the host's `table.new` rows, which is qualifier-
//       sensitive), held in a host `var`, and passed to `renderDebug`.
export type DbgLayout
    int    total
    string txt
    int    rowCont
    bool   secBreak
    int    rowBreak
    bool   secConf
    int    rowConf
    bool   secMom
    int    rowMom
    bool   secAccept
    int    rowAccept
    bool   secRetest
    int    rowRetest
    bool   secPassive
    int    rowPassive
    int    rowInflHdr
    bool   secInfl
    int    rowInfl
    int    rowVelHdr
    bool   secVel
    int    rowVel
 
// @type Debug-table config: the input-derived, session-constant values the renderer needs
//       (colors, visibility toggles, per-section debug toggles, signal-count limits).
export type DebugConfig
    color  txtColor
    color  bgColor
    string txtSize
    bool   showDebug
    bool   showCont
    bool   showInfl
    bool   showVel
    int    maxInit
    int    contMax
    int    inflMax
    int    velMax
    bool   dbgBreakBull
    bool   dbgBreakBear
    bool   dbgConfBull
    bool   dbgConfBear
    bool   dbgMomBull
    bool   dbgMomBear
    bool   dbgAcceptBull
    bool   dbgAcceptBear
    bool   dbgRetestBull
    bool   dbgRetestBear
    bool   dbgAboveBull
    bool   dbgAboveBear
    bool   dbgInflBull
    bool   dbgInflBear
    bool   dbgVelBull
    bool   dbgVelBear
 
// @type Debug-table state: the per-bar engine values displayed. Mostly raw (the renderer does the
//       toggle-gating and formatting); `contStateBull`/`contStateBear` are pre-resolved host-side
//       because the host's `stateLabel` maps the state int via host-only constants.
export type DebugState
    bool   noVolData
    bool   setupInitAllowed
    int    orStart
    float  vahPrice
    float  valPrice
    string contStateBull
    string contStateBear
    int    contBullSignals
    int    contBearSignals
    int    inflBullSignals
    int    inflBearSignals
    int    velBullSignals
    int    velBearSignals
    string dbgBullBreakEvt
    string dbgBearBreakEvt
    string dbgBullBreakClose
    string dbgBearBreakClose
    string dbgBullConfEvt
    string dbgBearConfEvt
    string dbgBullConfClose
    string dbgBearConfClose
    string dbgBullConfBrkClose
    string dbgBearConfBrkClose
    string dbgBullMomEvt
    string dbgBearMomEvt
    string dbgBullMomC1
    string dbgBearMomC1
    string dbgBullMomC2
    string dbgBearMomC2
    string dbgBullMomC3
    string dbgBearMomC3
    string dbgBullAcceptCount
    string dbgBearAcceptCount
    string dbgBullAcceptEvt
    string dbgBearAcceptEvt
    string dbgBullAcceptTrack
    string dbgBearAcceptTrack
    string dbgBullRetestAtt
    string dbgBearRetestAtt
    string dbgBullRetestEvt
    string dbgBearRetestEvt
    string dbgBullRetestExtr
    string dbgBearRetestExtr
    string dbgBullRetestClose
    string dbgBearRetestClose
    string dbgBullPassiveEvt
    string dbgBearPassiveEvt
    string dbgBullInflEvt
    string dbgBearInflEvt
    string dbgBullInflLevel
    string dbgBearInflLevel
    string dbgBullInflBreak
    string dbgBearInflBreak
    string dbgBullInflClose
    string dbgBearInflClose
    string dbgBullInflBody
    string dbgBearInflBody
    string dbgVelBullEvt
    string dbgVelBearEvt
    string dbgVelState
    string dbgVelER
    string dbgVelEscapeER
    string dbgVelRange
    string dbgVelBody
    string dbgVelEscBull
    string dbgVelEscBear
    string dbgVelConfBull
    string dbgVelConfBear
 
// ═══════════════════════════════════════════════════════════════════════════════════════════
// Setup (exported)
// ═══════════════════════════════════════════════════════════════════════════════════════════
 
// @function  Build the debug-table layout (row total + per-section presence flags and offsets)
//            from the config. Row offsets are a running accumulator in physical row order.
//            The host holds the result in a `var` and feeds its `total` to `table.new`'s rows.
// @param c   The debug config.
// @returns   A `DbgLayout`.
export makeDbgLayout(DebugConfig c) =>
    // ── Section presence flags ────────────────────────────────────────────────────
    bool secBreak   = c.showCont and (c.dbgBreakBull  or c.dbgBreakBear)
    bool secConf    = c.showCont and (c.dbgConfBull   or c.dbgConfBear)
    bool secMom     = c.showCont and (c.dbgMomBull    or c.dbgMomBear)
    bool secAccept  = c.showCont and (c.dbgAcceptBull or c.dbgAcceptBear)
    bool secRetest  = c.showCont and (c.dbgRetestBull or c.dbgRetestBear)
    bool secPassive = c.showCont and (c.dbgAboveBull  or c.dbgAboveBear)
    bool secInfl    = c.showInfl and (c.dbgInflBull or c.dbgInflBear)
    bool secVel     = c.showVel and (c.dbgVelBull  or c.dbgVelBear)
 
    // ── Row counts per section ────────────────────────────────────────────────────
    int N_ALWAYS       = 2  // Column header + Init Window
    int N_CONT         = c.showCont ? 3 : 0  // Cont.State + Cont.Signals + Level
    int N_BREAK        = 3  // Section header + Event + Close
    int N_CONF         = 4  // Section header + Event + Close + Break Close
    int N_MOM          = 5  // Section header + Event + C1 + C2 + C3
    int N_ACCEPT       = 4  // Section header + Count + Event + Tracked High/Low
    int N_RETEST       = 5  // Section header + Attempt + Event + Low/High + Close
    int N_PASSIVE      = 2  // Section header + Event
    int N_DBG_INFL_HDR = c.showInfl ? 2 : 0 // Section header + Signals
    int N_DBG_INFL     = 5 // Event + Level + Break + Close + Body
    int N_DBG_VEL_HDR  = c.showVel ? 2 : 0 // Section header + Vel. Signals
    int N_DBG_VEL      = 6 // Event + State + ER/EscapeER + Range/Body + EscapeLvl + Conf
 
    // ── Row start offsets (na when section is inactive) ───────────────────────────
    int acc = N_ALWAYS
    int rowCont = c.showCont ? acc : na
    acc := acc + N_CONT
    int rowBreak = secBreak ? acc : na
    acc := acc + (secBreak ? N_BREAK : 0)
    int rowConf = secConf ? acc : na
    acc := acc + (secConf ? N_CONF : 0)
    int rowMom = secMom ? acc : na
    acc := acc + (secMom ? N_MOM : 0)
    int rowAccept = secAccept ? acc : na
    acc := acc + (secAccept ? N_ACCEPT : 0)
    int rowRetest = secRetest ? acc : na
    acc := acc + (secRetest ? N_RETEST : 0)
    int rowPassive = secPassive ? acc : na
    acc := acc + (secPassive ? N_PASSIVE : 0)
    int rowInflHdr = c.showInfl ? acc : na
    acc := acc + N_DBG_INFL_HDR
    int rowInfl = secInfl ? acc : na
    acc := acc + (secInfl ? N_DBG_INFL : 0)
    int rowVelHdr = c.showVel ? acc : na
    acc := acc + N_DBG_VEL_HDR
    int rowVel = secVel ? acc : na
    acc := acc + (secVel ? N_DBG_VEL : 0)
    DbgLayout.new(
         total = acc, txt = c.txtSize,
         rowCont = rowCont, secBreak = secBreak, rowBreak = rowBreak,
         secConf = secConf, rowConf = rowConf, secMom = secMom, rowMom = rowMom,
         secAccept = secAccept, rowAccept = rowAccept,
         secRetest = secRetest, rowRetest = rowRetest,
         secPassive = secPassive, rowPassive = rowPassive,
         rowInflHdr = rowInflHdr, secInfl = secInfl, rowInfl = rowInfl,
         rowVelHdr = rowVelHdr, secVel = secVel, rowVel = rowVel)
 
// ═══════════════════════════════════════════════════════════════════════════════════════════
// Internal
// ═══════════════════════════════════════════════════════════════════════════════════════════
 
// Write a 3-column table row (own copy — the host keeps its own for the status table).
writeRow(table t, int r, string l1, string l2, string l3, color tc, color bg, string ts) =>
    table.cell(t, 0, r, l1, text_color = tc, bgcolor = bg, text_size = ts, text_halign = text.align_left)
    table.cell(t, 1, r, l2,  text_color = tc, bgcolor = bg, text_size = ts)
    table.cell(t, 2, r, l3,  text_color = tc, bgcolor = bg, text_size = ts)
 
// ═══════════════════════════════════════════════════════════════════════════════════════════
// Exported renderer
// ═══════════════════════════════════════════════════════════════════════════════════════════
 
// @function   Render the debug table (verbatim behavior of the host's former `renderDebugTable`).
//             The host gates the call (`i_showDebugTable and not na(t) and barstate.isconfirmed`);
//             the internal guard is kept as defense.
// @param t    The debug table (host-owned; created via `table.new`).
// @param lay  The debug layout (host-built `DbgLayout`).
// @param c    The debug config (input-derived).
// @param s    The per-bar debug state.
export renderDebug(table t, DbgLayout lay, DebugConfig c, DebugState s) =>
    if c.showDebug and not na(t) and barstate.isconfirmed
        // Show warning if data volume is not available for this chart instead of rendering the table
        if s.noVolData
            table.clear(t, 0, 0, 2, lay.total - 1)
            table.cell(t, 0, 1, "Volume data unavailable for this symbol/feed.\nValue Area cannot be calculated.",
                text_color = c.txtColor, bgcolor = c.bgColor, text_size = size.normal, text_halign = text.align_center)
        else
            // ── Row 0: Column headers ─────────────────────────────────────────────────
            writeRow(t, 0, "Phase / Field", "▲  BULLISH", "▼  BEARISH", c.txtColor, c.bgColor, lay.txt)
 
            // Row 1: Init Window — from host state + config.
            string _initWinStr = c.maxInit == 0 ? "NO LIMIT" : s.setupInitAllowed and not na(s.orStart) ? "OPEN" : "CLOSED"
            writeRow(t, 1, "Init Window", _initWinStr, _initWinStr, c.txtColor, c.bgColor, lay.txt)
 
            if c.showCont
                // ── Rows 2-4: Always-visible state block ──────────────────────────────────
                // Row 2: Cont. State (pre-resolved host-side).
                writeRow(t, lay.rowCont, "Cont. State", s.contStateBull, s.contStateBear, c.txtColor, c.bgColor, lay.txt)
 
                // Row 3: Cont. Signals (count / max)
                string bullSig = na(s.vahPrice) ? "—" : str.tostring(s.contBullSignals) + " / " + str.tostring(c.contMax)
                string bearSig = na(s.valPrice) ? "—" : str.tostring(s.contBearSignals) + " / " + str.tostring(c.contMax)
                writeRow(t, lay.rowCont + 1, "Cont. Signals", bullSig, bearSig, c.txtColor, c.bgColor, lay.txt)
 
                // Row 4: Active level prices
                string bullLvl = na(s.vahPrice) ? "VAH: —" : "VAH: " + str.tostring(s.vahPrice, format.mintick)
                string bearLvl = na(s.valPrice) ? "VAL: —" : "VAL: " + str.tostring(s.valPrice, format.mintick)
                writeRow(t, lay.rowCont + 2, "Level", bullLvl, bearLvl, c.txtColor, c.bgColor, lay.txt)
 
                // ── Break section ─────────────────────────────────────────────────────────
                if lay.secBreak
                    writeRow(t, lay.rowBreak, "── BREAK ──", "────", "────", c.txtColor, c.bgColor, lay.txt)
                    writeRow(t, lay.rowBreak + 1, "Event", c.dbgBreakBull ? s.dbgBullBreakEvt   : "—", c.dbgBreakBear ? s.dbgBearBreakEvt   : "—",
                    c.txtColor, c.bgColor, lay.txt)
                    writeRow(t, lay.rowBreak + 2, "Close", c.dbgBreakBull ? s.dbgBullBreakClose : "—", c.dbgBreakBear ? s.dbgBearBreakClose : "—",
                    c.txtColor, c.bgColor, lay.txt)
 
                // ── Confirmation section ──────────────────────────────────────────────────
                if lay.secConf
                    writeRow(t, lay.rowConf, "── CONFIRM ──", "────", "────", c.txtColor, c.bgColor, lay.txt)
                    writeRow(t, lay.rowConf + 1, "Event", c.dbgConfBull ? s.dbgBullConfEvt      : "—", c.dbgConfBear ? s.dbgBearConfEvt      : "—",
                    c.txtColor, c.bgColor, lay.txt)
                    writeRow(t, lay.rowConf + 2, "Close", c.dbgConfBull ? s.dbgBullConfClose    : "—", c.dbgConfBear ? s.dbgBearConfClose    : "—",
                    c.txtColor, c.bgColor, lay.txt)
                    writeRow(t, lay.rowConf + 3, "Break Close", c.dbgConfBull ? s.dbgBullConfBrkClose : "—", c.dbgConfBear ? s.dbgBearConfBrkClose : "—",
                    c.txtColor, c.bgColor, lay.txt)
 
                // ── Momentum section ──────────────────────────────────────────────────────
                if lay.secMom
                    writeRow(t, lay.rowMom, "── MOMENTUM ──", "────", "────", c.txtColor, c.bgColor, lay.txt)
                    writeRow(t, lay.rowMom + 1, "Event", c.dbgMomBull ? s.dbgBullMomEvt : "—", c.dbgMomBear ? s.dbgBearMomEvt : "—",
                    c.txtColor, c.bgColor, lay.txt)
                    writeRow(t, lay.rowMom + 2, "C1", c.dbgMomBull ? s.dbgBullMomC1 : "—", c.dbgMomBear ? s.dbgBearMomC1 : "—",
                    c.txtColor, c.bgColor, lay.txt)
                    writeRow(t, lay.rowMom + 3, "C2", c.dbgMomBull ? s.dbgBullMomC2 : "—", c.dbgMomBear ? s.dbgBearMomC2 : "—",
                    c.txtColor, c.bgColor, lay.txt)
                    writeRow(t, lay.rowMom + 4, "C3", c.dbgMomBull ? s.dbgBullMomC3 : "—", c.dbgMomBear ? s.dbgBearMomC3 : "—",
                    c.txtColor, c.bgColor, lay.txt)
 
                // ── Acceptance section ────────────────────────────────────────────────────
                if lay.secAccept
                    writeRow(t, lay.rowAccept,     "── ACCEPT ──", "────", "────", c.txtColor, c.bgColor, lay.txt)
                    writeRow(t, lay.rowAccept + 1, "Count",  c.dbgAcceptBull ? s.dbgBullAcceptCount : "—", c.dbgAcceptBear ? s.dbgBearAcceptCount : "—",
                    c.txtColor, c.bgColor, lay.txt)
                    writeRow(t, lay.rowAccept + 2, "Event",  c.dbgAcceptBull ? s.dbgBullAcceptEvt   : "—", c.dbgAcceptBear ? s.dbgBearAcceptEvt   : "—",
                    c.txtColor, c.bgColor, lay.txt)
                    writeRow(t, lay.rowAccept + 3, "Track H/L", c.dbgAcceptBull ? s.dbgBullAcceptTrack : "—", c.dbgAcceptBear ? s.dbgBearAcceptTrack : "—",
                    c.txtColor, c.bgColor, lay.txt)
 
                // ── Retest section ────────────────────────────────────────────────────────
                if lay.secRetest
                    writeRow(t, lay.rowRetest, "── RETEST ──", "────", "────", c.txtColor, c.bgColor, lay.txt)
                    writeRow(t, lay.rowRetest + 1, "Attempt", c.dbgRetestBull ? s.dbgBullRetestAtt   : "—", c.dbgRetestBear ? s.dbgBearRetestAtt   : "—",
                    c.txtColor, c.bgColor, lay.txt)
                    writeRow(t, lay.rowRetest + 2, "Event", c.dbgRetestBull ? s.dbgBullRetestEvt   : "—", c.dbgRetestBear ? s.dbgBearRetestEvt   : "—",
                    c.txtColor, c.bgColor, lay.txt)
                    writeRow(t, lay.rowRetest + 3, "Low / High", c.dbgRetestBull ? s.dbgBullRetestExtr  : "—", c.dbgRetestBear ? s.dbgBearRetestExtr  : "—",
                    c.txtColor, c.bgColor, lay.txt)
                    writeRow(t, lay.rowRetest + 4, "Close", c.dbgRetestBull ? s.dbgBullRetestClose : "—", c.dbgRetestBear ? s.dbgBearRetestClose : "—",
                    c.txtColor, c.bgColor, lay.txt)
 
                // ── Passive section ───────────────────────────────────────────────────────
                if lay.secPassive
                    writeRow(t, lay.rowPassive, "── PASSIVE ──", "────", "────", c.txtColor, c.bgColor, lay.txt)
                    writeRow(t, lay.rowPassive + 1, "Event", c.dbgAboveBull ? s.dbgBullPassiveEvt : "—", c.dbgAboveBear ? s.dbgBearPassiveEvt : "—",
                    c.txtColor, c.bgColor, lay.txt)
 
            // ── Inflection section ────────────────────────────────────────────────────────
            if c.showInfl
                writeRow(t, lay.rowInflHdr,     "── INFLECTION ──", "────", "────",
                    c.txtColor, c.bgColor, lay.txt)
 
                // Infl. Signals (count / max)
                string bullInflSig = na(s.vahPrice) ? "—" : str.tostring(s.inflBullSignals) + " / " + str.tostring(c.inflMax)
                string bearInflSig = na(s.valPrice) ? "—" : str.tostring(s.inflBearSignals) + " / " + str.tostring(c.inflMax)
                writeRow(t, lay.rowInflHdr + 1, "Inflection Signals", bullInflSig, bearInflSig,
                    c.txtColor, c.bgColor, lay.txt)
 
                if lay.secInfl
                    writeRow(t, lay.rowInfl, "Event",
                    c.dbgInflBull ? s.dbgBullInflEvt   : "—", c.dbgInflBear ? s.dbgBearInflEvt   : "—",
                    c.txtColor, c.bgColor, lay.txt)
                    writeRow(t, lay.rowInfl + 1, "Level",
                        c.dbgInflBull ? s.dbgBullInflLevel : "—", c.dbgInflBear ? s.dbgBearInflLevel : "—",
                        c.txtColor, c.bgColor, lay.txt)
                    writeRow(t, lay.rowInfl + 2, "Break",
                        c.dbgInflBull ? s.dbgBullInflBreak : "—", c.dbgInflBear ? s.dbgBearInflBreak : "—",
                        c.txtColor, c.bgColor, lay.txt)
                    writeRow(t, lay.rowInfl + 3, "Close",
                        c.dbgInflBull ? s.dbgBullInflClose : "—", c.dbgInflBear ? s.dbgBearInflClose : "—",
                        c.txtColor, c.bgColor, lay.txt)
                    writeRow(t, lay.rowInfl + 4, "Body",
                        c.dbgInflBull ? s.dbgBullInflBody  : "—", c.dbgInflBear ? s.dbgBearInflBody  : "—",
                        c.txtColor, c.bgColor, lay.txt)
 
            // ── Velocity section ────────────────────────────────────────────────────────
            if c.showVel
                writeRow(t, lay.rowVelHdr, "── VELOCITY ──", "────", "────",
                    c.txtColor, c.bgColor, lay.txt)
                string bullVelSig = str.tostring(s.velBullSignals) + " / " + str.tostring(c.velMax)
                string bearVelSig = str.tostring(s.velBearSignals) + " / " + str.tostring(c.velMax)
                writeRow(t, lay.rowVelHdr + 1, "Vel. Signals", bullVelSig, bearVelSig,
                    c.txtColor, c.bgColor, lay.txt)
                if lay.secVel
                    writeRow(t, lay.rowVel, "Event",
                        c.dbgVelBull ? s.dbgVelBullEvt : "—", c.dbgVelBear ? s.dbgVelBearEvt : "—",
                        c.txtColor, c.bgColor, lay.txt)
                    writeRow(t, lay.rowVel + 1, "State", s.dbgVelState, s.dbgVelState,
                        c.txtColor, c.bgColor, lay.txt)
                    writeRow(t, lay.rowVel + 2, "ER / Escape ER", s.dbgVelER, s.dbgVelEscapeER,
                        c.txtColor, c.bgColor, lay.txt)
                    writeRow(t, lay.rowVel + 3, "Range / Body", s.dbgVelRange, s.dbgVelBody,
                        c.txtColor, c.bgColor, lay.txt)
                    writeRow(t, lay.rowVel + 4, "Escape Lvl", s.dbgVelEscBull, s.dbgVelEscBear,
                        c.txtColor, c.bgColor, lay.txt)
                    writeRow(t, lay.rowVel + 5, "Conf", s.dbgVelConfBull, s.dbgVelConfBear,
                        c.txtColor, c.bgColor, lay.txt)
    true

← 返回列表