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)
Here’s a line-by-line explanation of the code:
//@version=5
: This line specifies the version of Pine Script being used, which is version 5 in this case.indicator('High Low Box', overlay=true)
: This line of code declares a new indicator with the name ‘High Low Box’. Theoverlay=true
parameter indicates that the indicator will be displayed directly on top of the price chart.var box id = na
: Here, a variable namedid
is declared to store the id of the box drawn on the chart. Thevar
keyword is used to declare a variable that maintains its value between updates. Initially,id
is set tona
, meaning not available or no value.highLevel = ta.highest(high, 5)
: This line uses thehighest
function from theta
(technical analysis) namespace to calculate the highest value of thehigh
series over the last 5 bars. This value is stored in the variablehighLevel
.lowLevel = ta.lowest(low, 5)
: Similar to the previous line, this line uses thelowest
function to find the lowestlow
price over the past 5 bars and assigns this value to thelowLevel
variable.if not na(id) box.delete(id)
: This condition checks ifid
is notna
. Ifid
has a value, meaning a box is currently drawn on the chart, thebox.delete
function is called withid
as its argument to delete the existing box.id := box.new(bar_index[4], highLevel, bar_index, lowLevel)
: Finally, a new box is drawn on the chart with thebox.new
function. The arguments for the function define the coordinates of the box: the x1 (starting bar index) isbar_index[4]
(four bars back from the current bar), y1 (starting price level) ishighLevel
, x2 (ending bar index) is the currentbar_index
, and y2 (ending price level) islowLevel
. The id of the newly created box is then assigned toid
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!
how do we go about iterating through a series box and delete box if closing price closes inside a box?