This tutorial will delve into the box.set_text_halign()
function, a powerful tool for aligning text horizontally within a box on your TradingView charts.
Syntax
The box.set_text_halign()
function is used to set the horizontal alignment of the text within a box object. Its syntax is straightforward:
box.set_text_halign(id, text_halign) → void
Arguments
- id (
series box
): This is the identifier of the box object whose text alignment you wish to modify. It’s essential to have created a box object prior to using this function. - text_halign (
series string
): This argument specifies the desired horizontal alignment of the text within the box. Pine Script provides three options for this: text.align_left
text.align_center
text.align_right
These alignment options give you the flexibility to position the box’s text according to your chart’s visual requirements.
Example
To better understand the box.set_text_halign()
function, let’s walk through a practical example where we create a box and then align its text to the center.
//@version=5 indicator("My Box Text Alignment Example", overlay=true) // Create a box object myBox = box.new(bar_index[10], high, bar_index, low, bgcolor=color.new(color.red, 90)) // Set the box's text box.set_text(myBox, "Center Aligned Text") box.set_text_color(myBox,color.white) // Align the box's text to the center box.set_text_halign(myBox, text.align_center) box.delete(myBox[1])
In this example, we first create a new box object named myBox
using the box.new()
function. The box spans from 10 bars ago to the current bar, covering the highest and lowest prices in this range. We then set some text for the box using box.set_text()
and finally align this text to the center of the box using the box.set_text_halign()
function with the text.align_center
argument.
Walking Through the Code
- Box Creation: The
box.new()
function creates a new box object. Its parameters define the starting and ending bars for the box’s position, as well as the highest and lowest prices it covers. - Setting Box Text: The
box.set_text()
function assigns text to the created box. This step is necessary before we can align the text since the function modifies the text’s alignment within an existing box. - Text Alignment: Finally, the
box.set_text_halign()
function is called withmyBox
andtext.align_center
as arguments, aligning the box’s text to the center.
Key Features and Takeaways
- The
box.set_text_halign()
function is essential for customizing the appearance of text within boxes, enhancing the visual presentation of charts. - The function supports three horizontal alignment options: left, center, and right, providing flexibility in text positioning.
- Remember to create a box and set its text before applying the
box.set_text_halign()
function to align the text within the box.
This tutorial has introduced you to the box.set_text_halign()
function in Pine Script, covering its syntax, usage, and a practical example. With this knowledge, you can now effectively align text within boxes on your TradingView charts, making your technical analysis more visually organized and appealing.