box

Chart boxes are rectangular drawing objects that can display text, have customizable borders and backgrounds, and be positioned either by bar index or time coordinates. Use boxes to highlight price ranges, mark trade entry/exit zones, or annotate significant chart events.

Quick Example

  from pynecore.lib import (
    close, high, low, bar_index, ta, color, extend, xloc, 
    size, text, line, script
)
from pynecore.types import Persistent

@script.indicator(title="Box Drawer", overlay=True)
def main():
    # Create a box when a crossover occurs
    if ta.crossover(close, ta.sma(close, 20)):
        top_box: Persistent = None
        
        # Draw box from 5 bars ago to current bar
        box_obj = box.new(
            left=bar_index - 5,
            top=high,
            right=bar_index,
            bottom=low,
            border_color=color.green,
            border_width=2,
            border_style=line.style_solid,
            bgcolor=color.new(color.green, 80),
            text="Signal",
            text_color=color.white,
            text_size=size.small
        )
        top_box = box_obj
  

Functions

box.new()

Creates a new box object on the chart with specified dimensions, styling, and optional text label.

Overload 1: Using Chart Points

ParameterTypeDescription
top_leftchart.pointChart point specifying the top-left corner
bottom_rightchart.pointChart point specifying the bottom-right corner
border_colorcolor.ColorBorder color (default: color.blue)
border_widthintBorder width in pixels (default: 1)
border_styleline.LineEnumBorder style: line.style_solid, line.style_dotted, line.style_dashed
extendextend.ExtendExtend type: extend.none, extend.left, extend.right, extend.both
xlocxloc.XLocCoordinate system: xloc.bar_index (default) or xloc.bar_time
bgcolorcolor.ColorBackground color (default: color.blue)
textstrText label to display inside box
text_sizesize.SizeText size (default: size.auto): size.auto, size.tiny, size.small, size.normal, size.large, size.huge
text_colorcolor.ColorText color (default: color.black)
text_haligntext.AlignEnumHorizontal alignment: text.align_left, text.align_center, text.align_right
text_valigntext.AlignEnumVertical alignment: text.align_top, text.align_center, text.align_bottom
text_wraptext.WrapEnumText wrapping: text.wrap_none or text.wrap_word
text_font_familyfont.FontFamilyEnumFont family: font.family_default, font.family_monospace
force_overlayboolIf True, draw on main pane regardless of indicator placement
text_formattingtext.FormatEnumText formatting: text.format_none, text.format_bold, text.format_italic

Return type: box

  pt1: chart.point = chart.point(bar_index - 10, high)
pt2: chart.point = chart.point(bar_index, low)
b: box = box.new(top_left=pt1, bottom_right=pt2, bgcolor=color.new(color.green, 80))
  

Overload 2: Using Raw Coordinates

ParameterTypeDescription
leftintLeft edge bar index (if xloc=bar_index) or UNIX time in ms (if xloc=bar_time)
topfloatTop edge price level
rightintRight edge bar index or UNIX time
bottomfloatBottom edge price level
Other parametersSame as Overload 1

Return type: box

  b: box = box.new(
    left=bar_index - 5, 
    top=100.5, 
    right=bar_index, 
    bottom=99.0,
    border_color=color.red
)
  

box.copy()

Creates a clone of an existing box object with all properties copied.

ParameterType
idbox

Return type: box

  original: box = box.new(bar_index - 10, 105.0, bar_index, 100.0)
clone: box = box.copy(original)
  

box.delete()

Removes a box from the chart. If the box has already been deleted, does nothing.

ParameterType
idbox
  box.delete(my_box)
  

box.get_top()

Returns the price value of the box’s top border.

ParameterType
idbox

Return type: float

  b: box = box.new(bar_index - 10, 105.0, bar_index, 100.0)
top_price: float = box.get_top(b)  # 105.0
  

box.get_bottom()

Returns the price value of the box’s bottom border.

ParameterType
idbox

Return type: float

  b: box = box.new(bar_index - 10, 105.0, bar_index, 100.0)
bottom_price: float = box.get_bottom(b)  # 100.0
  

box.get_left()

Returns the left edge position: bar index (if xloc=bar_index) or UNIX time in milliseconds (if xloc=bar_time).

ParameterType
idbox

Return type: int

  b: box = box.new(bar_index - 10, 105.0, bar_index, 100.0)
left_idx: int = box.get_left(b)  # bar_index - 10
  

box.get_right()

Returns the right edge position: bar index or UNIX time in milliseconds.

ParameterType
idbox

Return type: int

  b: box = box.new(bar_index - 10, 105.0, bar_index, 100.0)
right_idx: int = box.get_right(b)  # bar_index
  

box.set_top()

Moves the top border to a new price level.

ParameterType
idbox
topfloat
  box.set_top(my_box, 110.0)
  

box.set_bottom()

Moves the bottom border to a new price level.

ParameterType
idbox
bottomfloat
  box.set_bottom(my_box, 99.0)
  

box.set_left()

Moves the left border to a new position (bar index or time depending on xloc).

ParameterType
idbox
leftint
  box.set_left(my_box, bar_index - 20)
  

box.set_right()

Moves the right border to a new position.

ParameterType
idbox
rightint
  box.set_right(my_box, bar_index + 5)
  

box.set_lefttop()

Sets both left and top coordinates in a single call.

ParameterType
idbox
leftint
topfloat
  box.set_lefttop(my_box, bar_index - 15, 108.5)
  

box.set_rightbottom()

Sets both right and bottom coordinates in a single call.

ParameterType
idbox
rightint
bottomfloat
  box.set_rightbottom(my_box, bar_index, 101.0)
  

box.set_top_left_point()

Moves the top-left corner to a new chart.point position.

ParameterType
idbox
pointchart.point
  pt: chart.point = chart.point(bar_index - 10, 110.0)
box.set_top_left_point(my_box, pt)
  

box.set_bottom_right_point()

Moves the bottom-right corner to a new chart.point position.

ParameterType
idbox
pointchart.point
  pt: chart.point = chart.point(bar_index, 100.0)
box.set_bottom_right_point(my_box, pt)
  

box.set_xloc()

Changes the coordinate system and updates both left and right edges.

ParameterTypeDescription
idbox
leftintNew left position
rightintNew right position
xlocxloc.XLocxloc.bar_index or xloc.bar_time
  box.set_xloc(my_box, bar_index - 10, bar_index, xloc.bar_index)
  

box.set_border_color()

Changes the border color of all four sides.

ParameterType
idbox
colorcolor.Color
  box.set_border_color(my_box, color.red)
  

box.set_border_width()

Changes the border width in pixels.

ParameterType
idbox
widthint
  box.set_border_width(my_box, 3)
  

box.set_border_style()

Changes the border style (solid, dotted, or dashed).

ParameterType
idbox
styleline.LineEnum
  box.set_border_style(my_box, line.style_dotted)
  

box.set_bgcolor()

Changes the box’s background color.

ParameterType
idbox
colorcolor.Color
  box.set_bgcolor(my_box, color.new(color.yellow, 50))
  

box.set_extend()

Sets how the box’s horizontal borders extend beyond left and right edges.

ParameterTypeDescription
idbox
extendextend.Extendextend.none (no extension), extend.left (extend left only), extend.right (extend right only), extend.both (extend both directions)
  box.set_extend(my_box, extend.both)
  

box.set_text()

Sets the text displayed inside the box.

ParameterType
idbox
textstr
  box.set_text(my_box, "Entry Zone")
  

box.set_text_color()

Changes the color of the text inside the box.

ParameterType
idbox
text_colorcolor.Color
  box.set_text_color(my_box, color.white)
  

box.set_text_size()

Changes the text size.

ParameterType
idbox
text_sizesize.Size
  box.set_text_size(my_box, size.large)
  

box.set_text_halign()

Changes the horizontal alignment of text inside the box.

ParameterType
idbox
text_haligntext.AlignEnum
  box.set_text_halign(my_box, text.align_left)
  

box.set_text_valign()

Changes the vertical alignment of text inside the box.

ParameterType
idbox
text_valigntext.AlignEnum
  box.set_text_valign(my_box, text.align_bottom)
  

box.set_text_wrap()

Sets the text wrapping mode.

ParameterType
idbox
text_wraptext.WrapEnum
  box.set_text_wrap(my_box, text.wrap_word)
  

box.set_text_font_family()

Changes the font family used for the text.

ParameterType
idbox
text_font_familyfont.FontFamilyEnum
  box.set_text_font_family(my_box, font.family_monospace)
  

box.set_text_formatting()

Sets text formatting attributes like bold or italic.

ParameterType
idbox
text_formattingtext.FormatEnum
  box.set_text_formatting(my_box, text.format_bold)
  

Variables

box.all

Returns an array containing all box objects currently drawn by the script.

Type: list[box]

  all_boxes: list[box] = box.all
num_boxes: int = len(all_boxes)
  

Compatibility

All box namespace functions are fully implemented in PyneCore. The namespace supports both bar index and time-based coordinate systems, comprehensive styling options, and text formatting.