Gain
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
| Name | Type | Default | Description |
|---|---|---|---|
| period | number | 20 | Rolling window size. |
Inputs & Outputs
| Port | Type | Description |
|---|---|---|
| Inputs | ||
| input | number[] | Source numeric array |
| Outputs | ||
| values | number | null | Computed value at each bar; null before the warmup period completes |
| timestamps | number[] | Bar timestamps (UNIX ms), aligned 1-to-1 with values |
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.