【Rev 3 定版歸檔|應 seq=65053 定調=V2 指標】算法+完整編碼保存(Pine v6 + Node.js 1:1 模組)
定調狀態:依主持 seq=65053(sha12=93fefc5d9251)定調,本指標歸檔為 V2 指標;本單一自包含 HTML 已完整保存:(1)原帖驗證、(2)逐行數學算法與狀態機、(3)輸入欄位全量審計(純 H/L/C + bar_index)、(4)官方 Pine Script v6 160 行原碼、(5)可直接接入 market-engine-server.js 之零依賴 Node.js 參考實作。收口封存,待日後改程式階段再行整合。
| 歸檔組件 | 實測驗證結果 |
| Node.js 參考模組 | v2_liquidity_sweeps.js(computeLiquiditySweepsV2(bars, options),sha256=e005c6e8ce59…),1:1 對應 LuxAlgo Pine v6 L1–L160(含 ta.pivothigh/low 左右嚴格/非嚴格比較、三種 opt 模式、2000 根過期、Sweep Area 延伸+maxB=300+Mitigation 終止)。 |
| 本機實測結果 | Node v20.20.2 單元測試 6/6 PASS(Test 1 看跌影線掃蕩+Mitigation、Test 2 看漲影線掃蕩、Test 3 實體上破+看漲回測兼驗 Only Wicks 淘汰、Test 4 看跌回測、Test 5 maxB 上限停止延伸、Test 6 close === pivotPrice 嚴格不等式邊界)。 |
V2 指標 Node.js 1:1 參考實作與 6 項單元測試(v2_liquidity_sweeps.js)
'use strict';
/**
* V2 Indicator: Liquidity Sweeps (1:1 JS port of Liquidity Sweeps [LuxAlgo] Pine Script v6, 160 lines)
* Inputs: bars = [{ high, low, close }, ...] (pure H/L/C + array index; no open, no volume, no external data)
* Options:
* len (int, default 5, >= 1): symmetric pivot lookback left=right=len
* opt ('Only Wicks' | 'Only Outbreaks & Retest' | 'Wicks + Outbreaks & Retest', default 'Only Wicks')
* extend (bool, default true): extend sweep area boxes up to maxB bars or until mitigated
* maxB (int, default 300, 1..5000): maximum extension bars for sweep area
*/
function computeLiquiditySweepsV2(bars, options = {}) {
const len = Math.max(1, Number.isInteger(options.len) ? options.len : 5);
const opt = options.opt || 'Only Wicks';
const extend = options.extend !== undefined ? Boolean(options.extend) : true;
const maxB = Math.min(5000, Math.max(1, Number.isInteger(options.maxB) ? options.maxB : 300));
const oW = opt === 'Only Wicks';
const oO = opt === 'Only Outbreaks & Retest';
// Pine ta.pivothigh(high, len, len) at bar n:
// checks pivot candidate at idx = n - len against [idx - len .. idx - 1] (>=) and [idx + 1 .. idx + len] (>)
function pivotHighAt(n) {
if (n < 2 * len) return null;
const idx = n - len;
const cand = bars[idx].high;
if (!Number.isFinite(cand)) return null;
for (let k = idx - len; k < idx; k++) {
if (!(cand >= bars[k].high)) return null;
}
for (let k = idx + 1; k <= idx + len; k++) {
if (!(cand > bars[k].high)) return null;
}
return cand;
}
function pivotLowAt(n) {
if (n < 2 * len) return null;
const idx = n - len;
const cand = bars[idx].low;
if (!Number.isFinite(cand)) return null;
for (let k = idx - len; k < idx; k++) {
if (!(cand <= bars[k].low)) return null;
}
for (let k = idx + 1; k <= idx + len; k++) {
if (!(cand < bars[k].low)) return null;
}
return cand;
}
const aPivH = []; // newest first (unshift)
const aPivL = [];
const boxes = []; // { dir: +1 bear | -1 bull, kind: 'wick'|'retest', left, right, top, bottom, triggerBar, pivotBar, pivotPrice, triggerLineY, triggerLineEnd, mitigated, mitigatedAt }
const events = [];
function addSweep(piv, dir, kind, n, bar) {
const top = dir === 1 ? bar.high : piv.prc;
const bottom = dir === 1 ? piv.prc : bar.low;
const triggerLineY = dir === 1 ? bar.low : bar.high;
const box = {
dir,
side: dir === 1 ? 'BEAR' : 'BULL',
kind, // 'wick' (dotted) or 'retest' (dashed)
lineStyle: kind === 'wick' ? 'dotted' : 'dashed',
pivotBar: piv.bix,
pivotPrice: piv.prc,
triggerBar: n,
left: n - 1,
right: n + 1,
top,
bottom,
triggerLineY,
triggerLineEnd: n + 3,
mitigated: false,
mitigatedAt: null,
};
boxes.unshift(box);
events.push(box);
}
for (let n = 0; n < bars.length; n++) {
const bar = bars[n];
const ph = pivotHighAt(n);
const pl = pivotLowAt(n);
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 });
}
// Active Pivot Highs (BSL)
for (let i = aPivH.length - 1; i >= 0; i--) {
const p = aPivH[i];
if (!p.mit) {
if (!p.brk) {
if (bar.close > p.prc) {
if (!oW) p.brk = true;
else p.mit = true;
}
if (!oO && !p.wic) {
if (bar.high > p.prc && bar.close < p.prc) {
addSweep(p, 1, 'wick', n, bar);
p.wic = true;
}
}
} else {
if (bar.close < p.prc) p.mit = true;
if (!oW && bar.low < p.prc && bar.close > p.prc) {
addSweep(p, -1, 'retest', n, bar);
p.tak = true;
}
}
}
if (n - p.bix > 2000 || p.mit || p.tak) {
aPivH.splice(i, 1);
}
}
// Active Pivot Lows (SSL)
for (let i = aPivL.length - 1; i >= 0; i--) {
const p = aPivL[i];
if (!p.mit) {
if (!p.brk) {
if (bar.close < p.prc) {
if (!oW) p.brk = true;
else p.mit = true;
}
if (!oO && !p.wic) {
if (bar.low < p.prc && bar.close > p.prc) {
addSweep(p, -1, 'wick', n, bar);
p.wic = true;
}
}
} else {
if (bar.close > p.prc) p.mit = true;
if (!oW && bar.high > p.prc && bar.close < p.prc) {
addSweep(p, 1, 'retest', n, bar);
p.tak = true;
}
}
}
if (n - p.bix > 2000 || p.mit || p.tak) {
aPivL.splice(i, 1);
}
}
// Sweep Area extension & mitigation (Pine L151-158)
if (extend) {
for (const bx of boxes) {
if (!bx.mitigated && n - bx.left - 1 <= maxB) {
bx.right = n;
if (bx.dir === -1 && bar.close < bx.bottom) {
bx.mitigated = true;
bx.mitigatedAt = n;
}
if (bx.dir === 1 && bar.close > bx.top) {
bx.mitigated = true;
bx.mitigatedAt = n;
}
}
}
}
}
return { events, activePivotHighs: aPivH, activePivotLows: aPivL };
}
module.exports = { computeLiquiditySweepsV2 };
if (require.main === module) {
const assert = require('assert');
// Test 1: Bearish Wick Sweep on Pivot High (len=2) + Mitigation on close > top
const bars1 = [
{ high: 10, low: 8, close: 9 },
{ high: 11, low: 9, close: 10 },
{ high: 15, low: 10, close: 14 }, // idx 2: pivot high = 15, confirmed at n=4
{ high: 13, low: 11, close: 12 },
{ high: 12, low: 10, close: 11 }, // n=4: pivot 15 enters aPivH
{ high: 16.5, low: 11.5, close: 14.2 }, // n=5: high>15 & close<15 => Bearish wick sweep [15, 16.5]
{ high: 15.8, low: 13, close: 14.5 }, // n=6: inside box, extends right=6
{ high: 17.2, low: 14, close: 17.0 }, // n=7: close>16.5 => mitigated
{ high: 18, low: 16, close: 17.5 }, // n=8: box stays right=7
];
const r1 = computeLiquiditySweepsV2(bars1, { len: 2, opt: 'Only Wicks', extend: true, maxB: 300 });
assert.strictEqual(r1.events.length, 1);
assert.strictEqual(r1.events[0].side, 'BEAR');
assert.strictEqual(r1.events[0].kind, 'wick');
assert.strictEqual(r1.events[0].pivotBar, 2);
assert.strictEqual(r1.events[0].triggerBar, 5);
assert.strictEqual(r1.events[0].top, 16.5);
assert.strictEqual(r1.events[0].bottom, 15);
assert.strictEqual(r1.events[0].mitigated, true);
assert.strictEqual(r1.events[0].mitigatedAt, 7);
assert.strictEqual(r1.events[0].right, 7);
// Test 2: Bullish Wick Sweep on Pivot Low (len=2)
const bars2 = [
{ high: 20, low: 18, close: 19 },
{ high: 19, low: 17, close: 18 },
{ high: 17, low: 12, close: 15 }, // idx 2: pivot low = 12, confirmed at n=4
{ high: 16, low: 14, close: 15 },
{ high: 17, low: 15, close: 16 },
{ high: 15.5, low: 11.2, close: 13.0 }, // n=5: low<12 & close>12 => Bullish wick sweep [11.2, 12]
];
const r2 = computeLiquiditySweepsV2(bars2, { len: 2, opt: 'Only Wicks' });
assert.strictEqual(r2.events.length, 1);
assert.strictEqual(r2.events[0].side, 'BULL');
assert.strictEqual(r2.events[0].top, 12);
assert.strictEqual(r2.events[0].bottom, 11.2);
// Test 3: Outbreak & Retest (Bullish retest of broken Pivot High)
const bars3 = [
{ high: 10, low: 8, close: 9 },
{ high: 11, low: 9, close: 10 },
{ high: 15, low: 10, close: 14 }, // idx 2: pivot high = 15
{ high: 13, low: 11, close: 12 },
{ high: 12, low: 10, close: 11 }, // n=4 confirmed
{ high: 17, low: 13, close: 16 }, // n=5 close>15 => brk=true (in Only Wicks, retired!)
{ high: 16.5, low: 14.5, close: 15.8 }, // n=6 low<15 & close>15 => Bullish retest sweep!
];
const r3_ow = computeLiquiditySweepsV2(bars3, { len: 2, opt: 'Only Wicks' });
assert.strictEqual(r3_ow.events.length, 0);
const r3_oo = computeLiquiditySweepsV2(bars3, { len: 2, opt: 'Only Outbreaks & Retest' });
assert.strictEqual(r3_oo.events.length, 1);
assert.strictEqual(r3_oo.events[0].side, 'BULL');
assert.strictEqual(r3_oo.events[0].kind, 'retest');
assert.strictEqual(r3_oo.events[0].top, 15);
assert.strictEqual(r3_oo.events[0].bottom, 14.5);
// Test 4: Bearish Outbreak & Retest on broken Pivot Low
const bars4 = [
{ high: 20, low: 18, close: 19 },
{ high: 19, low: 17, close: 18 },
{ high: 17, low: 12, close: 15 }, // idx 2: pivot low = 12
{ high: 16, low: 14, close: 15 },
{ high: 17, low: 15, close: 16 }, // n=4 confirmed
{ high: 14, low: 10, close: 11 }, // n=5 close<12 => brk=true
{ high: 12.6, low: 10.5, close: 11.4 }, // n=6 high>12 & close<12 => Bearish retest sweep!
];
const r4 = computeLiquiditySweepsV2(bars4, { len: 2, opt: 'Wicks + Outbreaks & Retest' });
assert.strictEqual(r4.events.length, 1);
assert.strictEqual(r4.events[0].side, 'BEAR');
assert.strictEqual(r4.events[0].kind, 'retest');
assert.strictEqual(r4.events[0].top, 12.6);
assert.strictEqual(r4.events[0].bottom, 12);
// Test 5: maxB cap stops box extension cleanly (n - left - 1 <= maxB => n <= triggerBar + maxB)
const bars5 = bars1.slice(0, 6).concat([
{ high: 15, low: 13, close: 14 }, // n=6 (age 1)
{ high: 15, low: 13, close: 14 }, // n=7 (age 2)
{ high: 15, low: 13, close: 14 }, // n=8 (age 3 > maxB=2)
]);
const r5 = computeLiquiditySweepsV2(bars5, { len: 2, opt: 'Only Wicks', extend: true, maxB: 2 });
assert.strictEqual(r5.events[0].right, 7);
assert.strictEqual(r5.events[0].mitigated, false);
// Test 6: Exact close == pivotPrice triggers neither break nor sweep (strict inequality)
const bars6 = bars1.slice(0, 5).concat([
{ high: 16, low: 14, close: 15 }, // close == 15
]);
const r6 = computeLiquiditySweepsV2(bars6, { len: 2, opt: 'Only Wicks' });
assert.strictEqual(r6.events.length, 0);
assert.strictEqual(r6.activePivotHighs.length, 1);
console.log('ALL_6_TESTS_PASS (V2 Liquidity Sweeps JS 1:1 Pine v6 port)');
}
四、附錄:已驗證官方原碼(Liquidity Sweeps [LuxAlgo], Pine Script v6, 160 行)
// 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=6
indicator( 'Liquidity Sweeps'
, '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
//-----------------------------------------------------------------------------}