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

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

x_1 = input.time(defval = 1, title = "X 1", confirm = true)
y_1 = input.price(defval = 1, title = "Y 1", confirm = true)
x_2 = input.time(defval = 1, title = "X 2", confirm = true)
y_2 = input.price(defval = 1, title = "Y 2", confirm = true)
run_input = input.int(defval=0, title="run")
extend = input.bool(true, title="extnd trendline")

var int bar_index_x_1_table = 0
var int bar_index_x_2_table = 0

if time == x_1
    bar_index_x_1_table := bar_index
if time == x_2
    bar_index_x_2_table := bar_index
rise = (math.max(y_1, y_2) - math.min(y_1, y_2))
slope = rise / run_input

var float plot_line = na
var int bar_count = 0

if time == x_1 and barstate.isconfirmed
    plot_line := y_1
if time > x_1
    if y_2 > y_1
        plot_line := plot_line + slope
    else
        plot_line := plot_line - slope
    if time <= x_2
        bar_count += 1
if time > x_2 and extend == false
    plot_line := na

plot(run_input != 0 ? plot_line : na)

if barstate.isconfirmed
	var table3 = table.new(position = position.top_right, columns = 30, rows = 30, bgcolor = color.yellow, border_width = 1)
	table.cell(table_id = table3, column = 0, row = 2, text = "run")
	table.cell(table_id = table3, column = 1, row = 2, text = str.tostring(bar_index_x_2_table - bar_index_x_1_table))


