Daily Pivots with Fakeout Protection
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © cyatophilum

//@version=5
indicator("Daily Pivots with Fakeout Protection",overlay=true)

pivot_tf = input.timeframe('D','Pivots Timeframe')
fakeout_perc = input.float(0.2,'Fakeout Protection (%)')

[daily_high,daily_low] = request.security(syminfo.tickerid,pivot_tf,[high[1],low[1]],lookahead = barmerge.lookahead_on)
is_new_period = daily_high != daily_high[1]

is_bullish = close > daily_high
is_bearish = close < daily_low

breakout_long_line = daily_high * (1 + fakeout_perc/100)
breakout_short_line = daily_low * (1 - fakeout_perc/100)

volma = ta.sma(volume,100)
long_breakout  = ta.crossover(close,breakout_long_line)   and volume > 2 * volma
short_breakout = ta.crossunder(close,breakout_short_line) and volume > 2 * volma




// Render {
theme_color = color.rgb(0,255,255)
theme_color_transp = color.rgb(0,255,255,80)

p0 = plot(close,'close',display = display.none)
ph = plot(not is_new_period ? daily_high : na,'daily high',theme_color, 2, plot.style_linebr)
pl = plot(not is_new_period ? daily_low : na, 'daily low', theme_color, 2, plot.style_linebr)

fill(ph,pl,theme_color_transp,'Pivot Range background')
plotshape(long_breakout, 'Long Breakout Signal', shape.triangleup,  location.belowbar,theme_color, text='Breakout', textcolor = theme_color, size=size.small)
plotshape(short_breakout,'Short Breakout Signal',shape.triangledown,location.abovebar,theme_color, text='Breakout', textcolor = theme_color, size=size.small)

plot(not is_new_period and bar_index % 2 == 0 ? breakout_long_line : na, 'breakout long line',  theme_color, 1, plot.style_linebr)
plot(not is_new_period and bar_index % 2 == 0 ? breakout_short_line : na,'breakout short line', theme_color, 1, plot.style_linebr)

bgcolor(is_new_period ? color.rgb(255, 255, 255, 80) : na)
fill(p0,ph,is_bullish ? color.rgb(0, 255, 132, 80) : na)
fill(p0,pl,is_bearish ? color.rgb(255, 0, 0, 80) : na)

// }
