OGT Owl Group Trading by Dr. Ken Long
Home About Learn The Loop Courses Essays Store Partners FAQ Today's Briefing
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.

Concept: what RL10 means →

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;
}