Home » Pinescript Syntax » Objects in Pine Script

Objects in Pine Script

Photo of author
Published on

Introduction to User-Defined Types (UDTs) and Objects

Objects in Pine Script™ are instances of user-defined types (UDTs), similar to variables with multiple fields. These fields can hold different types of values, enabling UDTs to function as versatile data structures.

UDTs as Methodless Classes

  • Analogy: For programmers with object-oriented backgrounds, UDTs in Pine Script are akin to methodless classes.
  • Field Composition: UDTs include various fields, each holding unique values of types like int, float, or string.

Crafting Objects in Pine Script

Defining a UDT

The first step in creating objects is defining a UDT. Let’s define a pivotMarker type:

type pivotMarker
    int positionX
    float positionY
    string timeLocation = timeLocation.bar_time
  • UDT Declaration: Utilizing the type keyword to declare the pivotMarker.
  • Fields: The UDT includes positionX (integer), positionY (float), and timeLocation (string with a default value).

Instantiating Objects from UDTs

Create objects using the UDT’s new() method:

// Creating an object without specifying field values
markerPoint = pivotMarker.new()

// Creating an object with specific field values
markerPoint = pivotMarker.new(bar_index, high)
// Alternative syntax
markerPoint = pivotMarker.new(positionX = bar_index, positionY = high)
  • Field Defaults: Fields not explicitly set during object creation use default values.

Placeholder Objects

Declaration of placeholder objects with na:

pivotMarker markerPoint = na

Example Use Case: Pivot Point Labels

Using the pivotMarker UDT to display labels for high pivot points:

//@version=5
indicator("High Pivot Labels", overlay = true)



int pivotLegs = input(10)

// Defining the `pivotMarker` UDT.
type pivotMarker
    int positionX
    float positionY
    string timeLocation = xloc.bar_time

// Finding high pivots.
highPivotPrice = ta.pivothigh(pivotLegs, pivotLegs)
if not na(highPivotPrice)
    // Positioning a label at the high pivot
    markerPoint = pivotMarker.new(time[pivotLegs], highPivotPrice)
    label.new(markerPoint.positionX,markerPoint.positionY,str.tostring(markerPoint.positionY, format.mintick),markerPoint.timeLocation,textcolor = color.white)
Objects in Pine Script

Here, markerPoint is an object of type pivotMarker used to label high pivot points.

Persistent and Non-Persistent Objects

Demonstrating persistent and non-persistent objects:

//@version=5
indicator("Bar Information")
type barDetails
    int index = bar_index
    int timestamp = time
    float closingPrice = close

// Persistent object, created at the script's initialization
var firstBarDetails = barDetails.new()
// Non-persistent object, created at every new bar
currentBarDetails = barDetails.new()

plot(firstBarDetails.index)
plot(currentBarDetails.index)
  • Persistence: firstBarDetails is a persistent object, whereas currentBarDetails is recreated at each new bar.

Conclusion and Key Points

  • UDTs in Pine Script enable the creation of complex, customizable data structures.
  • Objects, instances of UDTs, manage various data types in their fields.
  • Mastery of UDTs and objects elevates the organization and capability of Pine Script coding.

Leave a Comment