MACD With Crossings and Above Below Zero
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Kgroomes

//@version=4
study(title="MACD With Crossings and Above Below Zero", shorttitle="MACD Xs & 0", resolution="")
// Getting inputs
fast_length = input(title="Fast Length", type=input.integer, defval=12)
slow_length = input(title="Slow Length", type=input.integer, defval=26)
src = input(title="Source", type=input.source, defval=close)
signal_length = input(title="Signal Smoothing", type=input.integer, minval = 1, maxval = 50, defval = 9)
sma_source = input(title="Simple MA(Oscillator)", type=input.bool, defval=false)
sma_signal = input(title="Simple MA(Signal Line)", type=input.bool, defval=false)
// Plot colors
col_grow_above = #26A69A
col_grow_below = #FFCDD2
col_fall_above = #B2DFDB
col_fall_below = #EF5350
col_macd = #0094ff
col_signal = #ff6a00
// 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

//ADX orginal
adxlen = input(14, title="ADX Smoothing")
dilen = input(14, title="DI Length")
dirmov(len) =>
	ADXup = change(high)
	ADXdown = -change(low)
	plusDM = na(ADXup) ? na : (ADXup > ADXdown and ADXup > 0 ? ADXup : 0)
	minusDM = na(ADXdown) ? na : (ADXdown > ADXup and ADXdown > 0 ? ADXdown : 0)
	truerange = rma(tr, adxlen)
	plus = fixnan(100 * rma(plusDM, adxlen) / truerange)
	minus = fixnan(100 * rma(minusDM, adxlen) / truerange)
	[plus, minus]
adx(dilen, adxlen) =>
	[plus, minus] = dirmov(dilen)
	sum = plus + minus
	adx = 100 * rma(abs(plus - minus) / (sum == 0 ? 1 : sum), adxlen)
sig = adx(dilen, adxlen)

plot(sig, title="ADX", display=display.none)
plot(hist, title="Histogram", style=plot.style_columns, color=(hist>=0 ? (hist[1] < hist ? col_grow_above : col_fall_above) : (hist[1] < hist ? col_grow_below : col_fall_below) ), transp=50 )

//background color based on ADX
bcol = sig>= 25 ? color.red : sig <= 25 ? color.blue : na
 
bgcolor(bcol, transp=85)

//Re-Coloring the MACD When Greater than Zero
plot(macd, title="MACD", color = macd <= 0 ? #00477d : #00f0ff, transp=0)
plot(signal, title="Signal", color = signal <= 0 ? #ff2600 : #ff6a00, transp=0)

//Adding Change in MACD to Show Turning Points
plot(change(macd), title="MACD Derivative", color = #ffff00, transp=50)

//Adding MACD Crossings Up and Down with Plotted Triangles...Bigger Double Triangles for Crossing Above/Below Zero
xUp = iff (macd > 0,crossover(signal, macd),na)
xDn = iff (macd < 0,crossunder(signal, macd),na)
xDnHigh = iff (macd >= 0,crossunder(signal, macd),na)
xUpLow = iff (macd <= 0,crossover(signal, macd),na)

plotshape(xUp, title="MACD X Up", style=shape.triangledown, size=size.tiny,
                 location=location.top, color=color.red)

plotshape(xDn, title="MACD X Dn", style=shape.triangleup, size=size.tiny,
                 location=location.bottom, color=color.green)

plotshape(xDnHigh, title="Big MACD X Up", style=shape.triangleup, size=size.small,
                 location=location.bottom, color=color.green)
                 
plotshape(xUpLow, title="Big MACD X Dn", style=shape.triangledown, size=size.small,
                 location=location.top, color=color.red)
                 
//RSI 
rsi_len = input(14, minval=1, title="Length")
rsi_src = input(close, "Source", type = input.source)
rsi_up = rma(max(change(rsi_src), 0), rsi_len)
rsi_down = rma(-min(change(rsi_src), 0), rsi_len)
rsi = (rsi_down == 0 ? 100 : rsi_up == 0 ? 0 : 100 - (100 / (1 + rsi_up / rsi_down)))/100
plot(rsi, "RSI", color=color.yellow)
rsi_band1 = hline(.70, "Upper Band", color=#C0C0C0)
rsi_band0 = hline(.30, "Lower Band", color=#C0C0C0)
fill(rsi_band1, rsi_band0, color=#9915FF, transp=90, title="Background")