Parabolic SAR of KAMA [Loxx]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © loxx

//@version=5
indicator("Parabolic SAR of KAMA [Loxx]", shorttitle ="PSARKAMA [Loxx]", overlay = true, timeframe="", timeframe_gaps=true)

greencolor =   #2DD204  
redcolor = #D2042D 

_jfract(count)=> 
    window = math.ceil(count/2)
    _hl1  = (ta.highest(high[window], window) - ta.lowest(low[window], window)) / window
    _hl2  = (ta.highest(high, window) - ta.lowest(low, window)) / window
    _hl   = (ta.highest(high, count) - ta.lowest(low, count)) / count
    _d    = (math.log(_hl1 + _hl2) - math.log(_hl)) / math.log(2)
    dim  = _d < 1 ? 1 : _d > 2 ? 2 : _d

_kama(src, len, fast, slow, jcount, efratiocalc) =>
    fastend = (2.0 /(fast + 1))
    slowend = (2.0 /(slow + 1))
    mom = math.abs(ta.change(src, len))
    vola = math.sum(math.abs(ta.change(src)), len)
    efratio = efratiocalc == "Regular" ? (vola != 0 ? mom / vola : 0) : math.min(2.0-_jfract(jcount), 1.0) 
    alpha = math.pow(efratio * (fastend - slowend) + slowend, 2)
    kama = 0.0
    kama := alpha * src + (1 - alpha) * nz(kama[1], src)
    kama

blsrc = input.source(hl2, "Source", group = "Kaufman AMA Settings")
period = input.int(10, "Period", minval = 0, group = "Kaufman AMA Settings")
kama_fastend = input.float(2, "Kaufman AMA Fast-end Period", minval = 0.0, group = "Kaufman AMA Settings")
kama_slowend = input.float(30, "Kaufman AMA Slow-end Period",  minval = 0.0, group = "Kaufman AMA Settings")
efratiocalc = input.string("Regular", "Efficiency Ratio Type", options = ["Regular", "Jurik Fractal Dimension Adaptive"], group = "Kaufman AMA Settings")
jcount = input.int(defval=2, title="Jurik Fractal Dimension Count ", group = "Kaufman AMA Settings")

start = input.float(0.02, "Start", minval = 0.0, step = 0.01, group = "Parabolic SAR")
accel = input.float(0.02, "Acceleration", minval = 0.0, step = 0.01, group = "Parabolic SAR")
finish = input.float(0.2, "Maximum", minval = 0.0, step = 0.01, group = "Parabolic SAR")

showSar = input.bool(true, "Show PSAR of Kaufman AMA?", group = "UI Options")
showBl = input.bool(true, "Show Kaufman AMA Baseline?", group = "UI Options")
trendmatch = input.bool(true, "Activate Trend Confluence?", group = "UI Options")
colorbars = input.bool(true, "Color bars?", group = "UI Options")

kamaHi = _kama(high, period, kama_fastend, kama_slowend, jcount, efratiocalc) 
kamaLo  = _kama(low, period, kama_fastend, kama_slowend, jcount, efratiocalc) 
kamaMid = (kamaHi + kamaLo) * 0.5


pine_sar(start, inc, max, cutoff, _high, _low, _close) =>
	var float result = na
	var float maxMin = na
	var float acceleration = na
	var bool isBelow = na
	bool isFirstTrendBar = false
	if bar_index < cutoff + cutoff * 0.2
		if _close > _close[1]
			isBelow := true
			maxMin := _high
			result := _low[1]
		else
			isBelow := false
			maxMin := _low
			result := 0
		isFirstTrendBar := true
		acceleration := start
	result := result + acceleration * (maxMin - result)
	if isBelow
		if result > _low
			isFirstTrendBar := true
			isBelow := false
			result := math.max(_high, maxMin)
			maxMin := _low
			acceleration := start
	else
		if result < _high
			isFirstTrendBar := true
			isBelow := true
			result := math.min(_low, maxMin)
			maxMin := _high
			acceleration := start
	if not isFirstTrendBar
		if isBelow
			if _high > maxMin
				maxMin := _high
				acceleration := math.min(acceleration + inc, max)
		else
			if _low < maxMin
				maxMin := _low
				acceleration := math.min(acceleration + inc, max)
	
	if isBelow
		result := math.min(result, _low[1])
		if bar_index > 1
			result := math.min(result, _low[2])
		
	else
		result := math.max(result, _high[1])
		if bar_index > 1
			result := math.max(result, _high[2])
	[result, isBelow]

[sar, isbelow] = pine_sar(start, accel, finish, math.max(period, kama_fastend, kama_slowend), kamaHi, kamaLo, kamaMid)

kamaClose = _kama(blsrc, period, kama_fastend, kama_slowend, jcount, efratiocalc) 

trendMatchColor = blsrc > kamaClose and isbelow ? greencolor  : blsrc < kamaClose and not isbelow ? redcolor : color.white
regSarColor = isbelow ? greencolor : redcolor
regBlColor = blsrc > kamaClose ? greencolor : redcolor

plot(showSar ? sar : na, "PSAR of Kaufman AMA", style = plot.style_circles, linewidth = 2, color = trendmatch ? trendMatchColor : regSarColor)
plot(showBl ? kamaClose : na, "Kaufman AMA Baseline", color = trendmatch ? trendMatchColor : regBlColor, linewidth = 2)
barcolor(colorbars ? trendMatchColor : na)



















