Indicator · Pine Script v6 · Own pane

Moving Average Convergence Divergence

by Zhekabas

This indicator plots a classic MACD line, signal line, and histogram in a separate pane.

View on GitHub

GitHub stars1on the repository
Pine versionv6current
Lines of code55
Settings6inputs
LicenceMIT
Last update8 months ago19 Feb 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

55 lines of Pine Script v6 from src/macd_alerts.pine, published under MIT. Keep the author's credit and licence lines when you reuse it.

macd_alerts.pine
//@version=6
indicator(title="Moving Average Convergence Divergence", shorttitle="MACD", timeframe="", timeframe_gaps=true)

// Inputs
fast_length   = input.int(title="Fast Length", defval=12)
slow_length   = input.int(title="Slow Length", defval=26)
src           = input.source(title="Source", defval=close)
signal_length = input.int(title="Signal Smoothing",  minval=1, maxval=50, defval=9, display=display.data_window)
sma_source    = input.string(title="Oscillator MA Type",  defval="EMA", options=["SMA","EMA"], display=display.data_window)
sma_signal    = input.string(title="Signal Line MA Type", defval="EMA", options=["SMA","EMA"], display=display.data_window)

// MACD core
fast_ma = sma_source == "SMA" ? ta.sma(src, fast_length) : ta.ema(src, fast_length)
slow_ma = sma_source == "SMA" ? ta.sma(src, slow_length) : ta.ema(src, slow_length)
macd    = fast_ma - slow_ma
signal  = sma_signal == "SMA" ? ta.sma(macd, signal_length) : ta.ema(macd, signal_length)
hist    = macd - signal

// ----- Direction (slope) detection -----
was_rising   = hist[1] > hist[2]
was_falling  = hist[1] < hist[2]
now_rising   = hist > hist[1]
now_falling  = hist < hist[1]

// ----- Zone helpers -----
in_pos = hist > 0
in_neg = hist < 0

// ===== ALERТS =====
// 1) Зміна кольору (нахилу) — ГЛОБАЛЬНО
alertcondition(was_rising  and now_falling, title="Rising → Falling",  message="MACD histogram turned from rising to falling")
alertcondition(was_falling and now_rising,  title="Falling → Rising",  message="MACD histogram turned from falling to rising")

// 2) Зміна кольору тільки ВИЩЕ нуля
alertcondition(was_rising  and now_falling and in_pos, title="Rising → Falling (above 0)",  message="MACD histogram turned down while above zero")
alertcondition(was_falling and now_rising  and in_pos, title="Falling → Rising (above 0)",  message="MACD histogram turned up while above zero")

// 3) Зміна кольору тільки НИЖЧЕ нуля
alertcondition(was_rising  and now_falling and in_neg, title="Rising → Falling (below 0)",  message="MACD histogram turned down while below zero")
alertcondition(was_falling and now_rising  and in_neg, title="Falling → Rising (below 0)",  message="MACD histogram turned up while below zero")

// (Опційно) перетини нуля гістограмою
alertcondition(ta.crossunder(hist, 0), title="Histogram cross↓ zero", message="MACD histogram crossed below 0")
alertcondition(ta.crossover(hist, 0),  title="Histogram cross↑ zero", message="MACD histogram crossed above 0")

// (Опційно) перетини MACD/Signal
alertcondition(ta.crossover(macd, signal),  title="MACD cross↑ Signal", message="MACD crossed above Signal")
alertcondition(ta.crossunder(macd, signal), title="MACD cross↓ Signal", message="MACD crossed below Signal")

// Plots
hline(0, "Zero Line", color=color.new(#787B86, 50))
plot(hist, title="Histogram", style=plot.style_columns,
     color=(hist >= 0 ? (hist[1] < hist ? #26A69A : #B2DFDB) : (hist[1] < hist ? #FFCDD2 : #FF5252)))
plot(macd,   title="MACD",   color=#2962FF)
plot(signal, title="Signal", color=#FF6D00)

Overview

Moving Average Convergence Divergence subtracts a slow moving average from a fast moving average, then smooths that difference into a signal and shows the gap as columns. Alerts fire on histogram slope flips, including above or below zero, histogram zero crosses, and MACD-signal crosses. Fast length, slow length, signal smoothing, source, and EMA or SMA choices change the calculation. It does not place trades or define exits.

Details

Published by

Zhekabas on GitHub

Type

Indicator, Pine Script v6

Draws

Separate pane below the chart

Category

Oscillators & momentum

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.

Topics

alertscryptomacdpine-scripttradingview

Settings

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

SettingTypeDefaultVariable
Fast Lengthint12fast_length
Slow Lengthint26slow_length
Sourcesourceclosesrc
Signal Smoothingint9signal_length
Oscillator MA TypestringEMAsma_source
Signal Line MA TypestringEMAsma_signal
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

Asymmetrical RSI

by everget
948 stars

Asymmetrical RSI is an indicator that computes a custom relative strength index from the rate of change of a user-selected source. It plots the resulting ARSI line in a separate pane with color coding that turns green above the overbought level and red below t

Indicator v4 Oscillators & momentum
Updated 10 days ago · GPL-3.0 GitHub repository of Asymmetrical RSI

This indicator calculates momentum setups based on the Relative Strength Index (RSI) and plots in a separate pane. It offers multiple scanning modes such as Continuous Break, Continuous Retracement, Flip Break, Flip Retracement, and both regular and hidden div

Indicator v6 Oscillators & momentumDivergence Repaints
Updated 1 year ago · MPL-2.0 GitHub repository of Momentum Setup - RSI Directional Momentum Indicator

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
261 stars

This indicator demonstrates an experimental graph-style scatter visualization within a separate pane using Pine Script arrays. It tracks two features across recent bars: the single-bar price change and a scaled 14-period Relative Strength Index. Values are loa

Indicator v4 Oscillators & momentum
Updated 4 years ago · None stated GitHub repository of Example: Graph Style - 4th Dimension RSI

KD指標

by TaichiS
219 stars

This script calculates and plots a Stochastic Oscillator (KD indicator) in a separate chart pane. It determines the fast stochastic value over a 9-period lookback, then applies a 3-period Simple Moving Average (SMA) to create the %K line, and a second 3-period

Indicator v4 Oscillators & momentum
Updated 5 years ago · None stated GitHub repository of KD指標

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