Stochastic Oscillator Divergence Indicator For ThinkOrSwim


New member
Many years ago I developed a Stochastic Divergence Indicator, (code below and photos below), works best on 5 min time frame and up. I made this from a lot of cutting and pasting/trial and error. Basically when a Divergence is detected a the indicator produces a spike in the lower study section. The photos show where these divergences are, please note, I drews these lines, not the indicator.
Here's the photo of how it works:

Stochastic Divergence Code:
input over_bought = 80;
input over_sold = 20;
input KPeriod = 14;
input DPeriod = 3;
input priceH = high;
input priceL = low;
input priceC = close;
input averageType = AverageType.SIMPLE;
input showBreakoutSignals = {default "No", "On FullK", "On FullD", "On FullK & FullD"};

def lowest_k = Lowest(priceL, KPeriod);
def c1 = pricel - lowest_k;
def c2 = Highest(priceH, KPeriod) - lowest_k;
def FastK = if c2 != 0 then c1 / c2 * 100 else 0;

def FullK = MovingAverage(averageType, FastK);
def FullD = MovingAverage(averageType, FullK, DPeriod);

def upK = FullK crosses above Over_sold;
def upD = FullD crosses above Over_sold;
def downK = FullK crosses below Over_bought;
def downD = FullD crosses below Over_bought;

plot data = (upd)or (upk);
Last edited by a moderator:

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

Would this work well with a macd divergence indicator? It would be interesting to compare the 2
Members have stated in this thread
and this thread

That there is not a reliable means for signaling divergences.
Read more in the above threads and here:

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
360 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.