Intermediate Trading Tutorial — Confirmed Normalized Midpoint Displacement in TradingView
A candle's closing price is only one observation within its full trading range. Sometimes closes move sharply while the centers of successive trading ranges move very little. In other circumstances, the entire range migrates upward or downward even though the final candle has a relatively modest body. This tutorial develops a Pine Script indicator that measures the migration of candle midpoints over a configurable number of bars and scales the displacement by a separate historical range average. The objective is to express directional movement in units of typical prior candle ranges rather than raw price units. The study is descriptive and does not predict future returns.
Establish the formula
For each candle t, define midpoint M as (High + Low) divided by two. Select K as the displacement horizon. The signed numerator is M at t minus M at t−K. Separately calculate the arithmetic mean of High minus Low across the B candles immediately preceding t. The signal candle is excluded from this denominator. The normalized displacement equals the numerator divided by that mean when the denominator is positive and both endpoints are available.
A score of +1.5 means the midpoint moved upward by one and a half average prior candle ranges over K bars. A negative score indicates downward displacement. Zero indicates coincident endpoint midpoints, but does not mean price was stationary between them. Unlike a bounded oscillator, this ratio has no fixed upper or lower limit. A very small baseline can produce a large score even when the absolute displacement is modest.

Work through the numbers
For the diagram, set both displacement bars and prior range average to three. The four midpoint values are 102, 104, 106 and 108. The numerator is 108−102 = 6. Each of the first three candles has range four, so the denominator is (4+4+4)/3 = 4. The final result is +1.50. Reversing the order of these four range pairs produces a displacement of −6 with the same baseline, giving −1.50. These are independent hypothetical examples, not live market observations.
Now replace the final high/low pair in the first example with 114/102. Its midpoint is still 108, so the numerator remains six. The signal candle's range increases to twelve, but it does not enter the prior baseline; the score remains +1.50. This illustrates the difference between a baseline estimated before the signal and one that includes the signal itself. If the signal were included in a three-bar mean alongside two ranges of four, the denominator would instead be 20/3 and the score would be 0.90. Neither convention is inherently superior, but they answer different questions. The implementation intentionally uses the former.
Complete native Pine Script source
//@version=6
indicator("PS Confirmed Midpoint Displacement",
overlay = false, max_bars_back = 2000)
lookback = input.int(3, "Displacement bars",
minval = 1, maxval = 1000)
baseline = input.int(20, "Prior range average",
minval = 1, maxval = 1000)
threshold = input.float(1.0, "Alert threshold",
minval = 0.01, step = 0.1)
// The signal candle is excluded from the baseline.
float midpoint = (high + low) / 2.0
float priorMean = ta.sma(high - low, baseline)[1]
float displacement = midpoint - midpoint[lookback]
float raw = na
if not na(displacement) and not na(priorMean) and priorMean > 0
raw := displacement / priorMean
// Publish only completed observations.
float confirmed = barstate.isconfirmed ? raw : na
color shade = na(confirmed) ? color.gray :
confirmed >= 0 ? color.teal : color.maroon
plot(confirmed, "Displacement", color = shade,
linewidth = 2, style = plot.style_linebr)
hline(0, "Zero", color = color.gray)
hline(threshold, "Upper threshold", color = color.teal)
hline(-threshold, "Lower threshold", color = color.maroon)
// Evaluate crossing functions on every bar.
bool crossUp = ta.crossover(raw, threshold)
bool crossDown = ta.crossunder(raw, -threshold)
alertcondition(barstate.isconfirmed and crossUp,
"Confirmed upward crossing", "Midpoint shift crossed above threshold")
alertcondition(barstate.isconfirmed and crossDown,
"Confirmed downward crossing", "Midpoint shift crossed below threshold")
Implementation details and history
The script uses Pine Script version six and defines an indicator in a separate panel. Two integer inputs control independent aspects of the measurement. Displacement bars determines how far back to locate the earlier midpoint; prior range average controls the volatility normalization sample. They need not be equal. The alert threshold is a positive floating-point input used symmetrically above and below zero. It is not an optimized recommendation.
The expression ta.sma(high − low, baseline)[1] reads the moving average calculated on the preceding bar. Under ordinary complete OHLC data this average covers exactly the preceding B ranges. The current midpoint and midpoint[lookback] determine the signed displacement. The raw ratio remains unavailable while required history is missing or the baseline is zero. A zero numerator with a positive baseline, in contrast, is a legitimate zero reading. With normal complete data the earliest eligible chronological index is the larger of K and B, assuming the initial loaded bar is index zero.
The confirmed variable applies a separate timing rule. Historical candles are confirmed, while the active realtime candle becomes confirmed on its closing update. The plot uses line-break style so missing values are not deliberately joined across gaps. The crossing functions operate on the raw series on every bar, and their results are gated by confirmation. This avoids restricting the history-dependent crossing calculations to a conditional branch. An upward crossing requires a current value above the positive threshold and a preceding value at or below it. A downward crossing uses the analogous negative threshold. Remaining above a threshold is not a new crossing.
Installation and alert setup
Open a chart in TradingView and select Pine Editor. Create a new indicator, replace the editor contents with the complete source, save it under a descriptive name, and add it to the chart. For the numerical illustration, set both integer inputs to three. Use the Data Window to inspect completed-bar values. Create an alert using one of the named indicator conditions and choose Once Per Bar Close when that frequency is offered. Alerts operate on realtime data and do not retroactively send notifications for historical crossings. Recreate existing alerts after changing the script or its inputs because the server-side alert uses its saved configuration. The source has not been compiled or executed in TradingView here.
Closed-bar timing and repainting
During a live candle, its high and low can change, so its midpoint and raw displacement can also change. The public plot stays unavailable until that candle is confirmed. The displayed value is associated with the signal candle, but it should not be treated as known earlier within that candle. The formula accesses only current and historical data and does not intentionally repaint completed observations under unchanged inputs. Historical data revisions, changes in chart type, adjustments and session settings can nevertheless alter the input series. Synthetic chart types may have OHLC values that do not represent directly executable market prices.
Testing and limitations
Recreate the two directional examples and the expanded final range example in a spreadsheet. Test insufficient history, a flat prior baseline, coincident endpoint midpoints and extreme displacement. In realtime, confirm the absence of a provisional plotted value and check that alerts occur only at closing crossings. Compare identical historical datasets before and after reloading. Test displacement and baseline periods independently to understand their sensitivity. The score ignores path length between endpoints, closing-price location, traded volume and execution costs. It is not ATR-normalized because the denominator uses High minus Low and excludes interbar gaps. Evaluate any proposed trading filter on independent data across market regimes and account for slippage and commissions.
For additional true-range normalization, alert logic, or multi-timeframe adaptations, contact our Custom TradingView indicator Development Services.
Risk disclosure: educational material only. Trading involves risk of loss; hypothetical examples do not establish profitability.