Indicator · Pine Script v4 · On the chart

MTF EMA + MACD

by krosenfeld7

This indicator plots two multi-timeframe exponential moving averages on the price chart and calculates a MACD from the chart source.

View on GitHub

Repaints

GitHub stars5on the repository
Pine versionv4older, still runs
Lines of code80
Settings10inputs
LicenceApache-2.0
Last update3 years ago12 May 2023

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

80 lines of Pine Script v4 from MTF_EMA+MACD/MTF_EMA+MACD.txt, published under Apache-2.0. Keep the author's credit and licence lines when you reuse it.

MTF_EMA+MACD.txt
//@version=4
// EMA Section
study(title="MTF EMA + MACD", shorttitle="MTF EMA + MACD", overlay=true)

// MTF EMA 1
mtfShort = input(title="Short MTF EMA", type=input.resolution, defval="15")
maShort_length = input(title = "Short EMA Period", defval = 50, minval = 1)
maShort = security(syminfo.tickerid, mtfShort, ema(close, maShort_length)[barstate.isrealtime ? 1 : 0])

// MTF EMA 2
mtfLong = input(title="Long MTF EMA", type=input.resolution, defval="60")
maLong_length = input(title = "Long EMA Period", defval = 50, minval = 1)
maLong = security(syminfo.tickerid, mtfLong, ema(close, maLong_length)[barstate.isrealtime ? 1 : 0])

pEma50  = plot(maShort, title="Short MTF EMA", color=color.white, linewidth=2)
pEma200 = plot(maLong,  title="Long MTF EMA", color=color.orange, linewidth=2)

emaLong  = maShort > maLong
emaShort = maShort < maLong

// MACD Section
fast_length = input(title="Fast MACD Length", type=input.integer, defval=12)
slow_length = input(title="Slow MACD Length", type=input.integer, defval=26)
macdSrc = input(title="MACD Source", type=input.source, defval=close)
signal_length = input(title="MACD Signal Smoothing", type=input.integer, minval = 1, maxval = 50, defval = 9)
sma_source = input(title="MACD Simple MA (Oscillator)", type=input.bool, defval=false)
sma_signal = input(title="MACD Simple MA (Signal Line)", type=input.bool, defval=false)

// Calculating
fast_ma = sma_source ? sma(macdSrc, fast_length) : ema(macdSrc, fast_length)
slow_ma = sma_source ? sma(macdSrc, slow_length) : ema(macdSrc, slow_length)
macd = fast_ma - slow_ma
signal = sma_signal ? sma(macd, signal_length) : ema(macd, signal_length)
hist = macd - signal

macdCrossDn = crossunder(macd, signal)
macdCrossUp = crossover(macd,  signal)

macdCross0 = cross(macd,   0)
sigCross0  = cross(signal, 0)

varip longCrossCount  = 0
varip shortCrossCount = 0

if macdCrossDn
    longCrossCount += 1

if longCrossCount == 1 and macdCrossUp
    longCrossCount += 1

if longCrossCount == 2 and macdCrossDn
    longCrossCount += 1

if macdCross0 or sigCross0 or macd > 0 or signal > 0
    longCrossCount := 0

if macdCrossUp
    shortCrossCount += 1

if longCrossCount == 1 and macdCrossDn
    shortCrossCount += 1

if longCrossCount == 2 and macdCrossUp
    shortCrossCount += 1

if macdCross0 or sigCross0 or macd < 0 or signal < 0
    shortCrossCount := 0

long  = emaLong  and longCrossCount == 3
short = emaShort and shortCrossCount == 3


if long
    label.new(x = bar_index, y = na, text = "Long", yloc = yloc.abovebar, color = color.green)
if short
    label.new(x = bar_index, y = na, text = "Short", yloc = yloc.abovebar, color = color.red)

// Alerts
alertcondition(long, title = "Long Signal", message = "Long @ {{timenow}}: {{ticker}} -> {{interval}}: {{close}}")
alertcondition(short, title = "Short Signal", message = "Short @ {{timenow}}: {{ticker}} -> {{interval}}: {{close}}")

Overview

MTF EMA + MACD marks Long when the shorter-timeframe EMA is above the longer one and a counted sequence of MACD and signal crosses reaches three, and Short when the EMA order is reversed with the opposite cross sequence. Users can change both EMA timeframes and lengths, MACD lengths, source, and whether simple moving averages replace the EMAs. It draws labels above bars and fires alerts. Signals can change on the live bar.

Details

Published by

krosenfeld7 on GitHub

Type

Indicator, Pine Script v4

Draws

On the price chart

Category

Trend, Moving averages, Oscillators & momentum

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

  • Signals can change on the live bar or after it closes (repainting)

Settings

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

SettingTypeDefaultVariable
Short MTF EMAtimeframe15mtfShort
Short EMA Periodint50maShort_length
Long MTF EMAtimeframe60mtfLong
Long EMA Periodint50maLong_length
Fast MACD Lengthint12fast_length
Slow MACD Lengthint26slow_length
MACD SourcesourceclosemacdSrc
MACD Signal Smoothingint9signal_length
MACD Simple MA (Oscillator)boolfalsesma_source
MACD Simple MA (Signal Line)boolfalsesma_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

948 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 10 days ago · GPL-3.0 GitHub repository of Adaptive RSI Moving Average
218 stars

TradersPost Trading Checklist is an overlay indicator that evaluates a suite of popular technical conditions for long and short trade setups. It checks session-anchored VWAP, fast and slow EMA alignments and crosses, 200 SMA, RSI overbought or oversold conditi

Indicator v6 Screeners & dashboardsTrend
Updated 3 months ago · None stated GitHub repository of TradersPost Trading Checklist

Daily Trend Filter

by agutinbaigo28
147 stars

Calculates market regime conditions by combining RSI, MACD, and ADX/DMI indicators. It evaluates whether the market is in a strong bullish, strong bearish, neutral, or non-trending regime. The script plots a colored line overlaying the close price to reflect t

Indicator v5 TrendOscillators & momentum
Updated 6 weeks ago · MIT GitHub repository of Daily Trend Filter

AAPP

by capissimo
114 stars

This indicator plots an adaptive moving average or scaled RSI divergence line directly on the price chart. It calculates dual RSI series to measure momentum divergence, scales the values to the chart's price range, and constructs an adaptive average filtered b

Indicator v4 Moving averagesTrend
Updated 1 year ago · None stated GitHub repository of AAPP

Signal Strategy 15m

by tripolskypetr
72 stars

This indicator generates trend-following trade signals on the price chart by combining moving average crossovers with multi-factor filters. It triggers entry alerts when fast and slow EMAs cross in the direction of a baseline trend EMA, confirmed by non-extrem

Indicator v5 TrendMoving averages
Updated 3 days ago · MIT GitHub repository of Signal Strategy 15m
56 stars

Double Screened Decision is a multi-timeframe trend and momentum indicator overlaid directly on the price chart. It dynamically selects a higher timeframe based on the active chart resolution to evaluate higher-timeframe MACD, Bollinger Bands, RSI, and Stochas

Indicator v5 TrendMoving averages Repaints
Updated 4 months ago · None stated GitHub repository of Double Screened Decision

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