Indicator · Pine Script v5 · On the chart

MACD Signals

by g-moe· code credited to goodgains

MACD Signals overlays buy and sell labels on the price chart by combining MACD crossovers with RSI conditions.

View on GitHub

GitHub stars3on the repository
Pine versionv5runs as is
Lines of code55
Settings10inputs
LicenceNone statedfile: MPL-2.0
Last update5 months ago27 May 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 v5 from Tradingview/macd-signals-v2.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

macd-signals-v2.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © goodgains


//@version=5
indicator("MACD Signals", overlay = true)

// @keywords: macd-signals, rsi-filter, buy-signal, sell-signal, macd-crossover, macd-crossunder, bars-between-signals, signal-waiting, overbought-rsi, oversold-rsi, signal-labels, histogram, macd-line, signal-line, rsi-threshold, cooldown-bars, entry-marker, exit-marker, combined-filter, overlay-labels

// trade settings
_bars_between = input.int(10, title = 'Bars Between Signals', group = 'Signal Settings', minval = 0)
_signal_waiting = input.int(3, title = 'Signal Waiting', group = 'Signal Settings', minval = 0)

// rsi_settings
rsi_ob = input.int(65, title = 'RSI Sell Above', group = 'RSI Settings')
rsi_os = input.int(35, title = 'RSI Buy Below', group = 'RSI Settings')
rsi_source = input.source(close, title = 'RSI Source', group = 'RSI Settings')
rsi_length = input.int(14, title = 'RSI Length', group = 'RSI Settings')

// macd settings
fast_length = input(title="Fast Length", defval=12, group = 'MACD Settings')
slow_length = input(title="Slow Length", defval=26, group = 'MACD Settings')
src = input(title="Source", defval=close, group = 'MACD Settings')
signal_length = input.int(title="Signal Smoothing",  minval = 1, maxval = 50, defval = 9, group = 'MACD Settings')


// calculate values
rsi_value = ta.rsi(rsi_source, rsi_length)
[macdLine, signalLine, histLine] = ta.macd(src, fast_length, slow_length, signal_length)

var int last_bsignal = 100
var int last_ssignal = 100
var bool buy_signal = false
var bool sell_signal = false

buy_signal := ta.barssince(rsi_value <= rsi_os) <= _signal_waiting and rsi_value <= 50
 and ta.crossover(macdLine, signalLine) 
 and last_bsignal > _bars_between

sell_signal := ta.barssince(rsi_value >= rsi_ob) <= _signal_waiting  and rsi_value >= 50 
 and ta.crossunder(macdLine, signalLine) 
 and last_ssignal > _bars_between

last_bsignal := buy_signal ? 0 : nz(last_bsignal[1],0) + 1
last_ssignal := sell_signal ? 0 : nz(last_ssignal[1],0) + 1

plotshape(buy_signal, title = 'Buy Signal', color = color.lime, textcolor = color.black,
 style = shape.labelup, text = 'BUY', location = location.belowbar)

plotshape(sell_signal, title = 'Sell Signal', color = color.red, textcolor = color.black,
 style = shape.labeldown, text = 'SELL', location = location.abovebar)


alertcondition(buy_signal, "Buy Signal", "Buy Signal")
alertcondition(sell_signal, "Sell Signal", "Sell Signal")

Overview

A buy signal is generated when a bullish MACD crossover occurs within a user-defined bar limit after the RSI dips below an oversold threshold, while keeping RSI under 50 and respecting a minimum cooldown period between signals. Sell signals use opposite rules with overbought RSI levels. Inputs allow full control over MACD periods, RSI limits, wait windows, and spacing between signals.

Details

Published by

g-moe on GitHub

Code credit

goodgains, as written in the script's header

Draws

On the price chart

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
Bars Between Signalsint10_bars_between
Signal Waitingint3_signal_waiting
RSI Sell Aboveint65rsi_ob
RSI Buy Belowint35rsi_os
RSI Sourcesourceclosersi_source
RSI Lengthint14rsi_length
Fast Lengthint12fast_length
Slow Lengthint26slow_length
Sourcesourceclosesrc
Signal Smoothingint9signal_length
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

947 stars

The Adaptive RSI Moving Average (ARSIMA) is an overlay indicator that dynamically adjusts its smoothing factor based on the Relative Strength Index (RSI). When the RSI moves toward extreme overbought or oversold conditions, the smoothing factor increases to ma

Indicator v4 Moving averagesOscillators & momentum
Updated 12 days ago · GPL-3.0 GitHub repository of Adaptive RSI Moving Average

This overlay indicator plots a Supertrend from an ATR length and factor, four exponential moving averages, and a selectable VWAP or TWAP taken from a chosen timeframe on the price chart. It also shows MACD and stochastic with adjustable lengths, smoothing, ver

Indicator v5 Oscillators & momentumMoving averages
Updated 1 year ago · MPL-2.0 GitHub repository of Momentum Setup: Vijay Thakare Option Buying Scalping Setup

This indicator plots trade entry and exit signals on the price chart by combining Bollinger Bands with optional filter conditions. It calculates standard Bollinger Bands along with RSI, Aroon, and moving average filters. Reversal signals trigger when price bre

Indicator v4 Volatility & bandsOscillators & momentum
Updated 2 months ago · None stated GitHub repository of Bollinger Bands Winner IND [Alorse]
318 stars

This indicator plots three moving averages on the price chart alongside colored fills between the lines to visualize trend structure. Internally, it calculates a Stochastic RSI oscillator to detect oversold reversals. When the Stochastic %K line rises above 10

Indicator v5 Moving averagesOscillators & momentum
Updated 2 months ago · MIT GitHub repository of TVT 3X Scale In Out
285 stars

Mean Reversion Study is an indicator that identifies trade setups combining exponential moving averages, RSI extremes, and engulfing candlestick patterns. It highlights bullish setups when a bullish engulfing candle forms below the EMA while RSI is oversold, a

Indicator v4 Mean reversionCandlestick patterns
Updated 3 years ago · None stated GitHub repository of Mean Reversion Study

More Oscillators & momentum scripts

Related on ForexR

Trade Your Charts With a Regulated Broker

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

Compare brokers