Indicator · Pine Script v4 · On the chart

MACD + Support / Resistance

by krosenfeld7

This indicator combines pivot-based support and resistance levels with multi-timeframe MACD crossovers and a higher-timeframe exponential moving average trend filter.

View on GitHub

Repaints

GitHub stars5on the repository
Pine versionv4older, still runs
Lines of code91
Settings13inputs
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

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

MACD+Support_Resistance.txt
//@version=4
// EMA Section
study(title="MACD + Support / Resistance", shorttitle="MACD + Support & Resist", overlay=true, max_labels_count = 500)

lookback = input(title = "Pivot Lookback", type = input.integer, defval = 10, minval = 1)
epsilon  = input(title = "Price Epsilon (%)", type = input.float, defval = 0.5, minval = 0.1)
emaRes = input(title = "EMA Resolution", type = input.resolution, defval = "D")
emaSrc = input(title = "EMA Source", type = input.source, defval = close)
emaLength = input(title = "EMA Length", type = input.integer, defval = 100, minval = 1)

ema100 = ema(emaSrc, emaLength)

ema100Daily = security(syminfo.tickerid, emaRes, ema100[barstate.isrealtime ? 1 : 0], barmerge.gaps_off, barmerge.lookahead_off)

emaLong = close > ema100Daily
emaShort = close < ema100Daily

plot(ema100Daily, "EMA", color = color.white, linewidth = 2)

epsilon /= 100

// 100 EMA, MACD, previous high or low

prevHigh = pivothigh(close, lookback, lookback)
prevLow  = pivotlow(close,  lookback, lookback)

// MACD
// Getting inputs
macdRes = input(title = "MACD Resolution", type = input.resolution, defval = "30")
fast_length = input(title="MACD Fast Length", type=input.integer, defval=12)
slow_length = input(title="MACD Slow Length", type=input.integer, defval=26)
src = 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)
displayPivots = input(title = "Display Pivot Labels", type = input.bool, defval = false)

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

macdSec   = security(syminfo.tickerid, macdRes, macd[barstate.isrealtime ? 1 : 0],   barmerge.gaps_off, barmerge.lookahead_off)
signalSec = security(syminfo.tickerid, macdRes, signal[barstate.isrealtime ? 1 : 0], barmerge.gaps_off, barmerge.lookahead_off)

macdCrossDown = crossunder(macdSec, signalSec)
macdCrossUp   = crossover(macdSec,  signalSec)

var bool highPointReached = na
var bool lowPointReached  = na

if (high + (epsilon * high)) >= prevHigh
    if displayPivots
        label.new(bar_index, y = na, yloc = yloc.abovebar, color = color.green, style = label.style_triangledown)
    highPointReached := true
else
    highPointReached := na
if (low - (epsilon * low)) <= prevLow
    if displayPivots
        label.new(bar_index, y = na, yloc = yloc.belowbar, color = color.red, style = label.style_triangleup)
    lowPointReached := true
else
    lowPointReached := na

macdPriceLong = false
macdPriceShort = false

if macdCrossDown and barssince(not na(highPointReached)) < lookback
    macdPriceShort := true

if macdCrossUp and barssince(not na(lowPointReached)) < lookback
    macdPriceLong := true

if not na(prevHigh) and displayPivots
    label.new(bar_index - lookback, y = na, yloc = yloc.abovebar, style = label.style_triangledown)
if not na(prevLow) and displayPivots
    label.new(bar_index - lookback, y = na, yloc = yloc.belowbar, style = label.style_triangleup)

long  = macdPriceLong and emaLong
short = macdPriceShort and emaShort

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

MACD + Support / Resistance identifies support and resistance touches using pivot highs and lows within a user-defined percentage band. Bullish signals occur when price bounces near support and the MACD crosses up above the higher-timeframe EMA; bearish signals require a resistance test, a MACD cross down, and price below the EMA. All visuals and signals plot directly on the price chart. Historical signals may repaint because higher-timeframe data handles historical and real-time bars differently.

Details

Published by

krosenfeld7 on GitHub

Type

Indicator, Pine Script v4

Draws

On the price chart

Category

Oscillators & momentum, Support & resistance, Moving averages, Trend

Markets

Any symbol

Timeframes

M15-H1

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
Pivot Lookbackint10lookback
Price Epsilon (%)float0.5epsilon
EMA ResolutiontimeframeDemaRes
EMA SourcesourcecloseemaSrc
EMA Lengthint100emaLength
MACD Resolutiontimeframe30macdRes
MACD Fast Lengthint12fast_length
MACD Slow Lengthint26slow_length
MACD Sourcesourceclosesrc
MACD Signal Smoothingint9signal_length
MACD Simple MA (Oscillator)boolfalsesma_source
MACD Simple MA (Signal Line)boolfalsesma_signal
Show all 13 settings
SettingTypeDefaultVariable
Display Pivot LabelsboolfalsedisplayPivots
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

BTE Acc AO SMIO

by f-cksociety
11 stars

This script provides an indicator-based backtesting engine framework that simulates trade entries, exits, and filtering directly on the price chart. It evaluates selected entry rules and multi-layered filters, including moving average trends, volume increases,

Indicator v5 TrendOscillators & momentum
Updated 4 years ago · MPL-2.0 GitHub repository of BTE Acc AO SMIO

This overlay indicator plots Central Pivot Range levels and bands across yearly, quarterly, monthly, weekly, daily, and six-hour periods. Each timeframe can be shown or hidden, and pivot and band colors are set separately. It also marks MACD momentum and EMA t

Indicator v5 Support & resistanceOscillators & momentum
Updated 3 years ago · None stated GitHub repository of Pivot Boss Multi Timeframe CPR with MACD and EMA
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

PivotHighLow

by Alorse
330 stars

This indicator calculates pivot high and pivot low points on the price chart based on configurable left and right bar counts. Confirmed pivot highs and lows are plotted with negative bar offsets directly on the price chart using line and cross styles. Addition

Indicator v4 Support & resistanceOscillators & momentum Repaints
Updated 2 months ago · None stated GitHub repository of PivotHighLow

Ichimoku Cloud

by harryguiacorn
219 stars

This indicator plots Ichimoku Cloud elements on the price chart, including Tenkan-sen, Kijun-sen, Senkou Span A and B lines with optional Chikou Span. It signals trend changes via Tenkan-sen and Kijun-sen crosses and places arrows at Kijun-sen direction shifts

Indicator v5 TrendMoving averages
Updated 1 year ago · None stated GitHub repository of Ichimoku Cloud

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