Home » Box Functions » box.delete function in pinescript

box.delete function in pinescript

Photo of author
Published on

In this Article we’re focusing on an integral function in Pine Script: the box.delete function.

Understanding the box.delete Function

The box.delete function, in its simplest form, is used to remove a specified box object from the chart. If you’re not yet familiar with box objects, they are visual elements that you can place on a chart for various purposes, like highlighting specific regions.

Syntax of box.delete

The syntax of the box.delete function is simple and straightforward:

box.delete(id)

Where id is a series box object you wish to delete.

Parameters of box.delete

The box.delete function takes a single argument:

  • id (series box) – A box object to delete.

If the specified box object has already been deleted, the function does nothing, thereby avoiding errors or disruptions in your script.

Unique Use Case: Highlighting and Deleting Price Range Boxes

Now, let’s look at a unique use case of the box.delete function – creating, then subsequently deleting, price range boxes based on certain conditions.

//@version=5
indicator('High Low Box', overlay=true)

var box id = na

highLevel = ta.highest(high, 5)
lowLevel = ta.lowest(low, 5)

if not na(id)
    box.delete(id)

id := box.new(bar_index[4], highLevel, bar_index, lowLevel)
box.delete function

Here’s a line-by-line explanation of the code:

  1. //@version=5: This line specifies the version of Pine Script being used, which is version 5 in this case.
  2. indicator('High Low Box', overlay=true): This line of code declares a new indicator with the name ‘High Low Box’. The overlay=true parameter indicates that the indicator will be displayed directly on top of the price chart.
  3. var box id = na: Here, a variable named id is declared to store the id of the box drawn on the chart. The var keyword is used to declare a variable that maintains its value between updates. Initially, id is set to na, meaning not available or no value.
  4. highLevel = ta.highest(high, 5): This line uses the highest function from the ta (technical analysis) namespace to calculate the highest value of the high series over the last 5 bars. This value is stored in the variable highLevel.
  5. lowLevel = ta.lowest(low, 5): Similar to the previous line, this line uses the lowest function to find the lowest low price over the past 5 bars and assigns this value to the lowLevel variable.
  6. if not na(id) box.delete(id): This condition checks if id is not na. If id has a value, meaning a box is currently drawn on the chart, the box.delete function is called with id as its argument to delete the existing box.
  7. id := box.new(bar_index[4], highLevel, bar_index, lowLevel): Finally, a new box is drawn on the chart with the box.new function. The arguments for the function define the coordinates of the box: the x1 (starting bar index) is bar_index[4] (four bars back from the current bar), y1 (starting price level) is highLevel, x2 (ending bar index) is the current bar_index, and y2 (ending price level) is lowLevel. The id of the newly created box is then assigned to id for future reference and potential deletion in the next update.

Key Takeaways

Working with the box.delete function in Pine Script allows you to dynamically manage box objects based on your scripting conditions. Here are the key points you should remember:

  • The box.delete function is used to delete a specified box object.
  • It accepts a single parameter, the id of the box to be deleted.
  • If the box has already been deleted, the function does nothing.
  • The function can be effectively used to highlight and delete elements based on trading conditions.

Conclusion

The box.delete function provides traders and developers with greater control over their charts, allowing for more dynamic and visual trading scripts. The key is understanding how to integrate this function seamlessly within your strategies. As always, successful trading involves much more than just scripting; however, Pine Script and its functions like box.delete are excellent tools to enhance your trading system. Happy coding!

1 thought on “box.delete function in pinescript”

Leave a Comment