DocsStrategy APIindicators

ROC (Rate of Change)

ROC measures the percentage change between the current price and the price N periods ago. It oscillates around zero, with positive values indicating upward momentum.

Declaration

function init(ctx) { ctx.indicator('roc', 'ROC', { period: 12, source: 'close' }); }

Options

OptionTypeDefaultDescription
periodnumber12Lookback period
sourcestring'close'Price source

Output

Returns a single number[] series (percentage values).

Accessing Values

function onBar(ctx, i) { const roc = ctx.ind.roc[i]; // e.g., 2.5 = 2.5% gain over period }

Use Cases

Momentum Filter

function onBar(ctx, i) { const roc = ctx.ind.roc[i]; // Only take trades when momentum is positive if (roc > 0 && q.crossOver(ctx.ind.fast, ctx.ind.slow, i)) { ctx.order.market('ASSET', 1, { signal: 'buy', reason: 'positive_momentum' }); } }

Zero-Line Cross

function onBar(ctx, i) { const roc = ctx.ind.roc[i]; const prevRoc = ctx.ind.roc[i - 1]; if (prevRoc <= 0 && roc > 0) { ctx.order.market('ASSET', 1, { signal: 'buy', reason: 'roc_positive' }); } }

Calculation

$$ROC = \frac{Price_t - Price_{t-N}}{Price_{t-N}} \times 100$$

Related

  • TRIX - Smoothed rate of change
  • MACD - Momentum via EMA difference
  • RSI - Bounded momentum oscillator
indicatorrocmomentumrate-of-changeqsl