Home » Pinescript Syntax » Understanding the const in Pine Script

Understanding the const in Pine Script

Photo of author
Published on

In Pine Script, the const keyword plays a crucial role in optimizing the performance and readability of your scripts. It is used to define values that are determined at compile time, meaning these values are set before the script even begins execution on a chart. This article dives deep into the concept of const values, showcasing how to properly utilize them in your Pine Script coding endeavors.

What are const Values?

const values are constants; once defined, their value does not change throughout the script’s execution. This immutability applies from the script’s compilation stage, making const values reliable for use in conditions that require unchanging data.

Types of Literal Values

Pine Script supports several types of literal values that can be marked as const:

  • Integer (int): Examples include 1, -1, 42
  • Floating-point (float): Examples are 1., 1.0, 3.14, 6.02E-23, 3e8
  • Boolean (bool): true, false
  • Color: Specified in hexadecimal, such as #FF55C6, #FF55C6ff
  • String: Text literals like “A text literal”, ‘Embedded double quotes “text”‘

Declaring const Variables

To declare a variable as constant, simply precede its declaration with the const keyword. Our style guide recommends naming these constants in uppercase SNAKE_CASE for better readability. Although not mandatory, using the var keyword alongside const for variable declarations is advised to ensure initialization only occurs once, at the first bar of the dataset.

Practical Example

Let’s examine the practical use of const values within the indicator() and plot() functions:

//@version=5
// Define global variables of the "const string" type for titles:
const string INDICATOR_TITLE = "Const Demo"
var const string PLOT1_TITLE = "High"
const string PLOT2_TITLE = "Low"
const string PLOT3_TITLE = "Midpoint between " + PLOT1_TITLE + " and " + PLOT2_TITLE

indicator(INDICATOR_TITLE, overlay = true)
plot(high, PLOT1_TITLE)
plot(low, PLOT2_TITLE)
plot(hl2, PLOT3_TITLE)

In this example, we define several const variables to hold titles for our indicators and plots. Note the use of const to ensure these titles do not change, ensuring consistency across the script’s execution.

Common Pitfall: Compilation Error Example

Attempting to use dynamic values, such as syminfo.ticker, which change based on the chart information at runtime, in conjunction with const will lead to compilation errors. This is because const values must be determined at compile time.

//@version=5
var NAME = "My indicator for " + syminfo.ticker // Incorrect usage

indicator(NAME, "", true) // This will cause a compilation error

The above snippet illustrates a common mistake, attempting to concatenate a constant string with a dynamic value (syminfo.ticker), which is not allowed for const variables.

Key Takeaways

  • Use of const: Enables defining immutable values that are set at compile time.
  • Naming Convention: Uppercase SNAKE_CASE for better readability of const variables.
  • Initialization: Consider using var with const for variables to ensure they’re only initialized once.
  • Compatibility: const values cannot be combined with dynamic values that change at runtime.

By adhering to these guidelines and understanding the use cases for const in Pine Script, developers can create more efficient, readable, and error-free scripts.

Leave a Comment