How a Chinese Stock Analysis Theory Migrated to Rust
In Asian algorithmic trading, there's a popular technical analysis concept called Chan Lun (Chan Lun, or the 'Chan Zhong Shuo Chan' theory). Stripping away the philosophical fluff about Zen and Eastern wisdom, at its core it's a strict geometric model of fractal decomposition of price charts into patterns: peaks, troughs, segments, and so-called central consolidation zones.
The czsc library brought these algorithms into code. The project was originally written in pure Python, but when it came to iterating through tick data and handling hundreds of signals on the fly, the interpreter couldn't keep up. In the 1.0 branch, the authors rewrote the entire math core in Rust, leaving familiar Python bindings on the outside via PyO3.
Let's break down how this tool works and why it's interesting beyond the Chinese market.
Under the Hood
The project architecture is clearly split into two layers: a fast computational layer in Rust and a user interface in Python.
The Rust layer (czsc._native) handles the heavy lifting:
- Basic data structures and merging bars into higher timeframes
- Geometric pattern recognition for Chan theory (fractals
FX, segmentsBI, zonesZS) - A library of over 220 ready-made signal functions
- Technical indicators (SMA, EMA, Bollinger Bands)
The Python layer organizes the workflows:
- Connectors to data sources like Tushare, TQSdk, and crypto exchanges via CCXT
- Strategy building and composition of trading events
- Generation of standalone HTML reports with interactive charts
This approach delivers a decent speed boost: parsing tens of thousands of candles and generating signals happens without the typical Python delays.
Quick Start
Python 3.10 or higher is required. If installing from PyPI, a Rust compiler isn't needed since the author builds ready-made wheel packages for popular platforms:
If building from source, you'll need a recent cargo and the maturin utility:
Working with Charts and Pattern Recognition
The library accepts quotes as a standard pandas.DataFrame, then converts them to RawBar structures and calculates price movement geometry:
The CZSC object automatically filters internal bars (the mechanics of one price range encompassing another), finds local extrema, and assembles directional segments from them.
Higher Timeframe Synthesis and Signal Generation
One handy utility inside the library is BarGenerator. It lets you feed minute bars in a stream and get synchronized candles for 5-minute, 30-minute, or daily intervals on the fly:
After synthesizing timeframes, you can attach ready-made signals. Over two hundred patterns are baked into the repository, implemented in Rust:
Visualization and Backtesting
Looking at raw arrays of points is inconvenient. czsc has a built-in visualization module based on plotly and lightweight-charts. It generates a ready self-contained HTML file with candlesticks overlaid with segments and entry points:
For strategy verification, the author extracted return calculations into a separate package called wbt (Weight Back Test), which is a hard dependency. The model operates on changes in target portfolio weights over time rather than discrete orders, which simplifies scaling to a basket of instruments.
Nuances and Rough Edges
The project has specifics worth knowing before you start:
- Almost all documentation, code comments, and signal names are in Chinese. To understand the logic of some functions, you'll need a translator at hand.
- Strong coupling to Asian data sources (Tushare, Tianqin), although CCXT availability smooths this over for those trading cryptocurrency.
- Version 1.0 broke backward compatibility with the 0.9 branch. If you've worked with czsc before, you'll need to rewrite your code for the new API.
Who Will Find This Project Useful
The library will be useful for quant developers and traders looking for an alternative to classic indicators like RSI or MACD. Chan Lun provides strict formalization of support levels and trend structures, and the Rust core inside czsc eliminates the need to write heavy chart geometry calculations from scratch. To get started, basic Python knowledge and an understanding of how price time series work is enough.
Related projects