Optimized Linear Regression Channel
"Optimized Linear Regression Channel" by alexgrover, MPL-2.0. Ported to FloScript by FloSignals; modifications under MPL-2.0. Original: Optimized Linear Regression Channel (v1.0), read 2026-11-15T18:34:34.880Z; source language v4.
Licence: Mozilla Public License 2.0 (MPL-2.0) — full text. The source is published with it, modifications included, under the same licence.
Optimized Linear Regression Channel by alexgrover, ported from Pine v4. Overlay indicator with drawings and 4 inputs.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// (Notice added by FloSignals: the author's source carries none. It was published open-source on 2020-08-10; since 2019-07-26 the platform it was published on has released such a script under MPL-2.0 automatically.)
// This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License https://creativecommons.org/licenses/by-sa/4.0/
// © alexgrover
// ------------------------------------------------------------------------------------------------
// Original author: alexgrover
// Original title: Optimized Linear Regression Channel
// Licence: MPL-2.0 (the notice above was added by FloSignals; the original carries none)
// Pine version: 4
// Fetched: 2026-11-15
// Ported to FloScript by FloSignals; modifications under MPL-2.0.
// Converter: @flosignals/floscript 0.1.0, strict mode, score 100/100
// ------------------------------------------------------------------------------------------------
//@floscript=1
//@version=5
// Converted by the FloScript converter from a Pine-compatible v4 source.
indicator("Optimized Linear Regression Channel", overlay=true)
min = input.int(14, minval=4)
max = input(28)
mult = input(2)
src = input(close)
//------------------------------------------------------------------------------
cmla = ta.cum(src)
cmlb = ta.cum(cmla[1])
cmlc = ta.cum(src * src)
//------------------------------------------------------------------------------
r(p) =>
sum = cmlb - cmlb[p]
a = p * cmla - sum
b = cmla - cmla[p]
c = cmlc - cmlc[p]
//----
num = (a - b * (p + 1) / 2) / p
vary = c / p - math.pow(b / p, 2)
varx = (p * p - 1) / 12
math.pow(num / math.sqrt(vary * varx), 2)
//------------------------------------------------------------------------------
k = 0.
p = 0
for i = min to max
c = r(i)
k := math.max(c, k)
p := k == c ? i : p
//------------------------------------------------------------------------------
den = p * (p + 1) / 2
wma = (p * cmla - (cmlb - cmlb[p])) / den
sma = (cmla - cmla[p]) / p
A = 4 * sma - 3 * wma
B = 3 * wma - 2 * sma
//------------------------------------------------------------------------------
Var = (cmlc - cmlc[p]) / p - math.pow((cmla - cmla[p]) / p, 2)
rmse = math.sqrt(Var - r(p) * Var) * mult
//----
n = bar_index
var line ln = na
var line up = na
var line dn = na
var label la = na
transparent = color.new(color.white, 100)
if n > min
ln := line.new(n[p] + 1, A, n, B, extend=extend.right, color=#ff9800, width=2)
up := line.new(n[p] + 1, A + rmse, n, B + rmse, extend=extend.right, color=#2157f3, width=2)
dn := line.new(n[p] + 1, A - rmse, n, B - rmse, extend=extend.right, color=#2157f3, width=2)
la := label.new(n[p] + 1, A - rmse, str.tostring(p), color=transparent, style=label.style_label_up, textcolor=#2157f3)
line.delete(ln[1])
line.delete(up[1])
line.delete(dn[1])
label.delete(la[1])
This file is under the Mozilla Public License 2.0; it is a port of the original by alexgrover, with the original licence header kept and the changes made for FloScript noted in the file.
- v1
Library refresh 2026-11-15
Disclaimer. Scripts are written and published by members, or ported by FloSignals from open-source originals, and are their authors' own work and opinion. FloSignals does not review them for correctness and nothing in a script is financial advice or a recommendation to buy or sell anything — read the Risk Disclaimer. Posting here is under the Community Rules; a script that is not the poster's to post can be reported as the Copyright & Takedowns page says.

Comments (0)
No comments yet. Members can leave one.