Master this unique momentum oscillator in TradingView's Pinescript that combines multiple timeframes for more reliable overbought/oversold signals and divergence detection.
The Ultimate Oscillator (UO), developed by Larry Williams in 1976, is a momentum oscillator designed to measure buying and selling pressure across three different timeframes (typically 7, 14, and 28 periods). Its unique construction aims to correct the shortcomings of other oscillators by preventing premature divergence signals and reducing whipsaws that often occur when using a single period. The UO oscillates between 0 and 100, providing signals for overbought/oversold conditions and, most notably, strong divergence patterns.
In Pinescript, the Ultimate Oscillator is a valuable tool for traders seeking a more robust momentum indicator that filters out noise and delivers more reliable signals for potential trend reversals.
The calculation of the Ultimate Oscillator is more complex than other oscillators, involving three weighted averages of "Buying Pressure" and "True Range" over short, medium, and long timeframes:
//@version=5
indicator("My Ultimate Oscillator", overlay=false) // overlay=false to plot in a separate pane
// Inputs for Ultimate Oscillator lengths
length1 = input.int(7, title="Length 1 (Short)", minval=1)
length2 = input.int(14, title="Length 2 (Medium)", minval=1)
length3 = input.int(28, title="Length 3 (Long)", minval=1)
// Calculate Ultimate Oscillator using the built-in function
uoValue = ta.uo(length1, length2, length3)
// Plot the Ultimate Oscillator line
plot(uoValue, title="Ultimate Oscillator", color=color.blue, linewidth=2)
// Plot horizontal lines for overbought and oversold levels
h_overbought = hline(70, "Overbought (70)", color.red, linestyle=hline.style_dashed)
h_oversold = hline(30, "Oversold (30)", color.green, linestyle=hline.style_dashed)
hline(50, "Centerline", color.gray, linestyle=hline.style_dotted)
// Fill background between UO and levels for visual clarity
fill(h_overbought, h_oversold, color.new(color.gray, 90))
The conventional overbought level is 70 and the oversold level is 30.
//@version=5
strategy("Ultimate Oscillator Overbought/Oversold Strategy", overlay=true)
// Inputs for Ultimate Oscillator
length1 = input.int(7, title="Length 1", minval=1)
length2 = input.int(14, title="Length 2", minval=1)
length3 = input.int(28, title="Length 3", minval=1)
overboughtLevel = input.int(70, title="Overbought Level")
oversoldLevel = input.int(30, title="Oversold Level")
// Calculate Ultimate Oscillator
uoValue = ta.uo(length1, length2, length3)
// Define conditions for entries and exits
// Buy when UO moves below oversold and then crosses back above it
longCondition = ta.crossunder(uoValue, oversoldLevel) // UO goes oversold
longExitCondition = ta.crossover(uoValue, oversoldLevel) // UO exits oversold (potential buy)
// Sell when UO moves above overbought and then crosses back below it
shortCondition = ta.crossover(uoValue, overboughtLevel) // UO goes overbought
shortExitCondition = ta.crossunder(uoValue, overboughtLevel) // UO exits overbought (potential sell)
// Strategy entries/exits (entry upon exiting extreme zones)
if (longExitCondition)
strategy.entry("Long", strategy.long)
if (shortExitCondition)
strategy.entry("Short", strategy.short)
// Optional: Plot UO in a separate pane for visualization
// plot(uoValue, "UO", color.blue)
// hline(overboughtLevel, "Overbought", color.red)
// hline(oversoldLevel, "Oversold", color.green)
//@version=5
indicator("Ultimate Oscillator Divergence Scanner", overlay=true)
// Inputs for Ultimate Oscillator
length1 = input.int(7, title="Length 1", minval=1)
length2 = input.int(14, title="Length 2", minval=1)
length3 = input.int(28, title="Length 3", minval=1)
overboughtLevel = input.int(70, title="Overbought Level")
oversoldLevel = input.int(30, title="Oversold Level")
centerLine = input.int(50, title="Centerline")
// Calculate Ultimate Oscillator
uoValue = ta.uo(length1, length2, length3)
// Plot UO in a separate pane
plot(uoValue, "Ultimate Oscillator", color.blue)
hline(overboughtLevel, "Overbought (70)", color.red)
hline(oversoldLevel, "Oversold (30)", color.green)
hline(centerLine, "Centerline (50)", color.gray)
// Simple divergence detection (conceptual, robust detection requires advanced pivot logic)
// This is a simplified example focusing on price vs UO divergence, not specific Williams' patterns.
// Bullish Divergence (Price lower low, UO higher low)
bullishDivergence = close[2] > close[1] and close[1] > close and uoValue[2] < uoValue[1] and uoValue[1] < uoValue
// Bearish Divergence (Price higher high, UO lower high)
bearishDivergence = close[2] < close[1] and close[1] < close and uoValue[2] > uoValue[1] and uoValue[1] > uoValue
plotshape(bullishDivergence, title="Bullish Divergence", location=location.belowbar, color=color.green, style=shape.triangleup, size=size.small)
plotshape(bearishDivergence, title="Bearish Divergence", location=location.abovebar, color=color.red, style=shape.triangledown, size=size.small)
alertcondition(bullishDivergence, "Bullish UO Divergence", "Potential bullish reversal based on Ultimate Oscillator divergence.")
alertcondition(bearishDivergence, "Bearish UO Divergence", "Potential bearish reversal based on Ultimate Oscillator divergence.")
// Implementing failure swings in Pinescript is more complex as it requires robust peak/trough detection within the oscillator itself, often combined with price action. The simplified strategy examples above focus on direct signals.
To get the most from the Ultimate Oscillator in Pinescript:
Remember UO's strength lies in its multi-timeframe calculation which inherently smooths out noise without adding significant lag, making its divergence signals particularly noteworthy.
The Ultimate Oscillator is a sophisticated and highly effective momentum indicator in Pinescript for TradingView. Its innovative construction, incorporating multiple timeframes and weighted averages, allows it to provide more reliable overbought/oversold signals and, crucially, robust divergence patterns than many single-period oscillators. By understanding its unique calculation, thoughtfully tuning its parameters, and integrating it strategically within your comprehensive trading approach, you can leverage the Ultimate Oscillator to gain deeper insights into market momentum and enhance your trading decisions.
Get a high-performance Pinescript analysis tool for actionable market insights, designed for traders on the move.
This strategy runs in live mode on TradingView, helping you identify potential opportunities.
Get Pinescript Strategy