PINE LIBRARY
Actualizado ContractResolver

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).
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).
Notas de prensa
v2 Added ability to use previous contractsUpdated:
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.
Biblioteca Pine
Fiel al espíritu de TradingView, el autor ha publicado este código de Pine como biblioteca de código abierto, para que otros programadores de nuestra comunidad puedan reutilizarlo. ¡Enhorabuena al autor! Puede usar esta biblioteca de forma privada o en otras publicaciones de código abierto, pero su reutilización en publicaciones está sujeta a nuestras Normas internas.
Exención de responsabilidad
La información y las publicaciones no constituyen, ni deben considerarse como, asesoramiento o recomendaciones financieras, de inversión, de trading u otro tipo, proporcionadas o respaldadas por TradingView. Obtenga más información en Condiciones de uso.
Biblioteca Pine
Fiel al espíritu de TradingView, el autor ha publicado este código de Pine como biblioteca de código abierto, para que otros programadores de nuestra comunidad puedan reutilizarlo. ¡Enhorabuena al autor! Puede usar esta biblioteca de forma privada o en otras publicaciones de código abierto, pero su reutilización en publicaciones está sujeta a nuestras Normas internas.
Exención de responsabilidad
La información y las publicaciones no constituyen, ni deben considerarse como, asesoramiento o recomendaciones financieras, de inversión, de trading u otro tipo, proporcionadas o respaldadas por TradingView. Obtenga más información en Condiciones de uso.