#/ 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