Graphical/probuilder

DRAWSEGMENT

DRAWSEGMENT in ProBuilder draws a line segment between two chart points given as bar index and price coordinates, with an optional COLOURED colour.

Syntax

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

Parameters

NameTypeDefaultDescription
x1integerrequiredX coordinate of the start point, normally a bar index.
y1pricerequiredY coordinate of the start point, a price or computed value.
x2integerrequiredX coordinate of the end point.
y2pricerequiredY coordinate of the end point.
R, G, Bintegerplatform defaultOptional COLOURED red, green and blue components, 0 to 255.
ainteger255Optional alpha (transparency), 0 fully transparent to 255 fully opaque.

How it works

DRAWSEGMENT is an indicator instruction. It draws a straight segment from the start point (x1, y1) to the end point (x2, y2). The x values are bar indices along the horizontal axis and the y values are prices on the vertical axis. Unlike DRAWRAY, the segment ends exactly at its two points and does not extend past them.

The x coordinates typically come from BarIndex, and the y coordinates from prices or indicator values. Because the object is redrawn on each bar, most scripts set DEFPARAM DrawOnLastBarOnly = true so the segment is drawn once. The COLOURED clause is optional and sets colour and transparency.

Examples

Example 1, A single coloured segment (Indicator)

probuilder
// A semi-transparent red horizontal segment between two fixed points
DRAWSEGMENT(BarIndex-90, close, BarIndex, close) COLOURED(255, 0, 0, 150)
RETURN

Preserved from the source idea: a segment drawn between two coordinate pairs, coloured red at partial opacity.

Example 2, Trend segment between two lows (Indicator)

probuilder
// Connect the two most recent lows within 30 bars
DEFPARAM DrawOnLastBarOnly = true
period = 30
y2 = Lowest[period](low)
FOR i = 0 TO period DO
    IF low[i] = y2 THEN
        x2 = BarIndex[i]
    ELSIF low[i] < y2 THEN
        y1 = low[i]
        x1 = BarIndex[i]
    ENDIF
NEXT
DRAWSEGMENT(x1, y1, x2, y2) COLOURED(0, 120, 255)
RETURN

The loop finds two low points and the segment links them as a support line.

Example 3, Segment plus a label (Indicator)

probuilder
// Segment from the recent high to the current close, with a caption
DEFPARAM DrawOnLastBarOnly = true
hh = Highest[20](high)
DRAWSEGMENT(BarIndex-20, hh, BarIndex, close) COLOURED(200, 120, 0)
DRAWTEXT("range", BarIndex, close) COLOURED(200, 120, 0)
RETURN

DRAWSEGMENT and DRAWTEXT combine to mark and annotate the same area.

Common errors and gotchas

  • Indicator only. DRAWSEGMENT does nothing 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 segment. Use BarIndex or offsets from it.
  • Redraw cost. Without DrawOnLastBarOnly = true, the segment is drawn on every bar and slows the chart.
  • Bounded segment. It stops at its endpoints. For a line that extends past the second point use DRAWRAY.
  • DRAWLINE, draws a line between two points, close in behaviour to DRAWSEGMENT.
  • DRAWHLINE, a horizontal line at one price level.
  • DRAWVLINE, a vertical line at one bar index.
  • DRAWRAY, extends beyond its second point.
  • COLOURED, sets colour and transparency.
  • DrawOnLastBarOnly, restricts drawing to the last bar for performance.
  • BarIndex, supplies the x coordinates.
  • DRAWTEXT, adds a caption to the segment.