Skip to main content

chanlun — 缠论技术分析 Python 绑定

PyPI License: MIT

基于 chanlun Rust 核心库的 PyO3 高性能 Python 绑定,API 参考 chan.py 设计,高度兼容。

安装

pip install chanlun

快速开始

import chanlun

# 创建配置(全部默认值)
config = chanlun.缠论配置()

# 读取 K 线数据文件(文件名需遵循 `符号-周期-起始时间戳-结束时间戳.nb` 格式,如 `btcusd-300-1631772074-1632222374.nb`)
obs = chanlun.观察者.读取数据文件("path/to/btcusd-300-1631772074-1632222374.nb", config)

# 查看各层级序列
print(f"K线数量: {len(obs.普通K线序列)}")
print(f"笔数量: {len(obs.笔序列)}")
print(f"线段数量: {len(obs.线段序列)}")
print(f"中枢数量: {len(obs.中枢序列)}")

# 或使用立体分析器进行多周期分析
analyzer = chanlun.立体分析器("BTCUSD", [60, 60*5, 60*5*6], config)
# 逐根投喂 K 线...

从源码构建

前置依赖: Rust + maturin

pip install maturin

# 开发模式(直接安装到当前 venv)
maturin develop

# 或构建 wheel
maturin build --release
pip install target/wheels/chanlun-*.whl

也可使用项目内的 build.sh:

./build.sh develop   # 开发安装
./build.sh wheel     # 构建 wheel

导出类

类别 类名 说明
枚举 买卖点类型, 相对方向, 分型结构 缠论基础枚举
数据 缺口, K线, 缠论K线 K 线数据结构
结构 分型, 虚线, 线段特征, 特征分型 分析层级结构
指标 平滑异同移动平均线, 相对强弱指数, 随机指标 MACD/RSI/KDJ
算法 笔, 线段, 中枢, 背驰分析 识别算法
业务 缠论配置, 基础买卖点, 买卖点, 观察者, K线合成器, 立体分析器 分析框架

兼容性

  • Python 3.9+
  • 类名 / 方法名 / 字段名与 chan.py 保持一致
  • 支持 .nb 二进制文件格式(大端字节序)

性能配置

缓存模式

Python 对象缓存有两种模式,通过环境变量 CHANLUN_CACHE_MODE 或函数调用切换:

from chanlun._chanlun import set_cache_mode, get_cache_mode

# 默认:thread_local,每线程独立缓存,零锁,多线程场景最佳
print(get_cache_mode())  # "thread_local"

# 全局:dashmap 分片哈希表,跨线程 Python `is` 身份一致
set_cache_mode("global")  # 必须在创建任何观察者之前调用
# 环境变量方式
CHANLUN_CACHE_MODE=global python main.py   # 全局缓存
python main.py                              # 默认:线程局部缓存
模式 性能 Python is 跨线程 适用场景
thread_local(默认) 零锁,最快 否 批量回测、多线程独立分析
global dashmap 分片锁 是 测试验证、跨线程对象共享

日志模式

日志输出有三种模式,通过环境变量 CHANLUN_LOG_MODE 或函数调用切换:

from chanlun._chanlun import set_log_mode, set_log_level, get_log_mode

# 默认:off,不输出,零开销
print(get_log_mode())  # "off"

# 简单模式:直接 eprintln/println
set_log_mode("simple")
set_log_level("debug")  # 必需:设置日志级别启用输出

# Tracing 模式:带时间戳和文件位置格式化输出
set_log_mode("tracing")
set_log_level("debug")
# 环境变量方式
CHANLUN_LOG_MODE=simple python main.py     # 简单输出
CHANLUN_LOG_MODE=tracing python main.py    # 格式化输出
python main.py                              # 默认:静默
模式 输出方式 性能 格式
off(默认) 无 零开销 —
simple eprintln! / println! 极轻 纯文本
tracing tracing-subscriber 稍重 2026-06-12 01:57:59.942 WARN file.rs:line

观察者直传(避免 Python list 转换)

背驰分析新增 _OBS 后缀方法,直接接受观察者引用,跳过 list[K线] ↔ Vec<Arc<K线>> 转换:

# 旧方式:构建 Python 列表
result = 背驰分析.MACD背驰(进入段, 离开段, obs.普通K线序列, "总")

# 新方式:直接传观察者
result = 背驰分析.MACD背驰_OBS(进入段, 离开段, obs, "总")

许可

本项目主体采用 MIT 许可。包含以下第三方开源代码:czsc(Apache 2.0)、parse(MIT)、termcolor(MIT)。

详见 NOTICE 和 LICENSES/ 目录。

Metadata

Release files for chanlun 2606.73

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for chanlun 2606.73
File Size Uploaded
chanlun-2606.73.tar.gz 1.3 MB Details

Built distributions (wheels)

Table of built distributions (wheels) for chanlun 2606.73
File
chanlun-2606.73-cp312-cp312-win_amd64.whl CPython 3.12 CPython 3.12 Windows x86-64 Details
chanlun-2606.73-cp312-cp312-macosx_11_0_arm64.whl CPython 3.12 CPython 3.12 macOS 11.0+ ARM64 Details
chanlun-2606.73-cp312-cp312-macosx_10_12_x86_64.whl CPython 3.12 CPython 3.12 macOS 10.12+ x86-64 Details
chanlun-2606.73-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.9 CPython 3.9 Linux glibc 2.17+ x86-64 Details

Total release size: 7.5 MB

Release files / chanlun-2606.73.tar.gz

Download URL chanlun-2606.73.tar.gz
Size 1.3 MB
Tags Source
SHA-256 checksum
How to use checksums
67061a6713483243e1d89798a7179828f3a5995e8efcd94cbed1cfc310454134
BLAKE2b-256 checksum
How to use checksums
a3a0f657fbd27a2f7c3b9959b5eda0f2a566586f95d1d0f1e98ef78de3a0271f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 12, 2026.

Transparency log

Release files / chanlun-2606.73-cp312-cp312-win_amd64.whl

Download URL chanlun-2606.73-cp312-cp312-win_amd64.whl
Size 1.4 MB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
10c5239fe5aab9dea1dd70312ff7cd0bdf392e0e2dbf2f067af81f18674e5b3b
BLAKE2b-256 checksum
How to use checksums
01fae5b6428595b66def88ad891036f250ff5c46dd2efcdf46fb18c09c6e3034
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 12, 2026.

Transparency log

Release files / chanlun-2606.73-cp312-cp312-macosx_11_0_arm64.whl

Download URL chanlun-2606.73-cp312-cp312-macosx_11_0_arm64.whl
Size 1.5 MB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
1011e5dd8cbcc8a0f3256c1ed2b7ed38c98088d526bec286ebec71a52d203ecf
BLAKE2b-256 checksum
How to use checksums
8ea07cc7e50dfb3c4b69d4da24129ce65b56649da224e52bb31d5129c682839a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 12, 2026.

Transparency log

Release files / chanlun-2606.73-cp312-cp312-macosx_10_12_x86_64.whl

Download URL chanlun-2606.73-cp312-cp312-macosx_10_12_x86_64.whl
Size 1.6 MB
Tags CPython 3.12 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
45254e250447d893a21087c4ca1ee91765e3899786e14493533572fc06eb5ff8
BLAKE2b-256 checksum
How to use checksums
057a67cd7f0ec6d6082a573ec4ed3c4740cb49b79ca7571a1c053695cf16fb07
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 12, 2026.

Transparency log

Release files / chanlun-2606.73-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL chanlun-2606.73-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 1.7 MB
Tags CPython 3.9 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
66358c735ba17ebddc8e3f831dab812c2f26e60f167e3657b827207900214874
BLAKE2b-256 checksum
How to use checksums
ca66ddf3ac03b5d4b315b29b013bd9303e1ed867d0a458502ee5bd417c6513fe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 12, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

2606.73 This release

5 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page