Files
Chan/CLAUDE.md
T
jackyu66gitandClaude Opus 4.6 ebcb3dce73 添加 StructureZone 结构价值区系统,支持多周期支撑/阻力分析
- 新增 ChanZone.py: 从笔中枢/线段中枢/EMA52 提取价格区,聚类评分
- ChanLun.py 新增 get_structure_zones() 方法
- web/app.py: 独立拉取多周期数据 + 缓存 + limit 传参避免全量传输
- web/index.html: 结构区勾选框 + K线数量输入 + 半透明填充区绘制
- tests/test_chan_zone.py: 24 个单元测试

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-05-12 01:31:56 +08:00

5.0 KiB

CLAUDE.md

This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.

Project Overview

缠论 (Chan Theory) technical analysis system for Freqtrade. Implements Chan Zhong Shui Chan's theory for crypto/stock trading, including fractal (分型), stroke (笔), segment (线段), pivot/center (中枢), and buy/sell point (买卖点) detection.

Core Architecture

Chan Theory Engine (Chan*.py)

Data processing pipeline (each step feeds the next):

  1. ChanKLU.py — Raw K-line unit with TA indicators (EMA, MACD, RSI, Bollinger Bands) and candlestick pattern recognition (Chan_KLU_PATTERN)
  2. ChanKLC.py — Combined K-line: inclusion processing (包含处理), fractal (分型) detection. Linked-list structure with .next/.pre pointers
  3. ChanBI.py — Stroke (笔): basic trend unit connecting alternating fractals
  4. ChanSBI.py — Special Stroke: aggregates multiple BI into higher-level units with fractal detection, feeds into SEG
  5. ChanSEG.py — Segment (线段): built from SBI strokes
  6. ChanZS.py / ChanBIZS.py — Center/pivot (中枢): consolidation zones (segment-level and stroke-level)
  7. ChanBSP.py — Buy/Sell points (买卖点): Type 1/2/3 signals
  8. ChanLun.py — Main orchestrator: ties all steps together, entry point
  9. TF_DF.py — Timeframe-aware DataFrame processor: resamples data, runs the full pipeline per timeframe, handles multi-timeframe analysis

Support modules

  • ChanEnum.py — All enumerations: K-line types, fractal types, MACD states, buy/sell point types, EMA position/semantic states, K-line patterns
  • ChanCTime.py — Chan theory time utility: auto-adaptive day understanding (e.g. crypto 24h vs stock market hours)
  • ChanMACD.py / ChanMACDHistSet.py / ChanMACDSeg.py / ChanMACDUnitTF.py — MACD state analysis and divergence detection
  • ChanPY.py — Consolidation (盘整) analysis
  • ChanHeng.py — Sideways market analysis
  • Chan_FX_Box.py — Fractal box (分型箱体) detection
  • ChanLun_Classifier.py — Standalone classifier script: runs full pipeline and classifies market states

Services

  • data_provider/ — FastAPI data service: fetches crypto data from Binance via CCXT, caches to CSV, serves REST API + WebSocket. Synthesizes derived timeframes (e.g. 5m/15m/4h from 1m/1h base). Port 9009.
  • web/ — Flask web UI for interactive chart visualization with Chan theory overlays. Port 8123.
  • strategies/ — Freqtrade trading strategies using the Chan theory engine (53 strategies)
  • config/ — Freqtrade JSON config files per pair/timeframe

Data Flow

Exchange (CCXT) → data_provider (CSV cache) → Freqtrade → Strategy → ChanLun → TF_DF
                                                                              → KLU → KLC → BI → SBI → SEG → ZS → BSP

Common Commands

Freqtrade Trading

# Live trade
freqtrade trade -c ./user_data/Chan/config/<config>.json --strategy <StrategyName> --strategy-path ./user_data/Chan/strategies

# Backtest
freqtrade backtesting -c ./user_data/Chan/config/<config>.json --strategy <StrategyName> --strategy-path ./user_data/Chan/strategies --timerange=20251008-

# Download data
freqtrade download-data -c ./user_data/Chan/config/<config>.json -t 1m 1h 1d --pairs BTC/USDT:USDT --timerange=20240101-

# Hyperopt
freqtrade hyperopt --hyperopt-loss SharpeHyperOptLossDaily --spaces roi --strategy <StrategyName> --strategy-path ./user_data/Chan/strategies -c ./user_data/Chan/config/<config>.json -e 200 --timerange=20250201-20250901

# Plot
freqtrade plot-dataframe --strategy <StrategyName> --datadir user_data/data/binance -c ./user_data/Chan/config/<config>.json --timerange=20250721-

Data Provider

# Docker
cd data_provider && docker compose up -d

# Direct
cd data_provider && python main.py

# With custom config
CONFIG_PATH=./config.json python main.py

Web UI

cd web && python app.py
# or via gunicorn:
gunicorn -w 4 -b 0.0.0.0:8123 app:app

# Deploy scripts:
cd web && ./deploy.sh          # standard
cd web && ./deploy_venv.sh     # Ubuntu 22.04+ (venv)

Docker (Freqtrade)

sudo docker compose run --rm chanlun_btc backtesting -c ./user_data/Chan/config/<config>.json --strategy <StrategyName> --strategy-path ./user_data/Chan/strategies --timerange=20250721-

Key Conventions

  • All Chan theory classes are prefixed with Chan (e.g., ChanBI, ChanZS)
  • Strategies import ChanLun and add sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) to import from parent
  • MACD params: MACD(26, 52, 9) by default (slow period 52 instead of standard 26)
  • Enums in ChanEnum.py use auto() values
  • ChanKLC is a linked-list style data structure with .next/.pre pointers
  • The TF_DF class is the primary data container per timeframe
  • K-line direction uses Chan_KLINE_DIR (UP/DOWN/COMBINE/INCLUDED)
  • All text comments/commits are in Chinese