Pine Script Ultimate Oscillator

Master this unique momentum oscillator in TradingView's Pine Script that combines multiple timeframes for more reliable overbought/oversold signals and divergence detection.

Subscribe now @ $99/month

What is the Ultimate Oscillator?

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 Pine Script, 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.

Components and Calculation

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:

  1. Buying Pressure (BP): `Close - Minimum(Low, Close[1])`
  2. True Range (TR): `Maximum(High, Close[1]) - Minimum(Low, Close[1])`
  3. Raw Ultimate Oscillator: For each of the three lengths (e.g., 7, 14, 28):
    • Calculate `Average Buying Pressure (ABP) = Sum(BP, length) / Sum(TR, length)`
  4. Weighted Average: The final UO value is a weighted sum of these three averages:
    `UO = ((4 * ABP(length1)) + (2 * ABP(length2)) + ABP(length3)) / (4 + 2 + 1) * 100`
    Typical lengths are 7, 14, and 28. The weights (4, 2, 1) prioritize the shorter periods while still incorporating information from longer ones.

This multi-timeframe approach ensures a more balanced view of market momentum.

Basic Ultimate Oscillator Implementation in Pine Script

Pine Script v5 provides a convenient built-in function `ta.uo()` for straightforward implementation.


//@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=color.red, linestyle=hline.style_dashed)
h_oversold = hline(30, "Oversold (30)", color=color.green, linestyle=hline.style_dashed)
hline(50, "Centerline", color=color.gray, linestyle=hline.style_dotted)

// Fill background between UO and levels for visual clarity
fill(h_overbought, h_oversold, color=color.new(color.gray, 90))

Standard Levels: The conventional overbought level is 70 and the oversold level is 30.

Practical Ultimate Oscillator Trading Strategies

1. Overbought and Oversold Conditions

Similar to other oscillators, UO identifies overbought and oversold regions. However, signals from UO are often considered more reliable due to its multi-timeframe calculation.


//@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)

2. Ultimate Oscillator Divergence (Key Strategy)

Divergence is considered the most reliable signal generated by the Ultimate Oscillator, especially the specific "triple divergence" pattern identified by Larry Williams. UO is designed to reduce false divergence signals seen in simpler oscillators.


//@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=color.blue)
hline(overboughtLevel, "Overbought (70)", color=color.red)
hline(oversoldLevel, "Oversold (30)", color=color.green)
hline(centerLine, "Centerline (50)", color=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.")

3. Failure Swings

Failure swings (also called "swing rejections") are another key signal in the Ultimate Oscillator. They indicate a failure of momentum to follow through, often confirming a reversal previously signaled by divergence.

Implementing failure swings in Pine Script 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.

Optimizing Ultimate Oscillator Performance

To get the most from the Ultimate Oscillator in Pine Script:

Unique Construction: 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.

Common Ultimate Oscillator Pitfalls

Conclusion

The Ultimate Oscillator is a sophisticated and highly effective momentum indicator in Pine Script 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.

Enhance Your Trading

Get a high-performance Pine Script 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.

Subscribe now @ $99/month