Parabolic sar with breaks
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © carlpwilliams2

//@version=5
indicator("Sar with breaks", overlay=true)
import carlpwilliams2/CarlLib/7 as carlLib

showBreakLabels = input.bool(true,title="Show breakouts", group="Breakout Labels")
showVolumeBreakLabels = input.bool(true,title="Show Breakouts with high volume", group="Breakout Labels")

timeframe = input.timeframe("",title="Timeframe")
[Open, High, Low, Close, Volume] = request.security(syminfo.tickerid,timeframe,[open,high,low,close,volume])

[bodySize, wickSize, upperShadow, lowerShadow, lowerShadowPercentage,upperShadowPercentage,shadowRatio,wickPercentage] = carlLib.candleDetails(Open,High,Low,Close)


ma50 = ta.ema(Close, 50)

volSpikeThresh = input.float(30,title="Spike Threshold %", group="Volume Spike")
volAverage = ta.sma(Volume,10)
volumeDifference = ((Volume-Volume[1])/Volume)*100
volumeAverageDifference = ((Volume-volAverage)/Volume)*100
volumeSpike = volumeDifference>volSpikeThresh and volumeAverageDifference>60

start = input(0.02, title="start", group="Parabolic SAR")
increment = input(0.02, title="Increment", group="Parabolic SAR")
maximum = input(0.2, "Max Value", group="Parabolic SAR")

out = request.security(syminfo.tickerid,timeframe, ta.sar(start, increment, maximum), gaps=barmerge.gaps_off)

sarChange = ta.change(out<High)

var prevSar = out

if(sarChange)
    prevSar := out[1] + ((out[1]/100)*0.02)

crossOverLong =  out< Close  and Close> Open and (ta.crossover(Close,prevSar) or ta.crossover(Close[1],prevSar))
crossOverShort = out > Close and Close < Open and ta.crossunder(Close,prevSar)

plotshape(showBreakLabels and crossOverLong ? out :na, text="Weak\nBreakout", location = location.absolute, style=shape.labelup, textcolor=color.white, color=color.green, title = "Long Signals" )
plotshape(showBreakLabels and crossOverShort ? out:na, text="Weak\nBreakout", location = location.absolute, style=shape.labeldown, textcolor=color.white, color=color.maroon, title="Short Signals" )

plotshape(showVolumeBreakLabels and crossOverLong and volumeSpike ? out :na, text="Strong\nBreakout", location = location.absolute, style=shape.labelup, textcolor=color.white, color=color.blue, title = "Long Signals" )
plotshape(showVolumeBreakLabels and crossOverShort  and volumeSpike ? out:na, text="Strong\nBreakout", location = location.absolute, style=shape.labeldown, textcolor=color.white, color=color.orange, title="Short Signals" )

plot(sarChange?na:prevSar, style=plot.style_linebr, offset=-1, color=out<high?color.new(color.lime,50):color.new(color.red,50), title="Break Lines", linewidth=2)
plot(out, "ParabolicSAR", style=plot.style_cross, color=#2962FF)

alertcondition(crossOverLong, title = "Bullish Breakout", message="Sar with breaks - bullish Breakout on {{ticker}}({{interval}}m)")
alertcondition(crossOverShort, title = "Beaish Breakout", message="Sar with breaks - bearish Breakout on {{ticker}}({{interval}}m)")

alertcondition(crossOverLong and volumeSpike, title = "Strong Bullish Breakout", message="Sar with breaks - strong bullish Breakout on {{ticker}}({{interval}}m)")
alertcondition(crossOverShort and volumeSpike, title = "Strong Bearish Breakout ", message="Sar with breaks - strong bearish Breakout on {{ticker}}({{interval}}m)")