Rate of Change (ROC) in Pine Script
The percentage change of price over the last n bars, plotted as an unbounded oscillator around zero, so the line reads the speed and direction of price directly.
Definition
Rate of change (ROC) is the simplest momentum oscillator: the percentage difference between the current close and the close n bars ago. A reading of 5 means price is 5 percent higher than it was n bars back; a reading of −3 means 3 percent lower. There is no smoothing and no upper or lower limit, so it sits in the unbounded family with MACD and CCI. Its close relative, the momentum indicator, uses the raw price difference instead of the percentage, and the two draw the same shape on different scales.
ROC measures speed. A rising ROC says price is pulling further ahead of where it was n bars ago. A falling ROC above zero says the advance is slowing even though price is still higher than it was. Speed changes before direction does, which is the whole case for momentum oscillators: a trend usually decelerates before it reverses. The zero line is the line of no change over the window, so a cross above zero means price has finally passed its level from n bars ago.
There is no single origin to credit; ROC is as old as percentage arithmetic on price series. Platform defaults vary between 9, 10, 12 and 14 bars, and the library's script uses 12. Longer windows such as 20, 50 or 250 bars turn the tool into a trend gauge, and the 12-month rate of change is the standard measure in cross-sectional momentum studies of stocks. The window should match the swing you want to read, because ROC only compares two points and knows nothing about what happened between them.
Mechanically the line is jumpy. Every bar, one old close drops out of the comparison and one new close enters. A large bar leaving the window therefore moves ROC even when today's bar is quiet, an effect traders call drop-off. Readings cluster around zero in ranges and hold one side in trends. Because the scale is in percent, ROC on a volatile crypto pair reaches 20 where ROC on a bond future reaches 2, so any overbought level has to be set per instrument. The library's script draws ROC as columns and marks Long on the cross above zero and Short on the cross below.
Reading the chart
Read the sign for position against the past, the slope for what is happening now, and the chart's own history for the extremes.
- Add a 12-bar ROC in a pane with a zero line. Confirm whether the platform shows the percentage version or the raw momentum difference, since the shape is the same but the numbers differ.
- Read the sign. Above zero price is higher than it was 12 bars ago; below zero it is lower. Runs of one sign describe a trend on the window's scale.
- Read the slope against the sign. ROC rising above zero is a trend gaining speed; ROC falling but still above zero is a trend losing speed, which is the early warning.
- Mark the zero crosses. The library's script marks Long on the cross above zero and Short on the cross below, on confirmed bars; in a trend these come after pullbacks, in a range they come constantly.
- Find the instrument's own extremes. Look back at the last few months and note the ROC values at which past swings ended; those, not a fixed number, are the overbought and oversold levels for this chart.
- Check for drop-off. If ROC jumps with no matching price bar, a big bar just left the window; discount that change and wait for the next bar.
The maths
ROC compares the current close with the close n bars earlier and expresses the change as a percentage.
Momentum and ROC share the same sign and turning points; only the scale differs. Percent makes different instruments, and different eras of the same chart, comparable.
The first n bars of a chart have no value, and the calculation has no memory beyond the two closes it compares.
A signal line, usually a short EMA of ROC, is sometimes added so the tool has a cross like MACD; the library's script marks the zero-line cross instead.
A log version, 100 × ln(C_t / C_{t−n}), is symmetric for rises and falls and is preferred in quantitative work.
Inputs that matter
| Input | Typical | What changing it does |
|---|---|---|
| Lookback | 12 | Shorter windows make the line cross zero on every small swing; longer windows smooth the shape, delay the zero crosses and turn it into a trend gauge. |
| Source | close | Using hl2 or a moving average as the source reduces single-bar spikes; the drop-off effect remains whatever the source. |
In practice
- As a zero-line trend switch: the rule goes long on the cross above zero and short on the cross below, which is what the library's script marks as Long and Short. On a longer window this is a slow trend switch.
- As a speed read for trend health: a trend whose ROC peaks keep getting lower is decelerating, and the third lower peak is often where the pullback deepens into a reversal.
- As a per-instrument extreme gauge: note the ROC readings at which past swings ended on the chart and treat a return to those readings as stretched, since no fixed level fits every market.
- As a ranking tool across a watchlist: the same window on every symbol lets you sort by percentage momentum, which is how relative-strength lists are built.
- As a filter: the filter requires a positive 50 or 100-bar ROC before it passes long triggers from a faster tool, so short-term entries line up with the medium-term direction.
- As a divergence read: a higher price high with a lower ROC peak says the second push was slower, which is the same warning MACD gives with less lag and more noise.
Where it goes wrong
- Using a fixed overbought level. Percent moves differ by market and by era; set the level from the chart's own history.
- Reading a drop-off jump as new information. When a big bar leaves the window the line moves without price doing anything; look at the bars, not only the line.
- Trading zero crosses in a range. With the window shorter than the range, ROC crosses zero every few bars; lengthen the window or add a trend filter.
- Confusing speed with direction. ROC falling from 8 to 4 is still a rising market; it is the slope of ROC that warns, and price that confirms.
Compared with
MACD. MACD is the gap between two EMAs with a signal line, so it is smoothed and gives a cross signal; ROC is two closes compared with no smoothing. ROC turns earlier and jumps more; MACD lags but is cleaner. Both are unbounded and read the speed of a trend.
RSI. RSI is bounded and built from average gains and losses, so it reads how one-sided the closes have been and saturates in a trend. ROC is unbounded percent change and keeps climbing while a trend accelerates. RSI has fixed zones; ROC needs zones set per instrument.
Moving Average Slope. A slope indicator measures the change of a smoothed average over a few bars, often in ATR units, so it is a normalised and smoothed cousin of ROC. ROC compares raw closes in percent; the slope tool trades the earliness of ROC for a reading that is comparable across markets.
CCI. CCI divides the distance from a moving average by the instrument's own mean deviation, so its 100 lines mean the same thing everywhere; ROC is raw percent change with no normalisation. CCI reads distance from the average; ROC reads change over time, and only the chart's history says what a large ROC is.
Build it in Quant
Type one of these in the Quant desk, or click it. A request that fits a checked template is assembled on the spot without a credit; anything beyond the blanks goes to the copilot, which edits on top of the checked script.
Questions
What is the difference between rate of change and momentum?
The momentum indicator subtracts the close n bars ago from today's close and plots the difference in price units. Rate of change divides that difference by the old close and plots it in percent. The two have identical turning points and zero crosses; percent simply makes readings comparable across instruments and across years of the same chart when the price level has changed.
Which lookback should I use?
Match it to the swing. 9 to 14 bars reads the short swing and crosses zero often; 20 to 50 reads the intermediate trend; 100 to 250 on daily bars reads the long trend used in momentum studies. There is no best number. Pick the one whose zero crosses line up with the swings you can see, then leave it alone.
What does a negative ROC mean?
Price is below where it was n bars ago. It does not mean the market is falling now: ROC can be negative and rising while a decline is decelerating, which is often the start of a base. Read the sign for position relative to the past and the slope for what is happening now.
Is there an overbought level for ROC?
Not a universal one. ROC is in percent and unbounded, so a stretched reading on a volatile crypto pair might be 25 while on an index it is 5. Look back at the chart, note the readings where previous swings ended, and treat those as the zones for this instrument and timeframe. Revisit them when volatility changes.
What does the library's rate of change script do?
It computes the 12-bar percentage rate of change of the close, draws it as columns coloured green at or above zero and red below, and adds a dotted zero line. It marks Long when ROC crosses above zero and Short when it crosses below, on confirmed bars only, with an optional alert on each cross. The lookback is the single input.
Why did ROC jump when price barely moved?
Because the close from n bars ago changed. Each bar the comparison point moves forward one bar, and if a large bar just left the window the old close jumps, moving ROC without any new price action. This drop-off effect is inherent to two-point measures. MACD and smoothed oscillators do not show it, which is one reason they are smoother.