Let’s delve into the syntax, arguments, and application of `ta.roc()`

to understand how it can be effectively used in trading strategies.

### Syntax

The function is defined as follows:

ta.roc(source, length) → series float

### Arguments

**source (series int/float):**This is the series of values that the ROC will be calculated from. It can be the price of a security, volume, or any other indicator that provides a numeric series.**length (series int):**This is the number of bars back from the current bar that the ROC will be calculated over. It defines the period of the rate of change.

## Example

To illustrate the use of `ta.roc()`

, let’s consider calculating the 14-period rate of change of a stock’s closing price. We’ll modify the variable names for uniqueness in this example.

//@version=5 indicator("My Rate of Change Indicator", overlay=false) lengthVal = 14 priceSeries = close rocResult = ta.roc(priceSeries, lengthVal) plot(rocResult, title="14-Period ROC", color=color.blue)

In this example:

- We set the
`lengthVal`

variable to 14 to calculate the ROC over 14 bars. `priceSeries`

is assigned the closing price series of the stock using`close`

.`rocResult`

computes the rate of change using`ta.roc(priceSeries, lengthVal)`

.- Finally, we plot
`rocResult`

on the chart with a blue line to visualize the 14-period ROC.

### Walkthrough of Each Line

`indicator("My Rate of Change Indicator", overlay=false)`

: Defines the script as an indicator and ensures it is plotted on its own pane instead of overlaying on the price chart.`lengthVal = 14`

: Sets the period over which the ROC will be calculated to 14 bars.`priceSeries = close`

: Assigns the series of closing prices to`priceSeries`

for ROC calculation.`rocResult = ta.roc(priceSeries, lengthVal)`

: Calculates the ROC using the closing prices over the specified period.`plot(rocResult, title="14-Period ROC", color=color.blue)`

: Plots the ROC values on the chart for visualization.

### Key Features and Takeaways

- The
`ta.roc()`

function calculates the momentum of an asset by measuring the percentage change over a specified period. - It requires two arguments: the data series to calculate the ROC from (
`source`

) and the period over which to calculate the ROC (`length`

). - The function can handle
`na`

values gracefully, ensuring accurate calculations even with incomplete data series. - This tool is versatile and can be applied to various types of data, including price, volume, or other technical indicators, making it a valuable addition to a trader’s analysis toolkit.

By understanding and utilizing the `ta.roc()`

function in Pine Script, traders and analysts can enhance their strategies with insights into the momentum and rate of change of financial assets or indicators.