Diff¶
Math · Temporal
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.
Diff measures change: the current value minus the value N bars ago. It's the bar-level analogue of a derivative — the slope of whatever you feed it. Apply it to price and you get raw momentum; apply it to an indicator and you get that indicator's rate of change, which is often a sharper, earlier signal than the indicator itself.
How it works¶
Each bar the block outputs a − a.shift(bars) — the source now minus the source Lag (bars) ago (default 1). The first N bars have no prior value to compare against and emit NaN. The result is in the source's own units (an absolute difference), so its scale depends on what you fed in. For a percentage change instead, use % Change.
When to use it¶
Use Diff to measure the rate of change of anything. The slope of a moving average (Diff of an EMA) tells you whether the trend is steepening or flattening — frequently a better trend read than the MA's level. Diff of price is basic momentum. Diff of an oscillator highlights when it's accelerating. Pair the result with a Greater Than against zero to convert "rising/falling" into a condition.
Example¶
Is the trend steepening? Apply Diff (1 bar) to a 50-EMA to get its slope, then a Greater Than against 0 for "EMA rising" — a cleaner trend-direction condition than comparing price to the EMA. Backtest in the Tester.
Tips & gotchas¶
- It's a slope/derivative — the rate of change of whatever you feed it.
- Absolute units — for a comparable percentage, use % Change.
- First N bars are NaN — no prior value to difference.
- Diff of an MA = trend slope — one of its most useful applications.
Related blocks¶
Inputs¶
| Socket | Type | What to wire in |
|---|---|---|
| In | series / bars |
Source series. BARS input uses close. |
Outputs¶
| Output | Type | Plots as | Description |
|---|---|---|---|
| Δ | series |
Line · sub-pane | a − a.shift(bars). |
Parameters¶
| Parameter | Type | Default | What it does |
|---|---|---|---|
| Lag (bars) | number · 1–500 | 1 |
How many bars to look back. |
Reference auto-generated from the block catalog · category Math.