Adaptive Universal Oscillator for ThinkOrSwim

Sesqui

Member
VIP
Here is another adaptive indicator but it uses a different approach.

The universal oscillator has essentially zero lag as an indicator however its implementation in TOS is not adaptive.

The indicator below changes the sensitivity to price moves using the volatility in much the same way as the TOS MovAvgAdaptive indicator. It becomes more sensitive when the price is moving smoothly and less sensitive when the price is volatile.

This is done by calculating the Efficiency Ratio (ER), a measure of relative trend strength, using close price relative to the high-low range. The MovAvgAdaptive gets a variable named ScaledSF. It uses the ScaledSF as an alpha value for an EMA, which is computed using:

EMA = alpha*Price + (1-alpha)*EMA[1].

Since we know that ScaledSF is the alpha value, and since it is known that for an N period SMA, alpha = 2/(N+1), setting ScaledSF = 2/(N+1) and solving for N yields:

N = 2/alpha -1.

This value of N is then used as the period for the Universal Oscillator in order to make it adaptive.

Here is a screenshot. The first indicator on the bottom is the adaptive Universal Oscillator. The second one is the Universal Oscillator that comes with TOS using default settings for comparison. It is evident there are trading opportunities that the adaptive oscillator identifies.

An HMA(34) in yellow was added to each oscillator as well as the zero line for potential use trading cross overs. But it seems that the peaks and valleys of the oscillator identify trading opportunities too.

1758243510714.png


CSS:
# Adaptive UniversalOsc Oscillator for TOS by Sesqui [18SEP2025]
 
# This  indicator changes its sensitivity to price moves using the volatility.
# It becomes more sensitive during periods when the price is moving smoothly in a certain direction and becomes less sensitive when the price is volatile. In much the same way as the TOS MovAvgAdaptive indicator does.
# This is done by calculating the Efficiency Ratio (ER), a measure of relative trend strength. The ER accounts for the location of the close price relative to the high-low range, which uses ER to then getActualEarnings a paramter called, ScaledSF. The AMA uses ScaledSF as an alpha value for an EMA, which is computed using:
# EMA = alpha*Price + (1-alpha)*EMA[1].
# Since we know that ScaledSF is the alpha value, and since it is known that for an N period SMA,
# alpha = 2/(N+1), setting ScaledSF = 2/(N+1) and solving for N yields: N =  2/alpha -1. This
# value of N is then used as the period for the Universal Oscillator in order to make it adaptive
# to the price action, adapting for the current volatilityStdDev in the market.
#

declare lower;

# *** Computes the Efficiency Ratio ***
def effRatioLength = 10;
def fastLength = 2;
def slowLength = 30;

def   ER = AbsValue((close - Lowest(low, effRatioLength)) - (Highest(high, effRatioLength) - close)) / (Highest(high, effRatioLength) - Lowest(low, effRatioLength));

def FastSF = 2 / (fastLength + 1);
def SlowSF = 2 / (slowLength + 1);
def ScaledSF = ER * (FastSF - SlowSF) + SlowSF;

# *** Determines the Period for the Universal Oscillator ***
# for an EMA: alpha = 1/ (L+1); where L is the lag
# Using this expression and solving for Lag:
# L = 2/alpha -1; let ScaledSF = alpha, and solve for the
# value of lag, L, to get: L = 2/scaledSF - 1;
# This lag, L becomes the Bandedge or cutoffLength passed to the
# EhlersSuperSmoother in the Universal Oscillator, making the
# Universal Osciallator adpative

def cutoffLength = 2 / ScaledSF - 1;

# *** Computes the Universal Oscillator in this section ***
def whiteNoise = (close - close[2]) / 2;
def filter = reference EhlersSuperSmootherFilter(price = whiteNoise, "cutoff length" = cutoffLength);
def peak = if IsNaN(filter) then peak[1] * 0.991 else Max(AbsValue(filter), peak[1] * 0.991);

plot UniversalOsc = filter / peak;
UniversalOsc.SetLineWeight(2);

def Diff = UniversalOSC - UniversalOSC[1];
UniversalOsc.AssignValueColor(if Diff >= 0 then if Diff > Diff[1] then Color.Green else Color.Dark_Green else if Diff < Diff[1] then Color.Red else Color.DARK_RED);

# *** Displays an HMA(34) for cross overs ***
input length = 45;

plot HMA = MovingAverage(AverageType.HULL, UniversalOsc, length );
HMA.AssignValueColor(Color.YELLOW);
HMA.SetLineWeight(2);

plot centerLine = 0;
centerLine.AssignValueColor(Color.GRAY);
 
Last edited:
I played with this indicator for a while to see if I could create a scan to find depressed stocks that might be close to a bounce. I first searched for stocks with a UniversalOsc value of less than -0.9 within the last 5 days. It returned no stocks. I changed the scan to search for values less than 0.75, still no results. Next I modified the code to create two lines, HiLimit and LowLimit. I set values of +0.9 for HiLimit and -0.9 for LowLimit. I scanned for UniversalOsc crossing LowLimit within the last 5 days. No results.

I modified the search to go out 10 days. Still no results. Am I missing something? Why won't it find any results. I know DKNG met the 5 day criteria.
 
@PonchoMike it's hard to tell what may be causing the issue since the code for the scanner is not posted. I have not made a scanner for this one before so I don't have insight into what snags might be involved...
 
I took the code I copied above and named it NewStudy46. I added the following lines of code at athe bottom:

plot lowlimit = -0.9;
plot hilimit = 0.9;

I set the line colors using "edit properties".

1759587638110.png


First I tried this scan:

NewStudy46()."UniversalOsc" crosses above NewStudy46()."lowlimit" within 10 bars

I also tried this scan:
NewStudy46()."UniversalOsc" is less than -0.9 within 10 bars

I changed the lowlimit to -0.5, again no scan results. I also searched for UniversalOsc less than -0.5, with no results.

I also tried multiplying the formula for UniversalOsc by 100 so the values would range from -100 to +100, then I set hilimit = 90 and lowlimit = -90. I then looked for falues of UniversalOsc less than 90, again no results. I then looked for a crossing of UniversalOsc and lowlimit within the last 5 days, and got no results.

I can't think of anything else to try.
 
Last edited:
@PonchoMike for some reason, the impression I am getting anyway, is that it does not like the adaptive portion in the scanner. Perhaps that makes it too complex to use in a scanner. That is my preliminary impression. I say that because you can run the regular UniversalOscillator right out of the box the way you have described and the scanner works. I just ran it for the -0.9 crossing above within 10 days on a 1D chart scanning in All stocks....and that works. But when I run the same scan on the adaptive UniversalOSC it does not work.
Hope this helps...and thanks for helping me to realize that the adapative variant of the study aparently cannot be used in scanners. I have found after using the adaptive variant for a given stock, that a length of a fixed value gets me what I need. Perhaps your scan can use the regular Universal Osc with a reasonably chosen fixed length to identify potentially lucrative stocks to trade then zero in with the adaptive one?

Sesqui
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
386 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top