Joker Linear Regression Channel
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Mr_Joker_Bot

//@version=5
indicator(title='Joker Linear Regression Channel', shorttitle='Joker LR', overlay = true)
src = input(title = "Source", defval = close)
len = input.int(title = "Length", defval = 100, minval = 10)
mult = input.float(title = "Deviation", defval = 2.0, minval = 0.1, step = 0.1)

lin_reg_channel(s, l) =>  // Linear Regresion Channel return: [y1, y2, dev, slope]
    mid = math.sum(s, l) / l
    slope = ta.linreg(s, l, 0) - ta.linreg(s, l, 1)
    intercept = mid - slope * math.floor(l / 2) + (1 - l % 2) / 2 * slope
    endy = intercept + slope * (l - 1)
    dev = 0.0
    for x = 0 to l - 1 by 1
        dev := dev + math.pow(s[x] - (slope * (l - x) + intercept), 2)
        dev
    dev := math.sqrt(dev / l)
    [intercept, endy, dev, slope]

[y1, y2, d, _] = lin_reg_channel(src, len)
float dev = d * mult

plot(y2 + dev, 'LR_U', color.fuchsia)
plot(y2, 'LR_M', color.fuchsia)
plot(y2 - dev, 'LR_L', color.fuchsia)

