ORB Smart Candle finder [With Volume Candle] with EXTEND
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Papadaulat

//@version=5
indicator(title="ORB Smart Candle finder [With Volume Candle] with EXTEND", overlay=true, precision=0, format=format.volume)
InSession(sessionTimesZ) =>
    not na(time(timeframe.period, sessionTimesZ))
barsize = input(40, title = "Candle Size" )
highbar1 = open
lowbar1 = close
sessionTime   = input.session("0915-1045", title="Time")
barvalue = math.abs(highbar1 - lowbar1)
candlerange = barvalue <  barsize
bool candlerange1 = na
candlerange1 := barvalue <  barsize and InSession(sessionTime)

//VOLUME 
avg_vol_len = input(title=' Voulme EMA Length', defval=20)
factor = input(0.25, title='Volume Factor %')
pesado = volume > ta.sma(volume, avg_vol_len) * factor
draw=ta.barssince(candlerange1)<ta.barssince(candlerange1[1] or candlerange1[2]) and candlerange1 and pesado
barcolor = if InSession(sessionTime) and draw and pesado
    color.black
else
    na
barcolor(color=barcolor)
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
var float barhigh2 = na
var float barlow2 = na
if InSession(sessionTime) and candlerange1 and (draw) and pesado // or (pesado and down))// or showLong)
    barhigh2 := highbar1
    barlow2 := lowbar1
barhigh3 = plot(barhigh2)//, plot=plot.style_circles)
barlow3 = plot(barlow2)//, plot=plot.style_circles
fill(barhigh3, barlow3, color = color.new(color.blue,50) )

//------------------------------------------------------------------------------------------------------------------------//
//EXTEND LINE
extendonoff1 = input(true, title ="Extend Line")
extend = input(5, title = "Extend Value")
f_y_given_x(_x1, _y1, _x2, _y2, _new_x) =>
    _m = (_y2 - _y1) / (_x2 - _x1)
    _b = _y1 - _m * _x1
    _new_y = _m * _new_x + _b
    _new_y

var ma_line1 = line.new(x1 = na, y1 = na, x2 = na, y2 = na, color = color.blue, extend = extend.none, style=line.style_solid)
var ma_line2 = line.new(x1 = na, y1 = na, x2 = na, y2 = na, color = color.blue, extend = extend.none, style=line.style_solid)

x1 = bar_index - 1
y1 = barhigh2[1]
x2 = bar_index
y2 = barhigh2
x3 = bar_index + extend
y3 = f_y_given_x(x1, y1, x2, y2, x3)
line.set_xy1(extendonoff1 ? ma_line1 : na, x = x1, y = y1)
line.set_xy2(extendonoff1 ? ma_line1 : na, x = x3, y = y3)

y22 = barlow2[1]
y33 = barlow2
y44 = f_y_given_x(x1, y22, x2, y33, x3)
line.set_xy1(extendonoff1 ? ma_line2 : na, x = x1, y = y22)
line.set_xy2(extendonoff1 ? ma_line2 : na, x = x3, y = y44)