PSAR-Support Resistance
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © traderharikrishna

//@version=5
indicator("PSAR-Support Resistance",overlay = true)
mt=input.timeframe('',title='S/R timeframe')
sh=input.int(10,title='Shift S/R Forward/Backward')
start = input.float(title="Start", step=0.01, defval=0.02)
increment = input.float(title="Increment",  step=0.01, defval=0.02)
maximum = input.float(title="Maximum",  step=0.01, defval=0.2)

sar=request.security(syminfo.tickerid,mt,ta.sar(start,increment,maximum))
plot(sar,style=plot.style_circles,linewidth=1,color=color.yellow)
dir = sar < close ? 1 : -1

buy= dir == 1 and dir[1] == -1
sell=dir == -1 and dir[1] == 1

alertcondition(buy,title='SAR BUY',message='PSAR/Buy Signal')
alertcondition(sell,title='SAR SELL',message='PSAR/Sell Signal')
alertcondition(buy or sell,title='SAR BUY/SELL',message='PSAR/Buy/Sell Signal')


h=ta.valuewhen(buy,close,0)
l=ta.valuewhen(sell,close,0)
buyalert=open>h and close>h and open[1]<h
sellalert=open<l and close<l and open[1]>h


//co=open>h and close>h?color.rgb(6, 217, 240):open<l and close<l?color.rgb(237, 20, 143):color.rgb(242, 231, 8)
//barcolor(color=co)


y=ta.valuewhen(buy,close[0],0)
u=plot(y,color=color.rgb(10, 187, 196),linewidth=1,title='SAR High',offset=sh)

z=ta.valuewhen(sell,close[0],0)
d=plot(z,color=color.rgb(240, 6, 127),linewidth=1,title='SAR Low',offset=sh)

fill(u,d,color=y>z?color.rgb(76, 175, 79, 78):color.rgb(255, 82, 82, 77))

plotshape(buy? sar : na, title="Buy", text="Buy", location=location.absolute, style=shape.labelup, size=size.tiny, color=#06b5e5, textcolor=color.white)
plotshape(sell? sar : na, title="Sell", text="Sell", location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.rgb(249, 11, 106), textcolor=color.white)

malen=input.int(5,title='Moving Average Length')
matype=input.string('ema',title='Moving Average',options=['hma','ema','wma','sma'])
ma1=matype=='wma'?ta.wma(close,malen):matype=='ema'?ta.ema(close,malen):matype=='hma'?ta.hma(close,malen):matype=='sma'?ta.sma(close,malen):ta.ema(close,malen)
plot(ma1,linewidth=2,color=#06ebf3e3,title='Moving Average')

malen2=input.int(22,title='Moving Average Length')
matype2=input.string('ema',title='Moving Average',options=['hma','ema','wma','sma'])
ma2=matype2=='wma'?ta.wma(close,malen2):matype2=='ema'?ta.ema(close,malen2):matype2=='hma'?ta.hma(close,malen2):matype2=='sma'?ta.sma(close,malen2):ta.ema(close,malen2)
plot(ma2,linewidth=2,color=#f10b3df1,title='Moving Average2')

[macd,signal,hist]=ta.macd (close,12,26,9)

buycx=hist>0 and ta.crossover (ma1,ma2)
sellcx=hist <0 and ta.crossunder(ma1,ma2)

plotshape (buycx,title='MA-Buy',location=location.belowbar,style=shape.triangleup, color=#03800acc,size=size.tiny)
plotshape  (sellcx,title='MA-Sell', location = location.abovebar,  style =shape.triangledown, color= #ff0404,size=size.tiny)
