PanosSmtUtils

Panos1389 · library · 176 行 · 点赞 4 · TradingView 原页

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

Pine Script

//@version=6
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at
// https://mozilla.org/MPL/2.0/
// © Panos1389
 
// PanosSmtUtils — small, pure, self-contained helpers factored out of Panos1389's own
// indicators (Sequence°) so they can be shared across scripts and published
// once instead of copy-pasted into every new indicator. No request.security(), no syminfo
// reads beyond what's passed in — every function here is a plain, deterministic string/value
// mapping, safe to call from anywhere (including deep inside loops) with zero extra cost.
library("PanosSmtUtils", overlay = true)
 
// ============================================================================================
// pairFor — correlated-asset symbol lookup for SMT / PSP divergence checks.
// ============================================================================================
// Resolves the OTHER symbol to compare against, given the chart's own bare ticker and exchange
// prefix. This is a fully self-contained registry — no external library, no network call — so
// it has nothing to break if any other library is ever removed. Every pair returned here moves
// in the SAME direction as the chart symbol (a correlated pair, not an inverse one) — that's
// deliberate, so the function's contract stays consistent across every asset class below and
// callers never need to track a separate "invert" flag. It covers four tiers:
//
//   1) CME-style futures roots (index: ES/NQ/YM/RTY + micros; metals: GC/SI + micros; crypto:
//      BTC/ETH + micros; energy: CL/BZ; rates: ZB/ZN) resolve to a FIXED CME/CBOT/COMEX/NYMEX/
//      ICE contract, because these are exchange-listed futures — the pair is the same contract
//      no matter which broker/platform the chart itself is on.
//   2) Broker CFD/forward index naming (NAS100, US500, SPX500, and their "F" forward-contract
//      suffix seen on feeds like Pepperstone) resolves to the SAME exchange prefix as the
//      chart, with only the index root swapped — e.g. PEPPERSTONE:NAS100F -> PEPPERSTONE:US500F
//      — since these are broker-specific feeds, not a single universal contract.
//   3) Gold/Silver spot CFD naming (XAUUSD/XAGUSD) — same exchange-prefix-preserving logic as
//      tier 2, for brokers that quote metals as a spot CFD rather than a GC/SI future.
//   4) FX majors — the two pairings most commonly cited for SMT in ICT/SMC (EURUSD<->GBPUSD,
//      AUDUSD<->NZDUSD). Unlike tiers 1-3, there's no single universal convention for FX SMT
//      pairing the way there is for ES/NQ or GC/SI — treat tier 4 as a starting point and
//      verify it matches how you actually trade before relying on it.
//
// @param ticker The bare ticker/root to resolve, e.g. syminfo.ticker (no exchange prefix).
// @param prefix The chart's own exchange prefix, e.g. syminfo.prefix. Only used for tiers 2-3
//   above (futures pairs in tier 1 and FX pairs in tier 4 always resolve to a fixed ticker).
// @returns The correlated symbol, either EXCHANGE:TICKER (tiers 2-3) or a bare ticker like
//   "NQ1!"/"EURUSD" (tiers 1/4), or "" when nothing in the registry matches.
export pairFor(string ticker, string prefix) =>
    string t = str.upper(ticker)
    string p = ""
 
    // Index futures / micros
    if str.contains(t, "MES")
        p := "MNQ1!"
    else if str.contains(t, "MNQ")
        p := "MES1!"
    else if str.contains(t, "MYM")
        p := "MES1!"
    else if str.contains(t, "M2K")
        p := "MES1!"
    else if str.contains(t, "ES")
        p := "NQ1!"
    else if str.contains(t, "NQ")
        p := "ES1!"
    else if str.contains(t, "YM")
        p := "ES1!"
    else if str.contains(t, "RTY")
        p := "ES1!"
 
    // Crypto futures — micros checked first ("MBT"/"MET" don't share a substring with "BTC"/
    // "ETH" so order doesn't actually matter here, but kept explicit for readability)
    else if str.contains(t, "MBT")
        p := "MET1!"
    else if str.contains(t, "MET")
        p := "MBT1!"
    else if str.contains(t, "BTC")
        p := "ETH1!"
    else if str.contains(t, "ETH")
        p := "BTC1!"
 
    // Metals futures
    else if str.contains(t, "MGC")
        p := "SIL1!"
    else if str.contains(t, "SIL")
        p := "MGC1!"
    else if str.contains(t, "GC")
        p := "SI1!"
    else if str.contains(t, "SI")
        p := "GC1!"
 
    // Energy futures — WTI (NYMEX "CL") <-> Brent (ICE "BZ")
    else if str.contains(t, "CL")
        p := "BZ1!"
    else if str.contains(t, "BZ")
        p := "CL1!"
 
    // Interest-rate futures — 30Y Bond ("ZB") <-> 10Y Note ("ZN")
    else if str.contains(t, "ZB")
        p := "ZN1!"
    else if str.contains(t, "ZN")
        p := "ZB1!"
 
    // Broker CFD / forward index naming — same exchange prefix as the chart, index root swapped
    else if str.contains(t, "NAS100")
        p := prefix + ":" + str.replace(t, "NAS100", "US500")
    else if str.contains(t, "SPX500")
        p := prefix + ":" + str.replace(t, "SPX500", "NAS100")
    else if str.contains(t, "US500")
        p := prefix + ":" + str.replace(t, "US500", "NAS100")
 
    // Gold / Silver spot CFD pairing — same exchange prefix, metal swapped. A separate tier
    // from the GC/SI futures check above since spot CFD tickers (XAUUSD/XAGUSD) never contain
    // "GC" or "SI" as a substring.
    else if str.contains(t, "XAUUSD")
        p := prefix + ":" + str.replace(t, "XAUUSD", "XAGUSD")
    else if str.contains(t, "XAGUSD")
        p := prefix + ":" + str.replace(t, "XAGUSD", "XAUUSD")
 
    // FX majors — the two pairings most commonly cited for SMT in ICT/SMC. No exchange-prefix
    // logic here since spot FX tickers are quoted the same way (EURUSD, GBPUSD, …) across
    // basically every broker/data feed that offers them.
    else if str.contains(t, "EURUSD")
        p := "GBPUSD"
    else if str.contains(t, "GBPUSD")
        p := "EURUSD"
    else if str.contains(t, "AUDUSD")
        p := "NZDUSD"
    else if str.contains(t, "NZDUSD")
        p := "AUDUSD"
 
    p
 
// ============================================================================================
// tablePosition — "vertical + horizontal" dropdown pair -> a table/label position constant.
// ============================================================================================
// Every one of Panos's scripts that offers a "Position" input built from two separate string
// dropdowns (vertical: Top/Middle/Bottom, horizontal: Left/Center/Right — friendlier than
// TradingView's flat 9-item position list) needs this exact mapping. Shared here once instead
// of redefined in each script.
//
// @param vert One of "Top", "Middle", "Bottom".
// @param horz One of "Left", "Center", "Right".
// @returns The matching position.* constant. Defaults to position.bottom_right on anything
//   that doesn't match, so a typo'd/renamed input value never breaks table.new()/label.new().
export tablePosition(string vert, string horz) =>
    switch vert + " " + horz
        "Top Left"      => position.top_left
        "Top Center"    => position.top_center
        "Top Right"     => position.top_right
        "Middle Left"   => position.middle_left
        "Middle Center" => position.middle_center
        "Middle Right"  => position.middle_right
        "Bottom Left"   => position.bottom_left
        "Bottom Center" => position.bottom_center
        "Bottom Right"  => position.bottom_right
        => position.bottom_right
 
// ============================================================================================
// textSize — a friendly size-name dropdown -> a size.* constant.
// ============================================================================================
// @param sizeLabel One of "Tiny", "Small", "Normal", "Large".
// @returns The matching size.* constant. Defaults to size.small on no match.
export textSize(string sizeLabel) =>
    switch sizeLabel
        "Tiny"   => size.tiny
        "Small"  => size.small
        "Normal" => size.normal
        "Large"  => size.large
        => size.small
 
// ============================================================================================
// xor — exactly-one-of-two.
// ============================================================================================
// Shared helper for any SMT-style divergence check: "this side swept and the correlated asset
// didn't, or vice versa — never both, never neither."
//
// @param a First condition.
// @param b Second condition.
// @returns true when exactly one of a/b is true.
export xor(bool a, bool b) =>
    (a and not b) or (not a and b)

← 返回列表