SBS Algo
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © hiimannshu


//@version=5

indicator(title="SBS Algo", shorttitle="SBS Algo",overlay=true)



//------------ADX---------//

adxfilter = input.bool(true,"ADX filter",group = "Filter Signals")
lvl = input.int(15,"ADX level",group = "Inputs")

sma_source = input.string(title="Oscillator MA Type",  defval="EMA", options=["SMA", "EMA"],group = "Inputs")
sma_signal = input.string(title="Signal Line MA Type", defval="EMA", options=["SMA", "EMA"],group = "Inputs")


TrueRange = math.max(math.max(high - low, math.abs(high - nz(close[1]))), math.abs(low - nz(close[1])))
DirectionalMovementPlus = high - nz(high[1]) > nz(low[1]) - low ? math.max(high - nz(high[1]), 0) : 0
DirectionalMovementMinus = nz(low[1]) - low > high - nz(high[1]) ? math.max(nz(low[1]) - low, 0) : 0

SmoothedTrueRange = 0.0
SmoothedTrueRange := nz(SmoothedTrueRange[1]) - nz(SmoothedTrueRange[1]) / 14 + TrueRange

SmoothedDirectionalMovementPlus = 0.0
SmoothedDirectionalMovementPlus := nz(SmoothedDirectionalMovementPlus[1]) - nz(SmoothedDirectionalMovementPlus[1]) / 14 + DirectionalMovementPlus

SmoothedDirectionalMovementMinus = 0.0
SmoothedDirectionalMovementMinus := nz(SmoothedDirectionalMovementMinus[1]) - nz(SmoothedDirectionalMovementMinus[1]) / 14 + DirectionalMovementMinus

DIPlus = SmoothedDirectionalMovementPlus / SmoothedTrueRange * 100
DIMinus = SmoothedDirectionalMovementMinus / SmoothedTrueRange * 100
DX = math.abs(DIPlus - DIMinus) / (DIPlus + DIMinus) * 100
ADX = ta.sma(DX, 14)


adxabove_Lvl = ADX>lvl

adx_buy = DIMinus<DIPlus and adxabove_Lvl
adx_sell= DIMinus>DIPlus and adxabove_Lvl



//----

length = input.int(defval =  20,title = "Pivot Length",group = "Inputs")
tl_Slope      = input.float(defval =  1.9,title = 'Degree of Slope',minval=0,step=0.1,group = "Inputs")
//---------EMA----------//

emafilter = input.bool(true,"EMA filter",group = "Filter Signals")

showEma = input.bool(false,"",inline="ema")
emalength = input.int(title="EMA",  minval = 1, maxval = 500, defval = 200,inline="ema")
emacol = input.color(color.black,"",inline="ema")

ema =  ta.ema(close,emalength)

above_ema = ema<close
below_ema = ema>close

plot(showEma?ema:na,color=emacol)

showLtSig = input(defval =  false,title = 'Show Latest Signals only')
showWS = input.bool(false,"Show Weak confluence")
showSS = input.bool(true,"Show Strong Confluence")

//-------

color buyCol = input.color(defval = color.green,title = "Buy Lable",group = "Lable Colors")
color sellCol = input.color(defval = color.red,title = "Sell Lable",group = "Lable Colors")
color txtCol = input.color(defval = color.white,title = "Text",group = "Lable Colors")
color trans = color.new(color.white,100)


showLines = input(defval =  false,title = 'Show Trend Lines')
showCrLines = input(defval =  false,title = 'Show Latest trend Lines')

highVal = 0.0
lowVal = 0.0

phSlope = 0.0
plSlope = 0.0

src = close
barIndex = bar_index

ph = ta.pivothigh(length,length)
pl = ta.pivotlow(length,length)

slope = math.abs(ta.sma(src*barIndex,length)-ta.sma(src,length)*ta.sma(barIndex,length))/ta.variance(barIndex,length)/2*tl_Slope

phSlope := ph ? slope : phSlope[1]
plSlope := pl ? slope : plSlope[1]

highVal := ph ? ph : highVal[1] - phSlope
lowVal := pl ? pl : lowVal[1] + plSlope

up_sig = 0
dn_sig = 0

up_sig := src[length] > highVal ? 0 : ph ? 1 : up_sig[1]
dn_sig := src[length] < lowVal ? 0 : pl ? 1 : dn_sig[1]

upper_breakout = up_sig[1] and src[length] > highVal and ( src > src[length] )
lower_breakout = dn_sig[1] and src[length] < lowVal and ( src < src[length] )

//---------MACD---------//




fast_ma = sma_source == "SMA" ? ta.sma(close, 12) : ta.ema(close, 12)
slow_ma = sma_source == "SMA" ? ta.sma(close, 26) : ta.ema(close, 26)

macd = fast_ma - slow_ma
signal = sma_signal == "SMA" ? ta.sma(macd, 9) : ta.ema(macd, 9)

below_buy = macd<0 and signal<0 and ta.crossover(macd,signal) and (emafilter?above_ema:true)
below_sell = macd<0 and signal<0 and ta.crossunder(macd,signal) and (emafilter?below_ema:true)
 
above_sell = macd>0 and signal>0 and ta.crossunder(macd,signal) and (emafilter?below_ema:true)
above_buy = macd>0 and signal>0 and ta.crossover(macd,signal) and (emafilter?above_ema:true)



arrow_buy = showSS and below_buy and adx_buy
arrow_sell = showSS and above_sell and adx_sell


plotshape(showWS and above_buy and adx_buy,"Above Buy",shape.circle,location.belowbar,buyCol,offset=1,size=size.tiny)
plotshape(showWS and below_sell and adx_sell,"Below Sell",shape.circle,location.abovebar,sellCol,offset=1,size=size.tiny)

plotshape(arrow_buy,"Below Buy",shape.triangleup,location.belowbar,buyCol,size=size.small)
plotshape(arrow_sell,"Above Sell",shape.triangledown,location.abovebar,sellCol,size=size.small)



//----

var line up_l = na
var line dn_l = na

var label rub = na
var label rdb = na

bool phtl_break = ta.crossover(src,highVal-phSlope*length)
bool pltl_break = ta.crossunder(src,lowVal+plSlope*length)

if ph[1]
    line.delete(up_l[1])
    label.delete(rub[1])
    
    up_l := line.new(barIndex-length-1,ph[1],barIndex-length,highVal,color=showCrLines?buyCol:trans, extend=extend.right,style=line.style_dotted)

if pl[1]
    line.delete(dn_l[1])
    label.delete(rdb[1])
    
    dn_l := line.new(barIndex-length-1,pl[1],barIndex-length,lowVal,color=showCrLines?sellCol:trans,extend=extend.right,style=line.style_dotted)

if phtl_break
    label.delete(rub[1])
    rub := label.new(barIndex,low,"𝐁𝐔𝐘",color=showLtSig? buyCol:trans,textcolor=showLtSig? txtCol: trans,style=label.style_label_up,size=size.small)

if pltl_break
    label.delete(rdb[1])
    rdb := label.new(barIndex,high,"𝐒𝐄𝐋𝐋",color=showLtSig ? sellCol: trans,textcolor= showLtSig? txtCol: trans,style=label.style_label_down,size=size.small)
    

//----

plotshape(upper_breakout and (emafilter?above_ema:true) and (adxfilter?adx_buy:true) and not(showLtSig)? low[length] : na,"Buy",shape.labelup,location.absolute,buyCol,-length,text="𝐁𝐔𝐘",textcolor=txtCol,size=size.tiny)
plotshape(lower_breakout and (emafilter?below_ema:true) and (adxfilter?adx_sell:true) and not(showLtSig)? high[length] : na,"Sell",shape.labeldown,location.absolute,sellCol,-length,text="𝐒𝐄𝐋𝐋",textcolor=txtCol,size=size.tiny)

plot(showLines ?highVal:na,'Upper Trend Line',color = ph ? na : buyCol,offset=-length)
plot(showLines ?lowVal:na,'Lower Trend Line',color = pl ? na : sellCol,offset=-length)



buy_cond = phtl_break and (emafilter?above_ema:true) and (adxfilter?adx_buy:true)
sell_cond = pltl_break and (emafilter?below_ema:true) and (adxfilter?adx_sell:true)

if buy_cond
    alert("Buy " + str.tostring(syminfo.ticker)+" @ "+ str.tostring(close),alert.freq_once_per_bar_close)

if sell_cond
    alert("Sell " + str.tostring(syminfo.ticker)+" @ "+ str.tostring(close),alert.freq_once_per_bar_close)

   
if arrow_buy
    alert("Bullish arrow on " + str.tostring(syminfo.ticker),alert.freq_once_per_bar_close)

if arrow_sell
    alert("Bearish arrow on  " + str.tostring(syminfo.ticker),alert.freq_once_per_bar_close)







