A complete Python implementation of Chan Theory (缠中说禅) technical analysis — from raw K-line data to buy/sell signals, position management, and backtesting.
Chan Theory is a recursive geometric framework for analyzing financial markets, developed from 108 blog posts by "缠中说禅" (Chán Zhōng Shuō Chán). It decomposes market movement into self-similar structures at multiple levels:
K-line → Containment → Fractal → Bi (Stroke) → Segment → ZhongShu (Pivot Zone) → Trend
Key features:
- Recursive self-similarity: Structure at each level mirrors the levels above and below
- Complete classification: Every market move can be categorized as consolidation, uptrend, or downtrend
- MACD-area divergence: Measures move "force" as area rather than point-to-point comparison
- Three buy/sell point types: Each with defined entry conditions and stop-loss rules
pip install -e .from chan.analyzer import ChanAnalyzer
from chan.data.fetcher import fetch_a_share_daily
# Fetch data (requires akshare)
bars = fetch_a_share_daily("000001", start_date="20230101")
# Run analysis
analyzer = ChanAnalyzer(min_signal_score=0)
result = analyzer.analyze(bars)
print(result.summary())
for signal in result.signals:
print(signal)PYTHONPATH=. python examples/basic_usage.pystreamlit run web_app.py功能:输入股票或 ETF 代码,自动完成缠论结构分析,展示关键点位(ZG/ZD、近20K高低点)、趋势判断与买卖建议,并绘制包含分型/笔/中枢/信号的结构图。
from chan.backtest.engine import BacktestEngine, BacktestConfig
config = BacktestConfig(
initial_capital=1_000_000,
commission_rate=0.0003,
symbol="000001",
)
engine = BacktestEngine(config)
result = engine.run(bars)
print(result.summary())| Module | Purpose |
|---|---|
chan/kline.py |
K-line containment processing |
chan/fractal.py |
Top/bottom fractal identification |
chan/bi.py |
Bi (stroke) construction |
chan/segment.py |
Segment construction via characteristic sequence |
chan/zhongshu.py |
ZhongShu (pivot zone) identification |
chan/macd.py |
MACD calculation and area measurement |
chan/divergence.py |
Trend and consolidation divergence detection |
chan/signals.py |
Three-type buy/sell signal generation |
chan/position.py |
Progressive position management (1B→2B→3B) |
chan/risk.py |
Four-layer risk control system |
chan/analyzer.py |
Pipeline orchestrator |
chan/backtest/ |
Backtesting engine and performance metrics |
chan/data/fetcher.py |
A-share data via akshare, CSV loading |
| Signal | Chinese | Trigger Condition |
|---|---|---|
| 1B (First Buy) | 一买 | Trend bottom divergence (≥2 ZhongShu, MACD area declining) |
| 2B (Second Buy) | 二买 | Pullback after 1B doesn't break 1B low |
| 3B (Third Buy) | 三买 | ZhongShu breakout retest stays above ZG |
| 1S (First Sell) | 一卖 | Trend top divergence |
| 2S (Second Sell) | 二卖 | Bounce after 1S doesn't exceed 1S high |
| 3S (Third Sell) | 三卖 | ZhongShu breakdown retest stays below ZD |
pytest tests/ -vAll parameters are centralized in chan/config.py. Key defaults:
- MACD: (12, 26, 9)
- Min Bi bars: 5 (new-bi rule)
- Divergence threshold: area ratio < 0.7
- Max position: 80% of capital
- Max single trade loss: 2% of capital
MIT