# Indicator to identify the trending market - ROC - PATR ratio- code explanation

Updated: May 18

An example code and the explanation of the parameters in the indicator to help understand it and use it better

This code calculates the ROC-PATR Ratio with adaptive thresholds and plots it along with the threshold gap. Here's a breakdown of each section of the code:

1. Input settings:

- Define the input settings for the lookback period, EMA period, threshold multiple, and gap multiple.

2. Calculate the ROC:

- Compute the Rate of Change (ROC) of the closing prices using the specified lookback period.

3. Calculate the ATR:

- Compute the Average True Range (ATR) using the specified lookback period.

4. Calculate the Percentage ATR:

- Compute the percentage ATR by dividing the ATR by the closing prices and multiplying by 100.

5. Calculate the ROC-PATR Ratio:

- Compute the ratio by dividing the ROC by the percentage ATR.

6. Calculate the EMA of the ROC-PATR Ratio:

- Compute the Exponential Moving Average (EMA) of the ROC-PATR Ratio using the specified EMA period.

7. Calculate the adaptive threshold values:

- Compute the upper and lower adaptive thresholds by multiplying the ROC-PATR EMA by the threshold multiple and its negative counterpart, respectively.

8. Calculate the threshold gap:

- Compute the threshold gap by subtracting the lower threshold from the upper threshold.

9. Calculate the significant gap level:

- Compute the significant gap level by multiplying the gap multiple by the absolute value of the ROC-PATR EMA.

10. Plot the various components:

- Plot the ROC-PATR Ratio, ROC-PATR EMA, adaptive thresholds, threshold gap, and significant gap level on the chart.

This indicator helps to identify trending and non-trending periods by analyzing the ROC-PATR Ratio, its EMA, adaptive thresholds, and the threshold gap. When the ROC-PATR Ratio is above the upper threshold, it indicates a strong upward trend, and when it's below the lower threshold, it indicates a strong downward trend. The threshold gap can provide information about the intensity of the trend. A larger gap between the thresholds could indicate a stronger trend, and a smaller gap could indicate a weaker or non-trending period. The significant gap level provides a reference point to help identify when the threshold gap is considered large enough to signify a strong trend.

```
//@version=5
indicator("ROC-PATR Adaptive Threshold - Gap", shorttitle="ROC-PATR-AT-G", overlay=false)
lookback = input.int(20, title="Lookback Period", minval=1)
ema_period = input.int(20, title="EMA Period", minval=1)
threshold_multiple = input.float(1.5, title="Threshold Multiple", step=0.1)
gap_multiple = input.float(2, title="Gap Multiple", step=0.1)
// Calculate the ROC
roc = ta.roc(close, lookback)
// Calculate the ATR
atr = ta.atr(lookback)
// Calculate the Percentage ATR
patr = (atr / close) * 100
// Calculate the ROC-PATR Ratio
roc_patr_ratio = roc / patr
// Calculate the EMA of the ROC-PATR Ratio
roc_patr_ema = ta.ema(roc_patr_ratio, ema_period)
// Calculate the adaptive threshold values
upper_threshold = roc_patr_ema * threshold_multiple
lower_threshold = roc_patr_ema * -threshold_multiple
// Calculate the threshold gap
threshold_gap = upper_threshold - lower_threshold
// Calculate the significant gap level
significant_gap = gap_multiple * math.abs(roc_patr_ema)
// Plot the ROC-PATR Ratio, EMA, adaptive thresholds, and threshold gap
plot(roc_patr_ratio, title="ROC-PATR Ratio", color=color.blue, linewidth=2)
plot(roc_patr_ema, title="ROC-PATR EMA", color=color.orange, linewidth=1)
plot(upper_threshold, title="Upper Threshold", color=color.red, linewidth=1, style=plot.style_stepline)
plot(lower_threshold, title="Lower Threshold", color=color.green, linewidth=1, style=plot.style_stepline)
plot(threshold_gap, title="Threshold Gap", color=color.purple, linewidth=1, style=plot.style_line)
// Plot the significant gap level
line.new(x1=bar_index[0], y1=significant_gap, x2=bar_index, y2=significant_gap, color=color.gray, width=1, style=line.style_dotted)
```

An example of how it looks on the chart. The different colors of the line will help ome idetify the different curves in the plot. The indicator is in lower larger pane. Note how the trend is pointed out by the red and green step lines whereas the strength is the purple line which is used to define whether the trend is really in effect or not. The blue line is the ROC-PATR ratio itself which also helps in identifying the areas where the momentum is peaking out and the trend is likely to either halt or reverse. Higher volatility after a prolonged trending period generally indicates a reversal is in order.