JavaScript · Indicator Code
RL10 - Regression Line in JavaScript
The fitted endpoint of a 10-bar linear regression - a smoothed read of true price that filters candle noise.
Verified. Python and JavaScript implementations agree to
7.11e-14 on a 60-bar reference price series (canonical Python (np.correlate) vs JavaScript (loop) - float64 round-off only).JavaScript
// RL10 - the fitted value of an n-bar linear regression at the most recent
// bar (the regression-line 'endpoint'). Canonical Owl Group Trading
// implementation. Returns an array the same length as `closes`; the first
// n-1 entries are null (not enough lookback). n defaults to 10, hence 'RL10'.
export function rl10(closes, n = 10) {
const len = closes.length;
const result = new Array(len).fill(null);
if (n <= 0 || len < n) return result;
// Fixed sums for x = [0, 1, ..., n-1]
const sumX = (n * (n - 1)) / 2;
const sumX2 = (n * (n - 1) * (2 * n - 1)) / 6;
const denom = n * sumX2 - sumX * sumX;
if (denom === 0) return result;
for (let end = n; end <= len; end++) {
const start = end - n;
let sumY = 0, sumXY = 0, hasNaN = false;
for (let j = 0; j < n; j++) {
const v = closes[start + j];
if (v == null || Number.isNaN(v)) { hasNaN = true; break; }
sumY += v;
sumXY += j * v;
}
if (hasNaN) continue;
// slope + intercept of the least-squares line, evaluated at x = n-1
const slope = (n * sumXY - sumX * sumY) / denom;
const intercept = (sumY - slope * sumX) / n;
result[end - 1] = slope * (n - 1) + intercept;
}
return result;
}
Other platforms: Python
← RL10 - Regression Line (all platforms) · All indicators · Glossary concept
← RL10 - Regression Line (all platforms) · All indicators · Glossary concept
Improve Your Craft Every Morning
Daily commentary from Dr. Ken Long — what he's seeing in markets, how he's framing trades, and what's worth practicing today. Free.
Your email:
Tue–Fri mornings. Unsubscribe anytime. No spam, no hype.