Trading Tips — Build a Confirmed Close Location Oscillator in TradingView

Where a candle finishes within its own range can provide information that its body size alone does not capture. A candle may open near its high, decline substantially and still finish in the upper portion of its range after recovering from a deeper low. This tutorial builds a bounded Pine Script oscillator to describe that location without confusing it with the direction of the real body. The study includes a moving average and confirmed threshold alerts, making it a practical example of how to separate calculation from publication in a realtime indicator.

Define the measurement

Subtract the low from the close and divide by the high minus the low. Multiply by two and subtract one. Under ordinary OHLC data this maps the low to minus one, the midpoint to zero and the high to plus one. A zero-range candle is assigned zero by convention. This convention avoids division by zero but treats an absence of range as a neutral observation, which can affect the moving average in inactive markets.

Confirmed Close Location Oscillator with Threshold Alerts
Three hypothetical candles with high 110 and low 100: closes of 108, 105 and 102 produce scores of +0.60, 0 and −0.60 respectively.

For the first example, the close is eight points above the low within a ten-point range. Twice 0.8 minus one equals 0.6. For the second example, the close is halfway through the range, giving zero. For the third, the close is two points above the low, giving minus 0.6. Notice that none of these examples requires the opening price. A bearish body can have a positive location score and a bullish body can have a negative score. These are not contradictory results; the two measurements answer different questions.

Complete Pine Script v6 indicator

//@version=6
indicator("Confirmed Close Location", overlay=false)
length = input.int(5, "Smoothing length", minval=1)
threshold = input.float(0.6, "Threshold", minval=0, maxval=1)
// Map the close within its own range to [-1, 1].
span = high - low
raw = span > 0 ? 2.0 * (close - low) / span - 1.0 : 0.0
// Calculate on every execution for consistent history.
smooth = ta.sma(raw, length)
// Suppress all output until the candle closes.
confirmed = barstate.isconfirmed
plot(confirmed ? raw : na, "Location", style=plot.style_columns,
     color=raw >= 0 ? color.teal : color.red)
plot(confirmed ? smooth : na, "Average", color=color.blue,
     style=plot.style_linebr)
hline(threshold, "Upper")
hline(-threshold, "Lower")
hline(0, "Center")
up = ta.crossover(smooth, threshold)
down = ta.crossunder(smooth, -threshold)
alertcondition(confirmed and up, "Upper crossing",
     "Confirmed average crossed upper threshold")
alertcondition(confirmed and down, "Lower crossing",
     "Confirmed average crossed lower threshold")

Separate calculation from confirmation

The raw series and its simple moving average are evaluated on every script execution. The average uses the most recent length observations, including the candidate candle. The output expressions then check barstate.isconfirmed before publishing either series. This is preferable to conditionally calling a history-dependent moving average only when a bar closes. The crossing functions are also evaluated on every execution and their Boolean results are gated by the same confirmation flag.

On historical bars the confirmation flag is true. On the realtime bar it becomes true on the closing update. As a result, the current histogram and average are hidden while the candle is developing, then appear on that candle when it closes. The line-break plotting style prevents the average from bridging missing observations. Horizontal reference levels remain visible because they are fixed inputs rather than calculated candle values.

Use smoothing without changing the meaning

With length set to three, suppose the latest confirmed raw scores are −0.6, 0 and +0.6. Their average is zero. This illustrates how the average can be neutral even when the latest candle closes relatively high. Increasing the smoothing length makes the average less responsive to individual observations, while reducing it makes the series more sensitive. The average remains bounded by minus one and plus one for ordinary input data. It is an average of individual candle locations, not the location of the latest close within the combined high-low range of the last several candles.

Installation and alert configuration

Open TradingView, select Pine Editor, create a new indicator and replace its contents with the supplied script. Save and add it to the chart. The oscillator appears in a separate pane. Set the smoothing length and threshold in the indicator settings. To create a notification, open the alert dialog and select the indicator's Upper crossing or Lower crossing condition. Choose once per bar close to match the intended timing. Existing alerts use a saved configuration snapshot, so recreate them after changing relevant settings. This source has not been compiled or executed in TradingView here.

Testing and repainting

Check candles with closes at the high, low and midpoint, then inspect a zero-range bar if available. Compare the Data Window against a manual calculation. Use a liquid instrument during an active session to observe the current bar: the plotted value should remain absent until confirmation. Test an average crossing above the positive threshold and below the negative threshold. A crossing requires the preceding average to be on or beyond the opposite side of the relevant boundary; merely remaining above the upper threshold does not create another crossing alert.

The code does not request future data or another timeframe. It does not intentionally revise confirmed values. Historical feed corrections, chart type changes and session configuration can nevertheless alter the underlying OHLC series. On synthetic charts such as Heikin Ashi, the calculation describes synthetic candle prices rather than conventional traded OHLC values. Use standard candles when validating the examples.

Limitations and extensions

The score ignores the absolute range, volume, spread and gaps between candles. A tiny candle closing at its high can score the same as a large candle closing at its high. Consider a separate minimum range or ATR filter if relative volatility matters. A sequence of positive readings may occur during a falling market because each candle can recover from its own low while the broader price trend declines. Do not treat the oscillator as proof of buying pressure or a standalone entry system. Test any additional conditions on independent data and account for trading costs. For customized alerts, multi-timeframe confirmation 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.