Quadratic Regression
"Quadratic Regression" by alexgrover, MPL-2.0. Ported to FloScript by FloSignals; modifications under MPL-2.0. Original: Quadratic Regression (v1.0), read 2026-11-15T18:35:07.936Z; 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.
Quadratic Regression 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-07-07; 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: Quadratic Regression
// 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("Quadratic Regression", overlay=true)
length = input.int(54, maxval=54)
offset = input.int(27, maxval=55)
src = input(close)
show = input(true, "Show R2")
//------------------------------------------------------------------------------
n = bar_index
Var(x) => ta.variance(x, length)
Cov(x, y) => ta.sma(x * y, length) - ta.sma(x, length) * ta.sma(y, length)
//------------------------------------------------------------------------------
sma = ta.sma(src, length)
varn = (math.pow(length, 2) - 1) / 12
norm = Var(n * n) * varn - math.pow(Cov(n, n * n), 2)
a = (Cov(n * n, src) * varn - Cov(n, src) * Cov(n, n * n)) / norm
b = (Cov(n, src) * Var(n * n) - Cov(n * n, src) * Cov(n, n * n)) / norm
c = sma - a * ta.sma(n * n, length) - b * ta.sma(n, length)
Q(x) => a * math.pow(n + x, 2) + b * (n + x) + c
//------------------------------------------------------------------------------
sse = 0.
sst = 0.
for i = 0 to length - 1
sse := sse + math.pow(src[i] - Q(-i), 2)
sst := sst + math.pow(src[i] - sma, 2)
r2 = 1 - sse / sst
//------------------------------------------------------------------------------
line l = na
dt = math.round(time - time[1])
for i = 1 - offset to 54 - offset
css = math.abs(i) > length ? na : #2157f3
l := line.new(time + dt * i, Q(i), time + dt * (i + 1), Q(i + 1), xloc=xloc.bar_time, color=css, width=2)
line.delete(l[1])
//------------------------------------------------------------------------------
label la = na
min = math.min(offset, length + 1)
transp = color.new(color.white, 100)
if show
la := label.new(n[min], Q(-min), str.tostring(r2, "#.###"), color=transp, style=label.style_label_up, textcolor=#2157f3)
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.