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

//@version=5


// @description : this show slope based on linear regression
// @param : "a" must input Source, [close, open, high, low]//
// @param : "b" the length about period.

library("Linear_Regression_Slope", overlay = false)
export linreg_slope(float a, int b) =>
	src = a, len = b
	float sum_x = 0, 
	float sum_y = 0, 
	float sum_xy = 0, 
	float sum_x2 = 0

	for i = 0 to len -1
		sum_x := sum_x + i
    	sum_y := sum_y + src[i]
    	sum_xy := sum_xy + i * src[i]
    	sum_x2 := sum_x2 + i * i

	result = (len * sum_xy - sum_x * sum_y) / (len * sum_x2 - sum_x * sum_x)
	result