Logo
OFFLINEPIXEL
/ pinescript-strategies / pine-script-roc

$ Pinescript ROC (Rate of Change)

Master this fundamental momentum indicator in TradingView's Pinescript for identifying trend strength, overbought/oversold conditions, and divergence.

500+ Clients Helped
100% Satisfaction
Live Trading Ready
⚠️
Trading financial markets carries risk. All content (PineScript code, indicators, strategies) on this website is for educational purposes only. Past performance is not indicative of future results. By using any code or information from this site, you agree that you are solely responsible for your trading decisions. The author disclaims all liability for any losses incurred. To gain from experts experiences, You can always try our Invite Only Scripts
01

What is the Rate of Change (ROC)?

The Rate of Change (ROC) is a pure momentum oscillator that measures the percentage change in price over a specified period. It indicates the speed and direction of price movement, helping traders identify trend strength and potential reversals. Unlike bounded oscillators (like RSI or Stochastic), ROC oscillates around a zero line and has no upper or lower limits, allowing it to reflect the full extent of price surges or drops.

In Pinescript, ROC is a straightforward yet powerful tool for assessing how quickly price is accelerating or decelerating, providing insights into momentum shifts and trend conviction.

02

Components and Calculation

The calculation of ROC is simple:

  1. Current Close (C): The most recent closing price.
  2. Previous Close (C_n): The closing price `N` periods ago.
  3. ROC Formula:
    `ROC = ((Current Close - Close N periods ago) / Close N periods ago) * 100`
    A positive ROC value indicates that the current price is higher than `N` periods ago, signaling bullish momentum. A negative ROC value indicates the current price is lower, signaling bearish momentum. The further away from zero, the stronger the momentum.
03

Basic Rate of Change (ROC) Implementation in Pinescript

pine-script@terminal
//@version=5
indicator("My Rate of Change Indicator", overlay=false) // overlay=false to plot in a separate pane

// Input for ROC length
length = input.int(14, title="ROC Length", minval=1)

// Calculate ROC value using the built-in function
rocValue = ta.roc(close, length)

// Plot the ROC line
plot(rocValue, title="ROC", color=color.blue, linewidth=2)

// Plot the Zero Line
hline(0, "Zero Line", color.gray, linestyle=hline.style_dotted)

// Optional: Add typical extreme levels for visual reference (adjust based on asset)
hline(10, "Upper Reference (+10)", color.red, linestyle=hline.style_dashed)
hline(-10, "Lower Reference (-10)", color.green, linestyle=hline.style_dashed)
$ ✓ Compiled successfully
04

Zero Line is Key

Zero Line is Key

The zero line is the most important level for ROC. Values above zero indicate upward momentum, values below zero indicate downward momentum.

05

Practical ROC Trading Strategies

06

1. Zero Line Crossover (Momentum Shift)

pine-script@terminal
//@version=5
strategy("ROC Zero Line Crossover Strategy", overlay=true)

// Input for ROC length
length = input.int(14, title="ROC Length", minval=1)
zeroLine = 0.0

// Calculate ROC
rocValue = ta.roc(close, length)

// Plot ROC in a separate pane
plot(rocValue, "ROC", color.blue, display=display.pane_only)
hline(zeroLine, "Zero Line", color.gray, linestyle=hline.style_dotted, display=display.pane_only)

// Define conditions for entries
longSignal = ta.crossover(rocValue, zeroLine)
shortSignal = ta.crossunder(rocValue, zeroLine)

// Strategy entries/exits
if (longSignal)
    strategy.entry("Long", strategy.long)

if (shortSignal)
    strategy.entry("Short", strategy.short)
$ ✓ Compiled successfully
07

2. Extreme Readings (Overbought/Oversold)

pine-script@terminal
//@version=5
indicator("ROC Extreme Readings Scanner", overlay=true)

// Input for ROC length
length = input.int(14, title="ROC Length", minval=1)

// Define custom overbought/oversold levels based on asset's historical behavior
// Adjust these values after observing your chosen asset's ROC range
customOverbought = input.float(15.0, title="Custom Overbought Level")
customOversold = input.float(-15.0, title="Custom Oversold Level")

// Calculate ROC
rocValue = ta.roc(close, length)

// Plot ROC in a separate pane
plot(rocValue, "ROC", color.blue)
hline(0, "Zero Line", color.gray, linestyle=hline.style_dotted)
hline(customOverbought, "Custom Overbought", color.red, linestyle=hline.style_dashed)
hline(customOversold, "Custom Oversold", color.green, linestyle=hline.style_dashed)

// Signals for potential reversals from extremes
alertcondition(ta.crossunder(rocValue, customOverbought), "ROC Overbought Exit", "ROC moved below custom overbought level.")
alertcondition(ta.crossover(rocValue, customOversold), "ROC Oversold Exit", "ROC moved above custom oversold level.")

plotshape(ta.crossunder(rocValue, customOverbought) and rocValue[1] > customOverbought, title="Overbought Reversal", location=location.abovebar, color=color.red, style=shape.triangledown, size=size.small)
plotshape(ta.crossover(rocValue, customOversold) and rocValue[1] < customOversold, title="Oversold Reversal", location=location.belowbar, color=color.green, style=shape.triangleup, size=size.small)
$ ✓ Compiled successfully
08

3. ROC Divergence Strategy

pine-script@terminal
//@version=5
indicator("ROC Divergence Scanner", overlay=true)

// Input for ROC length
length = input.int(14, title="ROC Length", minval=1)

// Calculate ROC
rocValue = ta.roc(close, length)

// Plot ROC in a separate pane
plot(rocValue, "ROC", color.blue)
hline(0, "Zero Line", color.gray, linestyle=hline.style_dotted)

// Simple divergence detection (conceptual, robust detection requires advanced pivot logic)
// This is a simplified example focusing on price vs ROC divergence.

// Bullish Divergence (Price lower low, ROC higher low)
bullishDivergence = close[2] > close[1] and close[1] > close and rocValue[2] < rocValue[1] and rocValue[1] < rocValue

// Bearish Divergence (Price higher high, ROC lower high)
bearishDivergence = close[2] < close[1] and close[1] < close and rocValue[2] > rocValue[1] and rocValue[1] > rocValue

plotshape(bullishDivergence, title="Bullish Divergence", location=location.belowbar, color=color.green, style=shape.triangleup, size=size.small)
plotshape(bearishDivergence, title="Bearish Divergence", location=location.abovebar, color=color.red, style=shape.triangledown, size=size.small)

alertcondition(bullishDivergence, "Bullish ROC Divergence", "Potential bullish reversal based on ROC divergence.")
alertcondition(bearishDivergence, "Bearish ROC Divergence", "Potential bearish reversal based on ROC divergence.")
$ ✓ Compiled successfully
09

Optimizing ROC Performance

To get the most from the Rate of Change in Pinescript:

  • Parameter Tuning: The `length` parameter is crucial. Shorter lengths (e.g., 5-10) make ROC more sensitive and suitable for short-term momentum shifts, while longer lengths (e.g., 20-30) provide smoother signals for longer-term trends. Experiment to find optimal settings for your specific asset and timeframe.
  • Combine with Trend Filters: ROC is a momentum indicator and can be noisy in sideways markets. Use a trend-following indicator (like a moving average) on the price chart to confirm the underlying trend direction before acting on ROC signals.
  • Identify Custom Levels: Since ROC is unbounded, observe its historical behavior for the asset you are trading to identify typical "extreme" overbought/oversold zones. These might not be fixed percentages but rather visual areas where price has historically reversed.
  • Multi-Timeframe Analysis: Confirm ROC signals on a higher timeframe for added reliability. A strong ROC signal on a 4-hour chart, confirmed by a similar signal or trend on a daily chart, adds conviction.
  • Confluence with Price Action: Always look for ROC signals to be confirmed by price action, such as candlestick patterns, breaks of support/resistance, or chart patterns.
10

Relative Power

Relative Power

ROC tells you not just *if* price is changing, but *how fast* that change is occurring relative to a past point, giving a sense of the momentum's power.

11

Common ROC Pitfalls

  • Whipsaws in Ranging Markets: In choppy or consolidating markets, ROC can oscillate frequently around the zero line, leading to many false signals.
  • No Fixed Overbought/Oversold Levels: The unbounded nature of ROC means there are no universal overbought/oversold lines like 70/30 for RSI. This requires more subjective analysis or historical tuning.
  • Lag: Despite being a momentum indicator, ROC still has some inherent lag as it compares current price to a past price.
  • Divergence Can Be Early: While divergence is powerful, it can appear early, and the trend might continue for some time after the divergence appears, leading to premature entries.
  • Not a Standalone Indicator: ROC should always be used as part of a broader trading system, combined with other indicators and price action analysis for confirmation.
12

Conclusion

Conclusion

The Rate of Change (ROC) is a fundamental and insightful momentum oscillator in Pinescript for TradingView. Its straightforward calculation provides a clear measure of price acceleration and deceleration, making it valuable for identifying shifts in momentum, confirming trend strength, and spotting potential reversals through divergence. By understanding its mechanics, thoughtfully tuning its parameters, and integrating it strategically with other technical analysis tools, you can leverage the ROC to gain a deeper understanding of market dynamics and enhance your trading decisions.

Enhance Your Trading

Get a high-performance Pinescript analysis tool for actionable market insights, designed for traders on the move.

This strategy runs in live mode on TradingView, helping you identify potential opportunities.

Get Pinescript Strategy