I'm having trouble with getting the line to plot properly with time inputs so that it is more interactive and easier to move trend lines around on the graph. My understanding is that to plot a line I need to reference the bar number and as such I'm using a loop to compare input.time to the time of a bar value for a match which successfully draws a line.
The problem however is that the line is not being plotted from x1 to x2 due to a n/a value until after the x2 value is calculated on that bar and thus anything before that bar is not drawn.
//@version=5
indicator("My Trend Line Script For Webhook", overlay=true)
start_x1_time = input.time(title="S Time/Profit #1", defval=(timestamp("06 Nov 2021 04:30 +0000")), inline = "start", group = "Start Position Trend Line", confirm=(true))
start_y1 = input.price(title="", defval=0.2, inline = "start", group = "Start Position Trend Line", confirm=(true))
start_x2_time = input.time(title="S Time/Profit #2", defval=(timestamp("07 Nov 2021 04:30 +0000")), inline = "start2", group = "Start Position Trend Line", confirm=(true))
start_y2 = input.price(title="", defval=0.3, inline = "start2", group = "Start Position Trend Line", confirm=(true))
// for correlating time to appropriate bar index
i=0
float start_x1_bar = na
float start_x2_bar = na
while i <= 1500
if start_x1_time == time[i]
start_x1_bar := bar_index-i
if start_x2_time == time[i]
start_x2_bar := bar_index-i
if start_x2_bar and start_x1_bar !=na
break
i += 1
// TRENDLINE CODE
// --------------
get_slope(x1,x2,y1,y2)=>
m = (y2-y1)/(x2-x1)
get_y_intercept(m, x1, y1)=>
b=y1-m*x1
get_y(m, b, ts)=>
Y = m * ts + b
res_m = get_slope(start_x1_bar,start_x2_bar,start_y1,start_y2)
res_b = get_y_intercept(res_m, start_x1_bar, start_y1)
res_y = get_y(res_m, res_b, bar_index)
plot(res_y, color=color.red, title='Trendline Price Value', linewidth=1)
Picture of missing line from x1 to x2: