Home » String Functions » Understanding the Concept of an Empty String in Pine Script

Understanding the Concept of an Empty String in Pine Script

Photo of author
Published on

This article will explore how to test for an empty string using a practical example: an indicator that plots two Exponential Moving Averages (EMAs) and highlights their crossover points with labels, based on a user-defined text input.


Before diving deeper into practical examples of how empty strings affect Pine Script behavior, let’s clarify what an empty string truly is and how it’s distinguished from a non-empty string.

An empty string is essentially a string variable that holds no characters. Its content is null, meaning it lacks letters, numbers, symbols, or even whitespace such as spaces ( ), tabs (\t), and newlines (\n). This absence of content makes it an  “empty” string.

Contrastingly, a non-empty string contains one or more characters, which may not always be visible. For example, a string consisting solely of whitespace (like spaces or tabs) may appear empty at a glance, but it indeed holds content, making it non-empty.


The script we’ll examine provides a dynamic way to annotate EMA crossovers on a chart. It utilizes a text input from the user to decide whether to display a label at each crossover point. The presence or absence of text in this input controls the indicator’s behavior, demonstrating a straightforward yet powerful application of string comparison in Pine Script.

indicator(title="Empty String Test Indicator", overlay=true)

// Define a text input for the crossover label
crossoverLabel = input.string("", title="Crossover Label Text")

// Calculate two EMAs with distinct periods
quickEma = ta.ema(close, 20)
slowEma = ta.ema(close, 50)

// Display the EMAs on the chart
plot(quickEma, color=color.orange, title="Quick EMA")
plot(slowEma, color=color.teal, linewidth=2, title="Slow EMA")

// Check if the crossover label is not empty and if the EMAs have crossed
if crossoverLabel != "" and ta.cross(quickEma, slowEma)
    label.new(bar_index, high, crossoverLabel, color=color.navy,
         textcolor=color.yellow, size=size.large)

Detailed Walkthrough

  1. Initializing the Indicator:
    • indicator(title="Empty String Test Indicator", overlay=true): This line sets up the indicator, naming it “Empty String Test Indicator” and ensuring it overlays the main chart.
  2. Creating a Text Input:
    • crossoverLabel = input.string("", title="Crossover Label Text"): A text input field is created for users to specify the label text for EMA crossovers. The default value is an empty string (""), meaning no label will be shown unless the user provides text.
  3. Calculating EMAs:
    • The script calculates two EMAs, quickEma and slowEma, with periods of 20 and 50, respectively. These are derived from the closing prices (close).
  4. Plotting EMAs:
    • The EMAs are plotted on the chart with distinct colors for easy differentiation. The quickEma appears in orange, while the slowEma is displayed in teal.
  5. Conditional Label Creation:
    • The core functionality lies within an if statement that checks two conditions using the and operator:
      • crossoverLabel != "": Determines if the user has entered text in the “Crossover Label Text” input.
      • ta.cross(quickEma, slowEma): Checks whether the quickEma has crossed the slowEma.
    • When both conditions are met, a label is created at the crossover point using label.new(). This label uses the text from crossoverLabel, is colored navy with yellow text, and is larger than the default label size.

Key Features and Takeaways

  • Functionality Through User Input: The script’s behavior changes based on user input, demonstrating the versatility of Pine Script in creating interactive and customizable indicators.
  • String Comparison for Conditional Logic: Testing for an empty string ("") is a simple yet effective way to implement conditional logic based on user input.
  • EMA Crossover Visualization: The script not only calculates and plots EMAs but also highlights significant events (crossovers) in a visually distinctive manner.
  • Customization Options: By altering the text input, users can dynamically choose whether and how to annotate the chart, offering a level of interaction and personalization that enhances the charting experience.

By understanding and applying these concepts, Pine Script developers can create more dynamic, interactive, and user-friendly indicators and scripts.

Leave a Comment