Stochastic Divergence Alert For ThinkOrSwim

TritonHawk

New member
Could someone please convert this indicator from TV? https://www.tradingview.com/v/lJiDn2VR/. It's the lower indicator "StochD". Please and Thank you!

1690824832633.png
 
Could someone please convert this indicator from TV? https://www.tradingview.com/v/lJiDn2VR/. It's the lower indicator "StochD". Please and Thank you!

View attachment 19335
try this. Arrow will show instead of divergence lines.

CSS:
#//@Jesse.Lau
#indicator(title="[JL] Stochastic Divergence Alert", shorttitle="StochD", format=format.price, precision=2
# -- Converted by Sam4Cok@Samer800    - 08/2023    - request form UseThinkScript.com member
Declare lower;

input periodK = 9;
input smoothK = 3;          # "%K Smoothing"
input periodD = 3;          # "%D Smoothing"
input TopLevel = 70;        # "Top Level"
input BottomLevel = 30;     # "Bottom Level"

def na = Double.NaN;
def last = IsNaN(close);
# stoch(source, high, low, length) =>
script stoch {
    input src = close;
    input h = high;
    input l = low;
    input len = 14;
    def hh = Highest(h, len);
    def ll = Lowest(l, len);
    def stoch = 100 * (src - ll) / (hh - ll);
    plot return = stoch;
}
def stoch = stoch(close, high, low, periodK);
def k = Average(stoch, smoothK);
def d = Average(k, periodD);

plot FullK = k;    # "%K"
plot FullD = d;    # "%D"
FullK.SetLineWeight(2);
FullD.SetLineWeight(2);
FullK.SetDefaultColor(CreateColor(33, 150, 243));
FullD.SetDefaultColor(GetColor(0));

plot ht = if last then na else TopLevel;    # "Upper Band"
plot hb = if last then na else BottomLevel; # "Lower Band"

ht.SetDefaultColor(Color.GRAY);
hb.SetDefaultColor(Color.GRAY);

AddCloud(ht, hb, Color.DARK_GRAY);    # "Background"


def GC1 = (k >= d);
def DC1 = (k <= d);
def GC = Crosses(k, d, CrossingDirection.ABOVE);
def DC = Crosses(k, d, CrossingDirection.BELOW);
def counter;#   = 0
def lastgc;#    = 0
def lastdc;#    = 0
def kh;# = 0.0
def kl;# = 100.0
def l1;# = 10000.0
def kl_ = if kl[1]==0 then 100 else kl[1];
def l1_ = if l1[1]==0 then 10000 else l1[1];
def h1;# = 0.0
def cnt = counter[1] + 1;

if(GC1) {
    kh = if k < kh[1] then kh[1] else k;
    kl = 100.0;
    h1 = if high < h1[1] then h1[1] else high;
    l1 = 10000.0;
    } else
if(DC1) {
    kl = if k > kl_ then kl_ else k;
    kh = 0.0;
    l1 = if low > l1_ then l1_ else low;
    h1 = 0.0;
    } else {
    kl = kl_;
    kh = kh[1];
    l1 = l1_;
    h1 = h1[1];
}

if (DC) {
    lastgc  = cnt;
    lastdc  = lastdc[1];
    counter = 0;
    } else
if (GC) {
    lastgc  = lastgc[1];
    lastdc  = cnt;
    counter = 0;
    } else {
    lastgc  = lastgc[1];
    lastdc  = lastdc[1];
    counter = cnt;
}
def klValue = GetValue(kl, lastdc + lastgc + 1);
def l1Value = GetValue(l1, lastdc + lastgc + 1);
def khValue = GetValue(kh, lastdc + lastgc + 1);
def h1Value = GetValue(h1, lastdc + lastgc + 1);

def turnGreen = GC and kl[1] > klValue and l1[1] <= l1Value and klValue < BottomLevel;
def turnRed   = DC and kh[1] < khValue and h1[1] >= h1Value and khValue > TopLevel;

def shapeGreen = if turnGreen then kl[1] - 5 else na;#Value - 5 else na;
def shapeRed   = if turnRed then kh[1] + 5 else na;#Value + 5 else na;

plot greenLine = shapeGreen;
plot redLine = shapeRed;

greenLine.SetDefaultColor(Color.GREEN);
redLine.SetDefaultColor(Color.RED);
greenLine.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
redLine.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);

#-- END of CODE
 
try this. Arrow will show instead of divergence lines.

CSS:
#//@Jesse.Lau
#indicator(title="[JL] Stochastic Divergence Alert", shorttitle="StochD", format=format.price, precision=2
# -- Converted by Sam4Cok@Samer800    - 08/2023    - request form UseThinkScript.com member
Declare lower;

input periodK = 9;
input smoothK = 3;          # "%K Smoothing"
input periodD = 3;          # "%D Smoothing"
input TopLevel = 70;        # "Top Level"
input BottomLevel = 30;     # "Bottom Level"

def na = Double.NaN;
def last = IsNaN(close);
# stoch(source, high, low, length) =>
script stoch {
    input src = close;
    input h = high;
    input l = low;
    input len = 14;
    def hh = Highest(h, len);
    def ll = Lowest(l, len);
    def stoch = 100 * (src - ll) / (hh - ll);
    plot return = stoch;
}
def stoch = stoch(close, high, low, periodK);
def k = Average(stoch, smoothK);
def d = Average(k, periodD);

plot FullK = k;    # "%K"
plot FullD = d;    # "%D"
FullK.SetLineWeight(2);
FullD.SetLineWeight(2);
FullK.SetDefaultColor(CreateColor(33, 150, 243));
FullD.SetDefaultColor(GetColor(0));

plot ht = if last then na else TopLevel;    # "Upper Band"
plot hb = if last then na else BottomLevel; # "Lower Band"

ht.SetDefaultColor(Color.GRAY);
hb.SetDefaultColor(Color.GRAY);

AddCloud(ht, hb, Color.DARK_GRAY);    # "Background"


def GC1 = (k >= d);
def DC1 = (k <= d);
def GC = Crosses(k, d, CrossingDirection.ABOVE);
def DC = Crosses(k, d, CrossingDirection.BELOW);
def counter;#   = 0
def lastgc;#    = 0
def lastdc;#    = 0
def kh;# = 0.0
def kl;# = 100.0
def l1;# = 10000.0
def kl_ = if kl[1]==0 then 100 else kl[1];
def l1_ = if l1[1]==0 then 10000 else l1[1];
def h1;# = 0.0
def cnt = counter[1] + 1;

if(GC1) {
    kh = if k < kh[1] then kh[1] else k;
    kl = 100.0;
    h1 = if high < h1[1] then h1[1] else high;
    l1 = 10000.0;
    } else
if(DC1) {
    kl = if k > kl_ then kl_ else k;
    kh = 0.0;
    l1 = if low > l1_ then l1_ else low;
    h1 = 0.0;
    } else {
    kl = kl_;
    kh = kh[1];
    l1 = l1_;
    h1 = h1[1];
}

if (DC) {
    lastgc  = cnt;
    lastdc  = lastdc[1];
    counter = 0;
    } else
if (GC) {
    lastgc  = lastgc[1];
    lastdc  = cnt;
    counter = 0;
    } else {
    lastgc  = lastgc[1];
    lastdc  = lastdc[1];
    counter = cnt;
}
def klValue = GetValue(kl, lastdc + lastgc + 1);
def l1Value = GetValue(l1, lastdc + lastgc + 1);
def khValue = GetValue(kh, lastdc + lastgc + 1);
def h1Value = GetValue(h1, lastdc + lastgc + 1);

def turnGreen = GC and kl[1] > klValue and l1[1] <= l1Value and klValue < BottomLevel;
def turnRed   = DC and kh[1] < khValue and h1[1] >= h1Value and khValue > TopLevel;

def shapeGreen = if turnGreen then kl[1] - 5 else na;#Value - 5 else na;
def shapeRed   = if turnRed then kh[1] + 5 else na;#Value + 5 else na;

plot greenLine = shapeGreen;
plot redLine = shapeRed;

greenLine.SetDefaultColor(Color.GREEN);
redLine.SetDefaultColor(Color.RED);
greenLine.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
redLine.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);

#-- END of CODE
It is possible to add alerts to the arrows and aggregation time frames? I've tried multiple times but horrible at coding. Please and Thank you!
 
It is possible to add alerts to the arrows and aggregation time frames? I've tried multiple times but horrible at coding. Please and Thank you!
add the below at the end of the code:

CSS:
input sound    = Sound.NoSound;
input alertType = Alert.BAR;

Alert(turnGreen, "Long", alertType, sound);
Alert(turnRed,   "Short", alertType, sound);
 
Can you make the arrow show up on the chart upper?. thanks
use the below

CSS:
#//@Jesse.Lau
#indicator(title="[JL] Stochastic Divergence Alert", shorttitle="StochD", format=format.price, precision=2
# -- Converted by Sam4Cok@Samer800    - 08/2023    - request form UseThinkScript.com member
# -- Update by Sam4Cok@Samer800 - 11/2023 - Upper study
input periodK = 9;
input smoothK = 3;          # "%K Smoothing"
input periodD = 3;          # "%D Smoothing"
input TopLevel = 70;        # "Top Level"
input BottomLevel = 30;     # "Bottom Level"

def na = Double.NaN;
def last = IsNaN(close);
# stoch(source, high, low, length) =>
script stoch {
    input src = close;
    input h = high;
    input l = low;
    input len = 14;
    def hh = Highest(h, len);
    def ll = Lowest(l, len);
    def stoch = 100 * (src - ll) / (hh - ll);
    plot return = stoch;
}
def stoch = stoch(close, high, low, periodK);
def k = Average(stoch, smoothK);
def d = Average(k, periodD);

def GC1 = (k >= d);
def DC1 = (k <= d);
def GC = Crosses(k, d, CrossingDirection.ABOVE);
def DC = Crosses(k, d, CrossingDirection.BELOW);
def counter;#   = 0
def lastgc;#    = 0
def lastdc;#    = 0
def kh;# = 0.0
def kl;# = 100.0
def l1;# = 10000.0
def kl_ = if kl[1]==0 then 100 else kl[1];
def l1_ = if l1[1]==0 then 10000 else l1[1];
def h1;# = 0.0
def cnt = counter[1] + 1;

if(GC1) {
    kh = if k < kh[1] then kh[1] else k;
    kl = 100.0;
    h1 = if high < h1[1] then h1[1] else high;
    l1 = 10000.0;
    } else
if(DC1) {
    kl = if k > kl_ then kl_ else k;
    kh = 0.0;
    l1 = if low > l1_ then l1_ else low;
    h1 = 0.0;
    } else {
    kl = kl_;
    kh = kh[1];
    l1 = l1_;
    h1 = h1[1];
}

if (DC) {
    lastgc  = cnt;
    lastdc  = lastdc[1];
    counter = 0;
    } else
if (GC) {
    lastgc  = lastgc[1];
    lastdc  = cnt;
    counter = 0;
    } else {
    lastgc  = lastgc[1];
    lastdc  = lastdc[1];
    counter = cnt;
}
def klValue = GetValue(kl, lastdc + lastgc + 1);
def l1Value = GetValue(l1, lastdc + lastgc + 1);
def khValue = GetValue(kh, lastdc + lastgc + 1);
def h1Value = GetValue(h1, lastdc + lastgc + 1);

def turnGreen = GC and kl[1] > klValue and l1[1] <= l1Value and klValue < BottomLevel;
def turnRed   = DC and kh[1] < khValue and h1[1] >= h1Value and khValue > TopLevel;

def shapeGreen = if turnGreen then low else na;#Value - 5 else na;
def shapeRed   = if turnRed then high else na;#Value + 5 else na;

plot greenLine = shapeGreen;
plot redLine = shapeRed;

greenLine.SetDefaultColor(Color.CYAN);
redLine.SetDefaultColor(Color.MAGENTA);
greenLine.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
redLine.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);

input sound    = Sound.NoSound;
input alertType = Alert.BAR;

Alert(turnGreen, "Long", alertType, sound);
Alert(turnRed,   "Short", alertType, sound);

#-- END of CODE
 
Scan For Stochastic Divergence Alert

shared scan link: http://tos.mx/zBUcjDo Click here for --> Easiest way to load shared links
xtHfEVE.png

Ruby:
#Stochastic Divergence Alert  SCANNER ONLY
input periodK = 9;
input smoothK = 3;          # "%K Smoothing"
input periodD = 3;          # "%D Smoothing"
input TopLevel = 70;        # "Top Level"
input BottomLevel = 30;     # "Bottom Level"

def na = Double.NaN;
def last = IsNaN(close);
# stoch(source, high, low, length) =>
script stoch {
    input src = close;
    input h = high;
    input l = low;
    input len = 14;
    def hh = Highest(h, len);
    def ll = Lowest(l, len);
    def stoch = 100 * (src - ll) / (hh - ll);
    plot return = stoch;
}
def stoch = stoch(close, high, low, periodK);
def k = Average(stoch, smoothK);
def d = Average(k, periodD);

def GC1 = (k >= d);
def DC1 = (k <= d);
def GC = Crosses(k, d, CrossingDirection.ABOVE);
def DC = Crosses(k, d, CrossingDirection.BELOW);
def counter;#   = 0
def lastgc;#    = 0
def lastdc;#    = 0
def kh;# = 0.0
def kl;# = 100.0
def l1;# = 10000.0
def kl_ = if kl[1]==0 then 100 else kl[1];
def l1_ = if l1[1]==0 then 10000 else l1[1];
def h1;# = 0.0
def cnt = counter[1] + 1;

if(GC1) {
    kh = if k < kh[1] then kh[1] else k;
    kl = 100.0;
    h1 = if high < h1[1] then h1[1] else high;
    l1 = 10000.0;
    } else
if(DC1) {
    kl = if k > kl_ then kl_ else k;
    kh = 0.0;
    l1 = if low > l1_ then l1_ else low;
    h1 = 0.0;
    } else {
    kl = kl_;
    kh = kh[1];
    l1 = l1_;
    h1 = h1[1];
}

if (DC) {
    lastgc  = cnt;
    lastdc  = lastdc[1];
    counter = 0;
    } else
if (GC) {
    lastgc  = lastgc[1];
    lastdc  = cnt;
    counter = 0;
    } else {
    lastgc  = lastgc[1];
    lastdc  = lastdc[1];
    counter = cnt;
}
def klValue = GetValue(kl, lastdc + lastgc + 1);
def l1Value = GetValue(l1, lastdc + lastgc + 1);
def khValue = GetValue(kh, lastdc + lastgc + 1);
def h1Value = GetValue(h1, lastdc + lastgc + 1);

plot turnGreen = GC and kl[1] > klValue and l1[1] <= l1Value and klValue < BottomLevel;
#plot turnRed   = DC and kh[1] < khValue and h1[1] >= h1Value and khValue > TopLevel;

defaults to scans for longs
to scan for shorts, change this:
plot turnGreen = GC and kl[1] > klValue and l1[1] <= l1Value and klValue < BottomLevel;
#plot turnRed = DC and kh[1] < khValue and h1[1] >= h1Value and khValue > TopLevel;
to this:
#plot turnGreen = GC and kl[1] > klValue and l1[1] <= l1Value and klValue < BottomLevel;
plot turnRed = DC and kh[1] < khValue and h1[1] >= h1Value and khValue > TopLevel;

@leo2022 @xxentre
 
A couple of questions,,,,,

1. My Stoch Divergence is painting as that single light blue square that you can see. Would it be possible to change the indicator so it draws the entire line?

2. I would be very grateful you also might have the scan for MACD divergences.

Thanks again and sorry for my lack knowledge here,,,,,,

"B"
 
Last edited by a moderator:

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