SGN
SGN in ProBuilder returns the sign of a number as -1 for negative, 0 for zero, or 1 for positive. Syntax, parameters and examples for direction logic.
Syntax
SGN(a)Parameters
| Name | Type | Description |
|---|---|---|
a | number | The numeric value or expression whose sign is returned. |
Formula
SGN(x) = -1 if x < 0, 0 if x = 0, 1 if x > 0The output is one of exactly three values regardless of how large or small the input is.
How it works
SGN reduces any value to its direction. SGN(-15) is -1, SGN(0) is 0, and SGN(10) is 1. It pairs with ABS, which returns magnitude, so value = SGN(value) * ABS(value) splits a number into direction and size.
The three-value output makes SGN convenient inside conditions and arithmetic. SGN(close - open) is +1 on up bars, -1 on down bars, and 0 on doji bars, which can be summed to build a simple direction score.
Examples
Example 1, Sign of three values (Indicator)
// Sign of a negative, a zero, and a positive input
variable1 = -15
variable2 = 0
variable3 = 10
sign1 = SGN(variable1) // -1
sign2 = SGN(variable2) // 0
sign3 = SGN(variable3) // 1
RETURN sign1 AS "neg", sign2 AS "zero", sign3 AS "pos"Each call collapses its input to -1, 0 or 1 based purely on sign.
Example 2, Direction score over recent bars (ProScreener)
// Net direction of the last three bar closes
score = SGN(close - close[1]) + SGN(close[1] - close[2]) + SGN(close[2] - close[3])
SCREENER[score >= 2](score AS "Up bars")Summing three SGN values yields a small integer score from -3 to 3 that measures how many of the recent bars moved up.
Example 3, Gate entries on momentum direction (ProOrder)
// Only go long while momentum direction is positive
mom = close - close[10]
IF SGN(mom) = 1 AND NOT LongOnMarket AND close CROSSES OVER Average[20](close) THEN
BUY 1 CONTRACT AT MARKET
ENDIFSGN(mom) = 1 isolates the direction of momentum without caring about its magnitude, keeping the filter simple.
Common errors and gotchas
- Returns only -1, 0 or 1.
SGNdiscards magnitude entirely.SGN(0.001)andSGN(1000)both return 1. UseABSwhen size matters. - Zero returns zero. An exactly zero input gives 0, which is neither positive nor negative. Account for this third case in comparisons rather than assuming only -1 or 1.
- Not a rounding function.
SGNdoes not round.SGN(3.7)is 1, not 4. UseROUND,CEILorFLOORfor rounding.
Related instructions
ABS, absolute value, the magnitude counterpart to the direction thatSGNreturns.MAX, larger of two values.MIN, smaller of two values.ROUND, rounds to the nearest integer or decimal.Momentum, raw price change often reduced to a sign withSGN.Variation, percentage change between bars.ROC, rate of change.
