TradingView indicator calculations

Ultimate Oscillator: check the three-window calculation

The Ultimate Oscillator combines buying-pressure-to-true-range ratios from three overlapping lookbacks, weighted 4:2:1. TradingView defaults to 7, 14, and 28 bars. Check the underlying sums before interpreting the line: a correct calculation still needs a separate rule for signal confirmation and trade exits.

Build a UO audit script

Compute the ratios before applying the weights

For each bar, let P be the previous close. BP = close - min(low, P), and TR = max(high, P) - min(low, P). For a window n, R(n) = sum(BP, n) / sum(TR, n). UO = 100 × [4R(short) + 2R(medium) + R(long)] / 7. The source is TradingView's Ultimate Oscillator documentation, linked below.

  • These are overlapping windows on the selected chart interval. Selecting 7/14/28 does not itself request three different chart resolutions.
  • A gap affects the comparison with P. Replacing TR with high minus low changes the result whenever P lies outside the current range.

Four hypothetical bars, including a gap

Assume standard candles, a previous close of 100 before bar 1, and completed observations. Prices are invented units. The short, medium, and long lengths are 2, 3, and 4 for this arithmetic exercise, not a parameter recommendation. Open prices are omitted because the formula does not use them.

  • Bar 3: BP = 101 - 100 = 1; TR = 105 - 100 = 5. The previous close lies above the high.
  • Bar 4: BP = 107 - 101 = 6; TR = 108 - 101 = 7. Using only high minus low would incorrectly give TR = 5.
BarPrevious closeHighLowCloseBPTR
11001049910345
210310610210534
310510410010115
410110810310767

Reproduce the weighted value of 60.7143

At bar 4, each window ends on the same observation. Keep full precision until the final display. The result is 100 × [4 × (7/12) + 2 × (10/16) + 14/21] / 7 = 60.7142857.

  • Do not average the individual BP/TR fractions. For bars 3 and 4, (1/5 + 6/7) / 2 = 0.5285714, which differs from the required 7/12.
  • For valid OHLC data, 0 ≤ BP ≤ TR. Positive window denominators therefore keep each ratio between zero and one and UO between zero and 100. This is an algebraic check, not a price forecast.
WindowIncluded barsBP sumTR sumRatioWeight
Short: 23, 41 + 6 = 75 + 7 = 120.58333334
Medium: 32, 3, 43 + 1 + 6 = 104 + 5 + 7 = 160.62500002
Long: 41, 2, 3, 44 + 3 + 1 + 6 = 145 + 4 + 5 + 7 = 210.66666671

Compare one window with three using a controlled change

Change only bar 4's close from 107 to 103, leaving all earlier bars and current high/low fixed. Its BP falls from 6 to 2; TR stays 7. This is a second hypothetical completed-bar scenario. It is not a measured market experiment.

  • The composite moves less in this example because the longer denominators dilute the changed BP. All three windows still contain bar 4, so none is an independent confirmation.
  • Longer memory also retains older observations. It can delay a threshold crossing, and expiring bars can move the ratios even when the newest bar looks ordinary.
  • This one calculation does not establish fewer false signals or better returns. Test signal counts, timing, and outcomes separately on a specified dataset.
CalculationClose = 107Close = 103Change
Short only: 100 × R(2)58.333325.0000-33.3333
Medium ratio10/166/16-4/16
Long ratio14/2110/21-4/21
Weighted UO60.714331.8027-28.9116

Separate divergence confirmation from a level reading

TradingView describes bullish divergence as a lower price low with a higher UO low, an initial UO low below 30, then a break above the intervening UO high. The bearish case reverses the pattern, starts above 70, and confirms below the intervening low. A threshold reading alone does not complete that sequence.

  • If swing detection waits for two bars to the right of a pivot, the pivot becomes known two bars later. Save that detection time; placing a historical label on the pivot is not permission to trade there.
  • Before automating the example, choose swing selection, maximum candidate age, cancellation rules, and whether a new lower low replaces or invalidates the candidate. Those choices are not supplied by the weighted formula.
Hypothetical observationWhat is known at that time
Price lows 100 then 98; corresponding UO lows 25 then 32; intervening UO high 48A bullish candidate exists under the stated swing selection. UO at 45 has not broken 48.
UO reaches 49 intrabar but closes at 47A live crossing occurred, but a close-confirmed crossing did not.
A later bar closes with UO at 49The chosen close-confirmed rule triggers then. It does not make an earlier entry available.
Price later falls below 98The confirmed pattern can still fail. A stop or invalidation policy must already be defined.

Audit the calculation in Pine Script v6

Paste this indicator into Pine Editor and compare its Data Window values with the built-in indicator using matching settings. It plots a live UO line and a gray short-window ratio. Its optional alert is a close-confirmed crossing above 50, not the divergence sequence described above. It places no orders.

  • Use 7/14/28 for the standard comparison. Use 2/3/4 only when independently reproducing the invented table; real chart prices will not reproduce its 60.7143 result.
  • The first dataset bar lacks a previous close. The script waits for longLength subsequent bars before displaying UO, and returns unavailable when any required range sum is zero.
  • The live line can change before close. barstate.isconfirmed gates the alert on the completed chart bar; it does not freeze the plotted live value.
  • Check gaps, flat-price windows, and the first available output as well as ordinary bars. Syntax validation does not establish numerical agreement with a particular market feed.
Calculation audit with ordered inputs, missing-history protection, zero-denominator handling, and a chart-bar-close alert condition.pine
//@version=6
indicator("UO calculation audit", overlay = false)
shortLength = input.int(7, "Short length", minval = 1)
mediumLength = input.int(14, "Medium length", minval = 2)
longLength = input.int(28, "Long length", minval = 3)
if shortLength >= mediumLength or mediumLength >= longLength
    runtime.error("Use short < medium < long.")
base = math.min(low, close[1])
bp = close - base
tr = math.max(high, close[1]) - base
ratio(int length) =>
    pressureSum = math.sum(bp, length)
    rangeSum = math.sum(tr, length)
    rangeSum > 0 ? pressureSum / rangeSum : na
shortRatio = ratio(shortLength)
mediumRatio = ratio(mediumLength)
longRatio = ratio(longLength)
ready = bar_index >= longLength
uo = ready ? 100 * (4 * shortRatio + 2 * mediumRatio + longRatio) / 7 : na
plot(uo, "UO", color = color.blue, linewidth = 2)
plot(100 * shortRatio, "Short window only", color = color.gray)
plot(bp, "BP", display = display.data_window)
plot(tr, "TR", display = display.data_window)
plot(shortRatio, "Short ratio", display = display.data_window)
plot(mediumRatio, "Medium ratio", display = display.data_window)
plot(longRatio, "Long ratio", display = display.data_window)
hline(70, "Upper reference")
hline(50, "Midpoint")
hline(30, "Lower reference")
crossedMidpoint = ta.crossover(uo, 50)
confirmedCross = barstate.isconfirmed and crossedMidpoint
alertcondition(confirmedCross, "UO close above 50", "UO crossed above 50 on a completed chart bar.")

Give Pineify the calculation and timing requirements

Ask the Pine Script AI Coding Agent to preserve the BP/TR diagnostics, expose the three lengths, and implement your selected confirmation rule. Supply the four-bar fixture above and ask for expected intermediate values. Compare bars and timestamps before extending the indicator into a strategy.

  • For divergence automation, specify how pivots are selected and when they become observable. Ask the script to distinguish candidate, confirmation, cancellation, and expiry.
  • Define exits, sizing, commissions, slippage, and order-fill assumptions separately. A correct oscillator is only one input to that strategy.
Where Pineify fits

Pine Script AI Coding Agent

Turn explicit UO inputs and confirmation rules into inspectable Pine Script, then compare its values and timestamps with your calculation.

Build a UO audit script

Frequently asked questions

Educational calculations, not investment advice. All numerical examples are hypothetical. Indicator confirmation does not guarantee a reversal or a trading return.

Sources and verification

Published by Pineify. Pineify is an independent product and is not affiliated with TradingView. Sources support the technical explanations; examples do not establish trading performance.