Graphical/probuilder

DRAWRECTANGLE

DRAWRECTANGLE in ProBuilder draws a rectangle from two opposite corners given as bar index and price, with an optional COLOURED outline and FILLCOLOR fill.

Syntax

probuilder
DRAWRECTANGLE(x1, y1, x2, y2) COLOURED(R, G, B, a)

Parameters

NameTypeDefaultDescription
x1integerrequiredX coordinate of the first corner, normally a bar index.
y1pricerequiredY coordinate of the first corner, a price or computed value.
x2integerrequiredX coordinate of the opposite corner.
y2pricerequiredY coordinate of the opposite corner.
R, G, Bintegerplatform defaultOptional COLOURED outline red, green and blue components, 0 to 255.
ainteger255Optional alpha (transparency) of the outline, 0 to 255.

How it works

DRAWRECTANGLE runs inside a ProBuilder indicator. It draws a rectangle whose two opposite corners are (x1, y1) and (x2, y2). The x coordinates are bar indices on the horizontal axis and the y coordinates are prices on the vertical axis, so the rectangle spans a range of both time and price. This suits marking consolidation zones, breakout areas or chart patterns.

The optional COLOURED clause sets the outline colour and transparency, and FILLCOLOR sets the interior fill. Because the object is redrawn on each bar, scripts usually set DEFPARAM DrawOnLastBarOnly = true.

Examples

Example 1, Highlight a tight range (Indicator)

probuilder
// Draw a red box around a 20-bar range if it stays inside a height limit
DEFPARAM DrawOnLastBarOnly = true
period = 20
boxheight = 20
hh = Highest[period](high)
ll = Lowest[period](low)
IF hh - ll <= boxheight * pointsize THEN
    FOR i = 0 TO period DO
        IF high[i] = hh THEN
            x2 = BarIndex[i]
        ENDIF
        IF low[i] = ll THEN
            x1 = BarIndex[i]
        ENDIF
    NEXT
    IF ABS(x2 - x1) >= period THEN
        DRAWRECTANGLE(x1, ll, x2, hh) COLOURED(255, 10, 10)
    ENDIF
ENDIF
RETURN

The box is drawn only when the range is narrow enough and the extremes are far enough apart.

Example 2, Rectangle with a filled interior (Indicator)

probuilder
// Green outline, purple fill, spanning the last five bars
DEFPARAM DrawOnLastBarOnly = true
DRAWRECTANGLE(BarIndex, close, BarIndex[5], close[5]) COLOURED(0, 200, 0) FILLCOLOR(255, 0, 255)
RETURN

FILLCOLOR shades the inside while COLOURED sets the border.

Example 3, Fixed reference zone (Indicator)

probuilder
// A grey semi-transparent zone between two price levels
DEFPARAM DrawOnLastBarOnly = true
upper = Highest[50](high)
lower = upper - 30 * pointsize
DRAWRECTANGLE(BarIndex-50, lower, BarIndex, upper) COLOURED(128, 128, 128, 120)
RETURN

A single call marks a fixed band across the last 50 bars.

Common errors and gotchas

  • Indicator only. DRAWRECTANGLE has no effect in ProScreener, ProOrder or ProBacktest. Keep it in a ProBuilder indicator.
  • X is a bar index. Passing a price into x1 or x2 misplaces the box horizontally. Use BarIndex or offsets from it.
  • Corners, not width and height. The four values are two opposite corners, not a position and a size.
  • Redraw cost. Without DrawOnLastBarOnly = true, the rectangle is drawn on every bar and slows the chart.