Change MACD Divergence to Stochastic..

EddieM

New member
Plus
I found this Free indicator link from the video below.
It works off MACD.
It auto draws the divergence line on the chart both up top and bottom so two parts

Can someone change this into a Stochastic version with the K and D so it draws the lines on both upper and lower but using the D Stochastic it looks for to create the lines it draws. The MACD misses a lot that the D Stochastic would find.

cANwWB9.png

Ruby:
# Mechanical MACD Divergence (lower chart)
# with both standard & volume-weighted MACD

declare lower;

input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input MACD_Type = {default "Standard", "Volume Weighted"};
input averageType = AverageType.EXPONENTIAL;

def fastAvg;
def slowAvg;

switch (MACD_Type) {
case "Standard":
    fastAvg = MovingAverage(averageType, close, fastLength);
    slowAvg = MovingAverage(averageType, close, slowLength);
case "Volume Weighted":
    fastAvg = sum(volume * close, fastLength) / sum(volume, fastLength);
    slowAvg = Sum(volume * close, slowLength) / Sum(volume, slowLength);
}

plot Value = fastAvg - slowAvg;
plot Avg = MovingAverage(averageType, Value, MACDLength);

plot Diff = Value - Avg;
plot ZeroLine = 0;

     Value.SetDefaultColor(GetColor(1));
     Avg.SetDefaultColor(GetColor(8));
     Diff.SetDefaultColor(GetColor(5));
     Diff.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
     Diff.SetLineWeight(3);
     Diff.DefineColor("Positive and Up", Color.GREEN); 
     Diff.DefineColor("Positive and Down", Color.DARK_GREEN); 
     Diff.DefineColor("Negative and Down", Color.RED); 
     Diff.DefineColor("Negative and Up", Color.DARK_RED); 
     Diff.AssignValueColor(if Diff >= 0 then if Diff > Diff[1] then Diff.color("Positive and Up") else Diff.color("Positive and Down") else if Diff < Diff[1] then Diff.color("Negative and Down") else Diff.color("Negative and Up"));
     ZeroLine.SetDefaultColor(GetColor(0));

def V = Diff < 0 and Diff < Diff[1] and Diff < Diff[-1];
def firstV = if Diff > 0 then 0 else if V then 1 else firstV[1];
def LD = if Diff crosses below 0 then Diff else if Diff < LD[1] then Diff else LD[1];
def vBar = if Diff == LD then BarNumber() else vBar[1];
def pvBar = if Diff crosses above 0 then vBar[1] else pvBar[1];
def bullV = (V and firstV[1] == 0 and Diff > GetValue(Diff, BarNumber() - pvBar)) and (low < GetValue(low, BarNumber() - pvBar));

def A = Diff > 0 and Diff > Diff[1] and Diff > Diff[-1];
def firstA = if Diff < 0 then 0 else if A then 1 else firstA[1];
def HD = if Diff crosses above 0 then Diff else if Diff > HD[1] then Diff else HD[1];
def aBar = if Diff == HD then BarNumber() else aBar[1];
def paBar = if Diff crosses below 0 then aBar[1] else paBar[1];
def bearA = (A and firstA[1] == 0 and Diff < GetValue(Diff, BarNumber() - paBar)) and (high > GetValue(high, BarNumber() - paBar));

def lastBar = HighestAll(if !IsNaN(close) then BarNumber() else 0);
# calculate bull div line
def cnV = fold i = 1 to lastBar with b = Double.NaN while IsNaN(b) do if GetValue(bullV, -i) == 1 then i else Double.NaN;
def startBullDiv = BarNumber() == GetValue(pvBar, -cnV);
def isBullDiv = if startBullDiv then 1 else if bullV[1] then 0 else isBullDiv[1];
def mBull = if startBullDiv then (GetValue(Diff, -cnV) - Diff) / cnV else mBull[1];
def line1 = if startBullDiv then Diff + (LowestAll(value) * 0.05) else line1[1] + mBull;
plot bullDivLine = if isBullDiv then line1 else Double.NaN;
     bullDivLine.SetDefaultColor(Color.YELLOW);
     bullDivLine.SetLineWeight(2);

# calculate bear div line
def cnA = fold j = 1 to lastBar with c = Double.NaN while IsNaN(c) do if GetValue(bearA, -j) == 1 then j else Double.NaN;
def startBearDiv = BarNumber() == GetValue(paBar, -cnA);
def isBearDiv = if startBearDiv then 1 else if bearA[1] then 0 else isBearDiv[1];
def mBear = if startBearDiv then (GetValue(Diff, -cnA) - Diff) / cnA else mBear[1];
def line2 = if startBearDiv then Diff + (HighestAll(value) * 0.05) else line2[1] + mBear;
plot bearDivLine = if isBearDiv then line2 else Double.NaN;
     bearDivLine.SetDefaultColor(Color.DARK_ORANGE);
     bearDivLine.SetLineWeight(2);


Here is the TOS link to it
http://tos.mx/e4lWbJJ

Here is the video link showing it is free and it has the TOS link in the video text area at the bottom.
 
Last edited by a moderator:

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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