[JL] TEMA Difference - Divergence and Double HH/LL
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © jesselau76

//@version=5
indicator(title="[JL] TEMA Difference", shorttitle="TEMAD", max_bars_back = 4900)
// Getting inputs
fast_length = input.int(50,title="Fast Length")
slow_length = input.int(150,title="Slow Length")
src = input.source(close,title="Source")
atr_length = input.int(20,title="ATR Length")
useATR = input(true, title='Use ATR to compare difference')
// Plot colors
col_grow_above = #26A69A
col_grow_below = #FFCDD2
col_fall_above = #B2DFDB
col_fall_below = #EF5350


//TEMA function
tema(src,length) =>
    ema1 = ta.ema(src, length)
	ema2 = ta.ema(ema1, length)
	ema3 = ta.ema(ema2, length)
	out = 3 * (ema1 - ema2) + ema3
	out
// Calculating
fast_ma = tema(src, fast_length) 
slow_ma = tema(src, slow_length) 
atr = ta.atr(atr_length)
mad = useATR ? (fast_ma - slow_ma)/atr : fast_ma - slow_ma

plot(mad, title="MAD", style=plot.style_columns, color=(mad>=0 ? (mad[1] < mad ? col_grow_above : col_fall_above) : (mad[1] < mad ? col_grow_below : col_fall_below) ), transp=0 )

turnGreen = mad[1]<0 and mad >0 
turnRed   = mad[1]>0 and mad <0 

// //plotshape(turnGreen, style = shape.triangleup,   location = location.bottom, color = color.green, size=size.small)
// //plotshape(turnRed,   style = shape.triangledown, location = location.top, color = color.red, size=size.small)

// // Alerts
// alertcondition(turnGreen, title= "Long_MAD", message= "{{ticker}} LONG")
// alertcondition(turnRed,   title= "Short_MAD",    message= "{{ticker}} SHORT")

// barsturngreen = ta.barssince(turnGreen)
// barsturngreen2 = ta.barssince(turnGreen[1])
// barsturnred = ta.barssince(turnRed)
// barsturnred2 = ta.barssince(turnRed[1])

// phigh = ta.pivothigh(mad, barsturngreen, 1)
// plow = ta.pivotlow(mad, barsturnred, 1)
// phigh2 = ta.pivothigh(mad, barsturngreen2, 1)
// plow2 = ta.pivotlow(mad, barsturnred2, 1)
// barssincePhigh = ta.barssince(phigh) 
// barssincePlow = ta.barssince(plow) 
// barsturngreen = 1
// phigh = 0.0
// barsturnred = 1
// plow = 0.0
// barssincePhigh = 0
// barssincePlow = 0

barsturngreen = bar_index - ta.valuewhen(turnGreen, bar_index, 0)
barsturngreen2 = bar_index - ta.valuewhen(turnGreen, bar_index, 1)
barsturnred = bar_index - ta.valuewhen(turnRed, bar_index, 0)
barsturnred2 = bar_index - ta.valuewhen(turnRed, bar_index, 1)
barsg = barsturngreen>0 ? barsturngreen : 1
phigh = ta.highest(mad, barsg)
h1 = ta.highest(high,barsg)
barsr = barsturnred>0 ? barsturnred : 1
plow = ta.lowest(mad,barsr)
l1 = ta.lowest(low,barsr)
// plot(phigh,color=color.white)
// plot(h1,color=color.white)
// barssincePhigh = ta.barssince(mad==phigh)
// barssincePlow = ta.barssince(mad==plow)
//plot(barsturngreen2,color=color.white)
// plow = ta.lowest(mad, barsturnred+1)
// barssincePlow = ta.barssince(plow)

if turnRed and h1[1]>h1[barsturnred2+1] and phigh[1]<phigh[barsturnred2+1]	 
	label1 = label.new(bar_index[1], phigh[barsturnred2+1]	, text="Top Divergence", style = label.style_label_down, color = color.red, textcolor=color.white, size=size.large)

if turnGreen and l1[1]<l1[barsturngreen2+1] and plow[1]>plow[barsturngreen2+1]	 
	
    label2 = label.new(bar_index[1], plow[barsturngreen2+1], text="Bottom Divergence", style = label.style_label_up, color = color.green, textcolor=color.white, size=size.large)


if turnRed and h1[1]<h1[barsturnred2+1] and phigh[1]<phigh[barsturnred2+1]	 
	label1 = label.new(bar_index[1], phigh[barsturnred2+1]	, text="Double Lower High", style = label.style_label_down, color = color.red, textcolor=color.white, size=size.large)

if turnGreen and l1[1]>l1[barsturngreen2+1] and plow[1]>plow[barsturngreen2+1]	 
	
    label2 = label.new(bar_index[1], plow[barsturngreen2+1], text="Double Higher Low", style = label.style_label_up, color = color.green, textcolor=color.white, size=size.large)