Linear Regression (All Data)
"Linear Regression (All Data)" by everget, MPL-2.0. Ported to FloScript by FloSignals; modifications under MPL-2.0. Original: Linear Regression (All Data) (v4.0), read 2026-11-15T18:29:09.793Z; 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.
Linear Regression (All Data) by everget, ported from Pine v4. Overlay indicator with drawings and 1 input.
// 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-07-29; since 2019-07-26 the platform it was published on has released such a script under MPL-2.0 automatically.)
// Copyright (c) 2020-present, Alex Orekhov (everget)
// Linear Regression (All Data) script may be freely distributed under the terms of the GPL-3.0 license.
// ------------------------------------------------------------------------------------------------
// Original author: everget
// Original title: Linear Regression (All Data)
// 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.
var int MAX_BARS_BACK = 5000
indicator("Linear Regression (All Data)", overlay=true, max_bars_back=MAX_BARS_BACK)
n = bar_index + 1
X = n
Y = input(title="Source", defval=close)
sumX = ta.cum(X)
sumY = ta.cum(Y)
sumX2 = ta.cum(math.pow(X, 2))
sumY2 = ta.cum(math.pow(Y, 2))
sumXY = ta.cum(X * Y)
// Pearson correlation coefficient
r = (n * sumXY - sumX * sumY) / math.sqrt((n * sumY2 - math.pow(sumY, 2)) * (n * sumX2 - math.pow(sumX, 2)))
// Coefficient of determination
r2 = math.pow(r, 2)
meanX = sumX / n
meanY = sumY / n
slope = (n * sumXY - sumX * sumY) / (n * sumX2 - math.pow(sumX, 2))
intercept = meanY - slope * meanX
int startIndex = n > MAX_BARS_BACK ? int(n - MAX_BARS_BACK) : 0
startY = slope * startIndex + intercept
endY = slope * (n - 1) + intercept
var line regLine = na
if barstate.islast
if na(regLine)
regLine := line.new(startIndex, startY, bar_index, endY, width=2, extend=extend.none, color=color.orange)
else
line.set_xy1(regLine, startIndex, startY)
line.set_xy2(regLine, bar_index, endY)
na
//------------------------------------------------------------------------
// Formatter
_format(value, decimals) =>
out = str.tostring(value, "#.#")
out := decimals == 2 ? str.tostring(value, "#.##") : decimals == 3 ? str.tostring(value, "#.###") : decimals == 4 ? str.tostring(value, "#.####") : decimals == 5 ? str.tostring(value, "#.#####") : decimals == 6 ? str.tostring(value, "#.######") : decimals == 7 ? str.tostring(value, "#.#######") : decimals == 8 ? str.tostring(value, "#.########") : decimals == 9 ? str.tostring(value, "#.#########") : decimals == 10 ? str.tostring(value, "#.##########") : decimals == 11 ? str.tostring(value, "#.###########") : decimals == 12 ? str.tostring(value, "#.############") : decimals == 13 ? str.tostring(value, "#.#############") : decimals == 14 ? str.tostring(value, "#.##############") : decimals == 15 ? str.tostring(value, "#.###############") : decimals == 16 ? str.tostring(value, "#.################") : out
out
summaryText = " Slope: " + _format(slope, 6) + "\n Intercept: " + _format(intercept, 6) + "\n r: " + _format(r, 3) + "\n r2: " + _format(r2, 3)
var label summaryLabel = label.new(x=time, y=na, xloc=xloc.bar_time, text="", textalign=text.align_left, textcolor=color.white, color=#1e90ff, style=label.style_label_left, size=size.small)
label.set_text(summaryLabel, summaryText)
label.set_xy(summaryLabel, time + 10 * (time - time[1]), endY)
This file is under the Mozilla Public License 2.0; it is a port of the original by everget, 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.