Instructions/probuilder · probacktest · proorder · proscreener

WEND

WEND marks the end of a WHILE loop in ProBuilder. When execution reaches WEND, the loop condition is evaluated again to decide whether to repeat or exit.

Syntax

probuilder
WHILE condition DO
  // statements repeated while the condition is true
WEND

How it works

WEND is not an independent instruction. It exists only as the terminator of a WHILE block, the same way NEXT terminates a FOR block and ENDIF terminates an IF block. Everything between the WHILE header and WEND forms the loop body.

When execution reaches WEND, control jumps back to the WHILE line and the condition is tested again. A true result runs the body once more, a false result transfers execution to the first statement after WEND. The condition is therefore checked once per pass, always at the top, never at WEND itself, so a WHILE loop cannot behave like a do-while loop that guarantees at least one pass.

A script with a WHILE that has no matching WEND does not compile. In nested loops, each WEND closes the nearest unclosed WHILE above it.

Examples

Example 1, Closing a counting loop (Indicator)

probuilder
i = 0
Count = 0
WHILE i <> 11 DO
  // each pass advances the counter by one
  i = i + 1
  Count = Count + 1
WEND
RETURN Count

Every time execution reaches WEND, the condition i <> 11 is tested again. After 11 passes the test fails, the loop ends at WEND, and the indicator returns 11.

Example 2, Bars since the last down close (ProScreener)

probuilder
n = 0
// step backward until a bar closed below its open
WHILE close[n] >= open[n] AND n < 50 DO
  n = n + 1
WEND
SCREENER[n >= 5] (n AS "Bars since down close")

WEND sends control back to the condition after each backward step. The screener lists instruments whose last 5 or more bars all closed at or above their open.

Example 3, Stepping a trailing level toward price (ProOrder)

probuilder
DEFPARAM CumulateOrders = false
IF NOT onmarket AND close CROSSES OVER Average[20](close) THEN
  BUY 1 CONTRACT AT MARKET
ENDIF
// raise the stop level in fixed steps while it lags the market
step = AverageTrueRange[14](close)
stopLevel = positionprice - 2 * step
WHILE stopLevel + 3 * step < close DO
  stopLevel = stopLevel + step
WEND
IF onmarket THEN
  SELL AT stopLevel STOP
ENDIF

The loop lifts the stop level one step at a time. Each pass ends at WEND, where the distance check decides whether another step is needed.

Common errors and gotchas

  • Missing WEND. Every WHILE requires exactly one WEND. The compiler often reports the problem at the end of the script rather than at the unclosed loop, so count WHILE and WEND pairs when the error location looks wrong.
  • Confusing WEND with ENDIF or NEXT. Each block type has its own terminator. Closing a WHILE with ENDIF, or an IF with WEND, is a syntax error even when the indentation looks consistent.
  • Expecting a bottom test. The condition is evaluated at the WHILE line, not at WEND. Code that relies on the body executing at least once must ensure the condition is true before the loop starts.
  • Statements after WEND inside the intended loop. Only lines between WHILE and WEND repeat. A statement placed after WEND by mistake runs once per bar instead of once per pass.
  • WHILE, opens the loop that WEND closes.
  • DO, keyword ending a WHILE or FOR header.
  • FOR, counter-driven loop closed by NEXT.
  • NEXT, the FOR equivalent of WEND.
  • BREAK, exits a loop without waiting for the condition to fail.
  • IF, conditional block closed by ENDIF.