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

Understanding the box.set_bgcolor() Function in Pine Script

Photo of author
Published on

This article will explore the syntax, usage, and practical examples of box.set_bgcolor() function, making your scripting journey smoother and more productive.

Syntax of box.set_bgcolor()

The box.set_bgcolor() function is used to set the background color of a box object in Pine Script. This function is particularly useful when you want to highlight specific areas on your chart for better visibility or to indicate different market conditions. The syntax of the function is straightforward:

box.set_bgcolor(id, color) → void

Let’s break down the syntax components:

  • id (series box): This argument specifies the box object whose background color you want to change. You must first create a box using the box.new() function and then reference its ID here.
  • color (series color): This argument defines the new background color of the box. Pine Script provides various ways to specify color, including predefined color constants (like color.red, color.green), hexadecimal color codes, and functions like color.new() for custom colors.

How to Use box.set_bgcolor()

To effectively utilize the box.set_bgcolor() function, follow these steps:

  1. Create a Box: Use the box.new() function to create a box. Remember to store the box’s ID as you will need it to set the background color.
  2. Set the Background Color: Use the box.set_bgcolor() function, referencing the box’s ID and specifying the desired color.


indicator("My Box Background Color Example", overlay=true)
myBox = box.new(bar_index[1], low, bar_index, high, border_color=color.blue, bgcolor=color.gray)
box.set_bgcolor(myBox, color = color.orange) 

Walkthrough of Code

  1. Indicator Declaration:
    • //@version=5: Specifies the script is using Pine Script version 5.
    • indicator("My Box Background Color Example", overlay=true): Declares a new indicator named “My Box Background Color Example” that will overlay on the main chart.
  2. Box Creation:
    • myBox = box.new(bar_index[1], low, bar_index, high, border_color=color.blue, bgcolor=color.gray): A new box object, myBox, is created. This box spans from one bar ago to the current bar, covering from the low to the high price of the current bar. It initially has a blue border and a gray background.
  3. Setting Box Background Color:
    • box.set_bgcolor(myBox, color = color.orange): Changes the background color of the previously created box to orange. The color argument specifies the new background color. Note: The correct syntax to set the color should not include the color = part; it should simply be box.set_bgcolor(myBox, color.orange).

Key Features and Takeaways

  • Function Usability: The box.set_bgcolor() function allows for dynamic visual modifications of box objects on the chart, enabling the creation of more interactive and visually appealing indicators and strategies.
  • Syntax and Application: The function requires the ID of the box object and the new color as arguments. This allows for straightforward application and flexibility in visual design.
  • Enhanced Chart Visualization: By changing box colors, scripts can signal different market conditions, thresholds, or any custom criteria defined by the trader or developer, enhancing the analytical capabilities of the chart.

Leave a Comment