Skip to content

Instantly share code, notes, and snippets.

@pequet
Created October 22, 2018 18:12
Show Gist options
  • Save pequet/8a749ea837b1ef46528cb53d1cc3125f to your computer and use it in GitHub Desktop.
Save pequet/8a749ea837b1ef46528cb53d1cc3125f to your computer and use it in GitHub Desktop.
// Created by @pequet (https://www.tradingview.com/u/pequet)
// https://github.com/pequet/
// @version=3
//
study(title="MACD", shorttitle="MACD")
// inputs
macdEMA1Opt = input("ema", options=["ema", "sma"], title="EMA/SMA")
macdEMA1Length = input(9, minval=1, title="MA Length")
macdEMA2Opt = input("ema", options=["ema", "sma"], title="EMA/SMA")
macdEMA2Length = input(30, minval=1, title="MA Length")
src = input(close, type=source, title="Source")
macdEMA3Length = input(9, minval=1, title="EMA")
macdEMA1Input = macdEMA1Opt=="ema"
macdEMA2Input = macdEMA2Opt=="ema"
// calculations
macdEMA1 = macdEMA1Input ? ema(src, macdEMA1Length) : sma(src, macdEMA1Length)
macdEMA2 = macdEMA2Input ? ema(src, macdEMA2Length) : sma(src, macdEMA2Length)
macd = macdEMA1 - macdEMA2
macdSignal = ema(macd, macdEMA3Length)
macdHistogram = macd - macdSignal
macdPlotObj = plot(macd, style=line, color=aqua, transp=0, linewidth=2, title="MACD")
macdSignalPlotObj = plot(macdSignal, style=line, color=red, transp=0, linewidth=2, title="Signal")
// crosses
crosses = crossover(macd, macdSignal) and macd <= 0 ? 2 : crossover(macd, macdSignal) and change(macdHistogram) > 0 ? 1 : crossunder(macd, macdSignal) and macd >= 0 ? -2 : crossunder(macd, macdSignal) and change(macdHistogram) < 0 ? 1 : na
strengthSignal = fixnan(crosses)
color = strengthSignal==-2 ? red : strengthSignal==-1 ? orange : strengthSignal==1 ? yellow : strengthSignal==2 ? lime : blue
histo = macd < 0 and change(macdHistogram) > 0 ? 2 : macd > 0 and change(macdHistogram) < 0 ? -2 : 0
color2 = histo == -2 ? red : histo == -1 ? orange : histo == 1 ? yellow : histo == 2 ? lime : gray
macdHistogramPlotObj = plot(macdHistogram, style=columns, color=color2, transp=0, linewidth=2, title="Histogram")
bgcolor(color, transp=65)
// -
@pequet
Copy link
Author

pequet commented Oct 22, 2018

image

@timelyart
Copy link

How do you use it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment