PINE LIBRARY
已更新

ContractResolver

132
Library "ContractResolver"
Classify a TradingView symbol (stock / crypto / future / etc.) and, for
continuous futures, resolve the real front-month contract ticker with a
4-digit year (e.g. "MNQ1!" -> "MNQU2026"). Prefers the built-in
syminfo.current_contract (authoritative, roll-aware) and falls back to a
calendar estimate only when current_contract is na.

monthCode(m)
  Month number (1-12) -> futures month-code letter. Returns na if out of range.
  Parameters:
    m (int): Month number, 1-12.
  Returns: Single-letter month code, or na.

monthNumber(code)
  Month-code letter -> month number (1-12). Returns na if not a valid code.
  Parameters:
    code (string): Single-letter month code (case-insensitive).
  Returns: Month number 1-12, or na.

resolve(ticker, exchange, symType, currentContract, refTime, rollDay)
  Resolve a symbol into its asset class and, for continuous futures, the
front-month contract ticker. Pass syminfo.current_contract for an exact,
roll-aware result; if it is na, a calendar estimate is used (approximated=true).
  Parameters:
    ticker (string): Symbol without exchange prefix (e.g. syminfo.ticker).
    exchange (string): Exchange / prefix (e.g. syminfo.prefix).
    symType (string): syminfo.type ("stock","crypto","futures","forex","fund","index","dr"...).
    currentContract (string): syminfo.current_contract (na when chart is not a continuous future).
    refTime (int): Reference time (ms) for the estimate fallback; 0 -> use timenow.
    rollDay (int): Day-of-month threshold for rolling to the next contract in the fallback.
  Returns: A Contract object.

resolveChart(rollDay)
  Convenience wrapper: resolve the chart's own symbol from its syminfo.* fields.
  Parameters:
    rollDay (int): Day-of-month threshold used only by the estimate fallback.
  Returns: A Contract object for the current chart symbol.

format(c, pattern)
  Build a custom string from a resolved Contract using a placeholder pattern.
Placeholders: {root} {mc} {m} {mm} {yyyy} {yy} {ticker} {exch} {class}
e.g. format(c, "{exch}:{root}{mc}{yyyy}") -> "CME_MINI:MNQU2026"
  Parameters:
    c (Contract): A Contract (typically from resolve / resolveChart).
    pattern (string): Template string containing any of the placeholders above.
  Returns: The pattern with placeholders substituted (missing fields -> "").

Contract
  Parsed/resolved symbol.
  Fields:
    ticker (series string): Original ticker without exchange prefix (e.g. "MNQ1!").
    exchange (series string): Exchange / prefix (e.g. "CME_MINI").
    assetClass (series string): "stock" | "crypto" | "future" | "forex" | "index" | "fund" | "other".
    isContinuous (series bool): True when the ticker is a continuous future (ends with "!").
    root (series string): Futures root (e.g. "MNQ"); na for non-futures.
    monthCode (series string): Futures month letter (F,G,H,J,K,M,N,Q,U,V,X,Z); na for non-futures.
    contractMonth (series int): Contract month 1-12; na for non-futures.
    contractYear (series int): 4-digit contract year; na for non-futures.
    resolved (series string): Final ticker: real contract for futures, unchanged for stock/crypto/other.
    approximated (series bool): True when month/year were estimated by calendar (not from current_contract).
版本注释
v2 Added ability to use previous contracts

Updated:
resolve(ticker, exchange, symType, currentContract, refTime, rollDay, previous)
  Resolve a symbol into its asset class and, for continuous futures, the
front-month contract ticker. Pass syminfo.current_contract for an exact,
roll-aware result; if it is na, a calendar estimate is used (approximated=true).
  Parameters:
    ticker (string): Symbol without exchange prefix (e.g. syminfo.ticker).
    exchange (string): Exchange / prefix (e.g. syminfo.prefix).
    symType (string): syminfo.type ("stock","crypto","futures","forex","fund","index","dr"...).
    currentContract (string): syminfo.current_contract (na when chart is not a continuous future).
    refTime (int): Reference time (ms) for the estimate fallback; 0 -> use timenow.
    rollDay (int): Day-of-month threshold for rolling to the next contract in the fallback.
    previous (bool): When true, return the previous contract (one step back in the cycle) instead of the front month.
  Returns: A Contract object.

resolveChart(rollDay, previous)
  Convenience wrapper: resolve the chart's own symbol from its syminfo.* fields.
  Parameters:
    rollDay (int): Day-of-month threshold used only by the estimate fallback.
    previous (bool): When true, return the previous contract instead of the front month.
  Returns: A Contract object for the current chart symbol.

免责声明

这些信息和出版物并非旨在提供,也不构成TradingView提供或认可的任何形式的财务、投资、交易或其他类型的建议或推荐。请阅读使用条款了解更多信息。