Beginner Trading Tutorial — Confirmed Close Location Average in TradingView

A candle can finish near its high even when its closing price is lower than the previous close. These are two different observations: one describes where the session ended within its own range, while the other measures a change between sessions. This tutorial builds a small Pine Script indicator that isolates the first observation and averages it across a configurable number of candles. It is useful for learning how to normalize price measurements, handle undefined inputs and keep a live indicator stable until the current candle closes.

Normalize the closing position

Subtract the low from the close and divide by the high minus the low. Multiply by 100 to express the result on a scale from zero to 100. A close at the low produces zero; a close at the high produces 100. A midpoint close produces 50. This calculation uses neither the open nor the previous close. It therefore measures a location, not a candle's return or its body size. If the high equals the low, the denominator is zero and the location is undefined. Treating such a candle as a midpoint would introduce an observation that was not actually measured.

Confirmed Close Location Average Indicator
Three hypothetical candles with the same high of 110 and low of 100. Closing prices of 102, 105 and 108 produce locations of 20%, 50% and 80% respectively.

Worked examples

Suppose the high is 110 and the low is 100. A close of 102 gives 100 × 2/10 = 20. A close of 105 gives 50, and a close of 108 gives 80. With an averaging length of three, these three observations produce an average of 50. If the next valid candle has a location of 90, the oldest observation of 20 leaves the window, and the new average becomes (50+80+90)/3 = 73.33. All prices in this example are hypothetical.

Now consider a flat candle with high, low and close all equal to 108. Its location is undefined. Under the strict window policy used here, the average is unavailable until the flat candle leaves the selected window. This is deliberately different from averaging the last three nonmissing observations, which could span more than three chart bars. The strict policy makes the time window predictable and highlights data limitations rather than hiding them.

Complete Pine Script v6 indicator

//@version=6
indicator("Confirmed Close Location", overlay=false)
length = input.int(10, "Average length", minval=1)
// A flat candle has no meaningful relative location.
float span = high - low
float location = span > 0 ? 100 * (close - low) / span : na
// Require a full window of valid observations.
float valid = math.sum(na(location) ? 0.0 : 1.0, length)
float mean = ta.sma(nz(location, 0), length)
float score = valid == length ? mean : na
// Hold the previous confirmed result during the live candle.
float confirmed = barstate.isconfirmed ? score : score[1]
plot(confirmed, "Confirmed location", color=color.blue, linewidth=2)
hline(50, "Midpoint", color=color.gray)
hline(80, "Upper reference", color=color.green)
hline(20, "Lower reference", color=color.red)

Understand each calculation

The indicator declaration creates a separate pane. The length input accepts positive integers, with ten as the default. The location expression checks the denominator before dividing. Pine's na value represents an unavailable observation. The math.sum call counts valid observations within the current window by summing ones and zeros. The ta.sma call receives a numeric series in which missing values are temporarily replaced by zero. This replacement is only a computational convenience: the average is exposed only when every observation is valid. As a result, a flat candle never contributes an artificial zero to a displayed average.

Both rolling calculations are evaluated on every bar. Keeping them outside conditional branches helps maintain consistent series history. The validity check also prevents an incomplete initial window from producing a misleading value. The plot is a continuous numeric series, and the horizontal lines provide fixed visual references. They are not optimized thresholds or automatic entry signals. The indicator contains no strategy orders and does not request a secondary timeframe.

Closed-bar behavior and repainting

On historical bars, barstate.isconfirmed is true. The plot therefore displays the final score associated with each historical candle. On a developing realtime candle, the score can change as the high, low and close update. Instead of plotting that temporary score, the confirmed expression returns the preceding bar's score. At the closing update it switches to the newly confirmed score. This design avoids displaying an unconfirmed numerical change as a completed observation. If the preceding score is unavailable, the held result is also unavailable.

The plot during a live candle is a held value, not a forecast of its eventual close location. Once the candle closes, the value at that bar may differ from what was visible while it was developing; this is the intended confirmation behavior. No future references or lookahead are used. Historical corrections, changes to session settings or synthetic chart construction can still alter recalculated values. A confirmed-bar policy cannot prevent changes to the underlying data itself.

Installation and practical checks

Open a TradingView chart and select Pine Editor. Create a new indicator, replace the template with the complete source and save it. Select Add to chart. The indicator should appear below the price chart. Start with a length of ten, then change it to one to compare individual candle locations against the Data Window. Use a regular candlestick chart for initial testing. The source has not been compiled or executed in TradingView here; confirm the build and behavior in your own editor before deployment.

For a manual check, find a candle whose high, low and close are easy to read. Calculate its location with a calculator and compare the result at length one. Repeat with a candle that closes near its high and one near its low. At length three, independently average three consecutive valid observations. Check a flat candle if your data contains one, and verify that the result remains unavailable for windows containing it. During an active market, observe the plot before and after a bar closes. It should retain the preceding confirmed score until the closing update. Reload the chart with the same settings and compare the completed observations.

Interpretation and limitations

A reading above 50 indicates that the average closing position is in the upper half of the individual candle ranges. It does not mean that every candle closed higher than its predecessor. A reading of 80 can arise during a falling market if candles repeatedly recover from their intrabar lows but still finish below earlier closes. Conversely, a low reading can appear during a rising market. Combining the location statistic with a separately defined trend measure may provide additional context, but the combination must be independently evaluated.

Every valid candle receives equal weight regardless of its absolute range. A one-tick candle can influence the average as much as a wide candle. This is a feature of normalization but may be undesirable in thin markets. The calculation also ignores volume, spreads, gaps between candles and the order of intrabar trades. On Heikin Ashi or other synthetic charts, the input values may not represent directly executable market prices. Use ordinary OHLC data when validating a price-based research idea, and avoid choosing the averaging length solely because it fits historical outcomes.

For range-weighted variants, alerts or integration with other studies, contact our Custom TradingView indicator Development Services.

Risk disclosure: educational material only. Trading involves risk of loss. Hypothetical examples do not establish profitability.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.