Home » Box Functions » box.set_left Function In Pinescript

box.set_left Function In Pinescript

Photo of author
Published on

In Pine Script, the box.set_left function is a versatile tool that allows users to set the left coordinate of a box. This function can be particularly useful when creating visual boxes on a TradingView chart for indicating certain price ranges, events, or other valuable pieces of trading information.

The box.set_left function is part of a suite of functions to manage box objects on TradingView. Before diving into box.set_left, let’s have a brief overview of box objects.

What is a box object in Pine Script?

A box object in Pine Script is a visual object drawn on the chart that is characterized by four borders: left, top, right, and bottom. It can be customized with different border styles, colors, text, and other properties to suit your specific requirements.

The box.new function is used to create a new box object with the specified parameters. Once the box object is created, you can manipulate it using various box functions, one of which is box.set_left.

Understanding box.set_left

The syntax for the box.set_left function is as follows:

box.set_left(id, left)


  • id is the box object.
  • left is the bar index or bar time of the left border.

It’s crucial to understand that the left border can only be defined within 500 bars into the future when using xloc.bar_index.

Box.set_left Example Use Case

Here’s an example code snippet that uses box.set_left and other box functions:

pinescriptCopy code
indicator("My Box Script", overlay = true)
box_id = box.new(bar_index - 8 ,ta.highest(5),  bar_index , ta.lowest(5))
box.set_left(box_id, bar_index - 5 )
box.set_bgcolor(box_id, color.new(color.green, 90))
box.set_extend(box_id, extend.right)

Let’s dissect the above script line by line:

  • The script starts by declaring the version of the Pine Script (version 5 in this case), followed by defining a new indicator with the title “My Box Script” that will overlay on the chart.
  • A new box is created using the box.new function and its ID is stored in the box_id variable. The left border of the box is set 8 bars back from the current bar, the top at the highest value of the last 5 bars, the right border at the current bar, and the bottom at the lowest value of the last 5 bars.
  • The box.set_left function is used to reset the left border of the box to 5 bars back from the current bar.
  • box.set_bgcolor and box.set_extend functions are then used to set the box’s background color and extend the box to the right, respectively.
  • Lastly, box.delete function is used to delete the previous box (box_id[1]).

Key Takeaway

The box.set_left function allows you to dynamically adjust the left border of a box object on your TradingView chart. While it’s a simple function, when combined with other box and Pine Script functions, it becomes a powerful tool for creating sophisticated trading indicators and strategies.


Mastering box objects and the box.set_left function in Pine Script can significantly improve your ability to visualize and test trading strategies. It enables you to create highly customizable box objects on your charts to better understand and interpret market conditions. Practice and experiment with this function to discover how it can best serve your trading strategy.

Leave a Comment