CT Reverse MACD Cross For ThinkOrSwim

sggarg

New member
VIP
I came across this Reverse MACD indicator (Author: The_Caretaker)
The unique feature is that the Fast and Slow lines change color to help identify candle by candle the Bullishness or Bearishness of the chart.

Author states:
The Reverse function of the MACD provides value by letting the user know the specific price needed to expect a MACD cross over in the opposite direction.

This function can be used to designate risk parameters for a potential trade if using the MACD as their source of edge, letting the user know exactly where and how much their risk is for a potential trade which can be used to design an effective trading plan.

Label shows:
  • Whether the MACD is falling or rising
  • the price level which will make the MACD to change from rising to falling or vice versa
  • the price level which will cause the MACD to cross the signal line
  • the price level which will cause the MACD to cross the zero line

ozaSbJd.png



I would appreciate if someone could convert it to be useful in ThinkorSwim.
on Trading view (https://www.tradingview.com/v/rLixSYji/),
 
Last edited by a moderator:
I came across this Reverse MACD indicator (Author: The_Caretaker) on Trading view (https://www.tradingview.com/v/rLixSYji/), the unique feature is that the Fast and Slow lines change color to help identify candle by candle the Bullishness or Bearishness of the chart. I would appreciate if someone could convert it to be useful in ThinkorSwim.
check below:
CSS:
#/ This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// @author  = The_Caretaker
#// © The_Caretaker
#// Much respect to the original creator of this idea Mr Johhny Dough
#// And the related works of Giorgos Siligardos and Dimitris Tsokakis
#// The prior work of the above mentioned inspired me to bring the idea to its logical conclusion on the MACD
#// by creating the reverse MACD EMA & SMA signal line cross functions
#// Feel free to reuse or develop this script further, please drop me a note below if you find it useful.
#indicator ( 'CT Reverse MACD Cross', 'CT_MACD', false, format.price, precision=2 )
# Converted by Sam4Cok@Samer800    - 06/2024
declare lower;

input avgType = AverageType.EXPONENTIAL;
input source    = close; #,          'Price Source'
input fast_length  = 12; #,             'Fast Period'
input slow_length  = 26; #,             'Slow Period'
input signalLength = 9; #,              'MA Length',
input showMacdLine = yes; #        'Show MACD'
input showSignalLine = yes; #        'Show EMA'
input showHistogram  = yes; #           'Show EMA Histogram',
input HistogramScalingFactor = 1.0; #        'Histogram Scaling Factor'
input showInfoLabel = yes;

def na = Double.NaN;
def last = IsNaN(close);

DefineGlobalColor("up", CreateColor(0, 94, 255));
DefineGlobalColor("dup", CreateColor(0, 51, 137));
DefineGlobalColor("dn", CreateColor(255, 157, 0));
DefineGlobalColor("ddn", CreateColor(137, 85, 0));
#// Function Declarations
script alpha {
    input len = 12;
    def alpha = 2 / (len + 1);
    plot out = alpha;
}
#    // returns exponential weighted multiplier for period len
script macd_eq {
    input price = close;
    input fast_len = 12;
    input slow_len = 26;
    def fastEMA = ExpAverage(price, fast_len)[1];
    def slowEMA = ExpAverage(price, slow_len)[1];
    def alphaFast = alpha(fast_len);
    def alphaSlow = alpha(slow_len);
    def macd_eq = (alphaFast * fastEMA - alphaSlow * slowEMA) / (alphaFast - alphaSlow);
    def level = ((1 - alphaSlow) * slowEMA - (1 - alphaFast) * fastEMA) / (alphaFast - alphaSlow);
    plot eq = macd_eq;
    plot lvl = level;
}
script macd_cross_ema {
    input price = close;
    input macd = close;
    input fast_len = 12;
    input slow_len = 26;
    input sig_len  = 9;
    def pEMAx = ExpAverage(price, fast_len)[1];
    def pEMAy = ExpAverage(price, slow_len)[1];
    def pEMAz = ExpAverage(macd, sig_len)[1];
    def alphaFast = alpha(fast_len);
    def alphaSlow = alpha(slow_len);
    def alphaSig = alpha(sig_len);
    def macd_cross_ema = (pEMAx * alphaFast * alphaSig - pEMAy * alphaSlow * alphaSig - pEMAx * alphaFast +
      pEMAy * alphaSlow + pEMAy * alphaSig + pEMAz * alphaSig -
      pEMAx * alphaSig - pEMAy - pEMAz + pEMAx ) / (alphaFast * alphaSig - alphaSlow * alphaSig - alphaFast + alphaSlow);
    plot out = macd_cross_ema;
}
#// Calculations

def fast_ema    = ExpAverage(source, fast_length );
def slow_ema    = ExpAverage (source, slow_length );
def macd        = fast_ema - slow_ema;
def c_macd      = if macd > macd[1] then 1 else 0;
def ema_sig_line = MovingAverage(avgType, macd, signalLength);
def c_ema        = if ema_sig_line > ema_sig_line[1] then 1 else 0;
def ema_hist     = macd - ema_sig_line;
def c_hist_e     = if ema_hist >= 0 then
                   if ema_hist[1] < ema_hist then 2 else 1 else
                   if ema_hist[1] < ema_hist then -1 else -2;
#-- plot
plot SignalLine = if showSignalLine then ema_sig_line else na; #, 'MACD EMA Signal Line'
plot macdLine = if showMacdLine then macd else na;
plot histo = if showHistogram then ema_hist * HistogramScalingFactor else na; #, 'EMA Histogram'
plot zero = if last then na else 0;

zero.SetDefaultColor(Color.DARK_GRAY);
macdLine.AssignValueColor(if c_macd then Color.GREEN else COlor.RED);
SignalLine.AssignValueColor(if c_ema then Color.CYAN else COlor.MAGENTA);
histo.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
histo.AssignValueColor(if c_hist_e== 2 then GlobalColor("up") else
                       if c_hist_e== 1 then GlobalColor("dup") else
                       if c_hist_e==-1 then GlobalColor("ddn") else
                       if c_hist_e==-2 then GlobalColor("dn") else Color.GRAY);

#-- Label
def reverse_macd    = macd_eq(source, fast_length, slow_length ).eq;
def macd_ema_cross  = macd_cross_ema(source, macd, fast_length, slow_length, signalLength );
def macd_zero_line  = macd_eq(source, fast_length, slow_length ).lvl;
def revMCD = Round(reverse_macd, 2);
def croMCD = Round(macd_ema_cross, 2);
def zroMCD = Round(macd_zero_line, 2);

def text_eq = source > reverse_macd;
def text_cross = source > macd_ema_cross;
def text_zero = source > macd_zero_line;


AddLabel(showInfoLabel,(if text_eq then "MACD: Continues Rising Above: " + AsDollars(revMCD) else
                            "MACD: Continues Falling Below (Eq): " + AsDollars(revMCD)) +
           (if text_cross then ", Cross Below Signal Line: " + AsDollars(croMCD) else
                               ", Cross Above Signal Line: " + AsDollars(croMCD)) +
           (if zroMCD then ", Cross Below Zero Line: " + AsDollars(zroMCD) else
                           ", Cross Above Zero Line: " + AsDollars(zroMCD)), if c_macd then Color.GREEN else Color.RED);

#-- END of CODE
 
Thank you very much @samer800
check below:
CSS:
#/ This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// @author  = The_Caretaker
#// © The_Caretaker
#// Much respect to the original creator of this idea Mr Johhny Dough
#// And the related works of Giorgos Siligardos and Dimitris Tsokakis
#// The prior work of the above mentioned inspired me to bring the idea to its logical conclusion on the MACD
#// by creating the reverse MACD EMA & SMA signal line cross functions
#// Feel free to reuse or develop this script further, please drop me a note below if you find it useful.
#indicator ( 'CT Reverse MACD Cross', 'CT_MACD', false, format.price, precision=2 )
# Converted by Sam4Cok@Samer800    - 06/2024
declare lower;

input avgType = AverageType.EXPONENTIAL;
input source    = close; #,          'Price Source'
input fast_length  = 12; #,             'Fast Period'
input slow_length  = 26; #,             'Slow Period'
input signalLength = 9; #,              'MA Length',
input showMacdLine = yes; #        'Show MACD'
input showSignalLine = yes; #        'Show EMA'
input showHistogram  = yes; #           'Show EMA Histogram',
input HistogramScalingFactor = 1.0; #        'Histogram Scaling Factor'
input showInfoLabel = yes;

def na = Double.NaN;
def last = IsNaN(close);

DefineGlobalColor("up", CreateColor(0, 94, 255));
DefineGlobalColor("dup", CreateColor(0, 51, 137));
DefineGlobalColor("dn", CreateColor(255, 157, 0));
DefineGlobalColor("ddn", CreateColor(137, 85, 0));
#// Function Declarations
script alpha {
    input len = 12;
    def alpha = 2 / (len + 1);
    plot out = alpha;
}
#    // returns exponential weighted multiplier for period len
script macd_eq {
    input price = close;
    input fast_len = 12;
    input slow_len = 26;
    def fastEMA = ExpAverage(price, fast_len)[1];
    def slowEMA = ExpAverage(price, slow_len)[1];
    def alphaFast = alpha(fast_len);
    def alphaSlow = alpha(slow_len);
    def macd_eq = (alphaFast * fastEMA - alphaSlow * slowEMA) / (alphaFast - alphaSlow);
    def level = ((1 - alphaSlow) * slowEMA - (1 - alphaFast) * fastEMA) / (alphaFast - alphaSlow);
    plot eq = macd_eq;
    plot lvl = level;
}
script macd_cross_ema {
    input price = close;
    input macd = close;
    input fast_len = 12;
    input slow_len = 26;
    input sig_len  = 9;
    def pEMAx = ExpAverage(price, fast_len)[1];
    def pEMAy = ExpAverage(price, slow_len)[1];
    def pEMAz = ExpAverage(macd, sig_len)[1];
    def alphaFast = alpha(fast_len);
    def alphaSlow = alpha(slow_len);
    def alphaSig = alpha(sig_len);
    def macd_cross_ema = (pEMAx * alphaFast * alphaSig - pEMAy * alphaSlow * alphaSig - pEMAx * alphaFast +
      pEMAy * alphaSlow + pEMAy * alphaSig + pEMAz * alphaSig -
      pEMAx * alphaSig - pEMAy - pEMAz + pEMAx ) / (alphaFast * alphaSig - alphaSlow * alphaSig - alphaFast + alphaSlow);
    plot out = macd_cross_ema;
}
#// Calculations

def fast_ema    = ExpAverage(source, fast_length );
def slow_ema    = ExpAverage (source, slow_length );
def macd        = fast_ema - slow_ema;
def c_macd      = if macd > macd[1] then 1 else 0;
def ema_sig_line = MovingAverage(avgType, macd, signalLength);
def c_ema        = if ema_sig_line > ema_sig_line[1] then 1 else 0;
def ema_hist     = macd - ema_sig_line;
def c_hist_e     = if ema_hist >= 0 then
                   if ema_hist[1] < ema_hist then 2 else 1 else
                   if ema_hist[1] < ema_hist then -1 else -2;
#-- plot
plot SignalLine = if showSignalLine then ema_sig_line else na; #, 'MACD EMA Signal Line'
plot macdLine = if showMacdLine then macd else na;
plot histo = if showHistogram then ema_hist * HistogramScalingFactor else na; #, 'EMA Histogram'
plot zero = if last then na else 0;

zero.SetDefaultColor(Color.DARK_GRAY);
macdLine.AssignValueColor(if c_macd then Color.GREEN else COlor.RED);
SignalLine.AssignValueColor(if c_ema then Color.CYAN else COlor.MAGENTA);
histo.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
histo.AssignValueColor(if c_hist_e== 2 then GlobalColor("up") else
                       if c_hist_e== 1 then GlobalColor("dup") else
                       if c_hist_e==-1 then GlobalColor("ddn") else
                       if c_hist_e==-2 then GlobalColor("dn") else Color.GRAY);

#-- Label
def reverse_macd    = macd_eq(source, fast_length, slow_length ).eq;
def macd_ema_cross  = macd_cross_ema(source, macd, fast_length, slow_length, signalLength );
def macd_zero_line  = macd_eq(source, fast_length, slow_length ).lvl;
def revMCD = Round(reverse_macd, 2);
def croMCD = Round(macd_ema_cross, 2);
def zroMCD = Round(macd_zero_line, 2);

def text_eq = source > reverse_macd;
def text_cross = source > macd_ema_cross;
def text_zero = source > macd_zero_line;


AddLabel(showInfoLabel,(if text_eq then "MACD: Continues Rising Above: " + AsDollars(revMCD) else
                            "MACD: Continues Falling Below (Eq): " + AsDollars(revMCD)) +
           (if text_cross then ", Cross Below Signal Line: " + AsDollars(croMCD) else
                               ", Cross Above Signal Line: " + AsDollars(croMCD)) +
           (if zroMCD then ", Cross Below Zero Line: " + AsDollars(zroMCD) else
                           ", Cross Above Zero Line: " + AsDollars(zroMCD)), if c_macd then Color.GREEN else Color.RED);

#-- END of CODE
You are just amazingly good, it works perfect. Thanks a lot for doing the conversion.
 
The current script above shows all price action conditions at once, the following correction will only show one actual condition:

Ruby:
#// @author = The_Caretaker --- original author
#indicator ( 'CT Reverse MACD Cross', 'CT_MACD', false, format.price, precision=2 )
# 3-10-16 -- will only show one actual condition
declare lower;

input avgType = AverageType.EXPONENTIAL;
input source = close; #, 'Price Source'
input fast_length = 3; #, 'Fast Period'
input slow_length = 10; #, 'Slow Period'
input signalLength = 16; #, 'MA Length',
input showMacdLine = yes; # 'Show MACD'
input showSignalLine = yes; # 'Show EMA'
input showHistogram = yes; # 'Show EMA Histogram',
input HistogramScalingFactor = 1.0; # 'Histogram Scaling Factor'
input showInfoLabel = yes;

def na = Double.NaN;
def last = IsNaN(close);

DefineGlobalColor("up", CreateColor(0, 94, 255));
DefineGlobalColor("dup", CreateColor(0, 51, 137));
DefineGlobalColor("dn", CreateColor(255, 157, 0));
DefineGlobalColor("ddn", CreateColor(137, 85, 0));

# Function Declarations
script alpha {
input len = 12;
def alpha = 2 / (len + 1);
plot out = alpha;
}

# Returns exponential weighted multiplier for period len
script macd_eq {
input price = close;
input fast_len = 12;
input slow_len = 26;
def fastEMA = ExpAverage(price, fast_len)[1];
def slowEMA = ExpAverage(price, slow_len)[1];
def alphaFast = alpha(fast_len);
def alphaSlow = alpha(slow_len);
def macd_eq = (alphaFast * fastEMA - alphaSlow * slowEMA) / (alphaFast - alphaSlow);
def level = ((1 - alphaSlow) * slowEMA - (1 - alphaFast) * fastEMA) / (alphaFast - alphaSlow);
plot eq = macd_eq;
plot lvl = level;
}

script macd_cross_ema {
input price = close;
input macd = close;
input fast_len = 12;
input slow_len = 26;
input sig_len = 9;
def pEMAx = ExpAverage(price, fast_len)[1];
def pEMAy = ExpAverage(price, slow_len)[1];
def pEMAz = ExpAverage(macd, sig_len)[1];
def alphaFast = alpha(fast_len);
def alphaSlow = alpha(slow_len);
def alphaSig = alpha(sig_len);
def macd_cross_ema = (pEMAx * alphaFast * alphaSig - pEMAy * alphaSlow * alphaSig - pEMAx * alphaFast +
pEMAy * alphaSlow + pEMAy * alphaSig + pEMAz * alphaSig -
pEMAx * alphaSig - pEMAy - pEMAz + pEMAx ) / (alphaFast * alphaSig - alphaSlow * alphaSig - alphaFast + alphaSlow);
plot out = macd_cross_ema;
}

# Calculations
def fast_ema = ExpAverage(source, fast_length );
def slow_ema = ExpAverage (source, slow_length );
def macd = fast_ema - slow_ema;
def c_macd = if macd > macd[1] then 1 else 0;
def ema_sig_line = MovingAverage(avgType, macd, signalLength);
def c_ema = if ema_sig_line > ema_sig_line[1] then 1 else 0;
def ema_hist = macd - ema_sig_line;
def c_hist_e = if ema_hist >= 0 then
if ema_hist[1] < ema_hist then 2 else 1 else
if ema_hist[1] < ema_hist then -1 else -2;

# Plot
plot SignalLine = if showSignalLine then ema_sig_line else na;
plot macdLine = if showMacdLine then macd else na;
plot histo = if showHistogram then ema_hist * HistogramScalingFactor else na;
plot zero = if last then na else 0;

zero.SetDefaultColor(Color.DARK_GRAY);
macdLine.AssignValueColor(if c_macd then Color.GREEN else Color.RED);
SignalLine.AssignValueColor(if c_ema then Color.CYAN else Color.MAGENTA);
histo.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
histo.AssignValueColor(if c_hist_e== 2 then GlobalColor("up") else
if c_hist_e== 1 then GlobalColor("dup") else
if c_hist_e==-1 then GlobalColor("ddn") else
if c_hist_e==-2 then GlobalColor("dn") else Color.GRAY);

# Label
def reverse_macd = macd_eq(source, fast_length, slow_length ).eq;
def macd_ema_cross = macd_cross_ema(source, macd, fast_length, slow_length, signalLength );
def macd_zero_line = macd_eq(source, fast_length, slow_length ).lvl;
def revMCD = Round(reverse_macd, 2);
def croMCD = Round(macd_ema_cross, 2);
def zroMCD = Round(macd_zero_line, 2);

def text_eq = source > reverse_macd;
def text_cross = source > macd_ema_cross;
def text_zero = source > macd_zero_line;

AddLabel(showInfoLabel,
if text_eq then "MACD: Continues Rising Above: " + AsDollars(revMCD)
else if text_cross then "MACD: Cross Below Signal Line: " + AsDollars(croMCD)
else if text_zero then "MACD: Cross Below Zero Line: " + AsDollars(zroMCD)
else "MACD: Falling",
if c_macd then Color.GREEN else Color.PINK
 
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
423 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