PINE LIBRARY
Updated 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).
Release Notes
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.
Pine library
In true TradingView spirit, the author has published this Pine code as an open-source library so that other Pine programmers from our community can reuse it. Cheers to the author! You may use this library privately or in other open-source publications, but reuse of this code in publications is governed by House Rules.
Disclaimer
The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.
Pine library
In true TradingView spirit, the author has published this Pine code as an open-source library so that other Pine programmers from our community can reuse it. Cheers to the author! You may use this library privately or in other open-source publications, but reuse of this code in publications is governed by House Rules.
Disclaimer
The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.