OPEN-SOURCE SCRIPT

Lempel Ziv Trend Coder [forexobroker]

320
Lempel Ziv Trend Coder encodes the recent sign sequence of price changes into 3-bit (sign[t], sign[t-1], sign[t-2]) windows and tracks the frequency distribution of the eight resulting patterns. When one pattern dominates the share, the price sequence is structurally biased — that is the trending regime. Direction comes from the up-bar fraction.

🔶 ALGORITHM

1. For each k in [0, N-3], encode (b0, b1, b2) where bi = close[k+i] > close[k+i+1].
2. Increment count

where p = b0 * 4 + b1 * 2 + b2 (0..7).
3. dominance = max(count) / sum(count). Uniform random = 0.125.
4. Trending regime when dominance >= threshold.
5. Direction bias from up-bar fraction in the same window.

🔶 SIGNAL LOGIC

- Buy: trending AND up-fraction > 0.5 AND close crosses EMA up AND not already long AND cooldown elapsed AND barstate.isconfirmed.
- Sell: trending AND up-fraction < 0.5 AND close crosses EMA down.
- Position-lock state machine.

🔶 INPUTS

- Sign Window (default 40)
- Pullback EMA Length (default 8)
- Min Dominance (default 0.20)
- Cooldown Bars (default 4)
- Visual: dashboard, glow, EMA toggle, buy / sell colors

🔶 ALERTS

LZT Buy, LZT Sell, LZT Any Signal, LZT Trend Start, LZT Chop Start, LZT Very Trendy, LZT Very Choppy, LZT EMA Up, LZT Webhook JSON.

🔶 LIMITATIONS

- 3-bit encoding ignores bar magnitude; a tiny tick contributes the same as a large move.
- "Distinct patterns observed" alone saturates at 8 quickly with N >= 40 bars; dominance is a more discriminative measure but still coarse.
- LZ true complexity is closer to a substring count than a pattern-frequency count; this is a tractable approximation.
- Direction comes from up-fraction, not the LZ measure itself; in true random regimes the direction signal is meaningless and the regime gate correctly mutes signals.

Thông báo miễn trừ trách nhiệm

Thông tin và các ấn phẩm này không nhằm mục đích, và không cấu thành, lời khuyên hoặc khuyến nghị về tài chính, đầu tư, giao dịch hay các loại khác do TradingView cung cấp hoặc xác nhận. Đọc thêm tại Điều khoản Sử dụng.