Strategy · Pine Script v5 · Own pane

MACD EMA Filter Strategy

by SynergOps· code credited to Salih Emin (cerebrux)

This strategy combines the Moving Average Convergence Divergence oscillator with an Exponential Moving Average trend filter.

View on GitHub

GitHub stars7on the repository
Pine versionv5runs as is
Lines of code40
Settings4inputs
LicenceMPL-2.0
Last update1 year ago4 Aug 2025

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 strategy, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart. The trades and results appear in the Strategy Tester tab; adjust the settings with the gear icon.

Source Code

40 lines of Pine Script v5 from MACD - EMA - Strategy/macd_ema.pine, published under MPL-2.0. Keep the author's credit and licence lines when you reuse it.

macd_ema.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Salih Emin (cerebrux)

//@version=5
strategy("MACD EMA filter strategy", shorttitle= "MACD / EMA filter", overlay=false)

// Variables that you can optimize based on the timeframe or underlining asset

fastLength = input(12)
slowlength = input(26)
MACDLength = input(9)
EMA = input(200)

// Calculation of MACD Components

MACD = ta.ema(close, fastLength) - ta.ema(close, slowlength)
signal = ta.ema(MACD, MACDLength)
deltaHistogram = MACD-signal

// ================ Strategy =============================================== 

// If there is a crossover of signal and MACD line and the price closed above the EMA, enter a long position
// Exit when a crossunder of signal and MACD line happens
if (ta.crossover(deltaHistogram, 0)) and close>ta.ema(close, EMA)
    strategy.entry("long",strategy.long, comment="Enter Long")
if (ta.crossunder(deltaHistogram, 0))
    strategy.close("long", comment="Exit Long")

// If there is a crossunder of signal and MACD line and the price closed bleow the EMA, enter a short position
// Exit when a crossover of signal and MACD line happens
if (ta.crossunder(deltaHistogram, 0)) and close<ta.ema(close, EMA)
    strategy.entry("short",strategy.short, comment="Enter Short")
if (ta.crossover(deltaHistogram, 0))
    strategy.close("short", comment="Exit Short")
//===========================================================================
// Display the MACD indicator components

plot(deltaHistogram,title="MACD histogram",style=plot.style_histogram,color=color.blue)
plot(MACD, title="MACD line",color=color.green)
plot(signal,title="signal line",color=color.red)

Overview

MACD EMA Filter Strategy enters long positions when the MACD histogram crosses above zero while price stays above the long-term EMA filter. Long trades close on a MACD histogram bearish crossover. Short entries trigger when the MACD histogram crosses below zero while price is below the EMA, closing on a bullish cross. Main settings control MACD lengths and the EMA period. The indicator plots the MACD line, signal line, and histogram in a separate pane. The strategy lacks a stop loss.

Details

Published by

SynergOps on GitHub

Code credit

Salih Emin (cerebrux), as written in the script's header

Type

Strategy, Pine Script v5

Draws

Separate pane below the chart

Category

Trend, Oscillators & momentum, Moving averages

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

  • The strategy enters trades without a stop loss

Settings

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

SettingTypeDefaultVariable
fastLengthint12fastLength
slowlengthint26slowlength
MACDLengthint9MACDLength
EMAint200EMA
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

330 stars

MACD + Divergences [Alorse] is a momentum strategy that calculates the MACD oscillator and identifies regular and hidden divergences using pivot high and low points. It filters trade signals with dual Exponential Moving Averages (EMA 150 and 600) to ensure tra

Strategy v4 Oscillators & momentumDivergence Repaints
Updated 2 months ago · None stated GitHub repository of MACD + Divergences [Alorse]

MACD Crossover (tutorial)

by pineforge-4pass
193 stars

This strategy calculates the Moving Average Convergence Divergence indicator and plots the MACD line, signal line, and histogram in a separate pane. It enters long positions when the MACD line crosses above the signal line and enters short positions when it cr

Strategy v6 Oscillators & momentumMoving averages
Updated today · Apache-2.0 GitHub repository of MACD Crossover (tutorial)

ADX Strategy

by hasnocool
106 stars

An ADX-based trend-following strategy that enters long positions when the positive directional indicator (DI+) crosses above the Average Directional Index (ADX) while ADX remains below a preset threshold and a fast EMA is above a slow EMA. It allows adding to

Strategy v4 TrendOscillators & momentum
Updated 2 years ago · GPL-3.0 GitHub repository of ADX Strategy

EMA Ribbon MACD

by iamrichardD
57 stars

This strategy combines an EMA ribbon with MACD confirmation and plots in a separate pane. It enters when ribbon alignment, optional MACD zero or signal crosses, volume, smart-money, and multi-timeframe filters agree, then exits with percentage stop loss and ta

Strategy v6 TrendMoving averages
Updated 1 year ago · Apache-2.0 GitHub repository of EMA Ribbon MACD

This strategy evaluates the dynamic relationship between price and a chosen moving average across six diagnostic dimensions: trend, respect, extension, compression, acceleration, and exhaustion. It plots a normalized relationship oscillator bounded between -10

Strategy v6 TrendMoving averages
Updated yesterday · MIT GitHub repository of Market–Average Relationship Engine [WavesUnchained]

Cerebr

by jerryinyang
5 stars

This strategy combines optional Hull moving average, SuperTrend, and MACD conditions in a separate pane. It marks HMA rising or falling over a chosen period and SuperTrend long or short direction, and it treats MACD above or below its signal line as buy or sel

Strategy v4 TrendMoving averages
Updated 2 years ago · None stated GitHub repository of Cerebr

More strategys

Trade Your Charts With a Regulated Broker

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

Compare brokers