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
result = expression1 XOR expression2How 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)
// 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 resultThis 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)
// 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)
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
ENDIFThe 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,
ORis true butXORis false. ChooseXORonly when you need to exclude the both-true case. - Each side must be a condition.
close XOR highis invalid because those are values, not booleans. Use full comparisons on each side. - Precedence in mixed expressions. When
XORsits alongsideANDorOR, wrap the parts to make the grouping explicit, as in Example 3. - Chaining is rarely what you want.
a XOR b XOR cis true when an odd number of the three are true, which is seldom the intended meaning. Structure the logic with explicit parentheses instead.
Related instructions
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 anIF.CROSSES OVER,CROSSES UNDER, conditions often combined with logical operators.
