gain
Positive price change (gain)
gain(source)
Measures the positive change from the previous bar. When price rises, gain() returns the amount of the increase. When price falls or stays flat, it returns 0. This isolates upward movement for analysis, and is a core building block for RSI and other momentum indicators.
Parameters
- source (field): Data series to measure (typically close)
Formula
gain = max(source - source[1], 0)
If today's price > yesterday's: gain = difference
If today's price <= yesterday's: gain = 0
Examples
Filter for Up Days
Find stocks that closed higher than the previous day:
// stock gained today
gain(close) > 0;
Significant Upward Movement
Find stocks with large single-day gains:
// gained more than $2 today
gain(close) > 2;
Cumulative Gains
Track total gains over a period using sum():
// total gains over last 14 days
sum(gain(close), 14) > 10;
Gain vs Loss Comparison
Compare today's upward vs downward movement:
// today was an up day (gain exceeded loss)
gain(close) > loss(close);
Returns
Float >= 0 (the positive price change, or 0 if price didn't rise)
Notes
- Always returns a non-negative value
- Returns 0 on the first bar (no previous bar to compare)
- Use with sum() or wilderma() to aggregate gains over time