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
DRAWRECTANGLE(x1, y1, x2, y2) COLOURED(R, G, B, a)Parameters
| Name | Type | Default | Description |
|---|---|---|---|
x1 | integer | required | X coordinate of the first corner, normally a bar index. |
y1 | price | required | Y coordinate of the first corner, a price or computed value. |
x2 | integer | required | X coordinate of the opposite corner. |
y2 | price | required | Y coordinate of the opposite corner. |
R, G, B | integer | platform default | Optional COLOURED outline red, green and blue components, 0 to 255. |
a | integer | 255 | Optional 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)
// 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
RETURNThe 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)
// 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)
RETURNFILLCOLOR shades the inside while COLOURED sets the border.
Example 3, Fixed reference zone (Indicator)
// 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)
RETURNA single call marks a fixed band across the last 50 bars.
Common errors and gotchas
- Indicator only.
DRAWRECTANGLEhas no effect in ProScreener, ProOrder or ProBacktest. Keep it in a ProBuilder indicator. - X is a bar index. Passing a price into
x1orx2misplaces the box horizontally. UseBarIndexor 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.
Related instructions
DRAWELLIPSE, draws an ellipse inside the same style of bounding box.DRAWTRIANGLE, draws a three-vertex shape.DRAWLINE, draws a line between two points.COLOURED, sets the outline colour and transparency.FILLCOLOR, sets the interior fill colour.DrawOnLastBarOnly, restricts drawing to the last bar for performance.BarIndex, supplies the x coordinates.Highest, finds a range top to enclose.
