Range Gap/Open to Close/Close to Close
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © seba34e

//@version=5
indicator("Range Gap/Open to Close/Close to Close")

GapOrDay = input.string("Gap", title = "Condition", options = ["Gap", "Open To Close", "Close To Close"] )
Gatillo = input.float(1.2, "Target Percentage", step=0.1)

filter_from = input (true, title="Apply From Filter", group = "FROM")
i_startTime   = input.time(title='Start Date', defval=timestamp('01 Jan 2022 05:00'), group= "FROM", tooltip='Date & time to begin trading from')

filter_until = input (true, title = "Apply Until Filter", group = "UNTIL")
i_endTime               = input.time(title='End Date', defval=timestamp('1 Jul 2022 04:00'), group= "UNTIL", tooltip='Date & time to stop trading')

FechaValida() =>
    from =  time >= i_startTime or not filter_from
    until =  time <= i_endTime   or not filter_until
    from and until    


var totalDays = 0
var trueDays = 0 

diferencia_puntos = math.abs(close[1]-open)
gap_porcentage = math.abs ((1-open/close[1] )  *100) 
openClosePorcentage  = math.abs ((1- close/open )      *100)
closeClosePorcentage   = math.abs ((1-close /close [1]) *100)

//plot (diferencia_puntos, style=plot.style_histogram)
plot (GapOrDay=="Gap"            ? gap_porcentage : na, style=plot.style_columns, color= gap_porcentage>=Gatillo   ? color.red : color.green)
plot (GapOrDay=="Open To Close"  ? openClosePorcentage : na, style=plot.style_columns, color= openClosePorcentage>=Gatillo   ? color.red : color.green)
plot (GapOrDay=="Close To Close" ? closeClosePorcentage : na, style=plot.style_columns, color= closeClosePorcentage>=Gatillo   ? color.red : color.green)

hline(Gatillo, linestyle = hline.style_solid, color=color.blue)

if GapOrDay == 'Gap' and gap_porcentage <= Gatillo and FechaValida()
    trueDays := trueDays + 1

if GapOrDay == 'Open To Close' and openClosePorcentage <= Gatillo and FechaValida()
    trueDays := trueDays + 1

if GapOrDay == 'Close To Close' and closeClosePorcentage <= Gatillo and FechaValida()
    trueDays := trueDays + 1

if FechaValida() 
    totalDays := totalDays + 1

bgcolor(FechaValida() ? color.new(color.yellow ,transp = 95) : na )

var table TheTable = table.new(position.top_right , 2, 8, border_width=2)

fill_Cell(_table, _column, _row, _title, _value, _bgcolor, _txtcolor) =>
    _cellText = _title + '' + _value
    table.cell(_table, _column, _row, _cellText, bgcolor=_bgcolor, text_color=_txtcolor, text_halign=text.align_right)

//Prepare cells
if barstate.islast
    fill_Cell(TheTable, 0, 0, "", 'Condition:' , color.blue, color.yellow)
    fill_Cell(TheTable, 1, 0, GapOrDay, " ", color.blue, color.yellow)

    fill_Cell(TheTable, 0, 1, "", 'Deviation:' , color.black, color.yellow)
    fill_Cell(TheTable, 1, 1, "", str.tostring(Gatillo,"0.00") + "%" , color.black, color.yellow)


    fill_Cell(TheTable, 0, 2, "", 'Total Days:' , color.blue, color.yellow)
    fill_Cell(TheTable, 1, 2, str.tostring(totalDays), " ", color.blue, color.yellow)

    fill_Cell(TheTable, 0, 3, "", 'Under Condition Days' , color.green, color.white)
    fill_Cell(TheTable, 1, 3, str.tostring(trueDays) + ' (' + str.tostring(trueDays/totalDays,"0.00%") + ')', " ",  color.green, color.white)

    fill_Cell(TheTable, 0, 4, "", 'Over Condition Days' , color.red, color.white)
    fill_Cell(TheTable, 1, 4, str.tostring(totalDays - trueDays) + ' (' + str.tostring((totalDays-trueDays)/totalDays,"0.00%") + ')', " ", color.red, color.white)
