Home » Pinescript Syntax » Understanding Method Overloading in Pine Script

Understanding Method Overloading in Pine Script

Photo of author
Published on

In this article, we delve into how to implement method overloading in Pine Script, using a practical example that demonstrates its utility in identifying variable types.

Method Overloading

Method overloading enables the creation of several method implementations with the same identifier but different parameter signatures. This capability is particularly useful in scenarios where the logic of a method varies based on the type or number of inputs it receives. Pine Script supports method overloading for both built-in and user-defined methods, allowing for customized method behavior aligned with specific parameter types.

Example: Identifying Variable Types

Consider a scenario where we need a method to identify the type of a given variable. Pine Script requires explicit specification of the type of object a method is associated with. To accommodate different variable types, we implement method overloads for each primitive type we wish to recognize.

Defining getType Method Overloads

Below is the implementation of the getType method, designed to return a string representation of a variable’s type. We define overloads for the five primitive types: int, float, bool, color, and string.

// Identifies an object's type.
// @param obj Object to inspect.
// @returns (string) A string representation of the type.
method getType(int obj) =>
    na(obj) ? "int(na)" : "int"

method getType(float obj) =>
    na(obj) ? "float(na)" : "float"

method getType(bool obj) =>
    na(obj) ? "bool(na)" : "bool"

method getType(color obj) =>
    na(obj) ? "color(na)" : "color"

method getType(string obj) =>
    na(obj) ? "string(na)" : "string"

Utilizing getType in a Script

To demonstrate the use of these overloads, we inspect variables of different types and format the results. This script utilizes the str.format() function to combine the outputs from calling getType() on five distinct variables, displaying the formatted string in a label on the chart.

//@version=5
indicator("Type Inspection")

method getType(int this) =>
    na(this) ? "int(na)" : "int"

method getType(float this) =>
    na(this) ? "float(na)" : "float"

method getType(bool this) =>
    na(this) ? "bool(na)" : "bool"

method getType(color this) =>
    na(this) ? "color(na)" : "color"

method getType(string this) =>
    na(this) ? "string(na)" : "string"

a = 1
b = 1.0
c = true
d = color.rgb(255, 0, 0)
e = "1"

// Inspect variables and format results.
results = str.format(
 "a: {0}\nb: {1}\nc: {2}\nd: {3}\ne: {4}",
 a.getType(), b.getType(), c.getType(), d.getType(), e.getType()
 )

var label lbl = label.new(0, 0)
lbl.set_x(bar_index)
lbl.set_text(results)
Utilizing getType in a Script

Explanation

  1. Version Declaration: The script begins with //@version=5, specifying that it uses version 5 of Pine Script. This version supports more advanced features, including method overloading.
  2. Indicator Declaration: indicator("Type Inspection") declares the script as an indicator with the name “Type Inspection”. This makes the script available for addition to trading charts within the TradingView platform.
  3. Method Overloading for Type Identification:
    • The script defines multiple overloads of the getType method, each designed to accept a different primitive type as its parameter (int, float, bool, color, and string).
    • Each getType method checks if the passed variable is na (not available or empty) and returns a string indicating the variable’s type. If the variable is na, it appends (na) to the type name in the returned string.
  4. Variable Declarations:
    • Variables a, b, c, d, and e are defined with different types: integer (1), float (1.0), boolean (true), color (color.rgb(255, 0, 0)), and string ("1"), respectively.
  5. Type Inspection and Result Formatting:
    • The str.format function is used to create a formatted string (results) that includes the type of each variable as identified by the getType method.
    • This formatted string includes the type of each variable on a new line, labeled a through e.
  6. Label Creation and Display:
    • A label (lbl) is created and positioned at the current bar index (bar_index) on the chart.
    • The set_text method of the label is used to display the results string, which contains the types of the inspected variables.
  7. Execution Flow: When the script runs, it evaluates the types of the defined variables using the overloaded getType methods, formats the output into a single string, and displays this information on the chart through a label. This demonstrates the practical application of method overloading in Pine Script to perform type inspection and result presentation in a concise and readable format.

Key Observations

  • Type-Specific Overloading: The compiler selects the appropriate overload of getType based on the variable’s underlying type, ensuring accurate type identification.
  • Handling na Values: The method appends “(na)” to the output when a variable is na, clearly indicating empty or undefined values.

Summary and Key Takeaways

  • Method overloading in Pine Script allows defining multiple versions of a method with the same name but different parameters, increasing code flexibility and readability.
  • Type-specific logic can be implemented through overloads, facilitating precise operations based on variable types.
  • Error handling and special cases, like na values, can be seamlessly integrated into method implementations, enhancing the robustness of Pine Script code.

By leveraging method overloading, Pine Script developers can create more versatile, maintainable, and readable scripts, tailored to the specific requirements of their trading strategies or analytical tools.

Leave a Comment