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

//@version=5

// @description TODO: add library description here
library("Trend_Channels_Lib",overlay=true)

// @function TODO: add function description here
// @param x TODO: add parameter x description here
// @returns TODO: add what function returns
export ttc(int start_index, int lookback, color color, float offset) =>
	//Channel ##
	//Equations
	H_x1 = array.new<float>()
	H_x2 = array.new<float>()
	H_y1 = array.new<float>()
	H_y2 = array.new<float>()
	H_m = array.new<float>()
	H_b = array.new<float>()
	H_lin = false
	H_env_m = array.new<float>()
	H_env_b = array.new<float>()
	H_len = 0
	H_error = array.new<float>()
	//
	//Highs Trend Line
	if bar_index == last_bar_index
		for j = start_index to start_index+lookback
			//int k = -1
			for i = start_index to start_index+lookback         
				//k := k + 1
				if j != i
					array.push(H_x1,last_bar_index-1-j)
					array.push(H_x2,last_bar_index-1-i)
					array.push(H_y1,high[j+1])
					array.push(H_y2,high[i+1])
					//line.new(last_bar_index-1-j,high[j+1],last_bar_index-1-i,high[i+1],xloc = xloc.bar_index,extend = extend.none, color= color.yellow)
		
		H_len := array.size(H_x1)

		for i = 0 to H_len-1
			slope = (array.get(H_y1,i)-array.get(H_y2,i))/(array.get(H_x1,i)-array.get(H_x2,i))
			array.push(H_m,slope)
			y_inter = array.get(H_y1,i)-array.get(H_m,i)*array.get(H_x1,i)
			array.push(H_b,y_inter)

		for i = 0 to H_len-1
			for j = start_index to start_index+lookback
				reg = array.get(H_m,i)*(last_bar_index-1-j) + array.get(H_b,i)
				if reg >= high[j+1]
					H_lin := true
				else
					H_lin := false
					break
			if H_lin == true
				u1 = last_bar_index-start_index-1
				v1 = array.get(H_m,i)*(last_bar_index-start_index-1) + array.get(H_b,i)
				u2 = last_bar_index-start_index-lookback-1
				v2 = array.get(H_m,i)*(last_bar_index-start_index-lookback-1) + array.get(H_b,i)
				slope = (v1-v2)/(u1-u2)
				y_inter = v1-slope*u1
				//
				if array.includes(H_env_m,slope) == false
					array.push(H_env_m,slope)
					array.push(H_env_b,y_inter)
				//
				//line.new(u1,v1,u2,v2,xloc = xloc.bar_index,extend = extend.none, color= color.white)
		
		H_len := array.size(H_env_m)

		//Method of Least Square
		for i = 0 to H_len-1
			err = 0.
			for j = start_index to start_index+lookback
				err := err + (array.get(H_env_m,i)*(last_bar_index-1-j) + array.get(H_env_b,i)) - high[j]
			array.push(H_error,err)

		//Minimum error
		min = array.min(H_error)
		//Index of
		ind = array.indexof(H_error,min)
		//Envelope
		u1 = last_bar_index-start_index
		v1 = array.get(H_env_m,ind)*(last_bar_index-start_index) + array.get(H_env_b,ind)
		u2 = last_bar_index-start_index-lookback-1
		v2 = array.get(H_env_m,ind)*(last_bar_index-start_index-lookback-1) + array.get(H_env_b,ind)
		//Offset
		line.new(u1,v1*(1-offset),u2,v2*(1-offset),xloc=xloc.bar_index,extend=extend.none, color=color.gray, width=1, style=line.style_dashed)
		//
		line.new(u1,v1,u2,v2,xloc=xloc.bar_index,extend=extend.none, color=color, width=2)
	//End of code

export p_ttc(int start_index, int lookback) =>
	//Channel ##
	//Equations
	H_x1 = array.new<float>()
	H_x2 = array.new<float>()
	H_y1 = array.new<float>()
	H_y2 = array.new<float>()
	H_m = array.new<float>()
	H_b = array.new<float>()
	H_lin = false
	H_env_m = array.new<float>()
	H_env_b = array.new<float>()
	H_len = 0
	H_error = array.new<float>()
	//
	//Highs Trend Line
	if bar_index == last_bar_index
		for j = start_index to start_index+lookback
			//int k = -1
			for i = start_index to start_index+lookback         
				//k := k + 1
				if j != i
					array.push(H_x1,last_bar_index-1-j)
					array.push(H_x2,last_bar_index-1-i)
					array.push(H_y1,high[j+1])
					array.push(H_y2,high[i+1])
					//line.new(last_bar_index-1-j,high[j+1],last_bar_index-1-i,high[i+1],xloc = xloc.bar_index,extend = extend.none, color= color.yellow)
		
		H_len := array.size(H_x1)

		for i = 0 to H_len-1
			slope = (array.get(H_y1,i)-array.get(H_y2,i))/(array.get(H_x1,i)-array.get(H_x2,i))
			array.push(H_m,slope)
			y_inter = array.get(H_y1,i)-array.get(H_m,i)*array.get(H_x1,i)
			array.push(H_b,y_inter)

		for i = 0 to H_len-1
			for j = start_index to start_index+lookback
				reg = array.get(H_m,i)*(last_bar_index-1-j) + array.get(H_b,i)
				if reg >= high[j+1]
					H_lin := true
				else
					H_lin := false
					break
			if H_lin == true
				u1 = last_bar_index-start_index-1
				v1 = array.get(H_m,i)*(last_bar_index-start_index-1) + array.get(H_b,i)
				u2 = last_bar_index-start_index-lookback-1
				v2 = array.get(H_m,i)*(last_bar_index-start_index-lookback-1) + array.get(H_b,i)
				slope = (v1-v2)/(u1-u2)
				y_inter = v1-slope*u1
				//
				if array.includes(H_env_m,slope) == false
					array.push(H_env_m,slope)
					array.push(H_env_b,y_inter)
				//
				//line.new(u1,v1,u2,v2,xloc = xloc.bar_index,extend = extend.none, color= color.white)
		
		H_len := array.size(H_env_m)

		//Method of Least Square
		for i = 0 to H_len-1
			err = 0.
			for j = start_index to start_index+lookback
				err := err + (array.get(H_env_m,i)*(last_bar_index-1-j) + array.get(H_env_b,i)) - high[j]
			array.push(H_error,err)

		//Minimum error
		min = array.min(H_error)
		//Index of
		ind = array.indexof(H_error,min)
		[array.get(H_env_m,ind),array.get(H_env_b,ind)]
		//Envelope
		//u1 = last_bar_index-start_index
		//v1 = array.get(H_env_m,ind)*(last_bar_index-start_index) + array.get(H_env_b,ind)
		//u2 = last_bar_index-start_index-lookback-1
		//v2 = array.get(H_env_m,ind)*(last_bar_index-start_index-lookback-1) + array.get(H_env_b,ind)
		//Offset
		//line.new(u1,v1*(1-offset),u2,v2*(1-offset),xloc=xloc.bar_index,extend=extend.none, color=color.gray, width=1, style=line.style_dashed)
		//
		//line.new(u1,v1,u2,v2,xloc=xloc.bar_index,extend=extend.none, color=color, width=2)
	//End of code

export new_ttc(int start_index, int lookback, float end_time) =>
	if time > end_time
		//Channel ##
		//Equations
		H_x1 = array.new<float>()
		H_x2 = array.new<float>()
		H_y1 = array.new<float>()
		H_y2 = array.new<float>()
		H_m = array.new<float>()
		H_b = array.new<float>()
		H_lin = false
		H_env_m = array.new<float>()
		H_env_b = array.new<float>()
		H_len = 0
		H_error = array.new<float>()
		//
		//Highs Trend Line
		for j = start_index to start_index+lookback
			//int k = -1
			for i = start_index to start_index+lookback         
				//k := k + 1
				if j != i
					array.push(H_x1,bar_index-1-j)
					array.push(H_x2,bar_index-1-i)
					array.push(H_y1,high[j+1])
					array.push(H_y2,high[i+1])
					//line.new(bar_index-1-j,high[j+1],bar_index-1-i,high[i+1],xloc = xloc.bar_index,extend = extend.none, color= color.yellow)

			H_len := array.size(H_x1)

		for i = 0 to H_len-1
			slope = (array.get(H_y1,i)-array.get(H_y2,i))/(array.get(H_x1,i)-array.get(H_x2,i))
			array.push(H_m,slope)
			y_inter = array.get(H_y1,i)-array.get(H_m,i)*array.get(H_x1,i)
			array.push(H_b,y_inter)
		//
		for i = 0 to H_len-1
			for j = start_index to start_index+lookback
				reg = array.get(H_m,i)*(bar_index-1-j) + array.get(H_b,i)
				if reg >= high[j+1]
					H_lin := true
				else
					H_lin := false
					break
			if H_lin == true
				u1 = bar_index-start_index-1
				v1 = array.get(H_m,i)*(bar_index-start_index-1) + array.get(H_b,i)
				u2 = bar_index-start_index-lookback-1
				v2 = array.get(H_m,i)*(bar_index-start_index-lookback-1) + array.get(H_b,i)
				slope = (v1-v2)/(u1-u2)
				y_inter = v1-slope*u1
				//
				if array.includes(H_env_m,slope) == false
					array.push(H_env_m,slope)
					array.push(H_env_b,y_inter)
				//
				//line.new(u1,v1,u2,v2,xloc = xloc.bar_index,extend = extend.none, color= color.white)
				//label.new(bar_index-1,high[1],text=str.tostring(H_env_m)+"\n"+str.tostring(H_env_b))
		//
		H_len := array.size(H_env_m)
		err = 0.
		//
		//Method of Least Square
		if array.size(H_env_m) > 0
			for i = 0 to H_len-1
				err := 0.
				for j = start_index to start_index+lookback
					err := err + (array.get(H_env_m,i)*(bar_index-1-j) + array.get(H_env_b,i)) - high[j]
				array.push(H_error,err)
			//label.new(bar_index-1,high[1],text=str.tostring(H_error))

		//Minimum error
		if array.size(H_env_m) > 0
			min = array.min(H_error)
			//Index of
			ind = array.indexof(H_error,min)
			//label.new(bar_index-1,high[1],text=str.tostring(ind))
			//Envelope
			//u1 = bar_index-start_index
			//v1 = array.get(H_env_m,ind)*(bar_index-start_index) + array.get(H_env_b,ind)
			//u2 = bar_index-start_index-lookback-1
			//v2 = array.get(H_env_m,ind)*(bar_index-start_index-lookback-1) + array.get(H_env_b,ind)
			//line.new(u1,v1,u2,v2,xloc=xloc.bar_index,extend=extend.none, color=color, width=2)
			//label.new(bar_index-1,high[1],text=str.tostring(v1))
			[array.get(H_env_m,ind),array.get(H_env_b,ind)]
			//End of code

export btc(int start_index, int lookback, color color, float offset) =>
	//Channel ##
	//Equations
	L_x1 = array.new<float>()
	L_x2 = array.new<float>()
	L_y1 = array.new<float>()
	L_y2 = array.new<float>()
	L_m = array.new<float>()
	L_b = array.new<float>()
	L_lin = false
	L_env_m = array.new<float>()
	L_env_b = array.new<float>()
	L_len = 0
	L_error = array.new<float>()
	//
	//Lows Trend Line
	if bar_index == last_bar_index
		for j = start_index to start_index+lookback
			int k = -1
			for i = start_index to start_index+lookback        
				k := k + 1
				if j != i
					array.push(L_x1,last_bar_index-1-j)
					array.push(L_x2,last_bar_index-1-i)
					array.push(L_y1,low[j+1])
					array.push(L_y2,low[i+1])
					//line.new(last_bar_index-1-j,high[j],last_bar_index-1-i,high[i],xloc = xloc.bar_index,extend = extend.none, color= color.yellow)
		
		L_len := array.size(L_x1)

		for i = 0 to L_len-1
			slope = (array.get(L_y1,i)-array.get(L_y2,i))/(array.get(L_x1,i)-array.get(L_x2,i))
			array.push(L_m,slope)
			y_inter = array.get(L_y1,i)-array.get(L_m,i)*array.get(L_x1,i)
			array.push(L_b,y_inter)

		for i = 0 to L_len-1
			for j = start_index to start_index+lookback
				reg = array.get(L_m,i)*(last_bar_index-1-j) + array.get(L_b,i)
				if reg <= low[j+1]
					L_lin := true
				else
					L_lin := false
					break
			if L_lin == true
				u1 = last_bar_index-start_index-1
				v1 = array.get(L_m,i)*(last_bar_index-start_index-1) + array.get(L_b,i)
				u2 = last_bar_index-start_index-lookback-1
				v2 = array.get(L_m,i)*(last_bar_index-start_index-lookback-1) + array.get(L_b,i)
				slope = (v1-v2)/(u1-u2)
				y_inter = v1-slope*u1
				//
				if array.includes(L_env_m,slope) == false
					array.push(L_env_m,slope)
					array.push(L_env_b,y_inter)
				//
				//line.new(u1,v1,u2,v2,xloc = xloc.bar_index,extend = extend.none, color= color.white)
		
		L_len := array.size(L_env_m)

		//Method of Least Square
		for i = 0 to L_len-1
			err = 0.
			for j = start_index to start_index+lookback
				err := err + low[j] - (array.get(L_env_m,i)*(last_bar_index-1-j) + array.get(L_env_b,i))
			array.push(L_error,err)

		//Minimum error
		min = array.min(L_error)
		//Index of
		ind = array.indexof(L_error,min)
		//Envelope
		u1 = last_bar_index-start_index
		v1 = array.get(L_env_m,ind)*(last_bar_index-start_index) + array.get(L_env_b,ind)
		u2 = last_bar_index-start_index-lookback-1
		v2 = array.get(L_env_m,ind)*(last_bar_index-start_index-lookback-1) + array.get(L_env_b,ind)
		//offset
		line.new(u1,v1*(1+offset),u2,v2*(1+offset),xloc=xloc.bar_index,extend=extend.none, color=color.gray, width=1, style=line.style_dashed)
		//
		line.new(u1,v1,u2,v2,xloc=xloc.bar_index,extend=extend.none, color=color, width=2)
	//End of code

export p_btc(int start_index, int lookback) =>
	//Channel ##
	//Equations
	L_x1 = array.new<float>()
	L_x2 = array.new<float>()
	L_y1 = array.new<float>()
	L_y2 = array.new<float>()
	L_m = array.new<float>()
	L_b = array.new<float>()
	L_lin = false
	L_env_m = array.new<float>()
	L_env_b = array.new<float>()
	L_len = 0
	L_error = array.new<float>()
	//
	//Lows Trend Line
	if bar_index == last_bar_index
		for j = start_index to start_index+lookback
			int k = -1
			for i = start_index to start_index+lookback        
				k := k + 1
				if j != i
					array.push(L_x1,last_bar_index-1-j)
					array.push(L_x2,last_bar_index-1-i)
					array.push(L_y1,low[j+1])
					array.push(L_y2,low[i+1])
					//line.new(last_bar_index-1-j,high[j],last_bar_index-1-i,high[i],xloc = xloc.bar_index,extend = extend.none, color= color.yellow)
		
		L_len := array.size(L_x1)

		for i = 0 to L_len-1
			slope = (array.get(L_y1,i)-array.get(L_y2,i))/(array.get(L_x1,i)-array.get(L_x2,i))
			array.push(L_m,slope)
			y_inter = array.get(L_y1,i)-array.get(L_m,i)*array.get(L_x1,i)
			array.push(L_b,y_inter)

		for i = 0 to L_len-1
			for j = start_index to start_index+lookback
				reg = array.get(L_m,i)*(last_bar_index-1-j) + array.get(L_b,i)
				if reg <= low[j+1]
					L_lin := true
				else
					L_lin := false
					break
			if L_lin == true
				u1 = last_bar_index-start_index-1
				v1 = array.get(L_m,i)*(last_bar_index-start_index-1) + array.get(L_b,i)
				u2 = last_bar_index-start_index-lookback-1
				v2 = array.get(L_m,i)*(last_bar_index-start_index-lookback-1) + array.get(L_b,i)
				slope = (v1-v2)/(u1-u2)
				y_inter = v1-slope*u1
				//
				if array.includes(L_env_m,slope) == false
					array.push(L_env_m,slope)
					array.push(L_env_b,y_inter)
				//
				//line.new(u1,v1,u2,v2,xloc = xloc.bar_index,extend = extend.none, color= color.white)
		
		L_len := array.size(L_env_m)

		//Method of Least Square
		for i = 0 to L_len-1
			err = 0.
			for j = start_index to start_index+lookback
				err := err + low[j] - (array.get(L_env_m,i)*(last_bar_index-1-j) + array.get(L_env_b,i))
			array.push(L_error,err)

		//Minimum error
		min = array.min(L_error)
		//Index of
		ind = array.indexof(L_error,min)
		[array.get(L_env_m,ind),array.get(L_env_b,ind)]
		//Envelope
		//u1 = last_bar_index-start_index
		//v1 = array.get(L_env_m,ind)*(last_bar_index-start_index) + array.get(L_env_b,ind)
		//u2 = last_bar_index-start_index-lookback-1
		//v2 = array.get(L_env_m,ind)*(last_bar_index-start_index-lookback-1) + array.get(L_env_b,ind)
		//offset
		//line.new(u1,v1*(1+offset),u2,v2*(1+offset),xloc=xloc.bar_index,extend=extend.none, color=color.gray, width=1, style=line.style_dashed)
		//
		//line.new(u1,v1,u2,v2,xloc=xloc.bar_index,extend=extend.none, color=color, width=2)
	//End of code

export new_btc(int start_index, int lookback,  float end_time) =>
	if time > end_time
		//Channel ##
		//Equations
		L_x1 = array.new<float>()
		L_x2 = array.new<float>()
		L_y1 = array.new<float>()
		L_y2 = array.new<float>()
		L_m = array.new<float>()
		L_b = array.new<float>()
		L_lin = false
		L_env_m = array.new<float>()
		L_env_b = array.new<float>()
		L_len = 0
		L_error = array.new<float>()
		//
		//Lows Trend Line
		for j = start_index to start_index+lookback
			//int k = -1
			for i = start_index to start_index+lookback         
				//k := k + 1
				if j != i
					array.push(L_x1,bar_index-1-j)
					array.push(L_x2,bar_index-1-i)
					array.push(L_y1,low[j+1])
					array.push(L_y2,low[i+1])
					//line.new(bar_index-1-j,low[j+1],bar_index-1-i,low[i+1],xloc = xloc.bar_index,extend = extend.none, color= color.yellow)

			L_len := array.size(L_x1)

		for i = 0 to L_len-1
			slope = (array.get(L_y1,i)-array.get(L_y2,i))/(array.get(L_x1,i)-array.get(L_x2,i))
			array.push(L_m,slope)
			y_inter = array.get(L_y1,i)-array.get(L_m,i)*array.get(L_x1,i)
			array.push(L_b,y_inter)
		//
		for i = 0 to L_len-1
			for j = start_index to start_index+lookback
				reg = array.get(L_m,i)*(bar_index-1-j) + array.get(L_b,i)
				if reg <= low[j+1]
					L_lin := true
				else
					L_lin := false
					break
			if L_lin == true
				u1 = bar_index-start_index-1
				v1 = array.get(L_m,i)*(bar_index-start_index-1) + array.get(L_b,i)
				u2 = bar_index-start_index-lookback-1
				v2 = array.get(L_m,i)*(bar_index-start_index-lookback-1) + array.get(L_b,i)
				slope = (v1-v2)/(u1-u2)
				y_inter = v1-slope*u1
				//
				if array.includes(L_env_m,slope) == false
					array.push(L_env_m,slope)
					array.push(L_env_b,y_inter)
				//
				//line.new(u1,v1,u2,v2,xloc = xloc.bar_index,extend = extend.none, color= color.white)
				//label.new(bar_index-1,high[1],text=str.tostring(H_env_m)+"\n"+str.tostring(H_env_b))
		//
		L_len := array.size(L_env_m)
		err = 0.
		//
		//Method of Least Square
		if array.size(L_env_m) > 0
			for i = 0 to L_len-1
				err := 0.
				for j = start_index to start_index+lookback
					err := err + low[j] - (array.get(L_env_m,i)*(bar_index-1-j) + array.get(L_env_b,i))
				array.push(L_error,err)
			//label.new(bar_index-1,high[1],text=str.tostring(H_error))

		//Minimum error
		if array.size(L_env_m) > 0
			min = array.min(L_error)
			//Index of
			ind = array.indexof(L_error,min)
			//label.new(bar_index-1,high[1],text=str.tostring(ind))
			//Envelope
			//u1 = bar_index-start_index
			//v1 = array.get(L_env_m,ind)*(bar_index-start_index) + array.get(L_env_b,ind)
			//u2 = bar_index-start_index-lookback-1
			//v2 = array.get(L_env_m,ind)*(bar_index-start_index-lookback-1) + array.get(L_env_b,ind)
			//line.new(u1,v1,u2,v2,xloc=xloc.bar_index,extend=extend.none, color=color, width=2)
			//label.new(bar_index-1,high[1],text=str.tostring(v1))
			[array.get(L_env_m,ind), array.get(L_env_b,ind)]
			//End of code

export param_ttc(int start_index, int lookback) =>
	//Channel ##
	//Equations
	H_x1 = array.new<float>()
	H_x2 = array.new<float>()
	H_y1 = array.new<float>()
	H_y2 = array.new<float>()
	H_m = array.new<float>()
	H_b = array.new<float>()
	H_lin = false
	H_env_m = array.new<float>()
	H_env_b = array.new<float>()
	H_len = 0
	H_error = array.new<float>()
	//
	//Highs Trend Line
	if bar_index == last_bar_index
		for j = start_index to start_index+lookback
			//int k = -1
			for i = start_index to start_index+lookback         
				//k := k + 1
				if j != i
					array.push(H_x1,last_bar_index-1-j)
					array.push(H_x2,last_bar_index-1-i)
					array.push(H_y1,high[j+1])
					array.push(H_y2,high[i+1])
					//line.new(last_bar_index-1-j,high[j+1],last_bar_index-1-i,high[i+1],xloc = xloc.bar_index,extend = extend.none, color= color.yellow)
		
		H_len := array.size(H_x1)

		for i = 0 to H_len-1
			slope = (array.get(H_y1,i)-array.get(H_y2,i))/(array.get(H_x1,i)-array.get(H_x2,i))
			array.push(H_m,slope)
			y_inter = array.get(H_y1,i)-array.get(H_m,i)*array.get(H_x1,i)
			array.push(H_b,y_inter)

		for i = 0 to H_len-1
			for j = start_index to start_index+lookback
				reg = array.get(H_m,i)*(last_bar_index-1-j) + array.get(H_b,i)
				if reg >= high[j+1]
					H_lin := true
				else
					H_lin := false
					break
			if H_lin == true
				u1 = last_bar_index-start_index-1
				v1 = array.get(H_m,i)*(last_bar_index-start_index-1) + array.get(H_b,i)
				u2 = last_bar_index-start_index-lookback-1
				v2 = array.get(H_m,i)*(last_bar_index-start_index-lookback-1) + array.get(H_b,i)
				slope = (v1-v2)/(u1-u2)
				y_inter = v1-slope*u1
				//
				array.push(H_env_m,slope)
				array.push(H_env_b,y_inter)
				//
				//line.new(u1,v1,u2,v2,xloc = xloc.bar_index,extend = extend.none, color= color.white)
		
		H_len := array.size(H_env_m)

		//Method of Least Square
		for i = 0 to H_len-1
			err = 0.
			for j = start_index to start_index+lookback
				err := err + (array.get(H_env_m,i)*(last_bar_index-1-j) + array.get(H_env_b,i)) - high[j]
			array.push(H_error,err)

		//Minimum error
		min = array.min(H_error)
		//Index of
		ind = array.indexof(H_error,min)
		//Envelope
		[array.get(H_env_m,ind),array.get(H_env_b,ind)]
	//End of code

export param_btc(int start_index, int lookback) =>
	//Channel ##
	//Equations
	L_x1 = array.new<float>()
	L_x2 = array.new<float>()
	L_y1 = array.new<float>()
	L_y2 = array.new<float>()
	L_m = array.new<float>()
	L_b = array.new<float>()
	L_lin = false
	L_env_m = array.new<float>()
	L_env_b = array.new<float>()
	L_len = 0
	L_error = array.new<float>()
	//
	//Lows Trend Line
	if bar_index == last_bar_index
		for j = start_index to start_index+lookback
			int k = -1
			for i = start_index to start_index+lookback        
				k := k + 1
				if j != i
					array.push(L_x1,last_bar_index-1-j)
					array.push(L_x2,last_bar_index-1-i)
					array.push(L_y1,low[j+1])
					array.push(L_y2,low[i+1])
					//line.new(last_bar_index-1-j,high[j],last_bar_index-1-i,high[i],xloc = xloc.bar_index,extend = extend.none, color= color.yellow)
		
		L_len := array.size(L_x1)

		for i = 0 to L_len-1
			slope = (array.get(L_y1,i)-array.get(L_y2,i))/(array.get(L_x1,i)-array.get(L_x2,i))
			array.push(L_m,slope)
			y_inter = array.get(L_y1,i)-array.get(L_m,i)*array.get(L_x1,i)
			array.push(L_b,y_inter)

		for i = 0 to L_len-1
			for j = start_index to start_index+lookback
				reg = array.get(L_m,i)*(last_bar_index-1-j) + array.get(L_b,i)
				if reg <= low[j+1]
					L_lin := true
				else
					L_lin := false
					break
			if L_lin == true
				u1 = last_bar_index-start_index-1
				v1 = array.get(L_m,i)*(last_bar_index-start_index-1) + array.get(L_b,i)
				u2 = last_bar_index-start_index-lookback-1
				v2 = array.get(L_m,i)*(last_bar_index-start_index-lookback-1) + array.get(L_b,i)
				slope = (v1-v2)/(u1-u2)
				y_inter = v1-slope*u1
				//
				array.push(L_env_m,slope)
				array.push(L_env_b,y_inter)
				//
				//line.new(u1,v1,u2,v2,xloc = xloc.bar_index,extend = extend.none, color= color.white)
		
		L_len := array.size(L_env_m)

		//Method of Least Square
		for i = 0 to L_len-1
			err = 0.
			for j = start_index to start_index+lookback
				err := err + low[j] - (array.get(L_env_m,i)*(last_bar_index-1-j) + array.get(L_env_b,i))
			array.push(L_error,err)

		//Minimum error
		min = array.min(L_error)
		//Index of
		ind = array.indexof(L_error,min)
		//Envelope
		[array.get(L_env_m,ind),array.get(L_env_b,ind)]
	//End of code