Lane's Stochastic Divergence Indicator for ThinkorSwim

BenTen

Administrative
Staff member
Staff
VIP
Lifetime
Here is one of the momentum oscillators for ThinkorSwim called Lane Divergence. It shows bullish and bearish signals on the lower study of your chart. The overbought area is set to 80 while the oversold area is set to 20, similar to RSI parameters.

The indicator also comes with an alert system.

rSEyrwr.png


thinkScript Code

Rich (BB code):
declare lower;

input over_bought = 80.0;
input over_sold = 20.0;
input percentDLength = 3;
input percentKLength = 14;
input AudibleAlert = yes;

def min_low = Lowest(low, percentKLength);
def max_high = Highest(high, percentKLength);
def rel_diff = close - (max_high + min_low) / 2;
def diff = max_high - min_low;
def avgrel = Average(Average(rel_diff, percentDLength), percentDLength);
def avgdiff = Average(Average(diff, percentDLength), percentDLength);
def SMIData = (avgrel / (avgdiff / 2) + 1) * 50;

def isLow = If (SMIData < SMIData[-1] and SMIData < SMIData[1], 1, 0);
def isHigh =   If (SMIData > SMIData[-1] and SMIData > SMIData[1], 1, 0);

rec prevLowSMI = CompoundValue(1, If(isLow[1], SMIData[1], prevLowSMI[1]),

0);
rec prevHighSMI = CompoundValue(1, If(isHigh[1], SMIData[1], prevHighSMI

[1]), 0);

rec prevLow =  CompoundValue(1, If(isLow[1], low, prevLow[1]), low);
rec prevHigh =  CompoundValue(1, If(isHigh[1], high, prevHigh[1]), high);

def positiveDivergenceReg = If (SMIData > prevLowSMI and low < prevLow, 1,

0);
def positiveDivergenceHid = If (SMIData < prevLowSMI and low > prevLow, 1,

0);

plot posDiv = If(isLow and (positiveDivergenceReg or

positiveDivergenceHid), SMIData, Double.NaN);
posDiv.AssignValueColor(if positiveDivergenceReg then Color.GREEN else

Color.white);
posDiv.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
posDiv.SetLineWeight(2);

def negativeDivergenceReg =  If (SMIData < prevHighSMI and high > prevHigh,

1, 0);
def negativeDivergenceHid = If (SMIData > prevHighSMI and high < prevHigh,

1, 0);

plot negDiv = If(isHigh and ( negativeDivergenceReg or

negativeDivergenceHid), SMIData, Double.NaN);
negDiv.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
negDiv.AssignValueColor(if negativeDivergenceReg then Color.RED else

Color.white);
negDiv.SetLineWeight(2);

plot AvgSMI = Average(SMIData, percentDLength);
AvgSMI.SetDefaultColor(Color.GRAY);

plot overbought = over_bought;
overbought.SetDefaultColor(Color.WHITE);
overbought.SetStyle(Curve.SHORT_DASH);

plot oversold = over_sold;
oversold.SetDefaultColor(Color.WHITE);
oversold.SetStyle(Curve.SHORT_DASH);

plot SMI = SMIData;
SMI.AssignValueColor(if SMI > SMI[1] then Color.GREEN else Color.RED);
SMI.SetLineWeight(2);

# Sound alerts
Alert(AudibleAlert and positiveDivergenceReg, "Time TO Buy", Alert.BAR,
Sound.Ring);
Alert(AudibleAlert and negativeDivergenceReg, "Time TO Sell", Alert.BAR,
Sound.Ring);

Shareable Link

https://tos.mx/Wcmk2L
 
Last edited:
Hi All,

I was wondering if anyone can please help me to create a code.

Here is what I am looking for:

I use a 1 minute chart to trade the /ES futures and the Fast Stochastic only showing the D% 3, and I look for divergence from price and the fast stoch. I look for price to make a low and the fast stoch to make a low and than a bounce and then price makes a lower low but the fast stoch makes a higher low... What I am looking for is a code that alerts me when this happens so I visually can see it... Its very similar to some of the Macd divergence codes out there of divergence... I just would like one for the fast stochastics... I also have what I call a "waterfall code" that paints the bar and sends an sound alert when we are making new lows on price and the current candle price breaks above the previous candles high by 1 tick. Below is my chart with the waterfall code so you can see it and pretty much just need a code now to draw lines of the divergence with conformation using the watrefall code... I hope that makes sense... Any help would be great... Thank you all..

Also below I will paste my trade rules of this trade setup for anyone who wants to learn to trade this trade setup....

Here is a chart with the waterfall code and the fast stoch. https://tos.mx/5LvnSw


Thank you again all.

-------------------------------------------------------------------------------------------------------
Trading Plan For 1 Minute Lower Low Divergence Using Micro /MES Contract:


*** Use the Fast Stoch using the D% 3 and hide the K% 9 on chart using TOS
*** Just flip everything over for a higher high stoch Divergence (Rising Price’s)

1. Look for a low in price with an oversold low stoch
2. Watch for a bounce in price and the stoch (They should bounce together)
3. Than look for a new low in price and a higher low on the Stoch
4. Watch for the Waterfall Trade Setup
5. Also look for a Support / Resistance level to lean against and a Reversal Candle for the trigger candle (Hammer, Shooting Star, Engulfment Candle...ect)
6. Place a buy bid order when price breaks the waterfall (1 tick above the high of the previous candle) and look for 1 - 2 point target
7. Your stop goes 1 tick below the lowest candles low of waterfall (in theory, price should not break below the lower low of price and the higher low of the stoch)
8. If target is not hit and price pulls back add a 2nd order to add to position half back (50% trade setup) for a possible pull back trade setup
9. The stop of 2nd order goes 1 tick below lowest low candle (same stop as first trade)
10. The target is 1 - 2 points on the pull back entry
11. Always be patient for the trade setup and don’t force the trade, if you miss the trade wait for the next trade setup


Trading Plan For 1 Minute Lower Low Divergence Using The Big \ES Contract:

*** Use the Fast Stoch using the D% 3 and hide the K% 9 on chart using TOS
*** Just flip everything over for a higher high stoch Divergence (Rising Price’s)

1. Look for a low in price with an oversold low stoch
2. Watch for a bounce in price and the stoch (They should bounce together)
3. Than look for a new low in price and a higher low on the Stoch
4. Watch for the waterfall trade setup
5. When price breaks the waterfall wait for a pullback and put an order 2-3 ticks above the waterfall’s lowest candle and your target should be 2-3 ticks profit
6. The stop should go 1 tick below the waterfall’s lowest low candle (in theory, price should not break below the lower low of price and the higher low of the stoch)
7. Your total risk should only be 2-3 ticks
8. You can always do multiple contracts and scale out with only 2-3 ticks per contract of risk
9. Always be patient for trade setup and don’t force the trade, if you miss the trade wait for the next trade setup
 
Hey @BenTen - Just found this indicator and it's something very close to what I've been looking quite a while for!

Would it be difficult to also make this same indicator based on the Fast Stochastic?
 
Hey @BenTen - Just found this indicator and it's something very close to what I've been looking quite a while for!

Would it be difficult to also make this same indicator based on the Fast Stochastic?
Changing the K value from 14 to say 5 would do this no? Fast stochastics run at a 5,3 whereas TOS default slow is 10,10 I believe. I know there might be some other differences but I've played around with this some as well.
 
@BenTen The white arrow signals a stochs crossover but no divergence?

The Red Arrows are "normal bearish divergence"
The Green Arrows are "normal bullish divergence"
The White Arrows on top (pointing down) are "hidden bearish divergence"
The White Arrows on bottom (pointing up) are "hidden bullish divergence"

normal bearish divergence = higher high on stock correlating w lower high on indicator
normal bullish divergence = lower low on stock correlating w higher low on indicator
hidden bearish divergence = lower high on stock corelating with higher high on indicator
hidden bullish divergence = higher low on stock correlating with lower low on indicator

Normal Divergence is Trend Reversal Signal
Hidden Divergence is a Trend Continuation Signal

Hope this is helpful.
 
Thanks @JakeD very helpful. Never heard of a hidden divergence. How strong of a signal is it relative to a normal divergence?
 
Awesome. I have seen nothing to suggest it is any less significant. Hidden Divergences are taken from the highs in a down trend and from the lows in an uptrend. Not a bad place to begin building a position for following the trend.

Here is a fine, clear explanation of it in simple, not overly complex terms.

 
  • Like
Reactions: DDW
hi guys, i am having a bit of an issue with this part of the script:

Code:
plot overbought = over_bought;
overbought.SetDefaultColor(Color.WHITE);
overbought.SetStyle(Curve.SHORT_DASH);

plot oversold = over_sold;
oversold.SetDefaultColor(Color.WHITE);
oversold.SetStyle(Curve.SHORT_DASH);

fyi, I am using light color scheme. however, when I change the above to 'black', the arrows still do not show.

another issue is, the indicator name box has no text - in other words the indicator name does not appear. i believe this is showing as white or something light too. does anyone know how I can fix this?

any help appreciated! 🙏
 
@Ronin13 I don't think there is a way to modify the color of the indicator's name. It's because you are using the light color scheme :(
 
@Ronin13 In this version the arrow colors are now global colors. The colors can be modified under settings. HTH
4Ag8UNx.png

IdMxw2A.png

Ruby:
# Lane's Stochastic Divergence Indicator for ThinkorSwim
# BenTen 3/19/19

declare lower;

input over_bought = 80.0;
input over_sold = 20.0;
input percentDLength = 3;
input percentKLength = 14;
input AudibleAlert = yes;

def min_low = Lowest(low, percentKLength);
def max_high = Highest(high, percentKLength);
def rel_diff = close - (max_high + min_low) / 2;
def diff = max_high - min_low;
def avgrel = Average(Average(rel_diff, percentDLength), percentDLength);
def avgdiff = Average(Average(diff, percentDLength), percentDLength);
def SMIData = (avgrel / (avgdiff / 2) + 1) * 50;

def isLow = If (SMIData < SMIData[-1] and SMIData < SMIData[1], 1, 0);
def isHigh =   If (SMIData > SMIData[-1] and SMIData > SMIData[1], 1, 0);

rec prevLowSMI = CompoundValue(1, If(isLow[1], SMIData[1], prevLowSMI[1]), 0);
rec prevHighSMI = CompoundValue(1, If(isHigh[1], SMIData[1], prevHighSMI[1]), 0);

rec prevLow =  CompoundValue(1, If(isLow[1], low, prevLow[1]), low);
rec prevHigh =  CompoundValue(1, If(isHigh[1], high, prevHigh[1]), high);

def positiveDivergenceReg = If (SMIData > prevLowSMI and low < prevLow, 1,0);
def positiveDivergenceHid = If (SMIData < prevLowSMI and low > prevLow, 1,0);

DefineGlobalColor("ArrowUp", CreateColor(0, 200, 255)) ;
DefineGlobalColor("ArrowDn",  CreateColor(225, 0, 0)) ;
DefineGlobalColor("neutral",  color.gray) ;

plot posDiv = If(isLow and (positiveDivergenceReg or positiveDivergenceHid), SMIData, Double.NaN);
posDiv.AssignValueColor(if positiveDivergenceReg then GlobalColor("ArrowUp") else
GlobalColor("neutral"));
posDiv.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
posDiv.SetLineWeight(2);

def negativeDivergenceReg =  If (SMIData < prevHighSMI and high > prevHigh,1, 0);
def negativeDivergenceHid = If (SMIData > prevHighSMI and high < prevHigh,1, 0);

plot negDiv = If(isHigh and ( negativeDivergenceReg or negativeDivergenceHid), SMIData, Double.NaN);
negDiv.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
negDiv.AssignValueColor(if negativeDivergenceReg then GlobalColor("ArrowDn") else
GlobalColor("neutral"));
negDiv.SetLineWeight(2);

plot AvgSMI = Average(SMIData, percentDLength);
AvgSMI.SetDefaultColor(Color.GRAY);

plot overbought = over_bought;
overbought.SetDefaultColor(Color.WHITE);
overbought.SetStyle(Curve.SHORT_DASH);

plot oversold = over_sold;
oversold.SetDefaultColor(Color.WHITE);
oversold.SetStyle(Curve.SHORT_DASH);

plot SMI = SMIData;
SMI.AssignValueColor(if SMI > SMI[1] then Color.GREEN else Color.RED);
SMI.SetLineWeight(2);


Share Link: http://tos.mx/NRHDTWE
 

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
493 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