Home » Box Functions » box.set_extend Function in Pinscript

box.set_extend Function in Pinscript

Photo of author
Published on

In this tutorial, we’ll delve into Pine Script’s box.set_extend function. This function is used to adjust the border extension of a box object created by box.new in Pine Script. We will explain the syntax, use cases, and provide examples to ensure you have a deep understanding of this function.

Understanding box.set_extend Function

Syntax

The box.set_extend function requires two parameters:

box.set_extend(id, extend)

Parameters:

  • id – This is the ID of the box object. It is of type series box.
  • extend – This specifies the new extending type. It is of type series string.

The extend Parameter

The extend parameter is used to determine how the horizontal borders of the box object should be extended. There are four possible values:

  • extend.none – The borders start and end at the left and right of the box respectively. There is no extension.
  • extend.left – The borders are indefinitely extended to the left of the box.
  • extend.right – The borders are indefinitely extended to the right of the box.
  • extend.both – The borders are indefinitely extended on both sides of the box.

Practical Use Case

To demonstrate the practical use of box.set_extend, let’s consider a situation where we want to draw a box on a specific bar, then extend the top and bottom borders to the right indefinitely. This box could represent a breakout zone, with the extended borders helping us to visualize how the price interacts with this zone.

//@version=5
indicator("My Box Script", overlay = true)
box_id = box.new(bar_index - 5,ta.highest(5),  bar_index , ta.lowest(5))

box.set_bgcolor(box_id, color.new(color.green, 90))
box.set_extend(box_id, extend.right)
box.delete(box_id[1])
box.set_extend Function

Explanation:

//@version=5

This line defines the version of Pine Script that is being used. In this case, it’s version 5.

indicator("My Box Script", overlay = true)

This line declares the script as a technical analysis indicator with the title “My Box Script”. The overlay = true part indicates that the script will overlay the current price chart rather than appearing in a separate panel.

box_id = box.new(bar_index - 5,ta.highest(5),  bar_index , ta.lowest(5))

This line creates a new box. The box starts at the bar 5 bars ago (bar_index – 5) and goes until the current bar (bar_index). The top of the box is defined by the highest price in the last 5 bars (ta.highest(5)), and the bottom of the box is defined by the lowest price in the last 5 bars (ta.lowest(5)). The ID of the box is stored in box_id.

box.set_bgcolor(box_id, color.new(color.green, 90))

This line sets the background color of the box. The color.new(color.green, 90) creates a new color that is semi-transparent green. The number 90 is the transparency level of the color (0 is fully transparent and 100 is fully opaque).

box.set_extend(box_id, extend.right)

This line sets the horizontal borders of the box to extend indefinitely to the right. The extend.right tells Pine Script to extend the top and bottom borders of the box to the right side.

box.delete(box_id[1])

This line deletes the box from the previous bar. The [1] indexer on box_id refers to the box ID of the previous bar. Note that boxes are persistent by default; once a box is drawn, it will remain on the chart unless explicitly deleted.

Key Takeaway

The box.set_extend function is a powerful tool in Pine Script that allows you to extend the horizontal borders of a box object in your charts. It’s important to understand how it works, as it can significantly enhance your visual chart analysis, enabling you to highlight and track specific areas on your chart.

Conclusion

Pine Script provides you with a wide range of functions to customize your charts, and box.set_extend is one of them. Remember, the extend parameter can take extend.none, extend.left, extend.right, and extend.both values depending on your requirement. Mastering these details will help you make the most out of Pine Script and create

Leave a Comment