V2 指標歸檔:Liquidity Sweeps [LuxAlgo] 算法規格與實作編碼(Seat A20)

任務定位(seq=65053 定調):將 X 帖(foodloss_otoko/status/2123461381030834442)所推之 Liquidity Sweeps [LuxAlgo] 納入 V2 指標規格庫,完整保存「算法+原版 Pine Script v5+Node.js V2 移植編碼+單元實測」,供後續修改 market-engine-server.js 時直接加插。

1. 原帖核實與資料來源

2. 算法規格與精確輸入(逐項核實,零猜測)

輸入/參數來源原碼位置用途
high單圖 K 線最高價L75, L92(pivothigh), L111, L120, L138, L143建立買方流動性高點 piv.prc、偵測上破影線及空方 Sweep Area 上界
low單圖 K 線最低價L76, L93(pivotlow), L113, L118, L136, L145建立賣方流動性低點 piv.prc、偵測下破影線及多方 Sweep Area 下界
close單圖 K 線收盤價L105, L111, L116, L118, L130, L136, L141, L143, L155, L157判斷乾淨收破(brk/mit)、同棒收復確認、方框失效(mitigation)
bar_index (n)圖表 K 線序號L33, L40, L154(與 L68..154)樞紐偏移 n - len、2000 棒過期清理、3 棒觸發點線、maxB 延長上限
靜態參數使用者設定L15–26len=5(Swings)、opt='Only Wicks'、extend=true、maxB=300
open/volume/盤口/跨時框不使用0 次引用原碼全 160 行皆為 0 次引用;普通 K 線 OHLCV 數據已完全滿足

server.js V2 接線注意:Pine 原碼無 barstate.isconfirmed,盤中實時 bar 亦會觸發。接入 market-engine-server.js(陣列格式 [ts, open, high, low, close, vol, amt])計算 V2 確認訊號時,建議只將 isFinal === true(或已收盤 bar,排除 forming 未收盤棒)送入偵測器,以完全對齊作者規則②「實體收盤確定後才進場」且杜絕重繪。

3. Node.js V2 確定性移植編碼(sha12=5001e3dd6fae,Node v20.20.2 3/3 PASS)

// Liquidity Sweeps V2 — Deterministic JS port matching market-engine-server.js ohlcArray [ts, o, h, l, c, v, amt]
// Faithful to LuxAlgo Pine Script v5 (PUB;26391b932d3145879b67deca62305c9a) for later server.js V2 integration.
export function detectLiquiditySweepsV2(ohlcArray, opts = {}) {
  const len = Number.isInteger(opts.len) && opts.len >= 1 ? opts.len : 5;
  const opt = opts.opt || "Only Wicks"; // "Only Wicks" | "Only Outbreaks & Retest" | "Wicks + Outbreaks & Retest"
  const extend = opts.extend !== undefined ? Boolean(opts.extend) : true;
  const maxB = Number.isInteger(opts.maxB) && opts.maxB >= 1 ? Math.min(5000, opts.maxB) : 300;
  const oW = opt === "Only Wicks";
  const oO = opt === "Only Outbreaks & Retest";

  if (!Array.isArray(ohlcArray) || ohlcArray.length === 0) {
    return { sweeps: [], boxes: [], activePivH: 0, activePivL: 0 };
  }

  const aPivH = [];
  const aPivL = [];
  const aBoxBr = [];
  const sweeps = [];

  for (let n = 0; n < ohlcArray.length; n++) {
    const high = ohlcArray[n][2];
    const low = ohlcArray[n][3];
    const close = ohlcArray[n][4];

    // Symmetric ta.pivothigh(len, len) / ta.pivotlow(len, len) confirmed at bar n for center bix = n - len
    if (n >= 2 * len) {
      const cIdx = n - len;
      const candH = ohlcArray[cIdx][2];
      const candL = ohlcArray[cIdx][3];
      let isPH = true;
      let isPL = true;
      for (let k = cIdx - len; k <= n; k++) {
        if (k === cIdx) continue;
        // TradingView ta.pivothigh/pivotlow breaks ties by requiring strict > / < on neighbors
        if (ohlcArray[k][2] >= candH) isPH = false;
        if (ohlcArray[k][3] <= candL) isPL = false;
        if (!isPH && !isPL) break;
      }
      if (isPH) aPivH.unshift({ prc: candH, bix: cIdx, brk: false, mit: false, tak: false, wic: false });
      if (isPL) aPivL.unshift({ prc: candL, bix: cIdx, brk: false, mit: false, tak: false, wic: false });
    }

    // Scan swing highs (BSL)
    for (let i = aPivH.length - 1; i >= 0; i--) {
      const get = aPivH[i];
      if (!get.mit) {
        if (!get.brk) {
          if (close > get.prc) {
            if (!oW) get.brk = true;
            else get.mit = true;
          }
          if (!oO && !get.wic) {
            if (high > get.prc && close < get.prc) {
              const bx = { left: n - 1, right: n + 1, top: high, bottom: get.prc, br: false, dir: 1, kind: "WICK", pivBix: get.bix, trigBar: n };
              aBoxBr.unshift(bx);
              sweeps.push({ barIdx: n, ts: ohlcArray[n][0], side: "BEAR", kind: "WICK", level: get.prc, pivBix: get.bix, sweepExtreme: high, triggerLine: low });
              get.wic = true;
            }
          }
        } else {
          if (close < get.prc) get.mit = true;
          if (!oW && low < get.prc && close > get.prc) {
            const bx = { left: n - 1, right: n + 1, top: get.prc, bottom: low, br: false, dir: -1, kind: "RETEST", pivBix: get.bix, trigBar: n };
            aBoxBr.unshift(bx);
            sweeps.push({ barIdx: n, ts: ohlcArray[n][0], side: "BULL", kind: "RETEST", level: get.prc, pivBix: get.bix, sweepExtreme: low, triggerLine: high });
            get.tak = true;
          }
        }
      }
      if (n - get.bix > 2000 || get.mit || get.tak) aPivH.splice(i, 1);
    }

    // Scan swing lows (SSL)
    for (let i = aPivL.length - 1; i >= 0; i--) {
      const get = aPivL[i];
      if (!get.mit) {
        if (!get.brk) {
          if (close < get.prc) {
            if (!oW) get.brk = true;
            else get.mit = true;
          }
          if (!oO && !get.wic) {
            if (low < get.prc && close > get.prc) {
              const bx = { left: n - 1, right: n + 1, top: get.prc, bottom: low, br: false, dir: -1, kind: "WICK", pivBix: get.bix, trigBar: n };
              aBoxBr.unshift(bx);
              sweeps.push({ barIdx: n, ts: ohlcArray[n][0], side: "BULL", kind: "WICK", level: get.prc, pivBix: get.bix, sweepExtreme: low, triggerLine: high });
              get.wic = true;
            }
          }
        } else {
          if (close > get.prc) get.mit = true;
          if (!oW && high > get.prc && close < get.prc) {
            const bx = { left: n - 1, right: n + 1, top: high, bottom: get.prc, br: false, dir: 1, kind: "RETEST", pivBix: get.bix, trigBar: n };
            aBoxBr.unshift(bx);
            sweeps.push({ barIdx: n, ts: ohlcArray[n][0], side: "BEAR", kind: "RETEST", level: get.prc, pivBix: get.bix, sweepExtreme: high, triggerLine: low });
            get.tak = true;
          }
        }
      }
      if (n - get.bix > 2000 || get.mit || get.tak) aPivL.splice(i, 1);
    }

    if (extend) {
      for (const bx of aBoxBr) {
        if (!bx.br && n - bx.left - 1 <= maxB) {
          bx.right = n;
          if (bx.dir === -1 && close < bx.bottom) bx.br = true;
          if (bx.dir === 1 && close > bx.top) bx.br = true;
        }
      }
    }
  }
  return { sweeps, boxes: aBoxBr, activePivH: aPivH.length, activePivL: aPivL.length };
}

實測結果(Node v20.20.2):ALL_NODE20_TESTS_PASS: 3/3 scenarios verified (Only Wicks, Outbreak+Retest, Box mitigation, H/L/C-only invariance)。

4. 原版 Pine Script v5 參考碼(160 行,sha12=18be09f34ebf,CC BY-NC-SA 4.0)

// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/
// © LuxAlgo

//@version=5
indicator( 'Liquidity Sweeps [LuxAlgo]'
         , 'LuxAlgo - Liquidity Sweeps'
         ,  max_lines_count=500
         ,  max_boxes_count=500
         ,  overlay=true
         )

//------------------------------------------------------------------------------
//Settings
//-----------------------------------------------------------------------------{
len    = input.int(5, 'Swings', minval=1, group='Liquidity Sweeps')
opt    = input.string('Only Wicks', 'options'
       , options=['Only Wicks', 'Only Outbreaks & Retest', 'Wicks + Outbreaks & Retest'], group='Liquidity Sweeps')

colBl  = input.color(#089981, 'Bull ', group='Liquidity Sweeps', inline='c1')
colBr  = input.color(#f23645, 'Bear' , group='Liquidity Sweeps', inline='c2')
colBl2 = input.color(#08998180,  ''  , group='Liquidity Sweeps', inline='c1')
colBr2 = input.color(#f2364580,  ''  , group='Liquidity Sweeps', inline='c2')

extend = input.bool(true, 'Extend', group='Sweep Area') 
maxB   = input.int(300, 'Max bars', minval=1, maxval=5000, group='Sweep Area')
colBl3 = input.color(#08998141, 'Bull ', group='Sweep Area')
colBr3 = input.color(#f2364541, 'Bear' , group='Sweep Area')

oW = opt == 'Only Wicks'
oO = opt == 'Only Outbreaks & Retest'
WO = opt == 'Wicks + Outbreaks & Retest'

n = bar_index 

//-----------------------------------------------------------------------------}      
//UDT's
//-----------------------------------------------------------------------------{
type piv
    float prc // price
    int   bix // bar_index
    bool  brk // broken
    bool  mit // mitigated
    bool  tak // taken
    bool  wic // wick
    line  lin 

type boxBr 
    box  bx 
    line ln
    bool br
    int  dr

//-----------------------------------------------------------------------------}      
//Variables
//-----------------------------------------------------------------------------{
var array< piv >aPivH  = array.new< piv >(1, piv.new  ())
var array< piv >aPivL  = array.new< piv >(1, piv.new  ())
var array<boxBr>aBoxBr = array.new<boxBr>(1, boxBr.new())

//-----------------------------------------------------------------------------}      
//Methods - functions
//-----------------------------------------------------------------------------{
method n(float piv) => bool out = not na(piv)

method p(piv piv, float val) => float out = (100 / piv.prc * val) - 100

method l(piv get, color c, string s='sd') => 
    style = switch s 
        'dt' => line.style_dotted
        'ds' => line.style_dashed
        =>      line.style_solid 
    line.new(get.bix, get.prc, n, get.prc, color=c, style = style)

method br(piv get, color c3, color c, int d) =>
    y1 = d == 1 ? high : get.prc
    y2 = d == 1 ? get.prc : low
    boxBr.new( 
     box.new(n -1, y1, n +1, y2
     , border_color
     = color.new(
       na, na   )
     , bgcolor=c3)
     , line.new(n , y1, n, y2, color=c, width=3)
     , false
     , d)

lnDot(y, c) => line.new(n, y, n+3, y, color=c, style=line.style_dotted)

//-----------------------------------------------------------------------------}      
//Execution
//-----------------------------------------------------------------------------{
ph = ta.pivothigh(len, len)
pl = ta.pivotlow (len, len)

if ph.n() 
    aPivH.unshift(piv.new(ph, n -len, false, false, false, false))

if pl.n() 
    aPivL.unshift(piv.new(pl, n -len, false, false, false, false)) 

for i = aPivH.size() -1 to 0
    get = aPivH.get(i)
    if not get.mit
        if not get.brk
            if close > get.prc 
                if not oW
                    get.brk := true
                else 
                    get.mit := true
            if not oO and not get.wic 
                if high > get.prc and close < get.prc                   
                    aBoxBr.unshift(get.br(colBr3, colBr, 1))       
                    get.l(colBr2, 'dt'), lnDot(low, colBr)
                    get.wic := true  
        else 
            if close < get.prc 
                get.mit := true
            if not oW and low < get.prc and close > get.prc                    
                aBoxBr.unshift(get.br(colBl3, colBl, -1))    
                get.l(colBl2, 'ds'), lnDot(high, colBl)
                get.tak := true 

    if n - get.bix > 2000 or get.mit or get.tak 
        aPivH.remove(i).lin.delete()

for i = aPivL.size() -1 to 0
    get = aPivL.get(i)
    if not get.mit
        if not get.brk
            if close < get.prc 
                if not oW
                    get.brk := true
                else 
                    get.mit := true
            if not oO and not get.wic 
                if low < get.prc and close > get.prc                              
                    aBoxBr.unshift(get.br(colBl3, colBl, -1))     
                    get.l(colBl2, 'dt'), lnDot(high, colBl)
                    get.wic := true 
        else 
            if close > get.prc 
                get.mit := true
            if not oW and high > get.prc and close < get.prc                     
                aBoxBr.unshift(get.br(colBr3, colBr, 1))    
                get.l(colBr2, 'ds'), lnDot(low, colBr)
                get.tak := true  

    if n - get.bix > 2000 or get.mit or get.tak 
        aPivL.remove(i).lin.delete()

if extend
    for bx in aBoxBr 
        if not bx.br and n - bx.bx.get_left() -1 <= maxB 
            bx.bx.set_right(bar_index)
            if bx.dr == -1 and close < bx.bx.get_bottom() 
                bx.br := true 
            if bx.dr ==  1 and close > bx.bx.get_top   () 
                bx.br := true 

//-----------------------------------------------------------------------------}