Days to expected ROI
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © trader24521

//** USE "1 Day" resolution   **//

//@version=5
indicator(title="Days to expected ROI", shorttitle="Days to ROI")

start = input.time(timestamp("2021-01-01T00:00:00"), "Start Date")
expected_roi = input.float(20.0, "Expected ROI %") / 100.0 + 1.0
not_found = input.int(0, "Value to plot if not found")
 
current_date = timestamp(year(time), month(time), dayofmonth(time), 0, 0)
start_date = timestamp(year(start), month(start), dayofmonth(start), 0, 0)

max_days = (current_date - start_date) / (24 * 60 * 60 * 1000)
max_days := math.max(max_days - 1, 0)

// plotchar(max_days, "days", "", location=location.top)

fg_values = array.new_int(max_days)

if barstate.islast
    for i=1 to max_days-1
        for j=i-1 to 0
            roi = close[j]/close[i]
            if roi >= expected_roi and na(array.get(fg_values, i))
                array.set(fg_values, i, math.abs(i-j))


diff = time - time[1]

var box b0 = na, box.delete(b0)
var box b1 = na, box.delete(b1)
var box b2 = na, box.delete(b2)
var box b3 = na, box.delete(b3)
var box b4 = na, box.delete(b4)
var box b5 = na, box.delete(b5)
var box b6 = na, box.delete(b6)
var box b7 = na, box.delete(b7)
var box b8 = na, box.delete(b8)
var box b9 = na, box.delete(b9)

var box b10 = na, box.delete(b10)
var box b11 = na, box.delete(b11)
var box b12 = na, box.delete(b12)
var box b13 = na, box.delete(b13)
var box b14 = na, box.delete(b14)
var box b15 = na, box.delete(b15)
var box b16 = na, box.delete(b16)
var box b17 = na, box.delete(b17)
var box b18 = na, box.delete(b18)
var box b19 = na, box.delete(b19)

var box b20 = na, box.delete(b20)
var box b21 = na, box.delete(b21)
var box b22 = na, box.delete(b22)
var box b23 = na, box.delete(b23)
var box b24 = na, box.delete(b24)
var box b25 = na, box.delete(b25)
var box b26 = na, box.delete(b26)
var box b27 = na, box.delete(b27)
var box b28 = na, box.delete(b28)
var box b29 = na, box.delete(b29)

var box b30 = na, box.delete(b30)
var box b31 = na, box.delete(b31)
var box b32 = na, box.delete(b32)
var box b33 = na, box.delete(b33)
var box b34 = na, box.delete(b34)
var box b35 = na, box.delete(b35)
var box b36 = na, box.delete(b36)
var box b37 = na, box.delete(b37)
var box b38 = na, box.delete(b38)
var box b39 = na, box.delete(b39)

var box b40 = na, box.delete(b40)
var box b41 = na, box.delete(b41)
var box b42 = na, box.delete(b42)
var box b43 = na, box.delete(b43)
var box b44 = na, box.delete(b44)
var box b45 = na, box.delete(b45)
var box b46 = na, box.delete(b46)
var box b47 = na, box.delete(b47)
var box b48 = na, box.delete(b48)
var box b49 = na, box.delete(b49)

if barstate.islast 
    index = max_days - 1, current_bar = start_date + 2 * diff, next_bar = current_bar + diff
    b0 := box.new(left=current_bar, top=nz(array.get(fg_values, index), not_found), right=next_bar, bottom=0, xloc=xloc.bar_time, bgcolor=color.blue, border_color=color.navy), index -= 1, index := math.max(0, index), current_bar += diff, next_bar += diff
    b1 := box.new(left=current_bar, top=nz(array.get(fg_values, index), not_found), right=next_bar, bottom=0, xloc=xloc.bar_time, bgcolor=color.blue, border_color=color.navy), index -= 1, index := math.max(0, index), current_bar += diff, next_bar += diff
    b2 := box.new(left=current_bar, top=nz(array.get(fg_values, index), not_found), right=next_bar, bottom=0, xloc=xloc.bar_time, bgcolor=color.blue, border_color=color.navy), index -= 1, index := math.max(0, index), current_bar += diff, next_bar += diff
    b3 := box.new(left=current_bar, top=nz(array.get(fg_values, index), not_found), right=next_bar, bottom=0, xloc=xloc.bar_time, bgcolor=color.blue, border_color=color.navy), index -= 1, index := math.max(0, index), current_bar += diff, next_bar += diff
    b4 := box.new(left=current_bar, top=nz(array.get(fg_values, index), not_found), right=next_bar, bottom=0, xloc=xloc.bar_time, bgcolor=color.blue, border_color=color.navy), index -= 1, index := math.max(0, index), current_bar += diff, next_bar += diff
    b5 := box.new(left=current_bar, top=nz(array.get(fg_values, index), not_found), right=next_bar, bottom=0, xloc=xloc.bar_time, bgcolor=color.blue, border_color=color.navy), index -= 1, index := math.max(0, index), current_bar += diff, next_bar += diff
    b6 := box.new(left=current_bar, top=nz(array.get(fg_values, index), not_found), right=next_bar, bottom=0, xloc=xloc.bar_time, bgcolor=color.blue, border_color=color.navy), index -= 1, index := math.max(0, index), current_bar += diff, next_bar += diff
    b7 := box.new(left=current_bar, top=nz(array.get(fg_values, index), not_found), right=next_bar, bottom=0, xloc=xloc.bar_time, bgcolor=color.blue, border_color=color.navy), index -= 1, index := math.max(0, index), current_bar += diff, next_bar += diff
    b8 := box.new(left=current_bar, top=nz(array.get(fg_values, index), not_found), right=next_bar, bottom=0, xloc=xloc.bar_time, bgcolor=color.blue, border_color=color.navy), index -= 1, index := math.max(0, index), current_bar += diff, next_bar += diff
    b9 := box.new(left=current_bar, top=nz(array.get(fg_values, index), not_found), right=next_bar, bottom=0, xloc=xloc.bar_time, bgcolor=color.blue, border_color=color.navy), index -= 1, index := math.max(0, index), current_bar += diff, next_bar += diff
    
    b10 := box.new(left=current_bar, top=nz(array.get(fg_values, index), not_found), right=next_bar, bottom=0, xloc=xloc.bar_time, bgcolor=color.blue, border_color=color.navy), index -= 1, index := math.max(0, index), current_bar += diff, next_bar += diff
    b11 := box.new(left=current_bar, top=nz(array.get(fg_values, index), not_found), right=next_bar, bottom=0, xloc=xloc.bar_time, bgcolor=color.blue, border_color=color.navy), index -= 1, index := math.max(0, index), current_bar += diff, next_bar += diff
    b12 := box.new(left=current_bar, top=nz(array.get(fg_values, index), not_found), right=next_bar, bottom=0, xloc=xloc.bar_time, bgcolor=color.blue, border_color=color.navy), index -= 1, index := math.max(0, index), current_bar += diff, next_bar += diff
    b13 := box.new(left=current_bar, top=nz(array.get(fg_values, index), not_found), right=next_bar, bottom=0, xloc=xloc.bar_time, bgcolor=color.blue, border_color=color.navy), index -= 1, index := math.max(0, index), current_bar += diff, next_bar += diff
    b14 := box.new(left=current_bar, top=nz(array.get(fg_values, index), not_found), right=next_bar, bottom=0, xloc=xloc.bar_time, bgcolor=color.blue, border_color=color.navy), index -= 1, index := math.max(0, index), current_bar += diff, next_bar += diff
    b15 := box.new(left=current_bar, top=nz(array.get(fg_values, index), not_found), right=next_bar, bottom=0, xloc=xloc.bar_time, bgcolor=color.blue, border_color=color.navy), index -= 1, index := math.max(0, index), current_bar += diff, next_bar += diff
    b16 := box.new(left=current_bar, top=nz(array.get(fg_values, index), not_found), right=next_bar, bottom=0, xloc=xloc.bar_time, bgcolor=color.blue, border_color=color.navy), index -= 1, index := math.max(0, index), current_bar += diff, next_bar += diff
    b17 := box.new(left=current_bar, top=nz(array.get(fg_values, index), not_found), right=next_bar, bottom=0, xloc=xloc.bar_time, bgcolor=color.blue, border_color=color.navy), index -= 1, index := math.max(0, index), current_bar += diff, next_bar += diff
    b18 := box.new(left=current_bar, top=nz(array.get(fg_values, index), not_found), right=next_bar, bottom=0, xloc=xloc.bar_time, bgcolor=color.blue, border_color=color.navy), index -= 1, index := math.max(0, index), current_bar += diff, next_bar += diff
    b19 := box.new(left=current_bar, top=nz(array.get(fg_values, index), not_found), right=next_bar, bottom=0, xloc=xloc.bar_time, bgcolor=color.blue, border_color=color.navy), index -= 1, index := math.max(0, index), current_bar += diff, next_bar += diff
    
    b20 := box.new(left=current_bar, top=nz(array.get(fg_values, index), not_found), right=next_bar, bottom=0, xloc=xloc.bar_time, bgcolor=color.blue, border_color=color.navy), index -= 1, index := math.max(0, index), current_bar += diff, next_bar += diff
    b21 := box.new(left=current_bar, top=nz(array.get(fg_values, index), not_found), right=next_bar, bottom=0, xloc=xloc.bar_time, bgcolor=color.blue, border_color=color.navy), index -= 1, index := math.max(0, index), current_bar += diff, next_bar += diff
    b22 := box.new(left=current_bar, top=nz(array.get(fg_values, index), not_found), right=next_bar, bottom=0, xloc=xloc.bar_time, bgcolor=color.blue, border_color=color.navy), index -= 1, index := math.max(0, index), current_bar += diff, next_bar += diff
    b23 := box.new(left=current_bar, top=nz(array.get(fg_values, index), not_found), right=next_bar, bottom=0, xloc=xloc.bar_time, bgcolor=color.blue, border_color=color.navy), index -= 1, index := math.max(0, index), current_bar += diff, next_bar += diff
    b24 := box.new(left=current_bar, top=nz(array.get(fg_values, index), not_found), right=next_bar, bottom=0, xloc=xloc.bar_time, bgcolor=color.blue, border_color=color.navy), index -= 1, index := math.max(0, index), current_bar += diff, next_bar += diff
    b25 := box.new(left=current_bar, top=nz(array.get(fg_values, index), not_found), right=next_bar, bottom=0, xloc=xloc.bar_time, bgcolor=color.blue, border_color=color.navy), index -= 1, index := math.max(0, index), current_bar += diff, next_bar += diff
    b26 := box.new(left=current_bar, top=nz(array.get(fg_values, index), not_found), right=next_bar, bottom=0, xloc=xloc.bar_time, bgcolor=color.blue, border_color=color.navy), index -= 1, index := math.max(0, index), current_bar += diff, next_bar += diff
    b27 := box.new(left=current_bar, top=nz(array.get(fg_values, index), not_found), right=next_bar, bottom=0, xloc=xloc.bar_time, bgcolor=color.blue, border_color=color.navy), index -= 1, index := math.max(0, index), current_bar += diff, next_bar += diff
    b28 := box.new(left=current_bar, top=nz(array.get(fg_values, index), not_found), right=next_bar, bottom=0, xloc=xloc.bar_time, bgcolor=color.blue, border_color=color.navy), index -= 1, index := math.max(0, index), current_bar += diff, next_bar += diff
    b29 := box.new(left=current_bar, top=nz(array.get(fg_values, index), not_found), right=next_bar, bottom=0, xloc=xloc.bar_time, bgcolor=color.blue, border_color=color.navy), index -= 1, index := math.max(0, index), current_bar += diff, next_bar += diff
    
    b30 := box.new(left=current_bar, top=nz(array.get(fg_values, index), not_found), right=next_bar, bottom=0, xloc=xloc.bar_time, bgcolor=color.blue, border_color=color.navy), index -= 1, index := math.max(0, index), current_bar += diff, next_bar += diff
    b31 := box.new(left=current_bar, top=nz(array.get(fg_values, index), not_found), right=next_bar, bottom=0, xloc=xloc.bar_time, bgcolor=color.blue, border_color=color.navy), index -= 1, index := math.max(0, index), current_bar += diff, next_bar += diff
    b32 := box.new(left=current_bar, top=nz(array.get(fg_values, index), not_found), right=next_bar, bottom=0, xloc=xloc.bar_time, bgcolor=color.blue, border_color=color.navy), index -= 1, index := math.max(0, index), current_bar += diff, next_bar += diff
    b33 := box.new(left=current_bar, top=nz(array.get(fg_values, index), not_found), right=next_bar, bottom=0, xloc=xloc.bar_time, bgcolor=color.blue, border_color=color.navy), index -= 1, index := math.max(0, index), current_bar += diff, next_bar += diff
    b34 := box.new(left=current_bar, top=nz(array.get(fg_values, index), not_found), right=next_bar, bottom=0, xloc=xloc.bar_time, bgcolor=color.blue, border_color=color.navy), index -= 1, index := math.max(0, index), current_bar += diff, next_bar += diff
    b35 := box.new(left=current_bar, top=nz(array.get(fg_values, index), not_found), right=next_bar, bottom=0, xloc=xloc.bar_time, bgcolor=color.blue, border_color=color.navy), index -= 1, index := math.max(0, index), current_bar += diff, next_bar += diff
    b36 := box.new(left=current_bar, top=nz(array.get(fg_values, index), not_found), right=next_bar, bottom=0, xloc=xloc.bar_time, bgcolor=color.blue, border_color=color.navy), index -= 1, index := math.max(0, index), current_bar += diff, next_bar += diff
    b37 := box.new(left=current_bar, top=nz(array.get(fg_values, index), not_found), right=next_bar, bottom=0, xloc=xloc.bar_time, bgcolor=color.blue, border_color=color.navy), index -= 1, index := math.max(0, index), current_bar += diff, next_bar += diff
    b38 := box.new(left=current_bar, top=nz(array.get(fg_values, index), not_found), right=next_bar, bottom=0, xloc=xloc.bar_time, bgcolor=color.blue, border_color=color.navy), index -= 1, index := math.max(0, index), current_bar += diff, next_bar += diff
    b39 := box.new(left=current_bar, top=nz(array.get(fg_values, index), not_found), right=next_bar, bottom=0, xloc=xloc.bar_time, bgcolor=color.blue, border_color=color.navy), index -= 1, index := math.max(0, index), current_bar += diff, next_bar += diff
    
    b40 := box.new(left=current_bar, top=nz(array.get(fg_values, index), not_found), right=next_bar, bottom=0, xloc=xloc.bar_time, bgcolor=color.blue, border_color=color.navy), index -= 1, index := math.max(0, index), current_bar += diff, next_bar += diff
    b41 := box.new(left=current_bar, top=nz(array.get(fg_values, index), not_found), right=next_bar, bottom=0, xloc=xloc.bar_time, bgcolor=color.blue, border_color=color.navy), index -= 1, index := math.max(0, index), current_bar += diff, next_bar += diff
    b42 := box.new(left=current_bar, top=nz(array.get(fg_values, index), not_found), right=next_bar, bottom=0, xloc=xloc.bar_time, bgcolor=color.blue, border_color=color.navy), index -= 1, index := math.max(0, index), current_bar += diff, next_bar += diff
    b43 := box.new(left=current_bar, top=nz(array.get(fg_values, index), not_found), right=next_bar, bottom=0, xloc=xloc.bar_time, bgcolor=color.blue, border_color=color.navy), index -= 1, index := math.max(0, index), current_bar += diff, next_bar += diff
    b44 := box.new(left=current_bar, top=nz(array.get(fg_values, index), not_found), right=next_bar, bottom=0, xloc=xloc.bar_time, bgcolor=color.blue, border_color=color.navy), index -= 1, index := math.max(0, index), current_bar += diff, next_bar += diff
    b45 := box.new(left=current_bar, top=nz(array.get(fg_values, index), not_found), right=next_bar, bottom=0, xloc=xloc.bar_time, bgcolor=color.blue, border_color=color.navy), index -= 1, index := math.max(0, index), current_bar += diff, next_bar += diff
    b46 := box.new(left=current_bar, top=nz(array.get(fg_values, index), not_found), right=next_bar, bottom=0, xloc=xloc.bar_time, bgcolor=color.blue, border_color=color.navy), index -= 1, index := math.max(0, index), current_bar += diff, next_bar += diff
    b47 := box.new(left=current_bar, top=nz(array.get(fg_values, index), not_found), right=next_bar, bottom=0, xloc=xloc.bar_time, bgcolor=color.blue, border_color=color.navy), index -= 1, index := math.max(0, index), current_bar += diff, next_bar += diff
    b48 := box.new(left=current_bar, top=nz(array.get(fg_values, index), not_found), right=next_bar, bottom=0, xloc=xloc.bar_time, bgcolor=color.blue, border_color=color.navy), index -= 1, index := math.max(0, index), current_bar += diff, next_bar += diff
    b49 := box.new(left=current_bar, top=nz(array.get(fg_values, index), not_found), right=next_bar, bottom=0, xloc=xloc.bar_time, bgcolor=color.blue, border_color=color.navy), index -= 1, index := math.max(0, index), current_bar += diff, next_bar += diff
