// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not // distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/. // © LazyBear: the WaveTrend calculation of "Indicator: WaveTrend Oscillator [WT]", https://www.tradingview.com/script/2KE8wTuF/ // Modified by ProEA, 2026: ported from Pine to TypeScript for a drill on simulated candles, with a guard against // division by zero, adjustable lengths, VuManChu Cipher B's preset values and pivot divergences. // Source of this file: https://proea.app/open-source/wavetrend.ts.txt, listed at https://proea.app/en/legal/open-source // // Deterministic simulation engine for /en/blog/wavetrend-oscillator-settings: // models LazyBear's 32-line WaveTrend [LazyBear] (2KE8wTuF, 61,565 boosts: n1=10, n2=21, sma=4) // and VuManChu Cipher B + Divergences (Msm4SjwI, 24,963 boosts: n1=9, n2=12, sma=3, wtVwap = wt1 - wt2, // plus 2-bar fractal divergence lag) across 88 simulated 15-minute candles (28 warmup + 60 visible bars). export type WaveTrendBar = readonly [ index: number, open: number, high: number, low: number, close: number, ]; export interface WaveTrendSettings { channelLen: number; // n1: 5..21 (LazyBear 10, VuManChu 9) avgLen: number; // n2: 6..34 (LazyBear 21, VuManChu 12) maLen: number; // wt2 SMA: 2..8 (LazyBear hardcoded 4, VuManChu 3) obOsTrigger: number; // 45..70 (53 or 60 extreme dot threshold) extremeOnly: boolean; // filter crosses to |wt2| >= obOsTrigger vs all crosses showSpreadWave: boolean; // VuManChu line 193 wtVwap = wt1 - wt2 yellow histogram } export const WT_LAZYBEAR_DEFAULTS: WaveTrendSettings = { channelLen: 10, avgLen: 21, maLen: 4, obOsTrigger: 53, extremeOnly: true, showSpreadWave: true, }; export const WT_WARM = 28; export const WT_SHOWN_COUNT = 60; function buildSimulatedBars(): WaveTrendBar[] { const total = WT_WARM + WT_SHOWN_COUNT; const bars: WaveTrendBar[] = []; let prevClose = 100.0; for (let i = 0; i < total; i++) { const shownIdx = i - WT_WARM; const fastCycle = Math.sin(i * 0.44) * 1.15; const medCycle = Math.cos(i * 0.19) * 1.65; let drift = 0; if (shownIdx < 0) { drift = i < 14 ? 0.55 : -0.58; } else if (shownIdx >= 0 && shownIdx <= 11) { drift = -0.72; // push into deep oversold (-60) } else if (shownIdx >= 12 && shownIdx <= 23) { drift = 0.78; // rally into overbought (+60) } else if (shownIdx >= 24 && shownIdx <= 33) { drift = (shownIdx % 2 === 0 ? 0.34 : -0.36); // mid-range chop around zero } else if (shownIdx >= 34 && shownIdx <= 44) { drift = 0.68; // second high push for bearish divergence check } else if (shownIdx >= 45 && shownIdx <= 53) { drift = -0.82; // sharp drop to oversold } else { drift = 0.64; // recovery bounce } const deltaFast = (fastCycle - Math.sin((i - 1) * 0.44) * 1.15) * 0.62; const deltaMed = (medCycle - Math.cos((i - 1) * 0.19) * 1.65) * 0.38; const open = Number(prevClose.toFixed(2)); const close = Number((open + drift + deltaFast + deltaMed).toFixed(2)); const wickUp = 0.42 + ((i * 7) % 5) * 0.15; const wickDn = 0.40 + ((i * 11) % 5) * 0.14; const high = Number((Math.max(open, close) + wickUp).toFixed(2)); const low = Number((Math.min(open, close) - wickDn).toFixed(2)); bars.push([i, open, high, low, close]); prevClose = close; } return bars; } export const WT_BARS: readonly WaveTrendBar[] = buildSimulatedBars(); function ema(values: readonly number[], len: number): number[] { const out: number[] = []; const k = 2 / (Math.max(1, len) + 1); let prev = values[0] ?? 0; for (let i = 0; i < values.length; i++) { const v = values[i]!; prev = i === 0 ? v : v * k + prev * (1 - k); out.push(prev); } return out; } function sma(values: readonly number[], len: number): number[] { const w = Math.max(1, len); const out: number[] = []; for (let i = 0; i < values.length; i++) { const s = Math.max(0, i - w + 1); const slice = values.slice(s, i + 1); out.push(slice.reduce((a, b) => a + b, 0) / slice.length); } return out; } export interface WaveTrendPoint { shownIndex: number; close: number; high: number; low: number; wt1: number; wt2: number; spread: number; // wt1 - wt2 (VuManChu line 193 wtVwap) rawCrossUp: boolean; rawCrossDn: boolean; activeBullDot: boolean; activeBearDot: boolean; bullDivConfirmed: boolean; // confirmed 2 bars after fractal low bearDivConfirmed: boolean; // confirmed 2 bars after fractal high } export interface WaveTrendSummary { shown: WaveTrendPoint[]; totalCrosses: number; extremeCrosses: number; activeSignals: number; bullDots: number; bearDots: number; whipsawCount: number; confirmedDivCount: number; peakAbsWt1: number; } export function summarizeWaveTrend(s: WaveTrendSettings): WaveTrendSummary { const hlc3 = WT_BARS.map((b) => (b[2] + b[3] + b[4]) / 3); const esa = ema(hlc3, s.channelLen); const absDiff = hlc3.map((v, i) => Math.abs(v - esa[i]!)); const d = ema(absDiff, s.channelLen); // Zero-division guarded version of LazyBear line 18: (ap - esa) / (0.015 * d) const ci = hlc3.map((v, i) => (d[i]! <= 1e-9 ? 0 : (v - esa[i]!) / (0.015 * d[i]!))); const wt1All = ema(ci, s.avgLen); const wt2All = sma(wt1All, s.maLen); // Detect 5-bar fractal highs/lows on wt2 matching TradingView's ta.pivothigh(wt2, 2, 2) / ta.pivotlow(wt2, 2, 2) // (>= / <= on older left bars, strict > / < on newer right bars; confirmed 2 bars after the swing on bar i) const bullDivAt = new Set(); const bearDivAt = new Set(); const divExtreme = Math.max(30, s.obOsTrigger - 15); let prevTopWt2: number | null = null; let prevTopHigh: number | null = null; let prevBotWt2: number | null = null; let prevBotLow: number | null = null; for (let i = 4; i < WT_BARS.length; i++) { const pIdx = i - 2; const wMid = wt2All[pIdx]!; const isTop = wMid >= wt2All[pIdx - 2]! && wMid >= wt2All[pIdx - 1]! && wMid > wt2All[pIdx + 1]! && wMid > wt2All[pIdx + 2]!; const isBot = wMid <= wt2All[pIdx - 2]! && wMid <= wt2All[pIdx - 1]! && wMid < wt2All[pIdx + 1]! && wMid < wt2All[pIdx + 2]!; if (isTop) { const hMid = WT_BARS[pIdx]![2]; if (prevTopWt2 !== null && prevTopHigh !== null && hMid > prevTopHigh && wMid < prevTopWt2 && wMid >= divExtreme) { bearDivAt.add(i); } prevTopWt2 = wMid; prevTopHigh = hMid; } if (isBot) { const lMid = WT_BARS[pIdx]![3]; if (prevBotWt2 !== null && prevBotLow !== null && lMid < prevBotLow && wMid > prevBotWt2 && wMid <= -divExtreme) { bullDivAt.add(i); } prevBotWt2 = wMid; prevBotLow = lMid; } } const shown: WaveTrendPoint[] = []; let totalCrosses = 0; let extremeCrosses = 0; let bullDots = 0; let bearDots = 0; let confirmedDivCount = 0; let peakAbsWt1 = 0; const crossBars: { idx: number; dir: 1 | -1 }[] = []; for (let k = 0; k < WT_SHOWN_COUNT; k++) { const i = WT_WARM + k; const [, , high, low, close] = WT_BARS[i]!; const w1 = wt1All[i]!; const w2 = wt2All[i]!; const pw1 = wt1All[i - 1]!; const pw2 = wt2All[i - 1]!; const rawCrossUp = pw1 <= pw2 && w1 > w2; const rawCrossDn = pw1 >= pw2 && w1 < w2; const isExtremeBull = rawCrossUp && w2 <= -s.obOsTrigger; const isExtremeBear = rawCrossDn && w2 >= s.obOsTrigger; if (rawCrossUp || rawCrossDn) totalCrosses++; if (isExtremeBull || isExtremeBear) extremeCrosses++; const activeBullDot = s.extremeOnly ? isExtremeBull : rawCrossUp; const activeBearDot = s.extremeOnly ? isExtremeBear : rawCrossDn; if (activeBullDot) { bullDots++; crossBars.push({ idx: k, dir: 1 }); } if (activeBearDot) { bearDots++; crossBars.push({ idx: k, dir: -1 }); } const bullDivConfirmed = bullDivAt.has(i); const bearDivConfirmed = bearDivAt.has(i); if (bullDivConfirmed || bearDivConfirmed) confirmedDivCount++; peakAbsWt1 = Math.max(peakAbsWt1, Math.abs(w1)); shown.push({ shownIndex: k, close, high, low, wt1: Number(w1.toFixed(2)), wt2: Number(w2.toFixed(2)), spread: Number((w1 - w2).toFixed(2)), rawCrossUp, rawCrossDn, activeBullDot, activeBearDot, bullDivConfirmed, bearDivConfirmed, }); } let whipsawCount = 0; for (let c = 1; c < crossBars.length; c++) { if (crossBars[c]!.dir !== crossBars[c - 1]!.dir && crossBars[c]!.idx - crossBars[c - 1]!.idx <= 3) { whipsawCount++; } } return { shown, totalCrosses, extremeCrosses, activeSignals: bullDots + bearDots, bullDots, bearDots, whipsawCount, confirmedDivCount, peakAbsWt1: Number(peakAbsWt1.toFixed(1)), }; }