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

//@version=5
indicator("conditional_returns", overlay = true)

override = input.float(0, "override") / 100
start = input.time(timestamp("1 Jan 1950"), "start")
end = input.time(timestamp("1 Jan 2300"), "end")

in_clr = color.new(color.gray, 95)
out_clr = color.new(color.white, 100)

var x = array.new_float()
var up = array.new_float()
var dn = array.new_float()
var up_all = array.new_float()
var dn_all = array.new_float()

ret = math.log(close / close[1])

in_range = time >= start and time < end
bgcolor(in_range ? in_clr : out_clr)

if in_range

    array.push(x, ret)

    if ret >= 0

        array.push(up_all, ret)

    else

        array.push(dn_all, ret)


if barstate.islast
    
    float selected = na
    
    if override == 0.
        
        selected := array.get(x, array.size(x) - 1)
        
    else
    
        selected := override
    
    for i = 0 to array.size(x) - 2
    
        val = array.get(x, i)
        next = array.get(x, i + 1)
    
        if (val >= selected and selected > 0) or (val <= selected and selected < 0)
        
            if next >= 0
            
                array.push(up, next)
        
            else
            
                array.push(dn, next)
    
    up_count = array.size(up)
    up_all_count = array.size(up_all)
    dn_count = array.size(dn)
    dn_all_count = array.size(dn_all)
    
    lbl_a = color.new(color.blue, 70)
    lbl_b = color.new(color.green, 70)
    lbl_c = color.new(color.yellow, 70)
    
    fmt = "#.##"
    
    t = table.new(position.top_right, 7, 3)
    
    table.cell(t, 0, 0, str.tostring(selected * 100, fmt), bgcolor = color.new(color.fuchsia, 70))
    table.cell(t, 1, 0, "selected", bgcolor = lbl_a)
    table.cell(t, 2, 0, "%", bgcolor = lbl_a)
    table.cell(t, 3, 0, "avg", bgcolor = lbl_a)
    table.cell(t, 4, 0, "all", bgcolor = lbl_b)
    table.cell(t, 5, 0, "%", bgcolor = lbl_b)
    table.cell(t, 6, 0, "avg", bgcolor = lbl_b)
    
    table.cell(t, 0, 1, "up", bgcolor = lbl_c)
    table.cell(t, 1, 1, str.tostring(up_count), bgcolor = lbl_a)
    table.cell(t, 2, 1, str.tostring(up_count / (up_count + dn_count) * 100, fmt), bgcolor = lbl_a)
    table.cell(t, 3, 1, str.tostring(array.avg(up) * 100, fmt), bgcolor = lbl_a)
    table.cell(t, 4, 1, str.tostring(up_all_count), bgcolor = lbl_b)
    table.cell(t, 5, 1, str.tostring(up_all_count / (up_all_count + dn_all_count) * 100, fmt), bgcolor = lbl_b)
    table.cell(t, 6, 1, str.tostring(array.avg(up_all) * 100, fmt), bgcolor = lbl_b)
    
    table.cell(t, 0, 2, "down", bgcolor = lbl_c)
    table.cell(t, 1, 2, str.tostring(dn_count), bgcolor = lbl_a)
    table.cell(t, 2, 2, str.tostring(dn_count / (up_count + dn_count) * 100, fmt), bgcolor = lbl_a)
    table.cell(t, 3, 2, str.tostring(array.avg(dn) * 100, fmt), bgcolor = lbl_a)
    table.cell(t, 4, 2, str.tostring(dn_all_count), bgcolor = lbl_b)
    table.cell(t, 5, 2, str.tostring(dn_all_count / (up_all_count + dn_all_count) * 100, fmt), bgcolor = lbl_b)
    table.cell(t, 6, 2, str.tostring(array.avg(dn_all) * 100, fmt), bgcolor = lbl_b)