Home » Pinescript Syntax » Modifying Object Field Values in Pine Script

Modifying Object Field Values in Pine Script

Photo of author
Published on

Using the := Reassignment Operator

In Pine Script™, the := operator is employed to change the value of an object’s fields after the object has been created. This is crucial for dynamically updating the fields of an object based on new data or calculations that occur as the script runs.

Example: Updating Fields of a pivotMarker Object

Consider the previous example where we created a pivotMarker object. The initial creation and subsequent field value updates can be done as follows:

Initial Object Creation
markerPoint = pivotMarker.new()

In this line, markerPoint is an instance of pivotMarker, created without initially setting field values.

Updating Field Values
markerPoint.positionX := bar_index[pivotLegs]
markerPoint.positionY := highPivotPrice
  • markerPoint.positionX is updated with the value bar_index[pivotLegs].
  • markerPoint.positionY is set to highPivotPrice.

These lines demonstrate how the := operator is used to modify the fields of the markerPoint object after its creation.

Practical Implications

  • Flexibility in Object Management: This approach allows for greater flexibility, as objects can be instantiated first and their fields can be updated as needed, based on the script’s logic or user input.
  • Dynamic Data Handling: It’s particularly useful in scenarios where the data needed to populate an object’s fields isn’t available at the time of object creation or when it changes over time.

Conclusion and Key Takeaways

  • The := operator is essential for changing the values of an object’s fields after its initial creation.
  • This capability adds a dynamic aspect to object management in Pine Script, accommodating changing values and conditions within a script.

Leave a Comment