Home » Box Functions » box.copy Function in Pine Script

box.copy Function in Pine Script

Photo of author
Published on

This tutorial will guide you through the use of box.copy(), including its syntax, application, and a unique use case.

What is box.copy?

Pine Script provides graphical elements to enhance the visualization of your trading strategies. One such element is the box, which is often used to highlight a specific range on a chart. The box.copy() function serves to clone a box object, effectively replicating its features.


The syntax of the box.copy() function is straightforward:

box.copy(id) → series box

In this syntax, id represents the box object you want to clone. The function returns a new box object identical to the original one.

Unique Use Case: Highlighting Price Ranges

To demonstrate the box.copy() function in action, let’s delve into a unique use case: visualizing the highest and lowest price ranges for a specified number of bars.


indicator('Last 50 bars price ranges', overlay = true)
highest = ta.highest(LOOKBACK)
lowest = ta.lowest(LOOKBACK)
if barstate.islastconfirmedhistory
    var BoxLast = box.new(bar_index[LOOKBACK], highest, bar_index, lowest, bgcolor = color.new(color.green, 80))
    var BoxPrev = box.copy(BoxLast)
    box.set_lefttop(BoxPrev, bar_index[LOOKBACK * 2], highest[50])
    box.set_rightbottom(BoxPrev, bar_index[LOOKBACK], lowest[50])
    box.set_bgcolor(BoxPrev, color.new(color.red, 80))
box.copy Function


  • This script creates boxes that highlight the last 50 bars’ price ranges. The LOOKBACK variable defines the number of bars to consider, which is set to 50 in this case.
  • highest and lowest functions compute the highest and lowest values for the lookback period, respectively. The if barstate.islastconfirmedhistory condition ensures that the script executes only for the last confirmed historical bar to avoid repainting.
  • A new box, BoxLast, is created with the box.new() function. It covers the range from the highest to the lowest value over the last 50 bars. This box’s background color is green.
  • Then, box.copy(BoxLast) is used to create BoxPrev, an exact copy of BoxLast. The box.set_lefttop() and box.set_rightbottom() functions are used to redefine the dimensions of BoxPrev.
  • It now represents the highest and lowest values over the previous 50 bars, effectively moving it 50 bars back. The color of BoxPrev is then set to red using box.set_bgcolor().

Key Takeaways:

  1. box.copy() is a versatile function in Pine Script that clones box objects for chart visualization.
  2. It is useful when you need to replicate boxes with the same attributes and properties.
  3. It can be used effectively to visualize trading ranges, among other use cases.
  4. Manipulation of copied boxes, such as modifying dimensions or colors, can be done using functions like box.set_lefttop(), box.set_rightbottom(), and box.set_bgcolor().


In summary, the box.copy() function in Pine Script allows for an enhanced, dynamic, and interactive approach to chart visualization. Its ability to clone box objects facilitates more efficient and diverse representation of data on charts, making it a significant asset in crafting custom technical analysis tools in TradingView. Whether you’re a seasoned trader or new to Pine Script, mastering such graphical tools can elevate your trading strategy design to a new level. Happy coding!

Leave a Comment