This original implementation uses the public least-squares definition above. Blue plots expose completed-bar endpoint values. Orange line segments approximate the most recent completed window's fitted curve at bar positions and are replaced on the next completed bar. The orange drawing is descriptive and deliberately revises history; it supplies no backtest entries. The example contains no alerts, orders or forward extension.
Pine Script v6. The final source passed the Pineify MCP syntax checker. Syntax success does not verify runtime chart behavior or trading performance.pine
//@version=6
indicator("Polynomial channel: endpoints and latest fit", overlay = true, max_bars_back = 100, max_lines_count = 500)
int n = input.int(7, "Window", minval = 3, maxval = 100)
int degree = input.int(2, "Degree", minval = 1, maxval = 3)
float k = input.float(2.0, "Residual multiplier", minval = 0.0)
float src = input.source(close, "Source")
bool fixture = input.bool(false, "Use repeating 14-bar test fixture")
bool drawWindow = input.bool(true, "Draw latest completed window")
if n <= degree + 1
runtime.error("Window must exceed degree + 1 for positive residual degrees of freedom.")
if fixture and n > 14
runtime.error("Test fixture requires Window <= 14.")
var array<float> samples = array.from(100.0, 101.0, 101.0, 104.0, 106.0, 110.0, 113.0, 111.0, 108.0, 106.0, 105.0, 103.0, 102.0, 104.0)
int phase = bar_index % 14
float y = fixture ? samples.get(phase) : src
int p = degree + 1
var matrix<float> design = matrix.new<float>(n, p, 0.0)
var matrix<float> pseudo = matrix.new<float>(p, n, 0.0)
if barstate.isfirst
for i = 0 to n - 1
float x = -1.0 + 2.0 * i / (n - 1)
for j = 0 to degree
design.set(i, j, math.pow(x, j))
pseudo := design.pinv()
bool ready = bar_index >= n - 1 and (not fixture or phase >= n - 1)
for i = 0 to n - 1
ready := ready and not na(y[i])
float center = na
float scale = na
float sse = na
matrix<float> fitted = matrix.new<float>(n, 1, na)
if ready
// Center y to reduce cancellation for large price offsets.
float base = y[n - 1]
matrix<float> values = matrix.new<float>(n, 1, 0.0)
for i = 0 to n - 1
values.set(i, 0, y[n - 1 - i] - base)
matrix<float> beta = pseudo.mult(values)
fitted := design.mult(beta)
sse := 0.0
for i = 0 to n - 1
float residual = values.get(i, 0) - fitted.get(i, 0)
sse += residual * residual
fitted.set(i, 0, fitted.get(i, 0) + base)
center := fitted.get(n - 1, 0)
scale := math.sqrt(sse / (n - p))
// Confirmed endpoint plots retain each bar's own trailing fit.
bool show = ready and barstate.isconfirmed
plot(show ? center : na, "Confirmed endpoint", color.blue, 2, plot.style_linebr)
upper = plot(show ? center + k * scale : na, "Endpoint upper", color.new(color.blue, 55), style = plot.style_linebr)
lower = plot(show ? center - k * scale : na, "Endpoint lower", color.new(color.blue, 55), style = plot.style_linebr)
fill(upper, lower, color.new(color.blue, 93))
plot(show ? sse : na, "Window SSE", display = display.data_window)
plot(show ? scale : na, "Residual scale", display = display.data_window)
plot(fixture ? phase : na, "Fixture index", display = display.data_window)
plot(fixture ? y : na, "Fixture value", display = display.data_window)
plot(show ? fitted.get(n - 2, 0) : na, "Previous coordinate in current fit", display = display.data_window)
// Orange drawing is replaced after each completed bar; it revises old coordinates.
var array<line> segments = array.new<line>()
bool refresh = barstate.islastconfirmedhistory or (barstate.islast and barstate.isconfirmed)
if refresh
for segment in segments
line.delete(segment)
array.clear(segments)
if drawWindow and show
for i = 1 to n - 1
int left = bar_index - n + i
float a = fitted.get(i - 1, 0)
float b = fitted.get(i, 0)
array.push(segments, line.new(left, a, left + 1, b, color = color.orange, width = 2))
array.push(segments, line.new(left, a + k * scale, left + 1, b + k * scale, color = color.orange, style = line.style_dashed))
array.push(segments, line.new(left, a - k * scale, left + 1, b - k * scale, color = color.orange, style = line.style_dashed))