Barbwire [yohtza]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © yohtza

//@version=5
indicator("Barbwire [yohtza]", overlay = true)

borderColor = input.color(color.new(color.gray, 80), "Barbwire border color")
bgColor = input.color(color.new(color.gray, 80), "Barbwire background color")

bool isDoji = math.abs(open-close) / (high - low) < 0.5

sixtysix_pbar = high[1] - ((high[1] - low[1]) * 0.66)
thirtythree_pbar = high[1] - ((high[1] - low[1]) * 0.33)
thirtythree = high - ((high - low) * 0.33) 
sixtysix = high - ((high - low) * 0.66) 
fifty_pbar = high[1] - ((high[1] - low[1]) * 0.5)
fifty = high - ((high - low) * 0.5)

bool bl = high > high[1] and low > low[1]
bool br = high < high[1] and low < low[1]
bool o = high >= high[1] and low <= low[1]
bool i = (high <= high[1] and low > low[1]) or (high < high[1] and low >= low[1]) 

bool bullBar = open[1] <= close[1]
// bool deepPb = bl ? low < sixtysix_pbar and high[1] > thirtythree : br ? high > thirtythree_pbar and low[1] < sixtysix : i ? high > thirtythree_pbar and low < sixtysix_pbar : o ? low[1] < sixtysix and high[1] > thirtythree : false
bool deepPb = bl ? low < fifty_pbar and high[1] > fifty : br ? high > fifty_pbar and low[1] < fifty : i ? high > fifty_pbar and low < fifty_pbar : o ? low[1] < fifty and high[1] > fifty : false
bool overlap = deepPb and deepPb[1] //and deepPb[2]
bool atLeastOneDojiInLastThreeBars = isDoji or isDoji[1] or isDoji[2]
bool barbwire = atLeastOneDojiInLastThreeBars and overlap

top = ta.highest(3)
bot = ta.lowest(3)
var int start = na
var float trtop = na
var float trbot = na
var box ttrbox = na

if barbwire[1] == false and barbwire
    start := bar_index -2
    right = bar_index
    trtop := top
    trbot := bot
    ttrbox := box.new(start,trtop,right,trbot, border_color = borderColor, bgcolor = bgColor, border_width = 3)

if barbwire[1] and barbwire
    right = bar_index 
    trtop := trtop > top ? trtop : top
    trbot := trbot < bot ? trbot : bot
    box.set_bottom(ttrbox, trbot)
    box.set_top(ttrbox, trtop)
    box.set_right(ttrbox,right)

