Home » Pinescript Syntax » Understanding Built-in Methods in Pine Script

Understanding Built-in Methods in Pine Script

Photo of author
Published on

Pine Script™ facilitates a streamlined approach to coding by incorporating built-in methods for handling special types such as arrays, matrices, maps, lines, linefills, boxes, polylines, labels, and tables. These methods significantly simplify the scripting process, allowing for more concise and readable code.

Simplifying Access with Method Notation

In Pine Script™, built-in methods enable two equivalent expressions for invoking functions:

  • Using the namespace: <namespace>.<functionName>([paramName =] <objectName>, …)
  • Directly on the object: <objectName>.<functionName>(…)

This duality offers a choice between explicit namespace declaration and a more streamlined, object-oriented approach. For instance, replacing array.get(id, index) with id.get(index) eliminates the need to explicitly mention the array namespace, simplifying code readability and maintenance.

Practical Example: Bollinger Bands Calculation

To illustrate the practical application of built-in methods, consider the example of computing Bollinger Bands, a common financial analysis tool. The original script uses traditional function calls like array.push() and array.shift() to manipulate an array of price data, followed by array.avg() and array.stdev() to calculate the mean and standard deviation. These calculations form the basis for the upper and lower Bollinger Bands, which are plotted on a chart.

Original Script:

//@version=5
indicator("Custom Sample BB", overlay = true)

float sourceInput  = input.source(close, "Source")
int   samplesInput = input.int(20, "Samples")
int   n            = input.int(10, "Bars")
float multiplier   = input.float(2.0, "StdDev")

var array<float> sourceArray = array.new<float>(samplesInput)
var float        sampleMean  = na
var float        sampleDev   = na

if bar_index % n == 0
    array.push(sourceArray, sourceInput)
    array.shift(sourceArray)
    sampleMean := array.avg(sourceArray)
    sampleDev  := array.stdev(sourceArray) * multiplier

float highBand = sampleMean + sampleDev
float lowBand  = sampleMean - sampleDev

plot(sampleMean, "Basis", color.orange)
plot(highBand, "Upper", color.lime)
plot(lowBand, "Lower", color.red)
Example

Revised Script Using Methods:

In the revised version, we utilize the built-in methods to streamline the code, directly invoking methods on the sourceArray object without explicitly referencing the array namespace. This approach not only makes the code cleaner but also emphasizes the object-oriented capabilities of Pine Script™.

//@version=5
indicator("Custom Sample BB", overlay = true)

float sourceInput  = input.source(close, "Source")
int   samplesInput = input.int(20, "Samples")
int   n            = input.int(10, "Bars")
float multiplier   = input.float(2.0, "StdDev")

var array<float> sourceArray = array.new<float>(samplesInput)
var float        sampleMean  = na
var float        sampleDev   = na

if bar_index % n == 0
    sourceArray.push(sourceInput)
    sourceArray.shift()
    sampleMean := sourceArray.avg()
    sampleDev  := sourceArray.stdev() * multiplier

float highBand = sampleMean + sampleDev
float lowBand  = sampleMean - sampleDev

plot(sampleMean, "Basis", color.orange)
plot(highBand, "Upper", color.lime)
plot(lowBand, "Lower", color.red)
Revised Script Using Methods

Key Features and Takeaways

  • Streamlined Syntax: Built-in methods allow for a more concise and readable syntax by enabling direct calls on objects.
  • Enhanced Readability: Direct method calls improve code readability and reduce the need for verbose namespace references.
  • Object-Oriented Approach: Utilizing methods emphasizes an object-oriented approach to scripting in Pine Script™, aligning with modern programming practices.
  • Simplified Code Maintenance: With methods, code modifications and maintenance become more straightforward, as the context of operations is clearer.

By leveraging built-in methods, Pine Script™ developers can write more efficient, readable, and maintainable code, enhancing the overall scripting experience.

Leave a Comment