Operators/probuilder · probacktest · proorder · proscreener

XOR

XOR in ProBuilder is the exclusive OR operator, true when exactly one of two conditions is true. Covers syntax, the exclusivity rule and examples.

Syntax

probuilder
result = expression1 XOR expression2

How it works

XOR compares two boolean conditions and returns true only when they differ, that is when one is true and the other is false. If both conditions are true, or both are false, the result is false. This is the difference from OR, which is also true when both operands are true.

Like the other logical operators, XOR can be combined with AND, OR, and NOT. NOT binds tightest, then AND, then OR, so mixing XOR into a larger expression is clearer with parentheses. XOR evaluates the same way in ProBuilder indicators, ProScreener, ProBacktest, and ProOrder.

Examples

Example 1, Exactly one comparison true (Indicator)

probuilder
// c beats a but not b, or beats b but not a
a = 5
b = 7
c = 6
IF (c > a XOR c > b) THEN
    result = 1
ELSE
    result = 0
ENDIF
RETURN result

This is the pattern from the official reference. Here c is greater than a but not b, so exactly one comparison is true and result is set to 1. If c were larger than both, the XOR would be false.

Example 2, One breakout but not both (ProScreener)

probuilder
// New 20-bar high on close or on high, but not on both at once
newCloseHigh = close >= Highest[20](close)
newHigh = high >= Highest[20](high)
cond = newCloseHigh XOR newHigh
SCREENER[cond]

The filter returns instruments where only one of the two high measures printed a fresh extreme, excluding those where both fired together.

Example 3, Conflicting signals (ProOrder)

probuilder
DEFPARAM CumulateOrders = false
// Act only when one filter agrees and the other does not
fast = close > Average[20](close)
slow = close > Average[200](close)
IF NOT OnMarket AND (fast XOR slow) THEN
    BUY 1 CONTRACT AT MARKET
ENDIF

The entry allows a position only when the two moving-average filters disagree, one above and one below. When both agree, in either direction, the XOR is false and no order is placed.

Common errors and gotchas

  • XOR is not OR. The two behave the same unless both operands are true. When both are true, OR is true but XOR is false. Choose XOR only when you need to exclude the both-true case.
  • Each side must be a condition. close XOR high is invalid because those are values, not booleans. Use full comparisons on each side.
  • Precedence in mixed expressions. When XOR sits alongside AND or OR, wrap the parts to make the grouping explicit, as in Example 3.
  • Chaining is rarely what you want. a XOR b XOR c is true when an odd number of the three are true, which is seldom the intended meaning. Structure the logic with explicit parentheses instead.
  • OR, true when at least one operand is true, including both.
  • AND, true only when every operand is true.
  • NOT, inverts a single condition.
  • IF, the conditional block these operators usually sit in.
  • THEN, ELSE, structure the branches of an IF.
  • CROSSES OVER, CROSSES UNDER, conditions often combined with logical operators.