box namespace
Overview
Section titled “Overview”The box namespace lets you draw rectangular regions on the chart. Boxes can be positioned by bar index/time and price, styled with colors/borders, and updated dynamically. They persist as drawings—ideal for highlighting ranges, zones, or sessions.
Common constructors & accessors
Section titled “Common constructors & accessors”| Function | Purpose |
|---|---|
box.new(x1, y1, x2, y2, border_color, bgcolor, border_width, extend, xloc, yloc) | Create a new box spanning coordinates (x1, y1) to (x2, y2). |
box.set_lefttop(id, x, y) | Move the top-left corner. |
box.set_rightbottom(id, x, y) | Move the bottom-right corner. |
box.get_left(id) / box.get_right(id) / box.get_top(id) / box.get_bottom(id) | Read current coordinates. |
box.set_bgcolor(id, color) | Update fill color. |
box.delete(id) | Remove the box. |
Example
Section titled “Example”//@version=5indicator("Box highlighter", overlay=true, max_boxes_count=50)
rangeLen = input.int(20, "Range lookback", minval=1)
var box sessionBox = na
if barstate.isfirst sessionBox := box.new(bar_index - rangeLen, low, bar_index, high, bgcolor=color.new(color.yellow, 85), border_color=color.new(color.orange, 0))
if not na(sessionBox) box.set_left(sessionBox, bar_index - rangeLen) box.set_right(sessionBox, bar_index) box.set_top(sessionBox, ta.highest(high, rangeLen)) box.set_bottom(sessionBox, ta.lowest(low, rangeLen))