Impactful pattern and candles pattern Alert
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © erfan_abedi

//@version=5
indicator("Impactful pattern and candles pattern Alert", shorttitle='Impactful pattern n Candles Alert', overlay=true , max_bars_back=5000)
//inputs
iptf = input.timeframe('60', title='IP:', group='timeframe', inline='tm', confirm = true, tooltip='choose timeframe for impactful patterns.')
sbuf = input.float(0.0 , 'stoploss:', group='buffering', inline='buff' , tooltip='amount of percent lower than real entry price or upper than stoploss in short position')
ebuf = input.float(0.0 , 'entry:', group='buffering', inline='buff', tooltip='amount of percent lower than real entry price or upper than stoploss in short position')
ipal = input.bool(true, title='ip alert', group='alertions', inline='al')
cal = input.bool(true, title='candle alert' , group='alertions', inline='al')
ibalt = input.bool(true, title= 'inside and outside bar', group='alertions', inline='al')
short = input.bool(false,title='Short position', confirm=true,  group='timeframe', inline='tm')
txt = input.text_area('', title='type your text for alert')
perbar = input.string(alert.freq_once_per_bar, title= 'Ip alert frequency option: ', options=[alert.freq_once_per_bar ,alert.freq_once_per_bar_close,alert.freq_all], group='advance')
ipoff =input.int(4, title='IP pivot left and rightbars', group='advance', minval=1)
off = input.int(3, title='candle pivot left and rightbars' , group='advance', minval=1)
sym = syminfo.tickerid
//.................................................................................................


//candle
hi = high
lo =low
cl = close
op = open
bar_inde = bar_index

//ip pattern

bar_indexx = request.security(sym, iptf, bar_index) 

//......................................................................................

//rally or drop or base?

hf = if open > close
    (high - open + close - low) / (open - close) * -1
else if close > open
    (high - close + open - low) / (close - open)
h = if hf == na
    2.6
else 
    hf


s = if open > close
    high - open + close - low - (open - close)
else if close > open
    high - close + open - low - (close - open)

rally = h > 0 and h < 1 and s<0
drop = h < 0 and h > -1 and s<0
base= s>0
//......................................................

//legal for candle pattern
rbup = (lo[off]<lo[off+1] and cl[off]>cl[off+1]) and ta.pivotlow(lo,off,off) and short==false
rbdw = (hi[off]>hi[off+1] and cl[off]<cl[off+1]) and ta.pivothigh(hi,off,off) and short
krbup = cl[off]>hi[off+1] and op[off]<lo[off+1] and ta.pivotlow(lo,off,off) and short==false
krbdw = cl[off]<lo[off+1] and op[off]>hi[off+1] and ta.pivothigh(hi,off,off) and short
ebup = hi[off]<lo[off+1] and cl[off]>op[off] and ta.pivotlow(lo,off,off)  and short==false
ebdw = lo[off]>hi[off+1] and cl[off]<op[off] and ta.pivothigh(hi,off,off) and short
pinup = (op[off] > cl[off] ? (cl[off]-lo[off])/(hi[off]-lo[off]) >= 2/3 : (op[off]-lo[off])/(hi[off]-lo[off]) >=2/3) and ta.pivotlow(lo,off,off) and short==false
pindw = (op[off] > cl[off] ? (hi[off]-op[off])/(hi[off]-lo[off]) >= 2/3 : (hi[off]-cl[off])/(hi[off]-lo[off]) >=2/3) and ta.pivothigh(hi,off,off) and short
tbrup = rally[off] and drop[off+1] and ta.pivotlow(lo,off,off) and short==false
tbrdw = drop[off] and rally[off+1] and ta.pivothigh(hi,off,off) and short
cup = cl[off+1]<op[off+1] and lo[off]<lo[off+1] and lo[off-1]>lo[off] and cl[off-1] > hi[off] and ta.pivotlow(lo,off,off) and short==false
cdw = cl[off+1]>op[off+1] and hi[off]>hi[off+1] and hi[off-1]<hi[off] and cl[off-1] < lo[off] and ta.pivothigh(hi,off,off) and short
ib= hi[off-1]<hi[off] and lo[off-1]>lo[off] and (ta.pivotlow(lo,off,off) or ta.pivothigh(hi,off,off) )
ob= hi[off]>hi[off+1] and lo[off]<lo[off+1] and (ta.pivotlow(lo,off,off) or ta.pivothigh(hi,off,off) )
//.............................................................................................................................




//-------------------ip pivots------------------------
pivot(a) =>
    hh = request.security(sym, iptf, ta.valuewhen(ta.pivothigh(high,ipoff,ipoff),high[ipoff], a))
    ll = request.security(sym, iptf, ta.valuewhen(ta.pivotlow(low,ipoff,ipoff),low[ipoff], a))
    barl = request.security(sym, iptf, ta.valuewhen(ta.pivotlow(low,ipoff,ipoff),bar_index-ipoff, a))
    barh= request.security(sym, iptf, ta.valuewhen(ta.pivothigh(high,ipoff,ipoff),bar_index-ipoff, a))
    [hh,ll,barl,barh]
[ph0,pl0,pbl0,pbh0] = pivot(0)
[ph1,pl1,pbl1,pbh1] = pivot(1)
[ph2,pl2,pbl2,pbh2] = pivot(2)
basen = ta.valuewhen(base,bar_index, 0)
basennhi = ta.valuewhen(base,high, 0)
basennlo = ta.valuewhen(base,low, 0)
//--------------------------base finder----------------------------
//vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv


basebuu() =>
    basebu1 = 0
    for i= ipoff to 0 
        if basen[i] <pbh0
            basebu1 := basen[i]
    basebu1


basebdd() =>
    basebd1 = 0
    for i= ipoff to 0 
        if basen[i] <pbl0
            basebd1 := basen[i]
    basebd1

basebu = request.security(sym, iptf, ta.valuewhen(basebuu()>0 , basebuu(), 0) )
basebd = request.security(sym, iptf, ta.valuewhen(basebdd()>0 , basebdd(), 0) )

basehiu =  request.security(sym, iptf, high[bar_index - basebu])
baselou =   request.security(sym, iptf,low[bar_index - basebu])

basehid =  request.security(sym, iptf,high[bar_index - basebd])
baselod =  request.security(sym, iptf, low[bar_index - basebd])

//...........................................................................

//legal for ip
//HnS and qm and we
hnsup = pl2<pl1 and ph0>ph1 and pl0>pl1 and ph1>pl1 and pl0<ph0 and pl0<ph1 and pbl0>pbh0 and pbh0>pbl1 and pbl1>pbh1 and pbl2<pbh1 and short
hnsdw = ph2>ph1 and ph0<ph1 and pl0<pl1 and ph1>pl1 and pl0<ph0 and pl0<ph1 and ph0>pl1 and pbl0<pbh0  and pbl1<pbh1 and pbl0>pbh1 and pbh2<pbl1 and short==false 
qmup = pl2<pl0 and ph0>ph1 and pl0<pl1 and ph1>pl1 and pl0<ph0 and pl0<ph1 and pbl0>pbh0 and pbh0>pbl1 and pbl1>pbh1 and pbl2<pbh1 and short
qmdw = ph2>ph0 and ph0>ph1 and pl0<pl1 and ph1>pl1 and pl0<ph0 and pl0<ph1 and ph0>pl1 and pbl0<pbh0  and pbl1<pbh1 and pbl0>pbh1 and pbh2<pbl1 and short==false 
weup =  pl1 < baselou and basehiu<ph0 and baselou>pl0 and basebu<pbh0 and pbh0<pbl0 and pbl1>pbh1 and basebu>pbl1 and short
wedw =  ph1>basehid and basehid<ph0 and baselod>pl0 and basebd<pbl0 and pbh0>pbl0 and basebd>pbh1 and pbl1<pbh1 and short==false
//.............................................................................................................................................


//lines
if (weup and iptf==timeframe.period) 
    line.new(pbl1, pl1, basebu, basehiu, color=color.red, width=5)
    line.new(basebu, basehiu, basebu, baselou, color=color.red, width=5)
    line.new(basebu, baselou, pbh0, ph0, color=color.red, width=5)
    line.new(pbh0, ph0, pbl0, pl0, color=color.red, style=line.style_solid, width=5)

if (wedw and iptf==timeframe.period) 
    line.new(pbh1, ph1, basebd, baselod, color=color.green, width=5)
    line.new(basebd, baselod, basebd, basehid, color=color.green, width=5)
    line.new(basebd, basehid, pbl0, pl0, color=color.green, width=5)
    line.new(pbl0, pl0, pbh0, ph0, color=color.green, style=line.style_solid, width=5)

if ((qmup or hnsup) and iptf==timeframe.period)
    line.new(pbl2, pl2, pbh1, ph1, color=color.red, width=5)
    line.new(pbh1, ph1, pbl1, pl1, color=color.red, width=5)
    line.new(pbl1, pl1, pbh0, ph0, color=color.red, style=line.style_solid, width=5)
    line.new(pbh0, ph0, pbl0, pl0, color=color.red, style=line.style_solid, width=5)
    
if ((qmdw or hnsdw) and iptf==timeframe.period)
    line.new(pbh2, ph2, pbl1, pl1, color=color.green, width=5)
    line.new(pbl1, pl1, pbh1, ph1, color=color.green, width=5)
    line.new(pbh1, ph1, pbl0, pl0, color=color.green, style=line.style_solid, width=5)
    line.new(pbl0, pl0, pbh0, ph0, color=color.green, style=line.style_solid, width=5)
//............................................................................................................................

//plots
plotshape(rbdw and short ? close-open:na, offset= -1 * off, style=shape.arrowdown, color=color.red, text='Bearish reversal bar', textcolor=color.red, editable = false, display =display.pane)
plotshape(krbdw and short ? close-open:na, offset= -1 * off, style=shape.arrowdown, color=color.red, text='Key reversal bar', textcolor=color.red , editable = false, display =display.pane)
plotshape(ebdw and short ? close-open:na, offset= -1 * off, style=shape.arrowdown, color=color.red, text='Bearish exhaustion bar', textcolor=color.red, editable = false, display =display.pane)
plotshape(pindw and short? close-open:na, offset= -1 * off, style=shape.arrowdown, color=color.red, text='Bearish pin bar', textcolor=color.red, editable = false, display =display.pane)
plotshape(tbrdw and short ? close-open:na, offset= -1 * off, style=shape.arrowdown, color=color.red, text='Bearish two-bar reversal', textcolor=color.red, editable = false, display =display.pane)
plotshape(cdw and short ? close-open:na, offset= -1 * off, style=shape.arrowdown, color=color.red, text='Bearish tree-bar riversal', textcolor=color.red, editable = false, display =display.pane)
plotshape(ib and short ? close-open:na, offset= -1 * off +1, style=shape.arrowdown, color=color.red, text='Inside bar', textcolor=color.red, editable = false, display =display.pane)
plotshape(ob and short ? close-open:na, offset= -1 * off, style=shape.arrowdown, color=color.red, text='Outside bar', textcolor=color.red, editable = false , display =display.pane)

plotshape(rbup and short==false ? close-open:na, offset= -1 * off, style=shape.arrowup, color=color.green, text='Bullish reversal bar', textcolor=color.green,location=location.belowbar, editable = false, display =display.pane)
plotshape(krbup and short==false ? close-open:na, offset= -1 * off, style=shape.arrowup, color=color.green, text='Key reversal bar', textcolor=color.green,location=location.belowbar, editable = false, display =display.pane)
plotshape(ebup and short==false ? close-open:na, offset= -1 * off, style=shape.arrowup, color=color.green, text='Bullish exhaustion bar', textcolor=color.green,location=location.belowbar, editable = false,display =display.pane)
plotshape(pinup and short==false ? close-open:na, offset= -1 * off, style=shape.arrowup, color=color.green, text='Bullish pin bar', textcolor=color.green,location=location.belowbar, editable = false,display =display.pane)
plotshape(tbrup and short==false ? close-open:na, offset= -1 * off, style=shape.arrowup, color=color.green, text='Bullish two-bar reversal', textcolor=color.green,location=location.belowbar, editable = false,display =display.pane)
plotshape(cup and short==false ? close-open:na, offset= -1 * off, style=shape.arrowup, color=color.green, text='Bullish three-bar riversal', textcolor=color.green,location=location.belowbar, editable = false,display =display.pane)
plotshape(ib and short==false ? close-open:na, offset= -1 * off +1, style=shape.arrowup, color=color.green, text='Inside bar', textcolor=color.green,location=location.belowbar, editable = false,display =display.pane)
plotshape(ob and short==false ? close-open:na, offset= -1 * off, style=shape.arrowup, color=color.green, text='Outside bar', textcolor=color.green,location=location.belowbar, editable = false,display = display.pane)
//............................................................................................................................................................................


//------------------------inside and outsidebar alerts-------------------------------------------------
bariu = ta.valuewhen(ta.crossover(close, ta.valuewhen(ib , high[off-1], 0)), bar_index, 0)
barou = ta.valuewhen(ta.crossover(close, ta.valuewhen(ob , high[off], 0)), bar_index, 0)
barid = ta.valuewhen(ta.crossunder(close, ta.valuewhen(ib , low[off-1], 0)), bar_index, 0)
barod = ta.valuewhen(ta.crossunder(close, ta.valuewhen(ob, low[off], 0)), bar_index, 0)
bari =  ta.valuewhen(ib , bar_index[off-1], 0)
baro = ta.valuewhen(ob, bar_index[off], 0)
bardib = bari > baro ? bar_inde - bari : bar_inde - baro
ibal = ''

if bari>baro and bari>bariu[1] and bari>=barid and ta.crossover(close, ta.valuewhen(ib , high[off-1], 0)) 
    if ibalt 
        alert( 'inside_bar just engulfed up.' + '\n' + 'good for long position.' + '\n' + txt)
    ibal := 'insidebar engulfed up at '
else if  bari<baro and baro>barou[1] and baro>=barod and ta.crossover(close, ta.valuewhen(ob , high[off], 0))
    if ibalt 
        alert('outside_bar just engulfed up.' + '\n' + 'good for long position.'+ '\n' + txt)
    ibal := 'outsidebar engulfed up at '
else if   bari>baro and bari>barid[1] and bari>=bariu and ta.crossunder(close, ta.valuewhen(ib , low[off-1], 0)) 
    if ibalt
        alert( 'inside_bar just engulfed down.' + '\n' + 'good for short position.'+ '\n' + txt)
    ibal := 'insidebar engulfed down at '
else if  bari<baro and baro>barod[1] and baro>=barou and ta.crossunder(close, ta.valuewhen(ob , low[off], 0)) 
    if ibalt
        alert('outside_bar just engulfed down.' + '\n' + 'good for short position.'+ '\n' + txt)
    ibal := 'outsidebar engulfed down at '

if ibal == ''
    ibal := ibal[1]


//---------------------------------------------------------------------------------------------------
//alerts

alertip = if qmup 
    'Open your position!' + '\n' + 'IP pattern is Quasimodo for short position in the .' + str.tostring(iptf) + ' timeframe' + '\n' + 'entery price:' + str.tostring(ph1 - ((ebuf/100)*ph1)) + '\n' + 'stop loss:' +str.tostring(ph0 + ((sbuf/100)*ph0))
else if qmdw 
    'Open your position!' + '\n' + 'IP pattern is Quasimodo for long position in the .' + str.tostring(iptf) + ' timeframe' + '\n' + 'entery price:' + str.tostring(pl1 + ((ebuf/100)*pl1)) + '\n' + 'stop loss:' +str.tostring(pl0 - ((sbuf/100)*pl0))    
else if hnsup 
    'Open your position!' + '\n' + 'IP pattern is Head and Shoulder for short position in the .' + str.tostring(iptf) + ' timeframe' + '\n' + 'entery price:' + str.tostring(ph1 - ((ebuf/100)*ph1)) + '\n' + 'stop loss:' +str.tostring(ph0 + ((sbuf/100)*ph0))
else if hnsdw 
    'Open your position!' + '\n' + 'IP pattern is Head and Shoulder for long position in the .' + str.tostring(iptf) + ' timeframe' + '\n' + 'entery price:' + str.tostring(pl1 + ((ebuf/100)*pl1)) + '\n' + 'stop loss:' +str.tostring(pl0 - ((sbuf/100)*pl0)) 
else if weup 
    'Open your position!' + '\n' + 'IP pattern is whipsaw engulfing for short position in the .' + str.tostring(iptf) + ' timeframe' + '\n' + 'entery price:' + str.tostring( baselou - ((ebuf/100)*baselou)) + '\n' + 'stop loss:' + str.tostring(ph0 + ((sbuf/100)*ph0))
else if wedw 
    'Open your position!' + '\n' + 'IP pattern is whipsaw engulfing for long position in the .' + str.tostring(iptf) + ' timeframe' + '\n' + 'entery price:' + str.tostring(basehid + ((ebuf/100)*basehid)) + '\n' + 'stop loss:' +str.tostring(pl0 - ((sbuf/100)*pl0)) 
else 
    'na'

barc = (bar_inde - ta.valuewhen(rbup or rbdw or krbup or krbdw or ebdw or ebup or pindw or pinup or tbrup or tbrdw or cup or cdw,bar_inde,0) ) + off

alertc = 'hi'
if rbup or rbdw
    alertc :='reversal bar detected at ' 
else if krbup or krbdw
    alertc :='key reversal bar detected at ' 
else if ebdw or ebup
    alertc :='exhaustion bar detected at '
else if pindw or pinup
    alertc :='pin bar detected at '
else if tbrup or tbrdw
    alertc :='two-bar riversal detected at '
else if cup or cdw
    alertc :='three-bar riversal detected at ' 
else 
    alertc:=alertc[1]
altimc = timeframe.period == 'D' ? 'Days ago' : timeframe.period== 'W' ? 'Weeks ago' : timeframe.period=='M' ? 'Mounth ago' : timeframe.period=='240' ?
     '4h ago' : timeframe.period=='180' ? '3h ago' : timeframe.period=='120' ? '2h ago' : timeframe.period=='60' ? '1h ago' : timeframe.period=='45'
     ? '45minutes ago' : timeframe.period=='30' ? '30minutes ago' : timeframe.period=='15' ? '15minutes ago' : timeframe.period=='5' ? '5minutes ago' : timeframe.period=='3' ? '3minutes ago' :
     timeframe.period=='1' ? '1minutes ago' : na
if (rbup or rbdw or krbup or krbdw or ebdw or ebup or pindw or pinup or  tbrup or tbrdw or cup or cdw) and cal
    alert(alertc +  str.tostring(barc ) +' *'  + altimc )

if (qmup or qmdw or wedw or weup or hnsup or hnsdw) and ipal
    alert(alertip + '\n'+ alertc + str.tostring(barc ) +' *' + altimc + '\n' + ibal + str.tostring(bardib) + ' *' + altimc + '\n' + 'now price: ' + str.tostring(close) + '\n' + txt, perbar )
//-----------------------------------------------------------------------------------------
