Files
Chan/CLAUDE.md
T

4.6 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)
  2. ChanKLC.py — Combined K-line: inclusion processing (包含处理), fractal (分型) detection
  3. ChanBI.py — Stroke (笔): basic trend unit connecting alternating fractals
  4. ChanSEG.py — Segment (线段): built from strokes
  5. ChanZS.py / ChanBIZS.py — Center/pivot (中枢): consolidation zones (stroke-level and segment-level)
  6. ChanBSP.py — Buy/Sell points (买卖点): Type 1/2/3 signals
  7. ChanLun.py — Main orchestrator: ties all steps together, entry point
  8. 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
  • ChanMACD.py / ChanMACDHistSet.py / ChanMACDSeg.py / ChanMACDUnitTF.py — MACD state analysis and divergence detection
  • ChanKLU.py — K-line unit with candlestick pattern recognition (Chan_KLU_PATTERN)
  • ChanPY.py — Consolidation (盘整) analysis
  • ChanHeng.py — Sideways market analysis
  • Chan_FX_Box.py — Fractal box (分型箱体) detection

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 (40+ 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 → 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