Vespa314/chan.py

开放式的缠论python实现框架,支持形态学/动力学买卖点分析计算,多级别K线联立,区间套策略,可视化绘图,多种数据接入,策略开发,交易系统对接;

chan.py – A Python framework for Chan‑Theory‑based market analysis and trading

What it is – chan.py is an open‑source Python library that implements the Chan‑Theory (缠论) technical‑analysis methodology. It provides a full‑stack pipeline:

  • Low‑level calculations of Chan elements – fractals, bi (strokes), seg (segments), zs (central zones), and buy‑sell points (both classic morphological points and custom strategy‑driven points).
  • Feature engineering – >500 engineered features per buy‑sell point, ready for machine‑learning models.
  • Model integration – wrappers for XGBoost, LightGBM and a simple MLP, plus an AutoML hyper‑parameter search utility.
  • Back‑testing & evaluation – a configurable back‑test engine with parameter‑sweep scripts.
  • Live trading – connectors for the Futu brokerage API (simulated and real‑time), plus realtime price feeds (Sina, pytdx, akshare, etc.) and database back‑ends (MySQL, SQLite).
  • Visualization – Matplotlib‑based static plots and an animation driver for step‑by‑step replay.

Key components (by folder)

Folder Purpose
Bi, Seg, ZS Core Chan elements (strokes, segments, central zones) with configurable algorithms.
BuySellPoint, CustomBuySellPoint Classic morphological buy‑sell points (bsp) and user‑defined strategy points (cbsp).
ModelStrategy Model training, feature reconciliation, AutoML, and back‑testing utilities.
ChanModel Abstract model class, feature registry, and a demo XGBoost model.
DataAPI Unified data‑access layer – akshare, baostock, Futu, offline CSV, and realtime snapshot APIs.
Trade Trade engine, order execution, position sizing, and database persistence.
Plot Plot drivers (CPlotDriver for static images, CAnimateDriver for animated replay) and COS upload helpers.
Debug Example strategy scripts, performance‑profiling tools, and notebooks.
App Example GUI scanner built on top of the library.

Installation (Python 3.11+)

# clone the repo
git clone https://github.com/Vespa314/chan.py.git
cd chan.py
# install required packages (the repo ships a requirements.txt)
pip install -r requirements.txt

The library requires Python 3.11 (the authors report ~16 % speed improvement over 3.8).


Quick start (static analysis & plot)

from Chan import CChan
from ChanConfig import CChanConfig
from Plot.PlotDriver import CPlotDriver

# basic configuration – all options are passed as a dict
cfg = CChanConfig({
    "zs_combine": True,
    "bi_strict": True,
    "seg_algo": "chan",
    "trigger_step": False,
})

# instantiate the core engine – here we request daily K‑lines for HK.00700
chan = CChan(
    code="HK.00700",
    begin_time="2012-01-01",
    end_time=None,
    data_src=DATA_SRC.FUTU,
    lv_list=[KL_TYPE.K_DAY],
    config=cfg,
    autype=AUTYPE.QFQ,
)

# plot everything (k‑line, bi, seg, zs, buy‑sell points)
plot_cfg = {
    "plot_kline": True,
    "plot_bi": True,
    "plot_seg": True,
    "plot_zs": True,
    "plot_bsp": True,
    "plot_cbsp": True,
}
plot_para = {"figure": {"width": 24}}

CPlotDriver(chan, plot_config=plot_cfg, plot_para=plot_para)

Running the script computes all Chan elements for the requested period and opens a Matplotlib window with the full visualisation.


Using a machine‑learning model to score signals

from ChanModel.XGBModel import CXGBModel

# attach a trained XGBoost model (the demo model ships with the repo)
model = CXGBModel(model_path="ModelStrategy/models/Xgboost/model.bin")
cfg.model = model          # enable model scoring
cfg.cal_feature = True     # force feature calculation
cfg.score_thred = 0.6      # only act on signals with score ≥ 0.6

The model receives the 500+ features generated for each candidate buy‑sell point and returns a probability‑like score that can be used to filter or rank signals.


Live trading workflow

  1. Data feed – configure a FutuAPI (or any of the other snapshot APIs) in Config/demo_config.yaml.
  2. Strategy – either use the built‑in CCustomStrategy/CSegBspStrategy or implement your own class inheriting from Strategy.
  3. Trade engine – TradeEngine consumes the generated signals, applies position‑sizing (OpenQuotaGen), and sends orders via FutuTradeEngine.
  4. Persistence – trade logs are stored in MySQL or SQLite as defined in the config.
  5. Monitoring – RealTimeTracker watches open positions for stop‑loss / take‑profit events.

When to use this library

  • Quant researchers who need a faithful implementation of Chan‑Theory for feature generation and back‑testing.
  • Algorithmic traders looking for an end‑to‑end stack: data ingestion → signal generation → ML scoring → live order execution.
  • Educators / students interested in a concrete, production‑grade example of a finance‑focused AI pipeline.

License & community

The project is released under the MIT license. A Telegram group (https://t.me/zen_python) is provided for questions, and the repository includes a quick_guide.md for onboarding.


All details above are taken directly from the repository’s README; no additional features have been inferred.

Related

  • Project
  • Project
  • Project
  • Project