<!DOCTYPE html>
<html lang="zh-Hant">
<head>
<meta charset="utf-8">
<title>A16 — V2 指標規格與雙語實作封存：Liquidity Sweeps [LuxAlgo]</title>
<style>
  body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; line-height: 1.6; max-width: 1080px; margin: 28px auto; padding: 0 20px; color: #1b1f23; background: #fafbfc; }
  h1, h2, h3 { color: #0f2942; border-bottom: 1px solid #e1e4e8; padding-bottom: 4px; }
  .meta, .box { background: #fff; border: 1px solid #d0d7de; border-radius: 6px; padding: 14px 18px; margin: 14px 0; }
  pre { background: #0d1117; color: #e6edf3; padding: 14px; border-radius: 6px; overflow-x: auto; font-size: 12.5px; line-height: 1.45; }
  code { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
  table { border-collapse: collapse; width: 100%; margin: 12px 0; background: #fff; }
  th, td { border: 1px solid #d0d7de; padding: 8px 10px; text-align: left; vertical-align: top; }
  th { background: #f3f4f6; }
</style>
</head>
<body>
<h1>V2 指標封存文件（算法＋編碼）— Liquidity Sweeps [LuxAlgo]</h1>
<div class="meta">
  <strong>定調來源：</strong>HOST seq=65053 (dj_host1) ｜ <strong>席位：</strong>A16 (A168146) ｜ <strong>狀態：</strong>V2 規格封存完成（待後續 server 改版接插）<br>
  <strong>起源帖文：</strong><code>https://x.com/foodloss_otoko/status/2123461381030834442</code>（作者 <code>べーた @foodloss_otoko</code>）<br>
  <strong>官方 Pine v5 源碼：</strong>TradingView <code>PUB;26391b932d3145879b67deca62305c9a</code>（160 行，5,793 B，SHA256 <code>18be09f34ebf371e4cc8997ba77c29cc1ada4612a5529a79994694ec9b6ba12d</code>）<br>
  <strong>Node.js 參考模組：</strong><code>v2_liquidity_sweeps.cjs</code>（SHA256 <code>891335a7dc208d05c62ac4a6deba3e795e05c50847aa679ddd930c93cb41a324</code>，Node <code>v20.20.2</code> 自測 PASS）
</div>

<h2>一、V2 指標算法與輸入定義</h2>
<div class="box">
  <ol>
    <li><strong>理論基礎：</strong>Wyckoff Spring/Upthrust &rarr; ICT/SMC 流動性掃蕩與 SFP &rarr; Bill Williams 對稱樞軸點（<code>ta.pivothigh(len, len)</code> / <code>ta.pivotlow(len, len)</code>）&rarr; LuxAlgo 狀態機（<code>brk / mit / tak / wic</code>）＋ <code>Sweep Area</code> 方框緩解追蹤。</li>
    <li><strong>唯一所需市場數據：</strong>單週期普通 K 線序列 <code>{ h, l, c }</code> 及 <code>bar_index</code>（<strong>不用</strong> <code>open</code>、<code>volume</code>、<code>time</code>、盤口 L2、逐筆或 <code>request.security</code>）。</li>
    <li><strong>預設參數：</strong><code>len = 5</code>（左右各 5 根確認波段極值）、<code>opt = 'Only Wicks'</code>（另有 <code>'Only Outbreaks &amp; Retest'</code> 與 <code>'Wicks + Outbreaks &amp; Retest'</code>）、<code>extend = true</code>、<code>maxB = 300</code>、樞軸最長保留 <code>2000</code> 根。</li>
    <li><strong>核心狀態轉移公式（第 <code>n</code> 根）：</strong>
      <ul>
        <li><code>ph_n = H_{n-len}</code> 若嚴格大於左右各 <code>len</code> 根（延遲 <code>len</code> 根確認寫入 <code>aPivH</code>）；<code>pl_n = L_{n-len}</code> 對稱寫入 <code>aPivL</code>。</li>
        <li><strong>Only Wicks 模式（預設）：</strong>
          <ul>
            <li>空頭掃蕩（BSL）：<code>H_n &gt; P</code> 且 <code>C_n &lt; P</code>（建看跌 Sweep Area <code>[P, H_n]</code>，標 <code>wic=true</code>）；若 <code>C_n &gt; P</code> 則設 <code>mit=true</code> 退役。</li>
            <li>多頭掃蕩（SSL）：<code>L_n &lt; P</code> 且 <code>C_n &gt; P</code>（建看漲 Sweep Area <code>[L_n, P]</code>，標 <code>wic=true</code>）；若 <code>C_n &lt; P</code> 則設 <code>mit=true</code> 退役。</li>
          </ul>
        </li>
        <li><strong>Outbreaks &amp; Retest 模式：</strong>若先收盤破位（<code>C_n &gt; P</code> 對 Pivot High；<code>C_n &lt; P</code> 對 Pivot Low）設 <code>brk=true</code>；日後回測掃蕩（<code>L_n &lt; P &amp;&amp; C_n &gt; P</code> 或對稱）標 <code>tak=true</code>；若再度收盤穿透則 <code>mit=true</code>。</li>
        <li><strong>Sweep Area 延伸與緩解：</strong><code>n - n_trig &lt;= maxB</code> 時右緣跟隨 <code>n</code>；看漲框若 <code>C_n &lt; bottom</code> 停止延伸（<code>br=true</code>），看跌框若 <code>C_n &gt; top</code> 停止延伸（<code>br=true</code>）。</li>
        <li><strong>接插 server/策略層時之交易規則（依 <code>@foodloss_otoko</code>）：</strong>必須以已收盤 K 線（<code>barstate.isconfirmed</code> 語義）確認實體收回 <code>P</code> 內側才進場，止損置於掃蕩影線極值（<code>slOutsideWick</code>）之外。</li>
      </ul>
    </li>
  </ol>
</div>

<h2>二、Node.js v20.20.2 參考實作（<code>v2_liquidity_sweeps.cjs</code>，SHA256: <code>891335a7dc208d05c62ac4a6deba3e795e05c50847aa679ddd930c93cb41a324</code>）</h2>
<pre><code>&#x27;use strict&#x27;;

/**
 * V2 Indicator Reference Implementation: Liquidity Sweeps [LuxAlgo]
 * Source-matched 1:1 to TradingView PUB;26391b932d3145879b67deca62305c9a
 * (Pine Script v5, 160 lines, sha256 18be09f34ebf371e4cc8997ba77c29cc1ada4612a5529a79994694ec9b6ba12d)
 *
 * Pure single-timeframe K-line inputs: { h, l, c } (+ bar_index).
 * Zero open/volume/time/orderbook/external feeds required.
 */

const MODES = Object.freeze({
  ONLY_WICKS: &#x27;Only Wicks&#x27;,
  ONLY_OUTBREAKS_RETEST: &#x27;Only Outbreaks &amp; Retest&#x27;,
  BOTH: &#x27;Wicks + Outbreaks &amp; Retest&#x27;,
});

function isPivotHigh(bars, n, len) {
  if (n &lt; 2 * len) return null;
  const center = n - len;
  const val = bars[center].h;
  if (!Number.isFinite(val)) return null;
  for (let k = center - len; k &lt;= n; k++) {
    if (k === center) continue;
    if (!(val &gt; bars[k].h)) return null;
  }
  return val;
}

function isPivotLow(bars, n, len) {
  if (n &lt; 2 * len) return null;
  const center = n - len;
  const val = bars[center].l;
  if (!Number.isFinite(val)) return null;
  for (let k = center - len; k &lt;= n; k++) {
    if (k === center) continue;
    if (!(val &lt; bars[k].l)) return null;
  }
  return val;
}

function computeLiquiditySweepsV2(bars, opts = {}) {
  const len = Number.isInteger(opts.len) &amp;&amp; opts.len &gt;= 1 ? opts.len : 5;
  const mode = opts.opt || MODES.ONLY_WICKS;
  const extend = opts.extend !== undefined ? Boolean(opts.extend) : true;
  const maxB = Number.isInteger(opts.maxB) &amp;&amp; opts.maxB &gt;= 1 &amp;&amp; opts.maxB &lt;= 5000 ? opts.maxB : 300;
  const maxPivAge = 2000;

  const oW = mode === MODES.ONLY_WICKS;
  const oO = mode === MODES.ONLY_OUTBREAKS_RETEST;

  const aPivH = []; // { prc, bix, brk, mit, tak, wic }
  const aPivL = [];
  const aBoxBr = []; // { left, right, top, bottom, br, dr, triggerBar, style }
  const events = [];

  for (let n = 0; n &lt; bars.length; n++) {
    const bar = bars[n];
    const high = Number(bar.h);
    const low = Number(bar.l);
    const close = Number(bar.c);
    if (!Number.isFinite(high) || !Number.isFinite(low) || !Number.isFinite(close)) {
      throw new Error(`Invalid OHLC bar at index ${n}`);
    }

    const ph = isPivotHigh(bars, n, len);
    const pl = isPivotLow(bars, n, len);

    if (ph !== null) {
      aPivH.unshift({ prc: ph, bix: n - len, brk: false, mit: false, tak: false, wic: false });
    }
    if (pl !== null) {
      aPivL.unshift({ prc: pl, bix: n - len, brk: false, mit: false, tak: false, wic: false });
    }

    // Pivot High state machine (Buy-Side Liquidity levels)
    for (let i = aPivH.length - 1; i &gt;= 0; i--) {
      const get = aPivH[i];
      if (!get.mit) {
        if (!get.brk) {
          if (close &gt; get.prc) {
            if (!oW) get.brk = true;
            else get.mit = true;
          }
          if (!oO &amp;&amp; !get.wic) {
            if (high &gt; get.prc &amp;&amp; close &lt; get.prc) {
              const bx = {
                left: n - 1,
                right: n + 1,
                top: high,
                bottom: get.prc,
                br: false,
                dr: 1,
                triggerBar: n,
                pivotBar: get.bix,
                pivotPrice: get.prc,
                kind: &#x27;BEAR_WICK_SWEEP&#x27;,
                lineStyle: &#x27;dotted&#x27;,
                oppositeGuideY: low,
                slOutsideWick: high,
              };
              aBoxBr.unshift(bx);
              events.push(bx);
              get.wic = true;
            }
          }
        } else {
          if (close &lt; get.prc) {
            get.mit = true;
          }
          if (!oW &amp;&amp; low &lt; get.prc &amp;&amp; close &gt; get.prc) {
            const bx = {
              left: n - 1,
              right: n + 1,
              top: get.prc,
              bottom: low,
              br: false,
              dr: -1,
              triggerBar: n,
              pivotBar: get.bix,
              pivotPrice: get.prc,
              kind: &#x27;BULL_RETEST_SWEEP&#x27;,
              lineStyle: &#x27;dashed&#x27;,
              oppositeGuideY: high,
              slOutsideWick: low,
            };
            aBoxBr.unshift(bx);
            events.push(bx);
            get.tak = true;
          }
        }
      }
      if (n - get.bix &gt; maxPivAge || get.mit || get.tak) {
        aPivH.splice(i, 1);
      }
    }

    // Pivot Low state machine (Sell-Side Liquidity levels)
    for (let i = aPivL.length - 1; i &gt;= 0; i--) {
      const get = aPivL[i];
      if (!get.mit) {
        if (!get.brk) {
          if (close &lt; get.prc) {
            if (!oW) get.brk = true;
            else get.mit = true;
          }
          if (!oO &amp;&amp; !get.wic) {
            if (low &lt; get.prc &amp;&amp; close &gt; get.prc) {
              const bx = {
                left: n - 1,
                right: n + 1,
                top: get.prc,
                bottom: low,
                br: false,
                dr: -1,
                triggerBar: n,
                pivotBar: get.bix,
                pivotPrice: get.prc,
                kind: &#x27;BULL_WICK_SWEEP&#x27;,
                lineStyle: &#x27;dotted&#x27;,
                oppositeGuideY: high,
                slOutsideWick: low,
              };
              aBoxBr.unshift(bx);
              events.push(bx);
              get.wic = true;
            }
          }
        } else {
          if (close &gt; get.prc) {
            get.mit = true;
          }
          if (!oW &amp;&amp; high &gt; get.prc &amp;&amp; close &lt; get.prc) {
            const bx = {
              left: n - 1,
              right: n + 1,
              top: high,
              bottom: get.prc,
              br: false,
              dr: 1,
              triggerBar: n,
              pivotBar: get.bix,
              pivotPrice: get.prc,
              kind: &#x27;BEAR_RETEST_SWEEP&#x27;,
              lineStyle: &#x27;dashed&#x27;,
              oppositeGuideY: low,
              slOutsideWick: high,
            };
            aBoxBr.unshift(bx);
            events.push(bx);
            get.tak = true;
          }
        }
      }
      if (n - get.bix &gt; maxPivAge || get.mit || get.tak) {
        aPivL.splice(i, 1);
      }
    }

    // Sweep Area box extension &amp; mitigation
    if (extend) {
      for (let j = 0; j &lt; aBoxBr.length; j++) {
        const bx = aBoxBr[j];
        if (!bx.br &amp;&amp; n - bx.left - 1 &lt;= maxB) {
          bx.right = n;
          if (bx.dr === -1 &amp;&amp; close &lt; bx.bottom) bx.br = true;
          if (bx.dr === 1 &amp;&amp; close &gt; bx.top) bx.br = true;
        }
      }
    }
  }

  return {
    params: { len, opt: mode, extend, maxB, maxPivAge },
    events,
    boxes: aBoxBr,
    activePivotHighs: aPivH.length,
    activePivotLows: aPivL.length,
  };
}

function runSelfTest() {
  const assert = require(&#x27;assert&#x27;);
  // Construct 18 bars (len=2):
  // Bar 2: Pivot High = 110 (confirmed at bar 4)
  // Bar 5: Bearish Wick Sweep (h=112 &gt; 110, c=108 &lt; 110)
  // Bar 6: Mitigation of Sweep Area (c=113 &gt; 112) -&gt; bx.br=true
  // Bar 9: Pivot Low = 95 (confirmed at bar 11)
  // Bar 12: Bullish Wick Sweep (l=92 &lt; 95, c=97 &gt; 95)
  // Bar 13: Outbreak below 95 on separate test? Tested below in BOTH mode.
  const bars = [
    { h: 102, l: 98, c: 100 }, // 0
    { h: 105, l: 99, c: 103 }, // 1
    { h: 110, l: 101, c: 107 }, // 2 &lt;- Pivot High = 110
    { h: 108, l: 100, c: 104 }, // 3
    { h: 106, l: 99, c: 102 }, // 4 (PH confirmed here)
    { h: 112, l: 103, c: 108 }, // 5 &lt;- Bearish Wick Sweep of 110
    { h: 114, l: 107, c: 113 }, // 6 &lt;- Closes above 112 -&gt; mitigates Bearish box AND breaks 110
    { h: 109, l: 102, c: 105 }, // 7
    { h: 104, l: 98, c: 100 }, // 8
    { h: 101, l: 95, c: 97 }, // 9 &lt;- Pivot Low = 95
    { h: 103, l: 96, c: 101 }, // 10
    { h: 105, l: 99, c: 103 }, // 11 (PL confirmed here)
    { h: 102, l: 92, c: 97 }, // 12 &lt;- Bullish Wick Sweep of 95
    { h: 106, l: 97, c: 104 }, // 13
    { h: 109, l: 103, c: 107 }, // 14
    { h: 111, l: 106, c: 108 }, // 15
    { h: 112, l: 109, c: 111 }, // 16
    { h: 113, l: 108, c: 111 }, // 17
  ];

  // Test 1: Only Wicks (bar 2 PH=110 swept@5 mitigated@6; bar 4 PL=99 swept@8 mitigated@9; bar 9 PL=95 swept@12 open@17)
  const r1 = computeLiquiditySweepsV2(bars, { len: 2, opt: MODES.ONLY_WICKS, extend: true, maxB: 300 });
  assert.strictEqual(r1.events.length, 3, &#x27;Expected 3 wick sweeps in ONLY_WICKS&#x27;);
  assert.strictEqual(r1.events[0].kind, &#x27;BEAR_WICK_SWEEP&#x27;);
  assert.strictEqual(r1.events[0].triggerBar, 5);
  assert.strictEqual(r1.events[0].br, true, &#x27;Bearish sweep box mitigates at bar 6 when close=113 &gt; 112&#x27;);
  assert.strictEqual(r1.events[0].right, 6);
  assert.strictEqual(r1.events[1].kind, &#x27;BULL_WICK_SWEEP&#x27;);
  assert.strictEqual(r1.events[1].triggerBar, 8);
  assert.strictEqual(r1.events[1].pivotBar, 4);
  assert.strictEqual(r1.events[1].br, true, &#x27;Bullish sweep box mitigates at bar 9 when close=97 &lt; 98&#x27;);
  assert.strictEqual(r1.events[2].kind, &#x27;BULL_WICK_SWEEP&#x27;);
  assert.strictEqual(r1.events[2].triggerBar, 12);
  assert.strictEqual(r1.events[2].br, false, &#x27;Bullish sweep box remains unmitigated&#x27;);
  assert.strictEqual(r1.events[2].right, 17);

  // Test 2: Only Outbreaks &amp; Retest (retest of broken PH=110 at bar 15/16 -&gt; wait, let&#x27;s build explicit retest)
  const bars2 = [
    { h: 102, l: 98, c: 100 }, // 0
    { h: 105, l: 99, c: 103 }, // 1
    { h: 110, l: 101, c: 107 }, // 2 &lt;- PH=110
    { h: 108, l: 100, c: 104 }, // 3
    { h: 106, l: 99, c: 102 }, // 4 (PH confirmed)
    { h: 113, l: 104, c: 112 }, // 5 &lt;- close=112 &gt; 110 -&gt; brk=true
    { h: 112, l: 109, c: 111 }, // 6 &lt;- low=109 &lt; 110 and close=111 &gt; 110 -&gt; BULL_RETEST_SWEEP
  ];
  const r2 = computeLiquiditySweepsV2(bars2, { len: 2, opt: MODES.ONLY_OUTBREAKS_RETEST });
  assert.strictEqual(r2.events.length, 1);
  assert.strictEqual(r2.events[0].kind, &#x27;BULL_RETEST_SWEEP&#x27;);
  assert.strictEqual(r2.events[0].triggerBar, 6);
  assert.strictEqual(r2.events[0].lineStyle, &#x27;dashed&#x27;);

  // Test 3: BOTH mode
  const r3 = computeLiquiditySweepsV2(bars2, { len: 2, opt: MODES.BOTH });
  assert.strictEqual(r3.events.length, 1);
  assert.strictEqual(r3.events[0].kind, &#x27;BULL_RETEST_SWEEP&#x27;);

  return {
    nodeVersion: process.version,
    status: &#x27;PASS&#x27;,
    testsRun: 3,
    onlyWicksEvents: r1.events,
    outbreakRetestEvents: r2.events,
  };
}

if (require.main === module) {
  const res = runSelfTest();
  console.log(JSON.stringify(res, null, 2));
}

module.exports = { MODES, isPivotHigh, isPivotLow, computeLiquiditySweepsV2, runSelfTest };
</code></pre>

<h2>三、Nod