this is the code check the title for question, copy and paste this code in pinescript to view the indicator and trendlines that are only plotting for upto 50 bars please suggest how to correct the output and plot for all the chart history
//@version=5
indicator("Least Squares Regression Line", shorttitle="LSRL", overlay=true)
var float[] lowPointsX = array.new_float(0)
var float[] lowPointsY = array.new_float(0)
var float[] highPointsX = array.new_float(0)
var float[] highPointsY = array.new_float(0)
// Input for lookback period
b = low[1] < low and low[2] > low[1]
a = high[1] > high and high[2] < high[1]
// Store x and y coordinates for swing lows
if (b)
array.unshift(lowPointsX, bar_index[1])
array.unshift(lowPointsY, low[1])
// Limit the array length to 3 for swing lows
if (array.size(lowPointsX) > 3)
array.pop(lowPointsX)
if (array.size(lowPointsY) > 3)
array.pop(lowPointsY)
// Store x and y coordinates for swing highs
if (a)
array.unshift(highPointsX, bar_index[1])
array.unshift(highPointsY, high[1])
// Limit the array length to 3 for swing highs
if (array.size(highPointsX) > 3)
array.pop(highPointsX)
if (array.size(highPointsY) > 3)
array.pop(highPointsY)
// Plotting shapes for swing lows and highs
plotshape(b, "lows", style=shape.triangledown, location=location.belowbar, offset=-1)
plotshape(a, "highs", style=shape.triangleup, location=location.abovebar, offset=-1)
// Calculate the least squares regression line for swing lows
var float slopeLow = na
var float interceptLow = na
if (array.size(lowPointsX) == 3)
sum_x_low = array.get(lowPointsX, 0) + array.get(lowPointsX, 1) + array.get(lowPointsX, 2)
sum_y_low = array.get(lowPointsY, 0) + array.get(lowPointsY, 1) + array.get(lowPointsY, 2)
sum_xy_low = array.get(lowPointsX, 0) * array.get(lowPointsY, 0) + array.get(lowPointsX, 1) * array.get(lowPointsY, 1) + array.get(lowPointsX, 2) * array.get(lowPointsY, 2)
sum_x_squared_low = array.get(lowPointsX, 0)*array.get(lowPointsX, 0) + array.get(lowPointsX, 1)*array.get(lowPointsX, 1) + array.get(lowPointsX, 2)*array.get(lowPointsX, 2)
slopeLow := (3 * sum_xy_low - sum_x_low * sum_y_low) / (3 * sum_x_squared_low - (sum_x_low*sum_x_low))
interceptLow := (sum_y_low - slopeLow * sum_x_low) / 3
// Calculate the least squares regression line for swing highs
var float slopeHigh = na
var float interceptHigh = na
if (array.size(highPointsX) == 3)
sum_x_high = array.get(highPointsX, 0) + array.get(highPointsX, 1) + array.get(highPointsX, 2)
sum_y_high = array.get(highPointsY, 0) + array.get(highPointsY, 1) + array.get(highPointsY, 2)
sum_xy_high = array.get(highPointsX, 0) * array.get(highPointsY, 0) + array.get(highPointsX, 1) * array.get(highPointsY, 1) + array.get(highPointsX, 2) * array.get(highPointsY, 2)
sum_x_squared_high = array.get(highPointsX, 0)*array.get(highPointsX, 0) + array.get(highPointsX, 1)*array.get(highPointsX, 1) + array.get(highPointsX, 2)*array.get(highPointsX, 2)
slopeHigh := (3 * sum_xy_high - sum_x_high * sum_y_high) / (3 * sum_x_squared_high - (sum_x_high*sum_x_high))
interceptHigh := (sum_y_high - slopeHigh * sum_x_high) / 3
// Plot points for 1st and 3rd swing low
var float y1low = na
var float y3low = na
if (array.size(lowPointsX) == 3)
y1low := slopeLow * array.get(lowPointsX, 0) + interceptLow
y3low := slopeLow * array.get(lowPointsX, 2) + interceptLow
// Plot lines connecting 1st and 3rd swing lows to the regression line
line.new(x1=int(array.get(lowPointsX, 0)), y1=y1low, x2=int(array.get(lowPointsX, 2)), y2=y3low, color=color.blue, width=2)
// Plot points for 1st and 3rd swing low
var float y1high = na
var float y3high = na
if (array.size(highPointsX) == 3)
y1high := slopeHigh * array.get(highPointsX, 0) + interceptHigh
y3high := slopeHigh * array.get(highPointsX, 2) + interceptHigh
// Plot lines connecting 1st and 3rd swing lows to the regression line
line.new(x1=int(array.get(highPointsX, 0)), y1=y1high, x2=int(array.get(highPointsX, 2)), y2=y3high, color=color.blue, width=2)
check the title for question, copy and paste this code in pinescript to view the indicator and trendlines that are only plotting for upto 50 bars please suggest how to correct the output and plot for all the chart history