Parabolic SAR MARSI, Adaptive MACD [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 MARSI, Adaptive MACD [Loxx]", shorttitle ="PSAR_MARSI_A_MACD [Loxx]", overlay = false, timeframe="", timeframe_gaps=true)

_maRSI(src, price, rsiperiod, speed) =>
    tprice = ta.sma(src, 1)
    workMaRsi = 0.0
    rsi = ta.rsi(src, rsiperiod)
    if bar_index < rsiperiod
        workMaRsi := tprice
    else
        workMaRsi := workMaRsi[1] + (speed * math.abs(rsi/100.0-0.5)) * (tprice-workMaRsi[1])
    workMaRsi

variant(type, src, len) =>
    sig = 0.0
    if type == "SMA"
        sig := ta.sma(src, len) 
    else if type == "EMA"
        sig := ta.ema(src, len) 
    else if type == "DEMA"
        sig := 2 * ta.ema(src, len) - ta.ema(ta.ema(src, len), len) 
    else if type == "TEMA"
        sig := 3 * (ta.ema(src, len) - ta.ema(ta.ema(src, len), len)) + ta.ema(ta.ema(ta.ema(src, len), len), len)  
    else if type == "WMA"
        sig := ta.wma(src, len)   
    else if type == "TRIMA"
        sig := ta.sma(ta.sma(src, math.ceil(len / 2)), math.floor(len / 2) + 1) 
    else if type == "RMA"
        sig := ta.rma(src, len)  
    else if type == "VWMA"
        sig := ta.vwma(src, len)  
    sig
    
rsiSource = input.source(close, "RSI Source", group = "MARSI MACD")
rsiPeriod1   = input.int(14, "Fast RSI Period", minval = 0, group = "MARSI MACD")
speed1       = input.float(1.2, "Fast MARSI Speed", minval = 0.0, group = "MARSI MACD")
rsiPeriod2   = input.int(34, "Slow RSI Period",  minval = 0, group = "MARSI MACD")
speed2       = input.float(0.8, "Slow MARSI Speed", minval = 0.0, group = "MARSI MACD")

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

signal_ma = input.string("EMA", title='Signal MA Type', options=["DEMA", "EMA", "RMA", "SMA", "TEMA", "TRIMA", "VWMA", "WMA"], group='MACD')
signalPer = input.int(9,"Signal Period", minval = 0, group = "MACD")

greencolor =   #2DD204  
redcolor = #D2042D 

lightgreencolor = #96E881 
lightredcolor =  #DF4F6C  
darkGreenColor = #1B7E02  
darkRedColor =   #93021F  

psarUp = input(greencolor, "PSAR Uptrend  ", group="Color Settings", inline="MACD")
psarDown = input(redcolor, "PSAR Downtrend  ", group="Color Settings", inline="MACD")

col_signal = input(color.white, "Signal Line  ", group="Color Settings", inline="Signal")

col_grow_above = input(color.new(lightgreencolor, 60), "Above   Grow", group="Histogram of MACD", inline="Above")
col_fall_above = input(color.new(darkGreenColor, 60), "Fall", group="Histogram of MACD", inline="Above")
col_grow_below = input(color.new(darkRedColor, 60), "Below Grow", group="Histogram of MACD", inline="Below")
col_fall_below = input(color.new(lightredcolor, 60), "Fall", group="Histogram of MACD", inline="Below")

macdHi = _maRSI(rsiSource, high, rsiPeriod1, speed1) - _maRSI(rsiSource, high, rsiPeriod2, speed2)
macdLo  = _maRSI(rsiSource, low,  rsiPeriod1, speed1) - _maRSI(rsiSource, low,  rsiPeriod2, speed2)
macdMid = (macdHi + macdLo) * 0.5

macd = _maRSI(rsiSource, macdMid, rsiPeriod1, speed1) - _maRSI(rsiSource, macdMid, rsiPeriod2, speed2)
signal= variant(signal_ma, macd, signalPer)

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

sar = pine_sar(start, accel, finish, math.max(rsiPeriod2, rsiPeriod1), macdHi, macdLo, macd)

plot(0, "Zero line", style = plot.style_circles, color = color.gray)
plot(macd, "Historgram of MACD", style=plot.style_columns, color=(macd>=0 ? (macd[1] < macd ? col_grow_above : col_fall_above) : (macd[1] < macd ? col_grow_below : col_fall_below)))
plot(sar, "PSAR", style=plot.style_cross, linewidth=1, color = sar > signal ? psarDown : psarUp)
plot(signal,"Signal", linewidth=1, color = col_signal)

plotshape(sar < signal and sar[1] > signal ? sar : na, title='PSAR Long Start', location=location.absolute, style=shape.circle, size=size.tiny, color=greencolor)
plotshape(sar > signal and sar[1] < signal ? sar : na, title='PSAR Long Start', location=location.absolute, style=shape.circle, size=size.tiny, color=redcolor)


alertcondition(sar < signal and sar[1] > signal, title="PSAR Uptrend", message="Parabolic SAR MARSI, Adaptive MACD: PSAR Uptrend\nSymbol: {{ticker}}\nPrice: {{close}}")
alertcondition(sar > signal and sar[1] < signal, title="PSAR Downtrend", message="Parabolic SAR MARSI, Adaptive MACD: PSAR Downtrend\nSymbol: {{ticker}}\nPrice: {{close}}")

alertcondition(ta.crossover(macd, 0), title="MACD cross over zero line", message="Parabolic SAR MARSI, Adaptive MACD: MACD cross over zero line\nSymbol: {{ticker}}\nPrice: {{close}}")
alertcondition(ta.crossunder(macd, 0), title="MACD cross under zero line", message="Parabolic SAR MARSI, Adaptive MACD: MACD cross under zero line\nSymbol: {{ticker}}\nPrice: {{close}}")

alertcondition(ta.crossover(macd, signal), title="MACD cross over signal line", message="Parabolic SAR MARSI, Adaptive MACD: MACD cross over signal line\nSymbol: {{ticker}}\nPrice: {{close}}")
alertcondition(ta.crossunder(macd, signal), title="MACD cross under signal line", message="Parabolic SAR MARSI, Adaptive MACD: MACD cross under signal line\nSymbol: {{ticker}}\nPrice: {{close}}")























