Beginner Trading Tutorial — Confirmed Range Compression Ratio
Small candles are easy to spot on a chart, but deciding whether a candle is unusually small requires a reference. A two-point range might be ordinary during a quiet session and unusually narrow during an active one. This tutorial builds a Pine Script indicator that compares each completed candle's high-to-low range with the average range of preceding candles. The result is a dimensionless ratio, displayed in a separate panel, with an optional alert for confirmed compression.
Define the measurement
The current range is high minus low. The baseline is the arithmetic mean of the preceding N candle ranges, excluding the evaluated candle. Divide the current range by that baseline. A ratio of one means the range matches its reference; a ratio of 0.4 means it is 40 percent of the reference; a ratio of two means it is twice as wide. The default baseline length is twenty and the default compression threshold is 0.5. These are educational defaults, not optimized trading parameters.
Excluding the evaluated candle matters because including it would allow an unusually large or small observation to alter the reference against which it is judged. This distinction is particularly noticeable with short lookbacks. The preceding-bar baseline also remains fixed throughout the current candle, assuming historical data does not change.

Work through the numbers
Set the baseline length to three for this hypothetical example. The preceding ranges are 8, 10 and 12. Their sum is 30, and dividing by three gives 10. The evaluated candle has a high of 104 and a low of 100. Its range is 4, so its ratio is 4 divided by 10, or 0.40. With a threshold of 0.50, this candle qualifies as compressed. If the next candle has a range of 13, its baseline uses 10, 12 and 4, giving 26 divided by three. Its ratio is approximately 1.50, so it does not qualify. Notice that the reference evolves as observations enter and leave the window.
Complete Pine Script v6 indicator
//@version=6
indicator("Confirmed Range Compression Ratio", overlay=false)
length = input.int(20, "Baseline length", minval=1)
threshold = input.float(0.5, "Compression threshold", minval=0.01, maxval=1.0)
// Exclude the evaluated candle from its own baseline.
barRange = high - low
baseline = ta.sma(barRange[1], length)
// Undefined when history is insufficient or baseline is zero.
ratio = baseline > 0 ? barRange / baseline : na
// Publish only on the closing update.
confirmed = barstate.isconfirmed ? ratio : na
compressed = not na(confirmed) and confirmed <= threshold
plot(confirmed, "Range ratio", color=compressed ? color.teal : color.blue, style=plot.style_columns)
hline(1.0, "Baseline", color=color.gray)
hline(threshold, "Compression", color=color.teal)
alertcondition(compressed, "Confirmed compression", "Closed bar range is below or equal to threshold")
Understand the implementation
The indicator declaration creates a separate panel. The two inputs control the reference window and the threshold. The expression high minus low measures the intrabar range, while the history reference on barRange shifts the input series back one candle before the moving average is calculated. The moving average is evaluated on every bar, not only when a signal occurs. This preserves a consistent history of the calculation.
The conditional ratio expression avoids dividing by zero. When the baseline is unavailable during the initial warm-up period, the result is undefined. A zero current range is a valid zero ratio if the baseline is positive. The plot uses columns so that the magnitude is visible relative to the horizontal reference lines. Teal indicates a confirmed value at or below the threshold; blue indicates other valid observations. The horizontal lines represent the reference ratio of one and the user-selected threshold.
Closed-bar behavior and alerts
During an open realtime candle, the high and low can change as new trades arrive. An initially narrow candle can become very wide before it closes. The barstate.isconfirmed condition suppresses the current plot until the closing update. Historical bars are already confirmed, so their values appear normally. The alert condition uses the same confirmed value as the plot. When creating an alert, choose the named compression condition and a once-per-bar-close frequency. Consecutive compressed candles can each trigger an alert; this is a state condition, not a first-entry transition detector.
The script does not request higher timeframe data or use future observations. Under unchanged source data and settings, a confirmed historical ratio should not change merely because a later candle forms. Data revisions, different session settings, or changes in chart type can change the input series. Alerts created on TradingView retain their saved script and input configuration; recreate an alert after changing settings if you want the new configuration to apply.
Installation
Open a TradingView chart and select Pine Editor. Create a new indicator, replace the sample text with the complete code, save it, and add it to the chart. Open the indicator settings to adjust the baseline length and threshold. For the numerical example, choose length three and threshold 0.5. Use the Data Window to inspect exact values rather than relying only on the apparent column height. This example has not been compiled or executed in TradingView here.
Testing and interpretation
Start with a standard time-based candlestick chart. Check the first valid output after the baseline has accumulated enough preceding observations. Compare several ratios against manual calculations using the chart's high and low values. Test a flat baseline, a zero-range current candle, a ratio exactly equal to the threshold, and a large expansion following a narrow candle. During a live session, verify that the forming candle remains blank and that the output appears on its closing update. Reload the chart and compare confirmed observations under the same settings.
Range compression is a description of relative activity, not a prediction of direction. A narrow candle can precede a breakout, continued consolidation, or a reversal. The high-to-low range also ignores gaps from the previous close, unlike true range. On instruments with overnight gaps, the two measures can therefore tell different stories. Session boundaries, illiquid periods, and synthetic chart types may distort comparisons. Evaluate any proposed trading rule with independent samples, realistic execution costs, and different market conditions. For gap-aware baselines, custom session filters or alert transitions, contact our Custom TradingView indicator Development Services.
Risk disclosure: educational content only. Trading involves risk of loss. Hypothetical calculations do not establish profitability.