Built-in Constants
Numeric constants available in every Soma script without declaration. User-declared names (let, input, param) shadow these if they use the same identifier.
Mathematical Constants
| Name | Value | Description | Common use in trading |
|---|---|---|---|
| pi | 3.141592653589793 | π — ratio of circle circumference to diameter. | Cycle analysis, sinusoidal curve fitting. |
| tau | 6.283185307179586 | τ = 2π — one full revolution in radians. | Period normalisation in wave-based indicators. |
| e | 2.718281828459045 | Euler's number — base of the natural logarithm. | Continuous compounding, log-return calculations. |
| phi | 1.618033988749895 | φ — the golden ratio (1 + √5) / 2. | Fibonacci retracement and extension levels. |
| sqrt2 | 1.4142135623730951 | √2 — square root of 2. | Diagonal geometry, volatility scaling. |
| ln2 | 0.6931471805599453 | Natural logarithm of 2. | Doubling-time calculations: t = ln2 / r. |
| ln10 | 2.302585092994046 | Natural logarithm of 10. | Conversion between log bases. |
Sentinel Constants
Sentinel values for initialising variables to extreme or undefined states, and for testing NaN-safe logic.
| Name | JS value | Description |
|---|---|---|
| nan | NaN | Not-a-number. Propagates through arithmetic — any operation with nan produces nan. Use isnan() to test, or ?? to substitute a fallback value. |
| inf | Infinity | Positive infinity. Useful for initialising a "best minimum" accumulator: let best = inf. |
| ninf | -Infinity | Negative infinity. Useful for initialising a "best maximum" accumulator: let worst = ninf. |
Usage examples
# Find the rolling highest close in the last 'period' bars
param period as int = 20
input prices
output highest
for (i, v) in prices {
let best = ninf # start below any possible value
let j = 0
while j < period and (i - j) >= 0 {
let c = prices[j] # look-back indexing
if c > best { best = c }
j = j + 1
}
highest[i] = best
}
# Guard against division by NaN
let ratio = a / (b ?? 1) # use 1 if b is NaN
# Fibonacci retracement level
let fib618 = range_high - (range_high - range_low) * phi / (phi + 1)Name Shadowing
If a user-declared identifier has the same name as a constant, the declaration takes precedence within its scope. Constants are resolved from the global scope at the start of execution, so a let inside a for block only shadows the constant in that block.
# "pi" is shadowed inside this loop — global pi is untouched
for (i, v) in prices {
let pi = 3.14 # shadows the constant only within this block
result[i] = v * pi
}
# Outside the loop, pi is still 3.141592653589793
let circumference = 2 * pi * radius