INVITE-ONLY SCRIPT

Chan Theory - Chanlun MAX | CJZT

70
# Chan Theory - Chanlun MAX | CJZT

## Overview
This is the full-featured version of Chan Theory (缠论) structural analysis indicator, built with Pine Script v6. It provides comprehensive multi-level structure recognition including fractals, pens, segments, trend lines, super trend lines, and pivot zones across three levels. The indicator offers advanced trading signal detection with customizable filtering options and MACD divergence analysis.

**Why This Indicator?**
Real market movements rarely follow textbook patterns. This indicator addresses this reality with:
- **Single Pen to Segment**: When a powerful pen breaks the previous level's extreme or meets K-line count requirements, it can directly become a segment - no need to wait for the standard 3-pen structure
- **9-Pen Segment to Trend**: A segment containing 9+ pens can upgrade to trend line level, enabling better same-level decomposition
- **Flexible Level Upgrade**: Multiple conditions (extremum breakthrough, count requirements, or either) allow the structure to adapt to actual market behavior
- **4K/5K Pen Types**: Designed to better fit sub-cycle and sub-sub-cycle movements with shorter K-line requirements
- **Top/Bottom to Pen**: When top/bottom fractal strength is significant, allows pen formation even with insufficient K-line count based on amplitude ratio
- **Main Chart MACD Divergence**: Displays divergence pens/segments directly on K-line chart, comparing entry and exit pen momentum for visual divergence confirmation
- **Sub-Chart MACD Indicator**: Works with the companion Chan Theory MACD sub-chart indicator for consolidation/trend divergence analysis on the MACD histogram

These features enable more accurate fitting of irregular market movements, proper same-level decomposition (同级别分解), and better alignment with sub-cycle structures - essential for practical Chan Theory application.

**Key Features:**
- 5 pen types: Classic, Optimized, 4K, 5K, Strict
- 3 segment modes: Dynamic Correction, Strict, Extension
- 3-level structure: Segments → Trend Lines → Super Trend Lines
- 3-level pivot zones with extension detection
- Complete Type I/II/III trading signals across all levels
- MACD divergence analysis for signal confirmation
- Advanced signal filtering system
- Real-time alerts for all trading signals

## Core Functions

### 1. Candlestick Containment Processing
Advanced containment processing with configurable options:
- **Left Containment**: Standard processing
- **Right Containment Check**: Optional additional validation
- Recursive processing ensures complete elimination

### 2. Pen Construction (5 Types)
| Type | Description |
|------|-------------|
| **Classic (老笔)** | 5+ processed K-lines per pen |
| **Optimized (新笔)** | 4+ processed + 5+ raw K-lines |
| **4K** | 4 raw K-lines with extremum validation |
| **5K** | 5 raw K-lines standard |
| **Strict (严笔)** | 5+ K-lines with directional validation |

### 3. Pen Endpoint Mode
- **Strict Highest/Lowest**: Pen endpoints must be absolute extremes
- **Allow Secondary High/Low**: Flexible endpoint detection when K-line count insufficient

### 4. Single Pen to Segment
Advanced level upgrade conditions:
- **Extremum Breakthrough**: Break previous level extreme
- **Count Condition**: Meet K-line/pen count requirements
- **Either Satisfied**: Flexible combination

### 5. Three-Level Segment System
- **Segments (线段)**: Grouped from pens
- **Trend Lines (趋势线)**: Grouped from segments
- **Super Trend Lines (大趋势线)**: Highest level structure

### 6. Segment Modes
| Mode | Description |
|------|-------------|
| **Dynamic Correction** | Real-time adjustment with correction |
| **Strict Mode** | Full classical compliance |
| **Extension Mode** | Flexible trend continuation |

### 7. Three-Level Pivot Zones
- Sub-level (pen-based) pivot zones
- Main-level (segment-based) pivot zones
- Big-level (trend-based) pivot zones
- Pivot extension detection and visualization
- Early pivot drawing options

## Trading Signal System

### Type I Signals (1buy/1sell)
Trend reversal based on divergence:
- Multiple divergence detection methods
- Configurable pivot/pen count requirements
- MACD area or amplitude comparison

### Type II Signals (2buy/2sell)
Pullback entries with chain detection:
- Standard Type II at position 2
- Type II chain continuation (position 4, 6, 8...)
- Automatic chain termination on Type III

### Type III Signals (3buy/3sell)
Breakout confirmations:
- Configurable search range (3-11 pens)
- Gap detection for confirmation
- Pivot zone boundary breaks

### Type I Divergence Methods
| Method | Description |
|--------|-------------|
| **Amplitude** | Compare price range |
| **MACD Area** | Compare MACD histogram sum |
| **Either** | Any method satisfied |
| **MACD Histogram** | Compare peak histogram values |

> **Pro Tip**: The MACD Histogram method works best when used together with the Chan Theory MACD sub-chart indicator, which visualizes the divergence relationship making it easier to confirm Type I signals visually.

## Signal Filtering System

### Fractal Filters
- **Fractal Validity Filter**: Post-fractal price confirmation
- **Basic Fractal Filter**: K-line color validation

### MACD Filters
- **Type I MACD Divergence**: Confirm divergence with MACD
- **Type II Zero-Axis**: MACD polarity confirmation

### Safety Filters
- **False Signal Avoidance (防狼术)**: MACD direction alignment

### Expected Signal System (预期买卖点)
A unique early-warning mechanism that identifies potential trading signals before full fractal confirmation:
- **Early Detection**: When a pen completes but the fractal is not yet confirmed, the system generates an "expected signal"
- **Multi-Timeframe Validation**: Traders can switch to the sub-timeframe or sub-sub-timeframe to check for divergence patterns, enabling earlier entries with confluence confirmation
- **Risk-Reward Optimization**: Expected signals allow preparation for high-probability setups while awaiting formal confirmation
- **Separate Alert Channels**: Expected and confirmed signals can be routed to different alert outputs for workflow customization

## Alert System
Comprehensive real-time notification system with full customization:

### Multi-Level Coverage
- All Type I/II/III buy/sell signals across 3 structural levels (sub-level, main-level, big-level)
- Separate tracking for each signal type and level combination

### Expected vs Confirmed Alerts
Configure alert behavior based on signal maturity:
| Mode | Description |
|------|-------------|
| **All Alerts** | Notify on both expected and confirmed signals |
| **Confirmed Only** | Alert only when fractal is formally confirmed |
| **Expected Only** | Early notification for anticipated signals |

### Custom Alert Combinations
Create personalized alert rules by combining signals from different levels:
- **Custom Alert 1 & 2**: Define your own trigger conditions
- **Main-Level + Sub-Level Combination**: e.g., alert when main-level shows 1buy AND sub-level shows 2buy
- **10 Signal Types Available**: 1buy/sell, 2buy/sell, 3buy/sell, L2buy/sell, L3buy/sell, or None

### Multi-Level Resonance Alerts
Detect powerful confluence when signals align across multiple levels:
- **Two-Level Resonance**: Sub-level + Main-level signal alignment
- **Three-Level Resonance**: Sub-level + Main-level + Big-level all showing same direction signals
- **Subscribe All Buy/Sell Points**: One-click subscription to all signals at any level

### Special Alerts
- **Type III Rapid Reversal**: Alerts when price reverses immediately after a Type III signal
- **Expected Type I Invalidation**: Alerts when expected Type I signal is invalidated due to no divergence (trend continuation)
- **Expected Type III Disappear**: Alerts when expected Type III signal disappears before confirmation

## Advanced Features

### Segment Correction Ratio
Filter insignificant segments using Fibonacci ratios:
- Options: 38.2%, 50%, 61.8%, 78.6%, or disabled
- When current segment amplitude < previous segment × ratio, correction is triggered

### Trend Breakthrough
When enabled, if a segment breaks the trend line's starting point in the opposite direction, a new trend line is generated instead of extending the existing one

### Trend Start Filter
Prevents premature level upgrades after trend line completion:
- Filter Mode: Only filter extremum breakthrough / Filter all single-pen segments
- Ensures first reversal pen after trend completion meets additional conditions

## Settings Reference

### Display Settings
| Setting | Description |
|---------|-------------|
| Show All Fractals | Display all top/bottom fractals |
| Running Pen | Show unconfirmed forming pen |
| K-line Merge | Visualize containment process |
| Right Containment | Enable right containment check |
| Fast Pen Mode | Allow pens without complete fractals |

### Pen Settings
| Setting | Description |
|---------|-------------|
| Pen Type | Classic/Optimized/4K/5K/Strict |
| Endpoint Mode | Strict or Secondary high/low |
| Fractal Overlap | Allow fractal overlap |
| Pen Extension Fix | Enable extension correction |

### Segment Settings
| Setting | Description |
|---------|-------------|
| Segment Mode | Dynamic/Strict/Extension |
| Trend Extension | Independent trend line extension |
| Super Trend Extension | Independent super trend extension |
| Feature Sequence Gap | Gap detection for repair |

### Pivot Settings
| Setting | Description |
|---------|-------------|
| Sub-level Pivot | Pen-based pivot zones |
| Main-level Pivot | Segment-based pivot zones |
| Big-level Pivot | Trend-based pivot zones |
| Early Draw | Preview forming pivots |

### Signal Settings
| Setting | Description |
|---------|-------------|
| Type II Chain | Continue Type II after position 4 |
| Type III Range | Search range (0-11 pens) |
| Divergence Method | Amplitude/MACD Area/Either/Histogram |
| Pivot Requirement | Pivot or pen count for Type I |

## Notes
- This script is for technical analysis reference only
- Does not constitute investment advice
- Users should make independent trading decisions
- Best used in conjunction with Chan Theory MACD Divergence indicator

---

# 概述
本指标是缠论(Chan Theory)结构分析的完整功能版本,使用Pine Script v6构建。提供全面的多级别结构识别,包括分型、笔、线段、趋势线、大趋势线,以及三个级别的中枢。指标提供高级买卖点检测、可配置的过滤选项和MACD背驰分析。

**为什么选择本指标?**
实际市场走势很少完全符合教科书模式。本指标通过以下功能解决这一问题:
- **单笔成段**:当一笔的力度足够强(突破前一级别极值或满足K线数量要求),可以直接升级为线段——无需等待标准的3笔结构
- **9笔线段成趋势**:包含9笔以上的线段可以升级为趋势线级别,实现更好的同级别分解
- **灵活的级别升级条件**:多种条件(突破极值、数量要求、或任一满足)让结构适应实际市场行为
- **4K/5K笔类型**:专为更好地拟合次周期和次次周期走势设计,使用更短的K线数量要求
- **顶底成笔**:当顶底分型力度较大时,即使K线数量不足也可根据振幅比例允许成笔
- **主图MACD背驰**:在K线图上直接显示背驰的笔和段,对比进入笔和离开笔的动量,可视化确认背驰
- **副图MACD指标**:配合缠论MACD副图指标使用,在MACD柱体上进行盘整背驰或趋势背驰的分析判断

这些功能能够更准确地拟合不规则的市场走势,实现正确的同级别分解,并更好地对齐次周期结构——这对于缠论的实战应用至关重要。

**核心功能:**
- 5种笔类型:老笔、新笔、4K、5K、严笔
- 3种线段模式:当下延伸后修正、严格模式、延伸模式
- 3级结构:线段 → 趋势线 → 大趋势线
- 3级中枢及延伸检测
- 完整的一二三类买卖点(覆盖所有级别)
- MACD背驰分析用于信号确认
- 高级信号过滤系统
- 所有买卖点的实时警报

## 核心功能

### 1. K线包含处理
高级包含处理,提供可配置选项:
- **左包含**:标准处理
- **右包含检查**:可选的额外验证
- 递归处理确保完全消除包含

### 2. 笔的构建(5种类型)
| 类型 | 说明 |
|------|------|
| **老笔** | 每笔至少5根处理后K线 |
| **新笔** | 4根处理后 + 5根原始K线 |
| **4K** | 4根原始K线 + 极值验证 |
| **5K** | 5根原始K线标准 |
| **严笔** | 5根K线 + 方向验证 |

### 3. 笔端点模式
- **严格最高最低点**:笔端点必须是绝对极值
- **允许次高次低点**:K线数量不足时灵活检测

### 4. 单笔成段
高级级别升级条件:
- **突破极值**:突破前一级别极值
- **数量条件**:满足K线/笔数量要求
- **任一满足**:灵活组合

### 5. 三级线段系统
- **线段**:由笔组成
- **趋势线**:由线段组成
- **大趋势线**:最高级别结构

### 6. 线段模式
| 模式 | 说明 |
|------|------|
| **当下延伸后修正** | 实时调整并修正 |
| **严格模式** | 完全符合经典定义 |
| **延伸模式** | 灵活的趋势延续 |

### 7. 三级中枢
- 次级别(基于笔)中枢
- 本级别(基于线段)中枢
- 大级别(基于趋势)中枢
- 中枢延伸检测和可视化
- 提前绘制中枢选项

## 买卖点系统

### 一类买卖点 (1buy/1sell)
基于背驰的趋势反转:
- 多种背驰检测方法
- 可配置中枢/笔数要求
- MACD面积或振幅比较

### 二类买卖点 (2buy/2sell)
回调入场,支持链式判断:
- 标准二类在第2位置
- 二类链延续(第4、6、8...位置)
- 出现三类时自动终止链

### 三类买卖点 (3buy/3sell)
突破确认:
- 可配置搜索范围(3-11笔)
- 缺口检测确认
- 中枢边界突破

### 一类背驰判断方式
| 方式 | 说明 |
|------|------|
| **振幅背驰** | 比较价格振幅 |
| **MACD面积背驰** | 比较MACD柱状图总和 |
| **振幅或MACD面积** | 任一满足 |
| **MACD柱体** | 比较柱状图峰值 |

> **使用技巧**:MACD柱体方式配合缠论MACD副图指标使用效果更佳,副图可视化背驰关系,更容易直观确认一类买卖点。

## 信号过滤系统

### 分型过滤
- **买卖点分型过滤**:分型后价格确认
- **买卖点分型基础过滤**:K线颜色验证

### MACD过滤
- **1买卖macd背驰过滤**:用MACD确认背驰
- **2买卖点过滤**:MACD极性确认

### 安全过滤
- **防狼术**:MACD方向一致性

### 预期买卖点系统
独特的早期预警机制,在完整分型确认之前识别潜在交易信号:
- **提前识别**:当笔完成但分型尚未正式确认时,系统生成"预期信号"
- **多周期验证**:交易者可切换至次周期或次次周期确认背驰形态,实现共振确认下的更早入场
- **优化风险回报**:预期信号允许在等待正式确认的同时,为高概率交易提前做好准备
- **独立警报通道**:预期信号和确认信号可分别路由至不同警报输出,满足个性化工作流需求

## 警报系统
全面的实时通知系统,支持完整自定义配置:

### 多级别覆盖
- 覆盖3个结构级别(次级别、本级别、大级别)的所有一二三类买卖点
- 每种信号类型和级别组合独立追踪

### 预期与确认警报分离
根据信号成熟度配置警报行为:
| 模式 | 说明 |
|------|------|
| **全部警报** | 预期信号和确认信号均通知 |
| **仅正式分型** | 仅在分型正式确认后发出警报 |
| **仅预期分型** | 提前通知预期信号 |

### 自定义警报组合
通过组合不同级别的信号创建个性化警报规则:
- **自定义警报1和2**:定义自己的触发条件
- **本级别+次级别组合**:例如当本级别出现1买且次级别出现2买时触发警报
- **10种信号类型可选**:1买/卖、2买/卖、3买/卖、类2买/卖、类3买/卖、或无

### 多级别共振警报
检测多级别信号对齐时的强势共振:
- **双级别共振**:次级别+本级别信号对齐
- **三级别共振**:次级别+本级别+大级别同时出现相同方向信号
- **一键订阅所有买卖点**:一键订阅任意级别的所有信号

### 特殊警报
- **三类买卖点急速反转**:三类信号出现后价格立即反转时发出警报
- **预期一类信号失效**:当预期一类信号因不背驰被清除时发出警报(趋势延续)
- **预期三类信号消失**:当预期三类信号在确认前消失时发出警报

## 高级功能

### 线段修正比例
使用斐波那契比例过滤不显著的线段:
- 选项:38.2%、50%、61.8%、78.6%、或禁用
- 当前线段振幅 < 前一线段 × 比例时,触发修正

### 趋势突破
启用后,当线段以相反方向突破趋势线起点时,生成新趋势线而非延伸现有趋势线

### 趋势起点过滤
防止趋势线完成后过早的级别升级:
- 过滤模式:仅过滤突破极值 / 过滤所有单笔段
- 确保趋势完成后的第一个反向笔满足额外条件

## 设置参考

### 显示设置
| 设置项 | 说明 |
|--------|------|
| 显示所有顶底 | 显示所有顶底分型 |
| 运行中的笔 | 显示未确认的形成中笔 |
| 显示K线合并过程 | 可视化包含处理 |
| 启用右包含检查 | 启用右包含检查 |
| 急速成笔 | 允许无完整分型成笔 |
| **递归设置(实验性)** | |
| 启用跨周期递归 | 使用低级别趋势线作为本级别笔 |
| 递归来源周期 | 指定低级别的周期(如1分钟) |

### 分型、笔设置
| 设置项 | 说明 |
|--------|------|
| 笔的类型 | 老笔/新笔/4K/5K/严笔 |
| 笔端点模式 | 严格或次高次低 |
| 允许分型重叠 | 允许分型重叠 |
| 笔延伸修正 | 启用延伸修正 |

### 线段设置
| 设置项 | 说明 |
|--------|------|
| 线段模式 | 当下延伸后修正/严格/延伸 |
| 趋势线延伸 | 独立趋势线延伸控制 |
| 大趋势线延伸 | 独立大趋势线延伸控制 |
| 特征序列缺口检测 | 缺口检测用于修复 |

### 中枢设置
| 设置项 | 说明 |
|--------|------|
| 次级别中枢 | 基于笔的中枢 |
| 本级别中枢 | 基于线段的中枢 |
| 大级别中枢 | 基于趋势的中枢 |
| 提前绘制 | 预览形成中的中枢 |

### 买卖点设置
| 设置项 | 说明 |
|--------|------|
| 启用类2链式判断 | 第4位置后继续类2 |
| 类3搜索范围 | 搜索范围(0-11笔) |
| 背驰判断方式 | 振幅/MACD面积/任一/柱体 |
| 中枢要求 | 一类的中枢或笔数要求 |

## 声明
- 本脚本仅供技术分析参考
- 不构成投资建议
- 用户应自行做出交易决策
- 建议结合缠论MACD背驰指标使用

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.