Built-ins

Built-in Elements in Pine Script

Introduction

Pine Script is a versatile scripting language developed by TradingView for creating custom technical analysis indicators and strategies. A key feature of Pine Script is its wide array of built-in variables and functions. These are pre-coded elements that enhance efficiency by automatically calculating various technical metrics, thereby freeing you from writing extensive code.

Mastering Pine Script built-ins can significantly extend your scripting capabilities. This guide provides a brief overview of these elements and demonstrates how to effectively leverage them. For a comprehensive understanding of the language, refer to the Pine Script v5 Reference Manual, an indispensable tool for both beginner and expert coders.

Namespace

Built-in variables and functions in Pine Script often belong to the same namespace, indicated by a prefix to their names. For instance, the ta.sma() function resides in the ta namespace, which stands for "technical analysis". A namespace can house both variables and functions.

Some variables also have corresponding function versions. For example:

  • The ta.tr variable computes the "True Range" of the current bar. However, ta.tr(true) function calculates the "True Range" by using high - low if the previous close value needed for computation is not available.

Built-in Variables

Built-in variables serve diverse purposes, including:

  • Price- and volume-related variables: open, high, low, close, hl2, hlc3, ohlc4, volume.

Built-in Functions

Built-in functions in Pine Script can be categorized based on their application:

  • Math-related functions in the math namespace: math.abs(), math.log(), math.max().

  • Technical indicators in the ta namespace: ta.sma(), ta.ema(), ta.macd().

  • Support functions used in technical indicator computations in the ta namespace: ta.barssince(), ta.crossover(), ta.highest().

  • Input definition functions under the input namespace: input(), input.color(), input.int().

  • Indicators functions in the indicator namespace for placing orders: indicator.signal().

Using Built-in Functions

Every built-in function in Pine Script has one or more parameters defined in its signature. Understanding these parameters and their requirements is crucial to correctly utilizing the function.

To illustrate, consider the ta.vwma() function, which computes the volume-weighted moving average of a source value. Its entry in the Reference Manual provides useful information:

  • What the function does.

  • Its signature (or definition): ta.vwma(source, length) β†’ series float.

  • The parameters it includes: source and length.

  • The form and type of the result it returns: β€œseries float”.

  • An example of its usage: plot(ta.vwma(close, 15)).

You can use the parameter names when calling the function. If you want to change the position of arguments, use keyword arguments for all your arguments.

Here's how you can use the ta.vwma() function:

myVwma = ta.vwma(close, 20)

or with keyword arguments:

myVwma = ta.vwma(source = close, length = 20)

Remember, it's vital to ensure that the arguments used match the form and type required by the function.

Understanding Pine Script's type system is key to efficient use of built-in functions. The "ARGUMENTS" section in the Reference Manual entry for each built-in function provides details on the required form-type for each function's parameters.

Last updated