Library/Geometric Chart Patterns/Level Confluence and Zone Scoring/Fibonacci Retracement and Extension Projection

D08-F06-A03 / Complete engineering topic

Fibonacci Retracement and Extension Projection

A production-minded guide to Fibonacci Retracement and Extension Projection.

Fibonacci Retracement and Extension ProjectionD08 / D08-F06

Project transparent linear-price Fibonacci levels without hiding anchor selection or implying a forecast.

The decision this tutorial makes visible

For charting engineers and analysts, a Fibonacci ratio is not a price level until anchors, direction, price scale, ratio set, and tick-rounding policy are frozen.

The precise question is: What retracement and extension prices follow from explicit anchors, declared ratios, direction, and tick rounding?

A practitioner needs to know what the diagnostic does and does not justify. A builder needs a contract that can be reproduced from the same point-in-time inputs in Python, TypeScript, a visual, and a browser lab.

Intuition before notation

Ratios only scale an explicitly chosen price leg; the anchors and price scale carry as much meaning as the ratio list.

The result depends on the declared algorithm scope, input clocks, units, equality and rounding policies, and unsupported-state treatment. Change one of those and the output represents a different decision even when its field name is unchanged.

Scope and nearby methods

Two-anchor retracements and three-anchor trend-based extensions on a linear price scale with nearest-tick output.

VariantDefinitionBest useMain limitation
Canonical repository conventionTwo-anchor retracements and three-anchor trend-based extensions on a linear price scale with nearest-tick output.Reproducible teaching and reference implementationRequires calibration before empirical use
Platform-specific implementationVendor-selected defaults, data resolution, and tiesParity with one named platformNot portable without a parity specification
Discretionary chart annotationHuman-selected levels and contextExploratory reviewNot reproducible without a written rubric

What is sourced, selected, synthetic, and derived

RoleMaterial claimEvidenceBoundary
Sourced nearby tool conventionDocuments the common two-extreme-point retracement construction and commonly displayed ratios.S1 exact source recordAutomatic pivot selection and any expectation of a market reaction are outside the package contract.
Sourced nearby three-anchor conventionDocuments a start anchor, end anchor, and retracement-end anchor for trend-based extensions.S2 exact source recordThe repository freezes linear-price arithmetic and explicit tick rounding; log-scale projection is excluded.
Primary computational technical-analysis researchShows why subjective chart concepts require explicit computational definitions and separates pattern recognition from statistical evaluation.S3 exact source recordIt does not define this family's zone algorithms or establish current out-of-sample economic value.
Implementation choiceTwo-anchor retracements and three-anchor trend-based extensions on a linear price scale with nearest-tick output.Frozen definition contract, formula, fixtures, and parity testsThis is the package convention, not a universal definition.
Synthetic teaching inputEvery displayed price, trade, profile, touch, bar, zone, and instrument snapshot is repository-authored.datasets/canonical-input.json and scenario-results.jsonNo value is presented as an observed security or provider record.
Author-derived calculationThe canonical fixture produces five retracement and four extension prices with both raw and tick-rounded values.Formula, expected output, examples, and cross-language parityCalculation correctness does not establish predictive or economic value.

The authoritative sources support only the exact facts named in the claim ledger. They do not certify the synthetic numbers in this tutorial. The repository fixture is deliberately invented for auditability, and the displayed output is author-derived under the selected implementation choice.

Formula, symbols, and numerical policy

Plain text
R_r = B - d r |B-A|; E_e = C + d e |B-A|, where d = sign(B-A)
SymbolMeaningUnitPolicy
Aleg startpriceexplicit anchor
Bleg endpriceexplicit anchor
Cretracement endpriceextension anchor
r/eretracement/extension ratioratiodeclared list
dleg directionsign+1 or -1
  • Prices and tolerances share one currency, adjustment basis, and tick table.
  • raw projected prices are snapped to the nearest declared instrument tick.
  • Published values use full-precision calculation and explicit display rounding; no hidden epsilon changes a decision.
  • Ties are broken deterministically and preserved in the audit output.

Read the formula in the same order as the algorithm. Validate identity, ordering, units, and supported state first. Apply the selected equality and window rules second. Calculate with unrounded numeric values. Round only at the declared presentation boundary, and preserve null as a diagnostic rather than coercing it to zero.

Build the algorithm

  1. Validate distinct leg anchors and positive tick size
  2. Resolve leg direction and absolute magnitude
  3. Project each declared retracement from B
  4. Project each declared extension from C
  5. Snap raw prices half away from zero and retain both values

Production-minded operational checklist

  1. Persist anchor timestamps and the linear/log scale policy with every projected level set.
  2. Partition by instrument, venue/session, currency, interval, and adjustment basis.
  3. Persist code version, source records, inputs, output diagnostics, and tie decisions.
  4. Evaluate empirical association separately with costs, leakage controls, and out-of-sample data.

The checklist is intentionally strict: an explicit rejection is safer than a plausible output built from stale, malformed, or unsupported state.

Worked synthetic example

The canonical fixture is synthetic teaching data, not an observed control event, customer order, or broker execution. Its primary author-derived output, retracement_levels, is five retracement and four extension prices with both raw and tick-rounded values. The complete input and output are in datasets/canonical-input.json and datasets/expected-output.json.

The labeled synthetic canonical fixture applies R_r = B - d r |B-A|; E_e = C + d e |B-A|, where d = sign(B-A) and returns five retracement and four extension prices with both raw and tick-rounded values. The expected JSON is generated once from the frozen contract and independently checked in both language implementations.

Counterfactual checkpoint

Fibonacci Retracement and Extension Projection boundary case. Move either anchor and every projected price changes; the ratios alone never define a level. The output changes because Reproducibility requires counterfactual behavior at the boundary.

The structured result retains state and diagnostics in addition to the primary number. That makes the calculation independently reviewable and prevents a partial, null, rejected, or venue-bounded outcome from being mistaken for an unqualified value.

Boundary and counterexample workbook

The playground computes every scenario at 61 deterministic parameter states. The table uses the declared focus step and states whether that focus reproduces the canonical fixture. The full state ledger and compressed transition segments are in datasets/scenario-results.json.

ScenarioReview focusPurposeStatePrimary outputDiagnosticDecision segments
Canonical anchor-leg sweepStep 30 · canonical fixtureMove anchor B around the exact canonical leg while retaining the midpoint fixture.calculated61.8% R 107.60 · 161.8% E 144.40State calculated; 61.8% R 107.60 · 161.8% E 144.40.1
Tick-rounding staircaseStep 30 · comparison focusMove anchor B over a narrow range so the raw 61.8% level crosses nearest-tick boundaries.calculated61.8% R 107.50 · 161.8% E 143.70State calculated; 61.8% R 107.50 · 161.8% E 143.70.1
Descending price legStep 30 · comparison focusReverse A and B to verify that retracement and extension direction reverses.calculated61.8% R 112.40 · 161.8% E 75.60State calculated; 61.8% R 112.40 · 161.8% E 75.60.1
Coarse tick gridStep 30 · comparison focusUse a 0.50 tick to separate continuous raw projections from tradable output levels.calculated61.8% R 107.50 · 161.8% E 144.50State calculated; 61.8% R 107.50 · 161.8% E 144.50.1
Sparse ratio setStep 30 · comparison focusRetain only 50%, 61.8%, 100%, and 161.8% levels while moving the leg.calculated61.8% R 107.60 · 161.8% E 144.40State calculated; 61.8% R 107.60 · 161.8% E 144.40.1
Third-anchor sensitivityStep 30 · comparison focusHold A and B fixed and move C to isolate extension sensitivity from retracements.calculated61.8% R 107.60 · 161.8% E 142.40State calculated; 61.8% R 107.60 · 161.8% E 142.40.1
Wide anchor stressStep 30 · comparison focusMove B over a broad valid ascending range without allowing A and B to coincide.calculated61.8% R 109.60 · 161.8% E 152.40State calculated; 61.8% R 109.60 · 161.8% E 152.40.1

These rows are not backtest observations. They are controlled counterexamples that expose how one driver changes the state, output, or reason code while the rest of the contract stays fixed.

Visualize the boundary

Fibonacci Retracement and Extension Projection annotated teaching map

Open this SVG at full size, or use the guided playground to compare the seven topic-specific canonical, boundary, policy, and failure scenarios.

The Mermaid flow answers where the selected calculation sits in the processing sequence. The SVG keeps the formula, output, decision boundary, and invariant visible together. The lab lets the reader step through the same structured states without changing the underlying definition.

Implementation walkthrough

The Python and TypeScript references begin with the same validation contract, reject malformed and unsupported state before calculation, preserve declared ordering and rounding policies, and return structured diagnostics rather than one context-free number.

The main implementation branches are:

  • Input or clock contract fails — Reject, because A precise output would be misleading.
  • raw projected prices are snapped to the nearest declared instrument tick — Apply the declared equality/tie policy, because Boundary behavior must be reproducible.
  • Valid evidence does not satisfy the algorithm — Return a valid rejection, empty set, or nonconfirmed state, because Absence is different from invalid data.
  • All required conditions pass — Return the structured result and diagnostics, because The frozen contract is satisfied.

Neither reference silently fetches data, mutates caller-owned inputs outside the declared engine behavior, guesses hidden state, or substitutes a provider default. Shared JSON fixtures make value, null, state, and reason-code drift visible across languages.

Testing and validation

Definition tests compare every canonical field, reject malformed state, and exercise the material boundary. Family validation recomputes every playground state from the reference function. Independent arithmetic is recorded beside the fixture rather than inferred only from implementation output.

The audit must preserve these invariants:

  • Return a named state rather than a bare scalar when decisions are sequential.
  • Preserve source IDs, members, expansion order, component scores, or state transitions where applicable.
  • Keep point-in-time cutoffs and selected parameters beside the output.
  • Separate invalid input from a valid nonmatch, rejection, or empty eligible result.

Passing definition and parity checks proves that the implementation matches the selected contract. It does not prove production performance, universal applicability, or a later market outcome.

Failure modes and misuse

  • Automatic pivot selection and logarithmic-price projection are separate variants and can materially change the levels.
  • Platform and analyst conventions can produce different results from the same chart.
  • A recognized level interaction is not evidence of future association or economic value.
  • The output is educational analytics, not investment advice or an order instruction.

Debugging order

When a result looks surprising, inspect the state in this order:

  1. Confirm identifiers, scope, side, and decision clock.
  2. Confirm units, ordering, and point-in-time inputs.
  3. Confirm equality, rounding, null, and reset policies.
  4. Recalculate the invariant and declared scenario focus before changing code.

Evidence and historical boundary

Historical decision: not useful. A named chart occurrence would add narrative appeal without improving reproducibility. Classification changes with provider, session, adjustment basis, tick table, anchors, thresholds, and the point-in-time record available to the detector.

The primary sources are TradingView Auto Fib Retracement, TradingView trend-based Fib Extension, Lo–Mamaysky–Wang. They support the source roles listed in the research ledger, not a redistributable historical observation, a private participant decision, production conformance certification, execution-quality result, profitability claim, or prediction claim.

Summary and next topic

You can now implement, inspect, and validate Fibonacci Retracement and Extension Projection with its full topic-specific audit evidence. The learning flow is: Point of Control, Value Area, HVN, and LVN Detection → Fibonacci Retracement and Extension Projection → Psychological Round-Number Level Generation. Carry the result forward only with its scope, clock, state, and evidence label.

Fibonacci Retracement and Extension Projection calculation flow

This flow identifies the selected calculation stages and the structured output.

Rendering system map…

Takeaway: The anchors and scale define the geometry; ratios only scale that declared leg, and raw and snapped levels must remain distinct.

ReferencesPrimary sources and evidence notes

Expand the source trail, evidence role, and limitations behind the engineering choices.

S1 — Auto Fib Retracement

  • Organization or authors: TradingView
  • Source type: Official maintained platform documentation
  • Publication or effective date: Current page accessed 2026-08-03
  • Version: Online documentation
  • URL or DOI: https://www.tradingview.com/support/solutions/43000585089-auto-fib-retracement/
  • Accessed: 2026-08-03
  • Jurisdiction: Platform convention; not market-specific
  • Supports: Documents the common two-extreme-point retracement construction and commonly displayed ratios.
  • Limitations: Automatic pivot selection and any expectation of a market reaction are outside the package contract.

S2 — Trend-based Fib Extension drawing tool

  • Organization or authors: TradingView
  • Source type: Official maintained platform documentation
  • Publication or effective date: Current page accessed 2026-08-03
  • Version: Online documentation
  • URL or DOI: https://www.tradingview.com/support/solutions/43000518137-trend-based-fib-extension-drawing-tool/
  • Accessed: 2026-08-03
  • Jurisdiction: Platform convention; not market-specific
  • Supports: Documents a start anchor, end anchor, and retracement-end anchor for trend-based extensions.
  • Limitations: The repository freezes linear-price arithmetic and explicit tick rounding; log-scale projection is excluded.

S3 — Foundations of Technical Analysis: Computational Algorithms, Statistical Inference, and Empirical Implementation

  • Organization or authors: Andrew W. Lo, Harry Mamaysky, and Jiang Wang
  • Source type: Original NBER working paper and Journal of Finance study
  • Publication or effective date: Working Paper 7613, March 2000; accessed 2026-08-03
  • Version: NBER Working Paper 7613
  • URL or DOI: https://www.nber.org/papers/w7613
  • Accessed: 2026-08-03
  • Jurisdiction: U.S. equity sample described by the paper
  • Supports: Shows why subjective chart concepts require explicit computational definitions and separates pattern recognition from statistical evaluation.
  • Limitations: It does not define this family's zone algorithms or establish current out-of-sample economic value.

Evidence boundary

The sources establish the exact rule, interface, protocol, or research context named above. They do not verify the repository-authored synthetic fixture, thresholds, empirical usefulness, execution probability, or profitability. Package-selected choices remain labeled as implementation choices wherever they are used.

level-confluence.ts
/** Reference algorithms for D08-F06. Repository teaching conventions only. */

type Row = Record<string, any>;

function numberValue(value: unknown, name: string, minimum?: number): number {
  if (typeof value !== "number" || !Number.isFinite(value)) throw new TypeError(`${name} must be a finite number`);
  if (minimum !== undefined && value < minimum) throw new RangeError(`${name} must be >= ${minimum}`);
  return value;
}

function integerValue(value: unknown, name: string, minimum = 0): number {
  const result = numberValue(value, name);
  if (!Number.isInteger(result) || result < minimum) throw new RangeError(`${name} must be an integer >= ${minimum}`);
  return result;
}

function timeValue(value: unknown, name: string): number {
  if (typeof value !== "string" || !/(Z|[+-]\d\d:\d\d)$/.test(value)) throw new TypeError(`${name} must be a zoned ISO-8601 string`);
  const result = Date.parse(value);
  if (!Number.isFinite(result)) throw new RangeError(`${name} must be a zoned ISO-8601 string`);
  return result;
}

function rounded(value: number): number { return Number(value.toFixed(12)); }

function alignedTicks(price: unknown, tick: number, name: string): number {
  const value = numberValue(price, name);
  const ticks = Math.round(value / tick);
  if (Math.abs(value - ticks * tick) > tick * 1e-8) throw new RangeError(`${name} must align to tick_size`);
  return ticks;
}

function median(values: number[]): number {
  const ordered = [...values].sort((a, b) => a - b);
  const middle = Math.floor(ordered.length / 2);
  return ordered.length % 2 ? ordered[middle] : (ordered[middle - 1] + ordered[middle]) / 2;
}

function profile(input: Row): Row {
  const tick = numberValue(input.tick_size, "tick_size", 1e-12);
  const widthTicks = integerValue(input.bin_size_ticks, "bin_size_ticks", 1);
  const windowEnd = timeValue(input.window_end, "window_end");
  if (!Array.isArray(input.trades) || input.trades.length === 0) throw new RangeError("trades must be non-empty");
  const seen = new Set<string>();
  const volumes = new Map<number, number>();
  let previous: number | null = null, total = 0, eligible = 0;
  input.trades.forEach((trade: Row, index: number) => {
    if (!trade || typeof trade !== "object") throw new TypeError("each trade must be an object");
    if (typeof trade.trade_id !== "string" || !trade.trade_id || seen.has(trade.trade_id)) throw new RangeError("trade_id must be unique");
    seen.add(trade.trade_id);
    if (trade.final !== true) throw new RangeError("all trades must be final");
    const timestamp = timeValue(trade.timestamp, `trades[${index}].timestamp`);
    if (timestamp > windowEnd || (previous !== null && timestamp < previous)) throw new RangeError("trades must be ordered and available");
    previous = timestamp;
    const priceTicks = alignedTicks(trade.price, tick, `trades[${index}].price`);
    const volume = numberValue(trade.volume, `trades[${index}].volume`, 0);
    if (volume === 0) return;
    eligible += 1; total += volume;
    const binIndex = Math.floor(priceTicks / widthTicks);
    volumes.set(binIndex, (volumes.get(binIndex) ?? 0) + volume);
  });
  if (total <= 0) throw new RangeError("eligible volume must be positive");
  const rows = [...volumes.keys()].sort((a, b) => a - b).map(binIndex => {
    const lowerTicks = binIndex * widthTicks, lower = lowerTicks * tick, upper = (lowerTicks + widthTicks) * tick;
    const volume = volumes.get(binIndex)!;
    return { bin_index: binIndex, lower: rounded(lower), upper: rounded(upper), midpoint: rounded((lower + upper) / 2), volume: rounded(volume), share: rounded(volume / total) };
  });
  return { state: "calculated", tick_size: tick, bin_size_ticks: widthTicks, bin_width: rounded(widthTicks * tick), total_volume: rounded(total), trade_count: input.trades.length, eligible_trade_count: eligible, rows };
}

function profileFeatures(input: Row): Row {
  if (!Array.isArray(input.profile_rows) || input.profile_rows.length < 3) throw new RangeError("profile_rows must contain at least three rows");
  const fraction = numberValue(input.value_area_fraction, "value_area_fraction");
  const hvnRatio = numberValue(input.hvn_median_ratio, "hvn_median_ratio", 1);
  const lvnRatio = numberValue(input.lvn_median_ratio, "lvn_median_ratio");
  if (!(fraction > 0 && fraction <= 1) || lvnRatio < 0 || lvnRatio > 1) throw new RangeError("fractions outside canonical range");
  let previousUpper: number | null = null;
  const rows = input.profile_rows.map((row: Row, index: number) => {
    const lower = numberValue(row.lower, `profile_rows[${index}].lower`), upper = numberValue(row.upper, `profile_rows[${index}].upper`), volume = numberValue(row.volume, `profile_rows[${index}].volume`, 0);
    if (upper <= lower || (previousUpper !== null && Math.abs(lower - previousUpper) > 1e-9)) throw new RangeError("profile rows must be ordered and contiguous");
    previousUpper = upper; return { lower, upper, midpoint: (lower + upper) / 2, volume };
  });
  const total = rows.reduce((sum: number, row: Row) => sum + row.volume, 0);
  if (total <= 0) throw new RangeError("profile volume must be positive");
  const profileMidpoint = (rows[0].lower + rows[rows.length - 1].upper) / 2;
  const maximum = Math.max(...rows.map((row: Row) => row.volume));
  const candidates = rows.map((row: Row, i: number) => row.volume === maximum ? i : -1).filter((i: number) => i >= 0);
  candidates.sort((a: number, b: number) => Math.abs(rows[a].midpoint - profileMidpoint) - Math.abs(rows[b].midpoint - profileMidpoint) || rows[a].midpoint - rows[b].midpoint);
  const pocIndex = candidates[0], included = new Set<number>([pocIndex]), trace = [pocIndex];
  let cumulative = rows[pocIndex].volume, low = pocIndex, high = pocIndex;
  const target = total * fraction;
  while (cumulative < target && (low > 0 || high < rows.length - 1)) {
    const below = low > 0 ? low - 1 : null, above = high < rows.length - 1 ? high + 1 : null;
    let chosen: number;
    if (below === null) chosen = above!; else if (above === null) chosen = below; else chosen = rows[above].volume >= rows[below].volume ? above : below;
    included.add(chosen); cumulative += rows[chosen].volume; low = Math.min(low, chosen); high = Math.max(high, chosen); trace.push(chosen);
  }
  const positiveMedian = median(rows.filter((row: Row) => row.volume > 0).map((row: Row) => row.volume));
  const hvn: Row[] = [], lvn: Row[] = [];
  for (let i = 1; i < rows.length - 1; i += 1) {
    const current = rows[i].volume, item = { price: rounded(rows[i].midpoint), volume: rounded(current), median_ratio: rounded(current / positiveMedian) };
    if (current > rows[i - 1].volume && current >= rows[i + 1].volume && current >= positiveMedian * hvnRatio) hvn.push(item);
    if (current < rows[i - 1].volume && current <= rows[i + 1].volume && current <= positiveMedian * lvnRatio) lvn.push(item);
  }
  return { state: "calculated", poc_index: pocIndex, poc_price: rounded(rows[pocIndex].midpoint), poc_volume: rounded(maximum), value_area_low: rounded(rows[low].lower), value_area_high: rounded(rows[high].upper), value_area_volume: rounded(cumulative), value_area_share: rounded(cumulative / total), target_share: fraction, included_indices: [...included].sort((a, b) => a - b), expansion_trace: trace, hvn, lvn };
}

function fibonacci(input: Row): Row {
  const start = numberValue(input.start_price, "start_price"), end = numberValue(input.end_price, "end_price"), correction = numberValue(input.retracement_end_price, "retracement_end_price"), tick = numberValue(input.tick_size, "tick_size", 1e-12);
  if (start === end || !Array.isArray(input.retracement_ratios) || !input.retracement_ratios.length || !Array.isArray(input.extension_ratios) || !input.extension_ratios.length) throw new RangeError("invalid anchors or ratios");
  const rr = input.retracement_ratios.map((v: unknown) => numberValue(v, "retracement ratio")), er = input.extension_ratios.map((v: unknown) => numberValue(v, "extension ratio"));
  if (rr.some((v: number) => v < 0 || v > 1) || er.some((v: number) => v < 0)) throw new RangeError("ratios outside canonical ranges");
  const direction = end > start ? 1 : -1, magnitude = Math.abs(end - start), snap = (value: number) => {
    const tickUnits = value / tick, roundedUnits = Math.sign(tickUnits) * Math.floor(Math.abs(tickUnits) + 0.5);
    return rounded(roundedUnits * tick);
  };
  return { state: "calculated", direction: direction > 0 ? "up" : "down", leg_size: rounded(magnitude), retracement_levels: rr.map((ratio: number) => ({ ratio, raw_price: rounded(end - direction * ratio * magnitude), price: snap(end - direction * ratio * magnitude) })), extension_levels: er.map((ratio: number) => ({ ratio, raw_price: rounded(correction + direction * ratio * magnitude), price: snap(correction + direction * ratio * magnitude) })), rounding: "nearest tick, half away from zero" };
}

function roundNumbers(input: Row): Row {
  const current = numberValue(input.current_price, "current_price"), lower = numberValue(input.lower_bound, "lower_bound"), upper = numberValue(input.upper_bound, "upper_bound"), tick = numberValue(input.tick_size, "tick_size", 1e-12), unit = numberValue(input.base_unit, "base_unit", tick);
  if (lower > current || current > upper || lower >= upper) throw new RangeError("invalid bounds");
  const unitTicks = alignedTicks(unit, tick, "base_unit"), lowerTicks = Math.ceil(lower / tick - 1e-9), upperTicks = Math.floor(upper / tick + 1e-9), first = Math.ceil(lowerTicks / unitTicks) * unitTicks;
  const levels: Row[] = [];
  for (let priceTicks = first; priceTicks <= upperTicks; priceTicks += unitTicks) {
    const multiple = Math.round(priceTicks / unitTicks), major = multiple % 10 === 0, half = multiple % 5 === 0, price = priceTicks * tick, weight = major ? 1 : half ? 0.75 : 0.5;
    levels.push({ price: rounded(price), class: major ? "major" : half ? "half" : "minor", salience_weight: weight, distance: rounded(Math.abs(price - current)), distance_bps: current === 0 ? null : rounded(Math.abs(price - current) / Math.abs(current) * 10000) });
  }
  if (!levels.length) throw new RangeError("bounds contain no levels");
  const closest = [...levels].sort((a, b) => a.distance - b.distance || b.salience_weight - a.salience_weight || a.price - b.price)[0];
  return { state: "calculated", base_unit: unit, level_count: levels.length, closest_level: closest, levels };
}

function fusion(input: Row): Row {
  if (!Array.isArray(input.levels) || !input.levels.length) throw new RangeError("levels must be non-empty");
  const tolerance = numberValue(input.fusion_tolerance, "fusion_tolerance", 0), minimumSources = integerValue(input.minimum_sources, "minimum_sources", 1), seen = new Set<string>();
  const parsed = input.levels.map((level: Row, index: number) => {
    if (typeof level.level_id !== "string" || !level.level_id || seen.has(level.level_id) || typeof level.source !== "string" || !level.source) throw new RangeError("level identity/source invalid");
    seen.add(level.level_id); return { level_id: level.level_id, source: level.source, price: numberValue(level.price, `levels[${index}].price`), weight: numberValue(level.weight, `levels[${index}].weight`, 0) };
  }).sort((a: Row, b: Row) => a.price - b.price || a.source.localeCompare(b.source) || a.level_id.localeCompare(b.level_id));
  const clusters: Row[][] = [];
  parsed.forEach((level: Row) => { if (!clusters.length || level.price - clusters[clusters.length - 1][0].price > 2 * tolerance) clusters.push([level]); else clusters[clusters.length - 1].push(level); });
  const zones: Row[] = [], rejected_clusters: Row[] = [];
  clusters.forEach((cluster, index) => {
    const sources = [...new Set(cluster.map(row => row.source))].sort(), weightSum = cluster.reduce((sum, row) => sum + row.weight, 0), center = weightSum > 0 ? cluster.reduce((sum, row) => sum + row.price * row.weight, 0) / weightSum : cluster.reduce((sum, row) => sum + row.price, 0) / cluster.length;
    const item = { zone_id: `Z${String(index + 1).padStart(2, "0")}`, lower: rounded(Math.min(...cluster.map(row => row.price)) - tolerance), upper: rounded(Math.max(...cluster.map(row => row.price)) + tolerance), center: rounded(center), source_count: sources.length, sources, weight_sum: rounded(weightSum), member_ids: cluster.map(row => row.level_id) };
    if (sources.length >= minimumSources) zones.push(item); else rejected_clusters.push({ ...item, reason: "insufficient-distinct-sources" });
  });
  zones.sort((a, b) => b.source_count - a.source_count || b.weight_sum - a.weight_sum || a.center - b.center);
  return { state: "calculated", zones, rejected_clusters, fusion_tolerance: tolerance, minimum_sources: minimumSources };
}

function strength(input: Row): Row {
  const source = numberValue(input.source_confluence, "source_confluence"), age = integerValue(input.zone_age_bars, "zone_age_bars"), halfLife = numberValue(input.half_life_bars, "half_life_bars", 1e-12), breakCount = integerValue(input.break_count, "break_count"), rejectionTarget = numberValue(input.rejection_target_atr, "rejection_target_atr", 1e-12);
  if (source < 0 || source > 1 || !Array.isArray(input.touches)) throw new RangeError("invalid source or touches");
  const decayed = input.touches.map((touch: Row, index: number) => { const touchAge = integerValue(touch.age_bars, `touches[${index}].age_bars`), rejection = numberValue(touch.rejection_atr, `touches[${index}].rejection_atr`, 0); return [0.5 ** (touchAge / halfLife), Math.min(rejection / rejectionTarget, 1)]; });
  const evidence = decayed.reduce((sum: number, value: number[]) => sum + value[0], 0), sourceComponent = 30 * source, touchComponent = 35 * (1 - Math.exp(-evidence)), rejectionComponent = evidence === 0 ? 0 : 25 * decayed.reduce((sum: number, value: number[]) => sum + value[0] * value[1], 0) / evidence, durabilityComponent = 10 * (0.5 ** (age / halfLife)), penalty = Math.min(30, 15 * breakCount), score = Math.max(0, Math.min(100, sourceComponent + touchComponent + rejectionComponent + durabilityComponent - penalty));
  return { state: "calculated", score: rounded(score), grade: score >= 75 ? "strong" : score >= 50 ? "moderate" : score >= 25 ? "weak" : "depleted", components: { source: rounded(sourceComponent), touch: rounded(touchComponent), rejection: rounded(rejectionComponent), durability: rounded(durabilityComponent), break_penalty: rounded(penalty) }, decayed_touch_evidence: rounded(evidence), touch_count: input.touches.length };
}

function roleReversal(input: Row): Row {
  const lower = numberValue(input.zone_lower, "zone_lower"), upper = numberValue(input.zone_upper, "zone_upper"), buffer = numberValue(input.break_buffer, "break_buffer", 0), confirmations = integerValue(input.confirmation_closes, "confirmation_closes", 1);
  if (lower >= upper || !["support", "resistance"].includes(input.initial_role) || !Array.isArray(input.closes) || !input.closes.length) throw new RangeError("invalid role-reversal input");
  const values = input.closes.map((value: unknown) => numberValue(value, "close")); let role = input.initial_role, state = `active-${role}`, outsideCount = 0, retestSeen = false; const transitions: Row[] = [];
  values.forEach((close: number, index: number) => {
    const side = close > upper + buffer ? "above" : close < lower - buffer ? "below" : "zone", previous = state;
    if (state === "active-support") { outsideCount = side === "below" ? outsideCount + 1 : 0; if (outsideCount >= confirmations) { state = "awaiting-resistance-retest"; outsideCount = 0; } }
    else if (state === "active-resistance") { outsideCount = side === "above" ? outsideCount + 1 : 0; if (outsideCount >= confirmations) { state = "awaiting-support-retest"; outsideCount = 0; } }
    else if (state === "awaiting-resistance-retest") { if (side === "zone") retestSeen = true; else if (retestSeen && side === "below") { state = "confirmed-resistance"; role = "resistance"; } else if (side === "above") state = "invalidated"; }
    else if (state === "awaiting-support-retest") { if (side === "zone") retestSeen = true; else if (retestSeen && side === "above") { state = "confirmed-support"; role = "support"; } else if (side === "below") state = "invalidated"; }
    if (state !== previous) transitions.push({ index, close, from: previous, to: state, side });
  });
  return { state, final_role: state === "invalidated" ? "none" : role, confirmed: state.startsWith("confirmed-"), retest_seen: retestSeen, transitions, observations: values.length };
}

function breakoutRetest(input: Row): Row {
  const lower = numberValue(input.zone_lower, "zone_lower"), upper = numberValue(input.zone_upper, "zone_upper"), buffer = numberValue(input.break_buffer, "break_buffer", 0), tolerance = numberValue(input.retest_tolerance, "retest_tolerance", 0), needed = integerValue(input.breakout_closes, "breakout_closes", 1), maxBars = integerValue(input.max_retest_bars, "max_retest_bars", 1), direction = input.direction;
  if (lower >= upper || !["up", "down"].includes(direction) || !Array.isArray(input.bars) || !input.bars.length) throw new RangeError("invalid breakout input");
  let state = "searching", count = 0, breakout_index: number | null = null, retest_index: number | null = null, confirmation_index: number | null = null; const transitions: Row[] = [];
  input.bars.forEach((bar: Row, index: number) => {
    const high = numberValue(bar.high, "bar.high"), low = numberValue(bar.low, "bar.low"), close = numberValue(bar.close, "bar.close");
    if (high < Math.max(low, close) || low > Math.min(high, close)) throw new RangeError("inconsistent bar");
    const previous = state, beyond = direction === "up" ? close > upper + buffer : close < lower - buffer, failed = direction === "up" ? close < lower - buffer : close > upper + buffer, contact = low <= upper + tolerance && high >= lower - tolerance;
    if (state === "searching" || state === "breakout-pending") { count = beyond ? count + 1 : 0; state = count ? "breakout-pending" : "searching"; if (count >= needed) { breakout_index = index; state = "awaiting-retest"; } }
    else if (state === "awaiting-retest") { if (breakout_index !== null && index - breakout_index > maxBars) state = "expired"; else if (failed) state = "failed"; else if (contact) { retest_index = index; state = "retest-contact"; } }
    else if (state === "retest-contact") { if (failed) state = "failed"; else if (beyond) { confirmation_index = index; state = "confirmed"; } else if (breakout_index !== null && index - breakout_index > maxBars) state = "expired"; }
    if (state !== previous) transitions.push({ index, from: previous, to: state, close });
  });
  return { state, confirmed: state === "confirmed", direction, breakout_index, retest_index, confirmation_index, transitions };
}

function scanner(input: Row): Row {
  const asOf = timeValue(input.as_of, "as_of"), maxBps = numberValue(input.max_distance_bps, "max_distance_bps", 0), halfLife = numberValue(input.freshness_half_life_hours, "freshness_half_life_hours", 1e-12);
  if (!Array.isArray(input.instruments) || !input.instruments.length) throw new RangeError("instruments must be non-empty");
  const seen = new Set<string>(), ranked: Row[] = [];
  input.instruments.forEach((item: Row, index: number) => {
    if (typeof item.instrument_id !== "string" || !item.instrument_id || seen.has(item.instrument_id)) throw new RangeError("instrument_id must be unique"); seen.add(item.instrument_id);
    const price = numberValue(item.current_price, `instruments[${index}].current_price`, 1e-12), lower = numberValue(item.zone_lower, "zone_lower"), upper = numberValue(item.zone_upper, "zone_upper"), zoneStrength = numberValue(item.zone_strength, "zone_strength"), observed = timeValue(item.observed_at, "observed_at");
    if (lower >= upper || zoneStrength < 0 || zoneStrength > 100 || observed > asOf) throw new RangeError("invalid point-in-time zone record");
    const distance = price < lower ? lower - price : price > upper ? price - upper : 0, distanceBps = distance / price * 10000, ageHours = (asOf - observed) / 3600000, freshness = 0.5 ** (ageHours / halfLife), proximity = maxBps > 0 ? Math.max(0, 1 - distanceBps / maxBps) : distanceBps === 0 ? 1 : 0, score = 100 * (0.55 * proximity + 0.30 * zoneStrength / 100 + 0.15 * freshness);
    if (distanceBps <= maxBps) ranked.push({ instrument_id: item.instrument_id, current_price: price, zone_lower: lower, zone_upper: upper, inside_zone: distance === 0, distance: rounded(distance), distance_bps: rounded(distanceBps), strength: zoneStrength, freshness: rounded(freshness), rank_score: rounded(score) });
  });
  ranked.sort((a, b) => b.rank_score - a.rank_score || a.distance_bps - b.distance_bps || a.instrument_id.localeCompare(b.instrument_id)); ranked.forEach((row, index) => { row.rank = index + 1; });
  return { state: "calculated", as_of: input.as_of, eligible_count: ranked.length, ranked };
}

const calculators: Record<string, (input: Row) => Row> = {
  "D08-F06-A01": profile,
  "D08-F06-A02": profileFeatures,
  "D08-F06-A03": fibonacci,
  "D08-F06-A04": roundNumbers,
  "D08-F06-A05": fusion,
  "D08-F06-A06": strength,
  "D08-F06-A07": roleReversal,
  "D08-F06-A08": breakoutRetest,
  "D08-F06-A09": scanner,
};

export function calculate(topicId: string, input: Row): Row {
  const calculator = calculators[topicId];
  if (!calculator) throw new RangeError(`unsupported topic_id: ${topicId}`);
  if (!input || typeof input !== "object" || Array.isArray(input)) throw new TypeError("inputs must be an object");
  return calculator(input);
}
Full-height labplaygroundOpen full screen