Draws a rectangle from two bar positions and two prices, with optional fill, outline, extension and centred text, and returns a handle to it.
box.new(bar_index - 10, high, bar_index, low) covers the last ten bars between the high and the low. The corners come first, in the order left, top, right, bottom; everything after them is optional.
With the default xloc.bar_index the horizontal edges are bar indexes. With xloc.bar_time they are timestamps in milliseconds, so a box can start at a session open or reach past the last bar into the future.
Unless bgcolor is set the fill is opaque blue and hides the candles under it. A tint such as color.new(color.orange, 85) keeps price readable, and na for bgcolor or border_color leaves only the outline or only the fill.
Syntax
box.new(top_left: chart.point, bottom_right: chart.point, border_color: series color (optional), border_width: series int (optional), border_style: series string (optional), extend: series string (optional), xloc: series string (optional), bgcolor: series color (optional), text: series string (optional), text_size: series int/string (optional), text_color: series color (optional), text_halign: series string (optional), text_valign: series string (optional), text_wrap: series string (optional), text_font_family: series string (optional), force_overlay: const bool (optional), text_formatting: series text_format (optional)) -> series box
box.new(left: series int, top: series int/float, right: series int, bottom: series int/float, border_color: series color (optional), border_width: series int (optional), border_style: series string (optional), extend: series string (optional), xloc: series string (optional), bgcolor: series color (optional), text: series string (optional), text_size: series int/string (optional), text_color: series color (optional), text_halign: series string (optional), text_valign: series string (optional), text_wrap: series string (optional), text_font_family: series string (optional), force_overlay: const bool (optional), text_formatting: series text_format (optional)) -> series boxArguments
| Name | Type | Default | Description |
|---|---|---|---|
| top_left | chart. | required | A chart.point holding the bar and price of the top-left corner. It replaces the separate left and top arguments. |
| bottom_right | chart. | required | A chart.point holding the bar and price of the bottom-right corner. It replaces the separate right and bottom arguments. |
| border_color | series color | color. | Color of the outline; the default is opaque blue. na removes the outline and leaves the fill. |
| border_width | series int | 1 | Outline thickness in pixels; the default is 1. A value of 0 hides the outline. |
| border_style | series string | line. | Outline pattern: line.style_solid (default), line.style_dashed or line.style_dotted. Arrow styles belong to lines, not boxes. |
| extend | series string | extend. | extend.none (default) keeps the stored edges; extend.left, extend.right or extend.both stretch the box to the chart edge on that side. |
| xloc | series string | xloc. | xloc.bar_index (default) reads left and right as bar indexes; xloc.bar_time reads them as timestamps in milliseconds, as the time variable gives them. |
| bgcolor | series color | color. | Fill color; the default is opaque blue, which hides the candles behind it. Use color.new(c, 85) for a see-through tint, or na for no fill. |
| text | series string | "" | Text drawn at the centre of the box; the default is an empty string. The text is not cut off at the box edges. |
| text_size | series int/ | size. | Size of the text: size.auto (default), size.tiny, size.small, size.normal, size.large or size.huge. It applies to the whole string. |
| text_color | series color | color. | Color of the text; the default is black, which disappears on a dark chart or a deep fill, so pick a light color there. |
| text_halign | series string | text. | Horizontal text position: text.align_left, text.align_center (default) or text.align_right. Has an effect only when the box is wider than its text. |
| text_valign | series string | text. | Vertical text position: text.align_top, text.align_center (default) or text.align_bottom. Has an effect only when the box is taller than its text. |
| text_wrap | series string | text. | text.wrap_none (default) keeps the text on one line; text.wrap_auto breaks it into lines that fit the box width. |
| text_font_family | series string | font. | Typeface of the text: font.family_default (default) or font.family_monospace, which gives every character the same width. |
| force_overlay | const bool | false | true asks for the box on the main price pane even when the script owns a separate pane; the default false follows the indicator's overlay setting. |
| text_formatting | series text_format | text. | text.format_none (default), text.format_bold or text.format_italic; join bold and italic with +. It applies to the whole string. |
| left | series int | required | Left edge: a bar index, or a timestamp in milliseconds when xloc is xloc.bar_time. na leaves the box without a position, so it is not drawn. |
| top | series int/ | required | Price of the top edge, in the chart's price units. A top below the bottom draws the same rectangle; na means the box is not drawn. |
| right | series int | required | Right edge: a bar index, or a timestamp in milliseconds when xloc is xloc.bar_time. It may lie beyond the last bar; na means no drawing. |
| bottom | series int/ | required | Price of the bottom edge, in the chart's price units. na means the box is not drawn until a real price is set. |
Returns
series box: A handle to the new box. Keep it in a var variable to move, recolor or delete the box on later bars.
Remarks
- Create the box once with var and move its edges on later bars. Calling
box.new()on every bar builds a new rectangle each time and reaches the box cap within a few dozen bars. - On Sidechair: text_halign, text_valign, text_wrap, text_font_family and text_formatting are accepted without a warning but change nothing in the drawing. The text is centred on one line.
- On Sidechair: text_size draws 9 pixels for tiny, 10 for small, 12 for normal, 15 for large and 19 for huge. auto draws 11 pixels, and a number given instead of a constant falls back to auto.
- On Sidechair: boxes are painted above the candles in creation order, so a later box covers an earlier one where they overlap. force_overlay is stored but the box follows the indicator's overlay setting.
Examples
//@version=6
indicator("box.new: inside bars", overlay = true)
inside = high < high[1] and low > low[1]
if inside
box.new(bar_index - 1, high[1], bar_index, low[1], border_color = color.orange, bgcolor = color.new(color.orange, 85))//@version=6
indicator("box.new: labelled zone", overlay = true)
if timeframe.change("D")
box.new(bar_index, open * 1.002, bar_index + 12, open * 0.998, border_color = color.gray, bgcolor = color.new(color.gray, 85), text = "Open", text_color = color.white, extend = extend.right)See also
Runs on Sidechair