Skip to content

Math

Combine and transform series — arithmetic, comparisons, logic and helpers.

  • % Change — Per-bar percentage change — (current − N bars ago) ÷ (N bars ago). Output is a fraction (e.g. 0.01 = +1%), NOT a percent. Multiply by 100 if you want percent units. First N bars emit NaN; division-by-zero (prior value was 0) emits NaN.
  • Add — Add two series element-wise. Either input may be price BARS (uses close) or any SERIES. Useful for offsets and combining indicators ('EMA + ATR×N', 'high + buffer', etc.).
  • Clamp — Bound a value between a lower and upper series: min(max(value, lo), hi). Values below lo snap up to lo, above hi snap down to hi. NaN in any input yields NaN.
  • Cumulative Product — Running product from the first bar — e.g. compound a series of growth factors (1 + return) into an equity multiple. NaN values count as 1 (identity), so a gap doesn't zero everything after it.
  • Cumulative Sum — Running total of a series — at bar i, the sum of values from bar 0 through bar i. Useful for cumulative volume, P&L curves built from per-bar deltas, or any 'so far' metric.
  • Diff — Discrete difference — current value minus the value N bars ago. The bar-level analogue of a derivative; first N bars emit NaN. Useful for momentum, slope-of-anything, change-rate.
  • Divide — Divide A by B element-wise. Useful for ratios ('close / sma', 'short EMA / long EMA'). Divide-by-zero produces NaN (engine treats as 'no value').
  • EMA Smooth — Exponential moving average of ANY series (not just price) — smooth a derived signal, an indicator, a spread. α = 2 ÷ (length + 1), seeded at the first value (matches EMA with adjust=off). Expects a finite series.
  • Lag — Shift a series back by N bars — at bar i, emits the value from bar (i − N). Useful for 'price now vs price N bars ago' comparisons. First N bars emit NaN (no value to look back to).
  • Log Return — Per-bar log return — ln(current ÷ prior bar). The statistically correct return for comparing/normalising across assets and for compounding. First bar and non-positive ratios emit NaN.
  • Maximum — Element-wise maximum of two series — the larger of A and B at each bar. Use for 'the wider stop', 'floor a level'. NaN if either input is NaN.
  • Min-Max Normalize — Rolling min-max rescale to [0, 1]: (value − rolling min) ÷ (rolling max − rolling min). 0 = at the window's bottom, 1 = at the top. Constant windows (zero range) emit NaN. Useful for combining heterogeneous indicators on a common 0–1 scale.
  • Minimum — Element-wise minimum of two series — the smaller of A and B at each bar. Use for 'the nearer take-profit', 'cap a level'. NaN if either input is NaN.
  • Multiply — Multiply two series element-wise. Useful for scaling one series by another ('ATR × multiplier', 'volume × close').
  • Percent Rank — Percent (0–100) of the prior N bars that are below the current value — where the current value ranks in its own recent history. Regime / relative-strength scoring ('ATR in the top quartile of the last 100 bars'). NaN for the first N bars.
  • Rescale — Affine remap: linearly map a value from the input range [in_min, in_max] onto the output range [out_min, out_max] (not clamped — values outside the input range map proportionally beyond). e.g. remap an RSI's 0–100 onto −1…+1. Degenerate input range emits NaN.
  • Rolling Correlation — Pearson correlation of two series over the trailing N bars (−1…+1). For pairs / cross-asset relationships and confirming that two signals move together. NaN during warmup or when a window is flat.
  • Rolling Max — Highest value in the trailing N bars.
  • Rolling Mean — Simple moving average of the trailing N bars — the mean of any series, not just price (smooth an RSI, an ATR, a spread, etc.).
  • Rolling Median — Median of the trailing N bars — the outlier-resistant cousin of a rolling mean. Useful when one bad print would skew an SMA.
  • Rolling Min — Lowest value in the trailing N bars.
  • Rolling Percentile — Pth-percentile value within the trailing N bars. With percentile=50 acts as median; 75 reads the upper-quartile value; 95 surfaces near-extreme thresholds.
  • Rolling Range — Spread between rolling max and rolling min — a simple proxy for volatility / regime expansion.
  • Rolling Slope — Least-squares linear-regression SLOPE over the trailing N bars — the per-bar rate of change of the fitted line (trend strength & direction). Positive = rising trend, negative = falling. NaN during warmup.
  • Rolling Stdev — Population standard deviation of the trailing N bars (ddof=0, matching Pine's ta.stdev). Pairs with Rolling Mean for z-scores and Bollinger-style bands on any series.
  • Rolling Sum — Sum of the trailing N bars — a windowed total (distinct from Cumulative Sum, which runs from the first bar). Useful for counting/adding over a fixed lookback.
  • Round to Tick — Snap each value to the nearest multiple of tick (round half away from zero). Use to quantise a price to a broker tick size or a round-number grid — e.g. an SL/TP level that must land on a real tick for honest MT5/Pine execution.
  • Scalar Math — Combine a series with a fixed numeric value: add, subtract, multiply, or divide every bar by the same constant. Cheaper than Constant + BinaryOp when the second operand is genuinely fixed (offsets, unit conversions, scaling).
  • Standardize — Rolling z-score: (value − rolling mean) ÷ rolling stddev. Output expresses each bar as 'how many standard deviations from the local mean it sits' — comparable across instruments and indicators. Constant windows (zero stddev) emit NaN.
  • Subtract — Subtract B from A element-wise. Useful for spreads ('close − sma'), differences, and pair-trading constructions.
  • Unary Math — Apply a single-input transformation: absolute value, sign, negate, square root, natural log, exponential, reciprocal (1/x), floor, ceil, or round (half away from zero). Domain failures (sqrt of negative, log of non-positive, 1/0) emit NaN.