[blackcat] L3 Jurik MACD for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
6ut2IGb.png


Author Message:

Level: 3
Background
Use Jurik MA to build MACD and many people need to judge the market trend against the main candlestick chart when using MACD .
Function
First of all, the MACD function is built with Jurik MA and ALMA for better performance.
Second, the principle of MACD is the difference between EMA's long-term and short-term values. So, I wonder if it is possible to use EMA to construct a set of candle charts that are similar in proportion to MACD values for overlapping comparisons? Because this can greatly facilitate traders to make quick trend judgments. So I used the 3-8 lines of EMA to simulate the KD of KDJ, constructed a set of candle charts, and generated buying and selling points through conditional constraints. Do you like this MACD + Candlestick chart?
Key Signal
Traditional Jurik MACD output signal
Candlesticks
Near Top --> Top is reached and reversal may happen soon. (fuchsia labels)
Near Bottom --> Bottom is reached and reversal may happen soon. (yellow labels)

CODE:
CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © blackcat1402
#indicator('[blackcat] L3 Jurik MACD', overlay=false,
#Converted and modifiedTrueRange by Sam4Cok@Samer800 - 01/2023
declare lower;
input ColorSignalBar = yes;
input Bubbles = yes;
input SignalLine = yes;
input MovAvgCandles = yes;
input ShowHistogram = yes;
input source = hl2;
input fastlen = 12;
input slowlen = 26;
input siglen = 9;

def na = Double.NaN;
script nz {
    input data  = close;
    input repl  = 0;
    def ret_val = if IsNaN(data) or !data then repl else data;
    plot return = ret_val;
}
#barssince(Condition) =>
script barssince {
input Condition = 0;
def barssince = if Condition then 1 else barssince[1] + 1;
plot return = barssince;
}
#pine_alma(series, windowsize, offset, sigma) =>
script ALMA {
    input series = close;
    input windowsize = 9;
    input Offset = 0.85;
    input Sigma = 6;
    def m = Offset * (windowsize - 1);
    def s = windowsize / Sigma;
    def norm  = fold z = 0 to windowsize with CW do
         CW + Exp(-(Sqr(z - m)) / (2 * Sqr(s)));
    def sum  = fold y = 0 to windowsize with WS do
         WS + Exp(-(Sqr(y - m)) / (2 * Sqr(s))) * GetValue(series, windowsize - 1 - y);
    plot ALMA = sum  / norm ;
}
#jma(source, period, phase, power) =>
script jma {
    input source = close;
    input period = 10;
    input phase = 0;
    input power = 1;
    def phaseRatio = if phase < -100 then 0.5 else
                     if phase > 100  then 2.5 else phase / 100 + 1.5;
    def beta = 0.45 * (period - 1) / (0.45 * (period - 1) + 2);
    def alpha = Power(beta, power);
    def jma;
    def e0;
    def e1;
    def e2;
    e0 = (1 - alpha) * source + alpha * nz(e0[1],source) ;
    e1 = (source - e0) * (1 - beta) + beta * e1[1];
    e2 = (e0 + phaseRatio * e1 - jma[1]) * Sqr(1 - alpha) + Sqr(alpha) * e2[1];
    jma = e2 + jma[1];
    plot return = jma;
}

def dif = jma(source, fastlen, 0, 1.0);
def de  = jma(source, slowlen, 0, 1.0);
def diff = (dif - de);
def dea = alma(diff, siglen, 0.85, 6);
def histo = (diff - dea) * 2;

#//plot MACD
def pdiff = diff;
def pdea = dea;
AddCloud(if SignalLine then pdiff else na, pdea, Color.YELLOW, CreateColor(224,64,251), yes);

#//define MACD histogram
def difMac = histo;
def ghist = difMac * 2;
def pos_ghist = Max(0, ghist);
def neg_ghist = Min(0, ghist);
def currhist = ghist[0];
def prevhist = ghist[1];
def plotColor = if !ShowHistogram then na else
                if ghist > 0 then
                if currhist > prevhist then 2 else 1 else
                if currhist < prevhist then -2 else -1;
#//plot MACD histogram
AddChart(high = if plotColor == 2 then pos_ghist else na, low = 0, open = pos_ghist, close = 0,
         type = ChartType.CANDLE, growcolor = Color.GREEN);
AddChart(high = if plotColor == 1 then pos_ghist else na, low = 0, open = pos_ghist, close = 0,
         type = ChartType.CANDLE, growcolor = Color.DARK_GREEN);

AddChart(high = 0, low = if plotColor == -2 then neg_ghist else na, open = 0, close = neg_ghist,
         type = ChartType.CANDLE, growcolor = Color.RED);
AddChart(high = 0 , low = if plotColor == -1 then neg_ghist else na, open = 0, close = neg_ghist,
         type = ChartType.CANDLE, growcolor = Color.DARK_RED);

#// Mov Avg Candles
def _c = ExpAverage(close, 3) - ExpAverage(close, 8);
def _o = ExpAverage(open, 3)  - ExpAverage(open, 8);
def _h = ExpAverage(high, 3)  - ExpAverage(high, 8);
def _l = ExpAverage(low, 3)   - ExpAverage(low, 8);

def long = (if histo > 0 then (_c crosses above  dea) else (_c crosses above diff)) or _c > _o and (_c crosses above 0);
def short = if histo > 0 then (_c crosses below diff) else (_c crosses below dea) or _c < _o and (_c crosses below 0);

def up = _c >= _o;

AddChart(high = if up and MovAvgCandles then _h else na, low = _l, open = _c, close = _o,
         type = ChartType.CANDLE, growcolor = Color.CYAN);
AddChart(high = if up or !MovAvgCandles then na else _h, low = _l, open = _o, close = _c,
         type = ChartType.CANDLE, growcolor = Color.BLUE);

#//Top and Bottom Warnings
def CrossUp = crosses(diff,dea,CrossingDirection.ABOVE);
def CrossDn = crosses(diff,dea,CrossingDirection.BELOW);
def a1 = barssince(CrossUp)[1];
def a2 = barssince(CrossDn)[1];
def diffVal1 = GetValue(diff,a1+1);
def CloVal1 = GetValue(close,a1+1);
def diffVal2 = GetValue(diff,a2+1);
def CloVal2 = GetValue(close,a2+1);
def bottom_zone = (CloVal1>close) and (diff>diffVal1) and CrossUp;
def top_zone = (CloVal2<close) and (diffVal2>diff) and CrossDn;

#/ Plot labels
AddChartBubble(Bubbles and top_zone, dea + 0.01, "Near Top", Color.MAGENTA, yes);
AddChartBubble(Bubbles and bottom_zone, diff - 0.01, "Near Bottom", Color.YELLOW, no);

# -- Sig Bar Colr

AssignPriceColor( if !ColorSignalBar then Color.CURRENT else
                  if top_zone then Color.MAGENTA else
                  if bottom_zone then Color.CYAN else Color.CURRENT);

#-- END CODE
 
Last edited by a moderator:
Thank you for sharing this code. I am wondering if someone can help with the plotting of arrows for the following part of the above code. I have been trying and just cannot figure it out...

AddChart(high = if plotColor == 2 then pos_ghist else na, low = 0, open = pos_ghist, close = 0,
type = ChartType.CANDLE, growcolor = Color.GREEN);

AddChart(high = if plotColor == 1 then pos_ghist else na, low = 0, open = pos_ghist, close = 0,
type = ChartType.CANDLE, growcolor = Color.DARK_GREEN);

AddChart(high = 0, low = if plotColor == -2 then neg_ghist else na, open = 0, close = neg_ghist,
type = ChartType.CANDLE, growcolor = Color.RED);

AddChart(high = 0 , low = if plotColor == -1 then neg_ghist else na, open = 0, close = neg_ghist,
type = ChartType.CANDLE, growcolor = Color.DARK_RED);
 
View attachment 17336

Author Message:

Level: 3
Background
Use Jurik MA to build MACD and many people need to judge the market trend against the main candlestick chart when using MACD .
Function
First of all, the MACD function is built with Jurik MA and ALMA for better performance.
Second, the principle of MACD is the difference between EMA's long-term and short-term values. So, I wonder if it is possible to use EMA to construct a set of candle charts that are similar in proportion to MACD values for overlapping comparisons? Because this can greatly facilitate traders to make quick trend judgments. So I used the 3-8 lines of EMA to simulate the KD of KDJ, constructed a set of candle charts, and generated buying and selling points through conditional constraints. Do you like this MACD + Candlestick chart?
Key Signal
Traditional Jurik MACD output signal
Candlesticks
Near Top --> Top is reached and reversal may happen soon. (fuchsia labels)
Near Bottom --> Bottom is reached and reversal may happen soon. (yellow labels)

CODE:
CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © blackcat1402
#indicator('[blackcat] L3 Jurik MACD', overlay=false,
#Converted and modifiedTrueRange by Sam4Cok@Samer800 - 01/2023
declare lower;
input ColorSignalBar = yes;
input Bubbles = yes;
input SignalLine = yes;
input MovAvgCandles = yes;
input ShowHistogram = yes;
input source = hl2;
input fastlen = 12;
input slowlen = 26;
input siglen = 9;

def na = Double.NaN;
script nz {
    input data  = close;
    input repl  = 0;
    def ret_val = if IsNaN(data) or !data then repl else data;
    plot return = ret_val;
}
#barssince(Condition) =>
script barssince {
input Condition = 0;
def barssince = if Condition then 1 else barssince[1] + 1;
plot return = barssince;
}
#pine_alma(series, windowsize, offset, sigma) =>
script ALMA {
    input series = close;
    input windowsize = 9;
    input Offset = 0.85;
    input Sigma = 6;
    def m = Offset * (windowsize - 1);
    def s = windowsize / Sigma;
    def norm  = fold z = 0 to windowsize with CW do
         CW + Exp(-(Sqr(z - m)) / (2 * Sqr(s)));
    def sum  = fold y = 0 to windowsize with WS do
         WS + Exp(-(Sqr(y - m)) / (2 * Sqr(s))) * GetValue(series, windowsize - 1 - y);
    plot ALMA = sum  / norm ;
}
#jma(source, period, phase, power) =>
script jma {
    input source = close;
    input period = 10;
    input phase = 0;
    input power = 1;
    def phaseRatio = if phase < -100 then 0.5 else
                     if phase > 100  then 2.5 else phase / 100 + 1.5;
    def beta = 0.45 * (period - 1) / (0.45 * (period - 1) + 2);
    def alpha = Power(beta, power);
    def jma;
    def e0;
    def e1;
    def e2;
    e0 = (1 - alpha) * source + alpha * nz(e0[1],source) ;
    e1 = (source - e0) * (1 - beta) + beta * e1[1];
    e2 = (e0 + phaseRatio * e1 - jma[1]) * Sqr(1 - alpha) + Sqr(alpha) * e2[1];
    jma = e2 + jma[1];
    plot return = jma;
}

def dif = jma(source, fastlen, 0, 1.0);
def de  = jma(source, slowlen, 0, 1.0);
def diff = (dif - de);
def dea = alma(diff, siglen, 0.85, 6);
def histo = (diff - dea) * 2;

#//plot MACD
def pdiff = diff;
def pdea = dea;
AddCloud(if SignalLine then pdiff else na, pdea, Color.YELLOW, CreateColor(224,64,251), yes);

#//define MACD histogram
def difMac = histo;
def ghist = difMac * 2;
def pos_ghist = Max(0, ghist);
def neg_ghist = Min(0, ghist);
def currhist = ghist[0];
def prevhist = ghist[1];
def plotColor = if !ShowHistogram then na else
                if ghist > 0 then
                if currhist > prevhist then 2 else 1 else
                if currhist < prevhist then -2 else -1;
#//plot MACD histogram
AddChart(high = if plotColor == 2 then pos_ghist else na, low = 0, open = pos_ghist, close = 0,
         type = ChartType.CANDLE, growcolor = Color.GREEN);
AddChart(high = if plotColor == 1 then pos_ghist else na, low = 0, open = pos_ghist, close = 0,
         type = ChartType.CANDLE, growcolor = Color.DARK_GREEN);

AddChart(high = 0, low = if plotColor == -2 then neg_ghist else na, open = 0, close = neg_ghist,
         type = ChartType.CANDLE, growcolor = Color.RED);
AddChart(high = 0 , low = if plotColor == -1 then neg_ghist else na, open = 0, close = neg_ghist,
         type = ChartType.CANDLE, growcolor = Color.DARK_RED);

#// Mov Avg Candles
def _c = ExpAverage(close, 3) - ExpAverage(close, 8);
def _o = ExpAverage(open, 3)  - ExpAverage(open, 8);
def _h = ExpAverage(high, 3)  - ExpAverage(high, 8);
def _l = ExpAverage(low, 3)   - ExpAverage(low, 8);

def long = (if histo > 0 then (_c crosses above  dea) else (_c crosses above diff)) or _c > _o and (_c crosses above 0);
def short = if histo > 0 then (_c crosses below diff) else (_c crosses below dea) or _c < _o and (_c crosses below 0);

def up = _c >= _o;

AddChart(high = if up and MovAvgCandles then _h else na, low = _l, open = _c, close = _o,
         type = ChartType.CANDLE, growcolor = Color.CYAN);
AddChart(high = if up or !MovAvgCandles then na else _h, low = _l, open = _o, close = _c,
         type = ChartType.CANDLE, growcolor = Color.BLUE);

#//Top and Bottom Warnings
def CrossUp = crosses(diff,dea,CrossingDirection.ABOVE);
def CrossDn = crosses(diff,dea,CrossingDirection.BELOW);
def a1 = barssince(CrossUp)[1];
def a2 = barssince(CrossDn)[1];
def diffVal1 = GetValue(diff,a1+1);
def CloVal1 = GetValue(close,a1+1);
def diffVal2 = GetValue(diff,a2+1);
def CloVal2 = GetValue(close,a2+1);
def bottom_zone = (CloVal1>close) and (diff>diffVal1) and CrossUp;
def top_zone = (CloVal2<close) and (diffVal2>diff) and CrossDn;

#/ Plot labels
AddChartBubble(Bubbles and top_zone, dea + 0.01, "Near Top", Color.MAGENTA, yes);
AddChartBubble(Bubbles and bottom_zone, diff - 0.01, "Near Bottom", Color.YELLOW, no);

# -- Sig Bar Colr

AssignPriceColor( if !ColorSignalBar then Color.CURRENT else
                  if top_zone then Color.MAGENTA else
                  if bottom_zone then Color.CYAN else Color.CURRENT);

#-- END CODE
Phenomenal work on this MACD! can anyone make it work on the Mobile?
 

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