Gain

Windowed · default period 20Statistics

The Gain node sums all positive first differences within the rolling window: for each step where the value rose, that rise is accumulated. The result is the total upward movement within the period. Alongside Loss, Gain forms the raw components for gain/loss ratio calculations and custom Relative Strength computations. Requires at least 2 non-null values.

Algorithm

  • Compute consecutive differences: d[i] = vals[i] − vals[i−1] for each step in window
  • Gain = Σ max(0, d[i]) — only accumulate positive changes
  • Returns null with fewer than 2 non-null values

Parameters

NameTypeDefaultDescription
periodnumber20Rolling window size.

Inputs & Outputs

PortTypeDescription
Inputs
inputnumber[]Source numeric array
Outputs
valuesnumber | nullComputed value at each bar; null before the warmup period completes
timestampsnumber[]Bar timestamps (UNIX ms), aligned 1-to-1 with values
Live mode: In live streaming mode the node updates only the last bar in-place rather than recalculating the full array, keeping CPU usage minimal during real-time data feeds.

Use Cases

Custom RSI Components

Compute Gain and Loss over the same period, then divide Gain/Loss to get the raw RS ratio. Feed into 100 − 100/(1+RS) to replicate RSI from primitives.

Directional Bias Score

Divide Gain by (Gain + Loss) to get an upward-movement fraction [0, 1] — a smooth directional bias indicator normalized to percentage.

Momentum Energy

Compare Gain and Loss magnitudes to detect whether recent price action has been predominantly advancing or declining over the window.