Indicator · Pine Script v6 · Own pane

CT Reverse MACD Cross

by mushroom-men-Trading· code credited to The_Caretaker

This indicator calculates and displays a Moving Average Convergence Divergence (MACD) oscillator alongside EMA and SMA signal lines and histograms in a separate pane.

View on GitHub

GitHub stars2on the repository
Pine versionv6current
Lines of code184
Settings40inputs
LicenceNone statedfile: MPL-2.0
Last update9 months ago25 Jan 2026

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

184 lines of Pine Script v6 from Indicators/indicators/reversals/rmacd.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

rmacd.pine
//@version=6
// 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)

//////////////////////////////////////////////////////////////////////////////
// Inputs

i_source_price = input.source(close, 'Price Source', inline = '1', group = 'MACD Properties')
i_fast_length = input.int(12, 'Fast Period', minval = 1, maxval = 500, inline = '2', group = 'MACD Properties')
i_slow_length = input.int(26, 'Slow Period', minval = 1, maxval = 500, inline = '3', group = 'MACD Properties')

i_MACDOn = input.bool(true, 'Show MACD', inline = '0', group = 'MACD Line Plot Settings')
i_c_typ_line = input.string('Dual', 'Color Type', options = ['Single', 'Dual'], inline = '1', group = 'MACD Line Plot Settings')
i_c_line = input.color(#ff9100, 'Single Color', inline = '2', group = 'MACD Line Plot Settings')
i_c_line_rise = input.color(#26A69A, 'Dual Color (Rising)', inline = '3', group = 'MACD Line Plot Settings')
i_c_line_fall = input.color(#EF5350, 'Dual Color (Falling)', inline = '3', group = 'MACD Line Plot Settings')
i_p_width_line = input.int(2, 'Line Width', minval = 1, maxval = 4, inline = '4', group = 'MACD Line Plot Settings')

i_ema_on = input.bool(true, 'Show EMA', inline = '1', group = 'EMA Properties & Crossover Plot Settings')
i_emaLen = input.int(9, 'MA Length', minval = 1, inline = '2', group = 'EMA Properties & Crossover Plot Settings')
i_c_typ_ema = input.string('Dual', 'Color Type', options = ['Single', 'Dual'], inline = '3', group = 'EMA Properties & Crossover Plot Settings')
i_c_ema = input.color(#ffffff, 'Single Color', inline = '3', group = 'EMA Properties & Crossover Plot Settings')
i_c_ema_rise = input.color(#005eff, 'Dual Color (Rising)', inline = '4', group = 'EMA Properties & Crossover Plot Settings')
i_c_ema_fall = input.color(#ff9d00, 'Dual Color (Falling)', inline = '4', group = 'EMA Properties & Crossover Plot Settings')
i_p_width_ema = input.int(2, 'Line Width', minval = 1, maxval = 4, inline = '5', group = 'EMA Properties & Crossover Plot Settings')

i_sma_on = input.bool(false, 'Show SMA', inline = '1', group = 'SMA Properties & Crossover Plot Settings')
i_smaLen = input.int(9, 'MA Length', minval = 1, inline = '2', group = 'SMA Properties & Crossover Plot Settings')
i_c_typ_sma = input.string('Dual', 'Color Type', options = ['Single', 'Dual'], inline = '3', group = 'SMA Properties & Crossover Plot Settings')
i_c_sma = input.color(#ffffff, 'Single Color', inline = '3', group = 'SMA Properties & Crossover Plot Settings')
i_c_sma_rise = input.color(#00ff00, 'Dual Color (Rising)', inline = '4', group = 'SMA Properties & Crossover Plot Settings')
i_c_sma_fall = input.color(#ff0000, 'Dual Color (Falling)', inline = '4', group = 'SMA Properties & Crossover Plot Settings')
i_p_width_sma = input.int(2, 'Line Width', minval = 1, maxval = 4, inline = '5', group = 'SMA Properties & Crossover Plot Settings')


i_hist_e_On = input.bool(true, 'Show EMA Histogram', inline = '1', group = 'EMA Histogram Settings')
i_hist_e_Fact = input.int(1, 'Histogram Scaling Factor', minval = 0, maxval = 10, inline = '2', group = 'EMA Histogram Settings')
i_c_hist_e_Urise = input.color(#005eff88, 'Upper Rising', inline = '3', group = 'EMA Histogram Settings')
i_c_hist_e_Ufall = input.color(#7babff87, 'Upper Falling', inline = '3', group = 'EMA Histogram Settings')
i_c_hist_e_Lfall = input.color(#ff9d0085, 'Lower Falling', inline = '4', group = 'EMA Histogram Settings')
i_c_hist_e_Lrise = input.color(#ffcb7884, 'Lower Rising', inline = '4', group = 'EMA Histogram Settings')

i_hist_s_On = input.bool(false, 'Show SMA Histogram', inline = '1', group = 'SMA Histogram Settings')
i_hist_s_Fact = input.int(1, 'Histogram Scaling Factor', minval = 0, maxval = 10, inline = '2', group = 'SMA Histogram Settings')
i_c_hist_s_Urise = input.color(#00ff0081, 'Upper Rising', inline = '3', group = 'SMA Histogram Settings')
i_c_hist_s_Ufall = input.color(#7fff7f80, 'Upper Falling', inline = '3', group = 'SMA Histogram Settings')
i_c_hist_s_Lfall = input.color(#ff000080, 'Lower Falling', inline = '4', group = 'SMA Histogram Settings')
i_c_hist_s_Lrise = input.color(#ff7f7f7f, 'Lower Rising', inline = '4', group = 'SMA Histogram Settings')

i_midLineOn = input.bool(true, '', inline = '3', group = 'Scale Settings')
i_midLine = input.int(0, 'Mid-line', minval = 0, maxval = 0, inline = '3', group = 'Scale Settings')
i_c_midline = input.color(#909090, '', inline = '3', group = 'Scale Settings')
i_p_width_mli = input.int(1, 'Width', minval = 1, maxval = 4, inline = '3', group = 'Scale Settings')

i_dec_places = input.int(2, 'Decimal Places', minval = 0, maxval = 10, inline = '1', group = 'Infobox Properties')
i_label_offset = input.int(6, 'Offset', minval = 0, maxval = 100, inline = '2', group = 'Infobox Properties')
i_c_label_txt = input.color(#FFFFFF, 'Text Color', inline = '3', group = 'Infobox Properties')
i_label_bg_fall = input.color(#EF5350, 'BG MACD Falling', inline = '4', group = 'Infobox Properties')
i_label_bg_rise = input.color(#26A69A, 'BG MACD Rising', inline = '5', group = 'Infobox Properties')

//////////////////////////////////////////////////////////////////////////////
// Function Declarations

alpha(len) =>
    2 / (len + 1)

// returns exponential weighted multiplier for period len

macd_eq(price, fast_len, slow_len) =>
    (alpha(fast_len) * ta.ema(price, fast_len)[1] - alpha(slow_len) * ta.ema(price, slow_len)[1]) / (alpha(fast_len) - alpha(slow_len))

// returns price where MACD is equal to previous bar MACD
// the user can infer from this that 
// closing above this price will cause an increase of the MACD compared to the prior MACD
// and closing below it will cause a decrease of the MACD compared to the prior MACD

macd_level(level, price, fast_len, slow_len) =>
    (level + (1 - alpha(slow_len)) * ta.ema(price, slow_len)[1] - (1 - alpha(fast_len)) * ta.ema(price, fast_len)[1]) / (alpha(fast_len) - alpha(slow_len))

// returns price where MACD is equal to level value
// e.g. macd_level ( 0, close, 12, 26 ) 
// would return the next closing price which would make MACD = 0

macd_cross_ema(P, V, X, Y, Z) =>
    pEMAx = ta.ema(P, X)[1]
    pEMAy = ta.ema(P, Y)[1]
    pEMAz = ta.ema(V, Z)[1]
    (pEMAx * alpha(X) * alpha(Z) - pEMAy * alpha(Y) * alpha(Z) - pEMAx * alpha(X) + pEMAy * alpha(Y) + pEMAy * alpha(Z) + pEMAz * alpha(Z) - pEMAx * alpha(Z) - pEMAy - pEMAz + pEMAx) / (alpha(X) * alpha(Z) - alpha(Y) * alpha(Z) - alpha(X) + alpha(Y))

// returns price where MACD is equal to the signal line
// the user can infer from this that 
// closing above this price will cause the MACD to cross above the EMA signal line
// and closing below it will cause the MACD to cross below the EMA signal line

macd_cross_sma(P, V, X, Y, Z) =>
    pEMAx = ta.ema(P, X)[1]
    pEMAy = ta.ema(P, Y)[1]
    (pEMAx * alpha(X) * Z - pEMAy * alpha(Y) * Z + math.sum(V[1], Z - 1) - pEMAx * alpha(X) + pEMAy * alpha(Y) - pEMAx * Z + pEMAy * Z + pEMAx - pEMAy) / ((-1 + Z) * (alpha(X) - alpha(Y)))

// returns price where MACD is equal to SMA signal line
// the user can infer from this that 
// closing above this price will cause the MACD to cross above the SMA signal line
// and closing below it will cause the MACD to cross below the SMA signal line

text_eq(p, x, d) =>
    text_1 = p > x ? 'MACD\n\n Continues Rising Above : ' + str.tostring(int(x * math.pow(10, d)) / math.pow(10, d)) : 'MACD\n\n Continues Falling Below (Eq) :' + str.tostring(int(x * math.pow(10, d)) / math.pow(10, d))
    text_1

text_cross(p, x, y, d, a, b) =>
    ema_text = p > x ? 'Cross Below EMA Signal Line : ' + str.tostring(int(x * math.pow(10, d)) / math.pow(10, d)) : 'Cross Above EMA Signal Line : ' + str.tostring(int(x * math.pow(10, d)) / math.pow(10, d))
    sma_text = p > y ? 'Cross Below SMA Signal Line : ' + str.tostring(int(y * math.pow(10, d)) / math.pow(10, d)) : 'Cross Above SMA Signal Line : ' + str.tostring(int(y * math.pow(10, d)) / math.pow(10, d))
    text_2 = a and b ? ema_text + '\n\n' + sma_text : a ? ema_text : b ? sma_text : ''
    text_2

text_zero(p, x, d) =>
    text_3 = p > x ? 'Cross Below Zero Line : ' + str.tostring(int(x * math.pow(10, d)) / math.pow(10, d)) : 'Cross Above Zero Line : ' + str.tostring(int(x * math.pow(10, d)) / math.pow(10, d))
    text_3

//////////////////////////////////////////////////////////////////////////////
// Calculations

fast_ema = ta.ema(i_source_price, i_fast_length)
slow_ema = ta.ema(i_source_price, i_slow_length)

macd = fast_ema - slow_ema

c_macd = i_c_typ_line == 'Single' ? i_c_line : macd > macd[1] ? i_c_line_rise : i_c_line_fall
p_c_macd = macd > macd[1] ? i_c_line_rise : i_c_line_fall

ema_sig_line = ta.ema(macd, i_emaLen)
sma_sig_line = ta.sma(macd, i_smaLen)

c_ema = i_c_typ_ema == 'Single' ? i_c_ema : ema_sig_line > ema_sig_line[1] ? i_c_ema_rise : i_c_ema_fall
c_sma = i_c_typ_sma == 'Single' ? i_c_sma : sma_sig_line > sma_sig_line[1] ? i_c_sma_rise : i_c_sma_fall

ema_hist = macd - ema_sig_line
sma_hist = macd - sma_sig_line

c_hist_e = ema_hist >= 0 ? ema_hist[1] < ema_hist ? i_c_hist_e_Urise : i_c_hist_e_Ufall : ema_hist[1] < ema_hist ? i_c_hist_e_Lrise : i_c_hist_e_Lfall
c_hist_s = sma_hist >= 0 ? sma_hist[1] < sma_hist ? i_c_hist_s_Urise : i_c_hist_s_Ufall : sma_hist[1] < sma_hist ? i_c_hist_s_Lrise : i_c_hist_s_Lfall

reverse_macd = macd_eq(i_source_price, i_fast_length, i_slow_length)

macd_ema_cross = macd_cross_ema(i_source_price, macd, i_fast_length, i_slow_length, i_emaLen)
macd_sma_cross = macd_cross_sma(i_source_price, macd, i_fast_length, i_slow_length, i_smaLen)

macd_zero_line = macd_level(0, i_source_price, i_fast_length, i_slow_length)

label_text = text_eq(i_source_price, reverse_macd, i_dec_places) + '\n\n' + text_cross(i_source_price, macd_ema_cross, macd_sma_cross, i_dec_places, i_ema_on, i_sma_on) + '\n\n' + text_zero(i_source_price, macd_zero_line, i_dec_places) + '\n'

c_label = macd < macd[1] ? i_label_bg_fall : i_label_bg_rise

//////////////////////////////////////////////////////////////////////////////
// Plots and Fills

label_X_Loc = time_close + (time_close - time_close[1]) * i_label_offset // Set Label offset

plot(i_hist_e_On ? ema_hist * i_hist_e_Fact : na, 'EMA Histogram', style = plot.style_columns, color = c_hist_e)
plot(i_hist_s_On ? sma_hist * i_hist_s_Fact : na, 'SMA Histogram', style = plot.style_columns, color = c_hist_s)

plot(i_MACDOn ? macd : na, 'MACD', c_macd, math.max(1, i_p_width_line), style = plot.style_line)
plot(i_midLineOn ? i_midLine : na, 'Zero Line', i_c_midline, math.max(1, i_p_width_mli), style = plot.style_line)

plot(i_ema_on ? ema_sig_line : na, 'MACD EMA Signal Line', c_ema, math.max(1, i_p_width_ema), style = plot.style_line)
plot(i_sma_on ? sma_sig_line : na, 'MACD SMA Signal Line', c_sma, math.max(1, i_p_width_sma), style = plot.style_line)

label = label.new(label_X_Loc, 0, label_text, xloc.bar_time, yloc.price, c_label, label.style_label_left, i_c_label_txt, size = size.normal)

label.delete(label[1]) // Delete Previous Label

//////////////////////////////////////////////////////////////////////////////
// End

Overview

CT Reverse MACD Cross offers reverse signal line calculations and detailed visual styling, including directional dual-coloring for rising and falling segments. Users can configure price source, fast and slow EMA periods, separate EMA or SMA signal line lengths, and histogram scaling.

Details

Published by

mushroom-men-Trading on GitHub

Code credit

The_Caretaker, as written in the script's header

Type

Indicator, Pine Script v6

Draws

Separate pane below the chart

Category

Oscillators & momentum, Moving averages

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
Price Sourcesourceclosei_source_price
Fast Periodint12i_fast_length
Slow Periodint26i_slow_length
Show MACDbooltruei_MACDOn
Color TypestringDuali_c_typ_line
Single Colorcolor#ff9100i_c_line
Dual Color (Rising)color#26A69Ai_c_line_rise
Dual Color (Falling)color#EF5350i_c_line_fall
Line Widthint2i_p_width_line
Show EMAbooltruei_ema_on
MA Lengthint9i_emaLen
Color TypestringDuali_c_typ_ema
Show all 40 settings
SettingTypeDefaultVariable
Single Colorcolor#ffffffi_c_ema
Dual Color (Rising)color#005effi_c_ema_rise
Dual Color (Falling)color#ff9d00i_c_ema_fall
Line Widthint2i_p_width_ema
Show SMAboolfalsei_sma_on
MA Lengthint9i_smaLen
Color TypestringDuali_c_typ_sma
Single Colorcolor#ffffffi_c_sma
Dual Color (Rising)color#00ff00i_c_sma_rise
Dual Color (Falling)color#ff0000i_c_sma_fall
Line Widthint2i_p_width_sma
Show EMA Histogrambooltruei_hist_e_On
Histogram Scaling Factorint1i_hist_e_Fact
Upper Risingcolor#005eff88i_c_hist_e_Urise
Upper Fallingcolor#7babff87i_c_hist_e_Ufall
Lower Fallingcolor#ff9d0085i_c_hist_e_Lfall
Lower Risingcolor#ffcb7884i_c_hist_e_Lrise
Show SMA Histogramboolfalsei_hist_s_On
Histogram Scaling Factorint1i_hist_s_Fact
Upper Risingcolor#00ff0081i_c_hist_s_Urise
Upper Fallingcolor#7fff7f80i_c_hist_s_Ufall
Lower Fallingcolor#ff000080i_c_hist_s_Lfall
Lower Risingcolor#ff7f7f7fi_c_hist_s_Lrise
i_midLineOnbooltruei_midLineOn
Mid-lineint0i_midLine
i_c_midlinecolor#909090i_c_midline
Widthint1i_p_width_mli
Decimal Placesint2i_dec_places
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

948 stars

The Derivative Oscillator calculates a double-exponentially smoothed Relative Strength Index (RSI) and subtracts its Simple Moving Average (SMA) to form the main oscillator line. A secondary signal line is plotted using an SMA of the main oscillator values. Th

Indicator v4 Oscillators & momentumMoving averages
Updated 10 days ago · GPL-3.0 GitHub repository of Derivative Oscillator

Beep Boop

by magic8bot
409 stars

This indicator calculates a modified MACD oscillator and signal line alongside a trend EMA filter, plotting the output as a stylized column histogram in a separate pane. Instead of plotting true MACD distance, it quantifies positive and negative histogram mome

Indicator v4 Oscillators & momentumTrend
Updated 3 years ago · MIT GitHub repository of Beep Boop

MACD

by Alorse
330 stars

This indicator calculates and plots the standard Moving Average Convergence Divergence oscillator in a separate pane. It computes the difference between a fast and slow moving average, derives a signal line through smoothing, and plots a histogram representing

Indicator v4 Oscillators & momentumMoving averages
Updated 2 months ago · None stated GitHub repository of MACD

MACD-V

by harryguiacorn
219 stars

MACD-V is a volatility-normalized momentum indicator based on research by Alex Spiroglou. It calculates the standard MACD histogram and normalizes it by dividing by the Average True Range (ATR). Plotted in a separate pane as a colored histogram, it includes us

Indicator v5 Oscillators & momentumVolatility & bands
Updated 1 year ago · None stated GitHub repository of MACD-V
218 stars

Multi-Timeframe Analysis Suite calculates trend direction and momentum conditions across five user-selected timeframes using moving averages, RSI, and Stochastic oscillators. It evaluates moving average slope and oscillator levels to compute trend alignment, m

Indicator v6 Screeners & dashboardsTrend
Updated 3 months ago · None stated GitHub repository of Multi-Timeframe Analysis Suite

KamaFama

by Mastaaa1987
141 stars

KamaFama is an indicator that translates a Freqtrade trading strategy into Pine Script for visualization. It combines Kaufman Adaptive Moving Average (KAMA), MAMA/FAMA calculations derived via Hilbert Transform, Williams %R, and correlation trend filters to ev

Indicator v6 Moving averagesOscillators & momentum
Updated 6 weeks ago · MIT GitHub repository of KamaFama

More indicators

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers