Extreme Bars
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © faytterro

//@version=5
indicator("Extreme Bars", overlay=true, max_bars_back = 500)
x=ta.rsi((volume)/(high-low),14)*16-750
len=input.int(100, title="sensitivity", minval=1, maxval=100)
y=0.0
if barstate.islast
    y:=100
else
    y:=x>len? 100 : x<5? 5 : x
plotcandle(open, high, low, close, title='Transo bars', 
 color = open < close ? color.rgb(76, 175, 80, 100-y) : color.rgb(255, 82, 82, 100-y),
 wickcolor=open < close ? color.rgb(76, 175, 80, 100-y) : color.rgb(255, 82, 82, 100-y),
 bordercolor=open < close ? color.rgb(76, 175, 80, 100-y) : color.rgb(255, 82, 82, 100-y))
lb=(x[1]-x[2]<-80 and math.abs(close[1]-open[1])>high[2]*2-2*low[2] and close[1]<open[1])
 or ( (math.abs(close[1]-open[1])+math.abs(close-open)>high[2]*3-3*low[2]) and x[2]>x[1]+x and (close<open and close[1]<open[1]) )

lb:= lb and lb[1]==false
//plotshape(lb, offset =-2)
ass= ta.crossover(close,high[ta.barssince(lb)+2])
for i = 1 to  ta.barssince(lb)
    ass:= ass and ass[i]==false
k=ta.barssince(ass)<ta.barssince(lb)? ta.barssince(ass) : -2
box=box.new(last_bar_index-ta.barssince(lb)-2,high[ta.barssince(lb)+2], last_bar_index-k-2,low[ta.barssince(lb)+2], bgcolor = color.rgb(255,0,0,80), border_color = color.rgb(71, 89, 139,100))
box.delete(box[1])
ls=(x[1]-x[2]<-80 and math.abs(close[1]-open[1])>high[2]*2-2*low[2] and close[1]>open[1])
 or ( (math.abs(close[1]-open[1])+math.abs(close-open)>high[2]*3-3*low[2]) and x[2]>x[1]+x and (close>open and close[1]>open[1]) )
ls:= ls and ls[1]==false
//plotshape(ls, offset =-2)
akk = ta.crossunder(close,low[ta.barssince(ls)+2])
for i=1 to ta.barssince(ls)
    akk:= akk and akk[i]==false
k2=ta.barssince(akk)<ta.barssince(ls)? ta.barssince(akk) : -2
box2=box.new(last_bar_index-ta.barssince(ls)-2,high[ta.barssince(ls)+2], last_bar_index-k2-2,low[ta.barssince(ls)+2], bgcolor = color.rgb(0,255,0,80), border_color = color.rgb(71, 89, 139,100))
box.delete(box2[1])

