[Builtins]

Dispatch architecture for ta.*, strategy.*, collections, plotting, request.*, and other standard namespaces.

Builtins

Abstract

Pine’s standard library is not a single module: it is a flat qualified-name dispatch table ("ta.sma", "strategy.entry", "array.push", …) assembled from category mixins. Each entry is a handler (args, kwargs?) → value with side effects when the language requires them (orders, plots, drawings, inputs). This hub orients the surface; child pages go deep on technical, strategy, collections, drawing/plotting, and request/input.

Conceptual model

BuiltinEvaluator merges mixin maps in a fixed order and then registers ticker, logging, color, timeframe, and script-declaration functions. The strategy() declaration handler is wrapped so broker settings apply to StrategyState.

Interface surface

Namespace / areaMixin / moduleDocs
ta.*TechnicalAnalysisMixin + technical_submodules/*Technical
strategy.*StrategyBuiltinsMixin, StrategyConstantsMixinStrategy
array.* / matrix.* / map.*ArrayBuiltinsMixin, matrix/map evaluatorsCollections
plot* / hline / line / label / …PlottingFunctionsMixin, DrawingBuiltinsMixinDrawing & plotting
request.* / input.*RequestBuiltinsMixin, InputBuiltinsMixinRequest & input
math.*, min/max, nz, …NumericBuiltinsMixin(this page)
str.*StringBuiltinsMixin(this page)
color.*register_color_functions(this page)
syminfo / ticker.*register_ticker_functions(this page)
timeframe.*register_timeframe_functions(this page)
alert / loggingAlertsMixin, register_logging_functions(this page)
indicator / strategy / libraryregister_script_declaration_functionsLibraries
Footprint / volume_rowFootprintBuiltinsMixinRequest & input

Numeric and string essentials

  • Numeric: math.* constants from BaseEvaluator (math.pi, math.e, golden-ratio helpers); functions cover abs, min/max, rounding, logs, trig, and NA helpers (nz, na predicates via utility).
  • String: str.* formatting, conversion (str.tostring respects format.* constants), and manipulation aligned with common Pine scripts in the builtin corpus.

Color, ticker, timeframe

  • Colors: hex constants (color.red#F23645, etc.) plus composition helpers.
  • syminfo / ticker: host-provided Syminfo object or flat context keys; ticker construction for request.security symbols.
  • timeframe: period flags (isintraday, isdaily, …) default daily in base constants; hosts override from bar spacing.

Alerts

alert() / related helpers record structured messages with optional bar_index / time from context—side channel for hosts, not strategy events.

Internals

PathRole
src/pynescript/ast/evaluator/builtins/__init__.pyBuiltinEvaluator composition
src/pynescript/ast/evaluator/builtins/base.pyDispatch mixin, _call_builtin, registration
src/pynescript/ast/evaluator/builtins/*.pyCategory implementations
scripts/generate_builtin_metadata.pyLSP/metadata surface (not hand-edited JSON)

Handlers are looked up by the fully qualified string built during name/attribute evaluation. Zero-arg series (e.g. strategy.equity) are registered as builtins that ignore empty args.

Invariants & edge cases

  1. One map, many mixins. New builtins require a map entry and (usually) metadata regeneration for LSP.
  2. Kwargs and positionals. Handlers accept both; Pine named arguments arrive as kwargs when the call AST carries them.
  3. Bar mode. Stateful ta.crossover / similar use per-bar call indices reset by the host (_cross_call_i).
  4. Mock fallbacks. request.* without a feed still returns synthetic data so offline evaluation does not hard-fail—hosts must wire real providers for production accuracy.
  5. Compile path subset. Numba builtins reimplement only a fraction of this table; object mode covers more via Python helpers—see Compiler.

Worked example — resolution path

plot(ta.sma(close, 14))
  1. Attribute/call chain builds qualified name ta.sma.
  2. _call_builtin("ta.sma", [close_series, 14]).
  3. Handler coerces series → list, computes SMA, finalizes scalar or list.
  4. plot registers a Plot with that series value and returns the plot id.

Failure modes

SymptomFix
Unknown built-in functionTypo, version surface gap, or failed attribute chain
Always mock pricesWire data_feed / data_provider into evaluator context
LSP knows a function runtime lacksMetadata ahead of implementation—check missing features
Handler arity errorsPositional vs keyword mismatch in script

See also