WWW Tradingview Com Pine Script Docs Language Methods #Methods...
WWW Tradingview Com Pine Script Docs Language Methods #Methods...
Execution model
Methods Built-in methods
User-defined methods
Time series Notice! This page contains advanced material. If you are a beginning Pine Script™ Method overloading
programmer, we recommend you become familiar with other, more accessible Pine
Advanced example
Script structure Script™ features before you venture here.
Identifiers
Operators Introduction
Variable declarations Pine Script™ methods are specialized functions associated with values of specific built-in
types, user-defined types, or enum types. They behave the same as regular functions in most
Conditional structures regards while offering a shorter, more convenient syntax. Users can access methods using dot
notation syntax on variables of the associated type, similar to accessing the fields of a Pine
Loops
Script™ object.
Type system
Built-ins
Built-in methods
Pine Script™ includes built-in methods for all special types, including array, matrix, map, line,
User-defined functions
linefill, box, polyline, label, and table. These methods provide users with a more concise way
Objects to call specialized routines for these types within their scripts.
Enums
When using these special types, the expressions:
Arrays
and:
Matrices
<objectName>.<functionName>(…)
Maps
array.get(id, index)
FAQ
Error messages to get the value from an array id at the specified index , we can simply use:
id.get(index)
Migration guides
Where can I get more information? to achieve the same effect. This notation eliminates the need for users to reference the
function’s namespace, as get() is a method of id in this context.
Written below is a practical example to demonstrate the usage of built-in methods in place of
functions.
The following script computes Bollinger Bands over a specified number of prices sampled once
every n bars. It calls array.push() and array.shift() to queue sourceInput values through the
sourceArray , then array.avg() and array.stdev() to compute the sampleMean and sampleDev .
The script then uses these values to calculate the highBand and lowBand , which it plots on
the chart along with the sampleMean :
Pine Script™
//@version=6
indicator("Custom Sample BB", overlay = true)
// Calculate bands.
float highBand = sampleMean + sampleDev
float lowBand = sampleMean - sampleDev
Let’s rewrite this code to utilize methods rather than built-in functions. In this version, we
have replaced all built-in array.* functions in the script with equivalent methods:
Pine Script™
//@version=6
indicator("Custom Sample BB", overlay = true)
Note that:
We call the array methods using sourceArray.* rather than referencing the array
namespace.
We do not include sourceArray as a parameter when we call the methods since they
already reference the object.
User-defined methods
Pine Script™ allows users to define custom methods for use with objects of any built-in or
user-defined type. Defining a method is essentially the same as defining a function, but with
two key differences:
Let’s apply user-defined methods to our previous Bollinger Bands example to encapsulate
operations from the global scope, which will simplify the code and promote reusability. See
this portion from the example:
Pine Script™
We will start by defining a simple method to queue values through an array in a single call.
This maintainQueue() method invokes the push() and shift() methods on a srcArray when
takeSample is true and returns the object:
Pine Script™
Page 1 of 4
srcArray.push(value)
srcArray.shift()
srcArray
Note that:
Pine Script™
From here, we will further simplify our code by defining a method that handles all Bollinger
Band calculations within its scope.
This calcBB() method invokes the avg() and stdev() methods on a srcArray to update mean
and dev values when calculate is true. The method uses these values to return a tuple
containing the basis, upper band, and lower band values respectively:
Pine Script™
With this method, we can now remove Bollinger Band calculations from the global scope and
improve code readability:
Pine Script™
// Update the queue and compute new BB values on each new sample.
[sampleMean, highBand, lowBand] = sourceArray.maintainQueue(sourceInput, newSample).calcBB(m
Note that:
Rather than using an if block in the global scope, we have defined a newSample
variable that is only true once every n bars. The maintainQueue() and calcBB()
methods use this value for their respective takeSample and calculate parameters.
Since the maintainQueue() method returns the object that it references, we’re able to
call calcBB() from the same line of code, as both methods apply to array<float>
instances.
Here is how the full script example looks now that we’ve applied our user-defined methods:
Pine Script™
//@version=6
indicator("Custom Sample BB", overlay = true)
// Update the queue and compute new BB values on each new sample.
[sampleMean, highBand, lowBand] = sourceArray.maintainQueue(sourceInput, newSample).calcBB(m
Method overloading
User-defined methods can override and overload existing built-in and user-defined methods
with the same identifier. This capability allows users to define multiple routines associated
with different parameter signatures under the same method name.
As a simple example, suppose we want to define a method to identify a variable’s type. Since
we must explicitly specify the type of object associated with a user-defined method, we will
need to define overloads for each type that we want it to recognize.
Pine Script™
Now we can use these overloads to inspect some variables. This script uses str.format() to
format the results from calling the getType() method on five different variables into a single
results string, then displays the string in the lbl label using the built-in set_text() method:
Pine Script™
//@version=6
indicator("Type Inspection")
a = 1
b = 1.0
c = true
d = color.white
e = "1"
Note that:
The underlying type of each variable determines which overload of getType() the
compiler will use.
The method will append “(na)” to the output string when a variable is na to demarcate
Page 2 of 4
that it is empty.
Advanced example
Let’s apply what we’ve learned to construct a script that estimates the cumulative
distribution of elements in an array, meaning the fraction of elements in the array that are
less than or equal to any given value.
There are many ways in which we could choose to tackle this objective. For this example, we
will start by defining a method to replace elements of an array, which will help us count the
occurrences of elements within a range of values.
Written below is an overload of the built-in fill() method for array<float> instances. This
overload replaces elements in a srcArray within the range between the lowerBound and
upperBound with an innerValue , and replaces all elements outside the range with an
outerValue :
Pine Script™
With this method, we can filter an array by value ranges to produce an array of occurrences.
For example, the expression:
Pine Script™
copies the srcArray object, replaces all elements between min and val with 1.0, then
replaces all elements above val with 0.0. From here, it’s easy to estimate the output of the
cumulative distribution function at the val , as it’s simply the average of the resulting array:
Pine Script™
Note that:
The compiler will only use this fill() overload instead of the built-in when the user
provides innerValue , outerValue , lowerBound , and upperBound arguments in the call.
If either lowerBound or upperBound is na , its value is ignored while filtering the fill
range.
We are able to call copy() , fill() , and avg() successively on the same line of code
because the first two methods return an array<float> instance.
We can now use this to define a method that will calculate our empirical distribution values.
The following eCDF() method estimates a number of evenly spaced ascending steps from
the cumulative distribution function of a srcArray and pushes the results into a cdfArray :
Pine Script™
Lastly, to ensure that our eCDF() method functions properly for arrays containing small and
large values, we will define a method to normalize our arrays.
This featureScale() method uses array min() and range() methods to produce a rescaled copy
of a srcArray . We will use this to normalize our arrays prior to invoking the eCDF() method:
Pine Script™
// @function Rescales the elements within a `srcArray` to the interval [0, 1].
// @param srcArray (array<float>) Array to normalize.
// @returns (array<float>) Normalized copy of the `srcArray`.
method featureScale(array<float> srcArray) =>
float min = srcArray.min()
float rng = srcArray.range()
array<float> scaledArray = array.new<float>()
// Push normalized `element` values into the `scaledArray`.
for element in srcArray
scaledArray.push((element - min) / rng)
scaledArray
Note that:
This method does not include special handling for divide by zero conditions. If rng is 0,
the value of the array element will be na .
The full example below queues a sourceArray of size length with sourceInput values using
our previous maintainQueue() method, normalizes the array’s elements using the
featureScale() method, then calls the eCDF() method to get an array of estimates for n
evenly spaced steps on the distribution. The script then calls a user-defined makeLabel()
function to display the estimates and prices in a label on the right side of the chart:
Pine Script™
//@version=6
indicator("Empirical Distribution", overlay = true)
// @function Rescales the elements within a `srcArray` to the interval [0, 1].
// @param srcArray (array<float>) Array to normalize.
// @returns (array<float>) Normalized copy of the `srcArray`.
method featureScale(array<float> srcArray) =>
float min = srcArray.min()
float rng = srcArray.range()
array<float> scaledArray = array.new<float>()
// Push normalized `element` values into the `scaledArray`.
for element in srcArray
scaledArray.push((element - min) / rng)
scaledArray
// @function Draws a label containing eCDF estimates in the format "{price}: {percent
// @param srcArray (array<float>) Array of source values.
// @param cdfArray (array<float>) Array of CDF estimates.
// @returns (void)
makeLabel(array<float> srcArray, array<float> cdfArray) =>
float max = srcArray.max()
float rng = srcArray.range() / cdfArray.size()
string results = ""
var label lbl = label.new(0, 0, "", style = label.style_label_left, text_font_family =
// Add percentage strings to `results` starting from the `max`.
cdfArray.reverse()
for [i, element] in cdfArray
results += str.format("{0}: {1}%\n", max - i * rng, element * 100)
// Update `lbl` attributes.
lbl.set_xy(bar_index + 1, srcArray.avg())
lbl.set_text(results)
Page 3 of 4
// Queue `sourceArray`, feature scale, then estimate the distribution over `n` steps.
array<float> distArray = sourceArray.maintainQueue(sourceInput).featureScale().eCDF(n)
// Draw label.
makeLabel(sourceArray, distArray)
Previous Next
Enums Arrays
Pine Q&A chat Stack Overflow ↗ Telegram ↗ Reddit ↗ Copyright © 2025 TradingView, Inc.
Page 4 of 4