Home » Box Functions » Understanding the box.set_text_valign() Function in Pine Script

Understanding the box.set_text_valign() Function in Pine Script

Photo of author
Published on

In this tutorial, we will delve into the syntax, usage, and application of the box.set_text_valign() function, complete with code examples and a detailed walkthrough.

Syntax

The syntax for the box.set_text_valign() function is straightforward:

box.set_text_valign(id, text_valign) → void
  • id (series box): This is the identifier for the box object you want to modify. It references a previously created box on the chart.
  • text_valign (series string): This argument specifies the vertical alignment of the box’s text. It accepts one of the following values: text.align_top, text.align_center, text.align_bottom.

Example

Let’s create a box and then use the box.set_text_valign() function to align text within it. In this example, we will generate a box and vertically center its text.

//@version=5
indicator("My Box with Text VAlign", overlay=true)

// Create a box
myBox = box.new(bar_index - 10, high, bar_index, low, border_color=color.red, bgcolor=color.new(color.blue, 90))
box.set_text(myBox,"Example Text")
box.set_text_color(myBox,color.white)
// Set vertical text alignment to center
box.set_text_valign(myBox, text.align_center)
box.delete(myBox[1])

Example

Walkthrough

  1. Indicator Declaration: We start by declaring a new indicator using indicator(). This example creates an overlay indicator named “My Box with Text VAlign”.
  2. Creating a Box: The box.new() function creates a new box on the chart. Our box extends from bar_index - 10 to the current bar_index and spans from the high to the low price within that range. The box’s border color is set to red, and its background color is blue with 90% transparency.
  3. Setting Text Vertical Alignment: We use box.set_text_valign() to adjust the vertical alignment of any text that will be displayed within the box. By passing text.align_center as the text_valign argument, we ensure that text is centered vertically within the box.

Key Features and Takeaways

  • Function Useability: The box.set_text_valign() function is invaluable for creating visually appealing chart annotations that require text within boxes. It enhances readability by allowing precise control over text positioning.
  • Syntax and Application: With just two parameters, id and text_valign, this function is simple yet powerful. It enables text alignment within boxes to be easily adjusted, improving the visual organization of chart annotations.
  • Versatile Text Alignment Options: Offering three vertical alignment options (text.align_top, text.align_center, text.align_bottom), the function caters to a variety of aesthetic and informational needs in chart presentation.

By mastering the box.set_text_valign() function, you can significantly enhance the visual clarity and effectiveness of your Pine Script chart annotations, making your trading analysis both more insightful and visually engaging.

Leave a Comment