$ show Vespa314/chan.py
This is a Python library for technical analysis of financial markets based on 'Chan Theory' (įž čŪš). It computes complex structural elements such as fractals, strokes (bi), line segments, trading ranges (zhongshu), and buy/sell points from candlestick data. The library supports multiple data sources (BaoStock, Akshare, CSV, CCXT), multi-level analysis, customizable algorithms, and plotting via Matplotlib. The code is well-structured with clear separation of concerns, extensive use of caching for performance, and a flexible configuration system. It includes a GUI scanner application for A-shares and demo strategies. The library is designed for both research and production use, with incremental calculation capabilities for real-time updates. While the open-source version omits trading engine and ML features described in the full version, the core analytical engine is robust and reusable.
The LLM identified 5 files worth highlighting for reuse:
A clean memoization decorator that caches method results on a per-instance basis. It uses a `_memoize_cache` attribute and supports only zero-argument methods (except self). The implementation is thread-safe for single-threaded use and provides a clear pattern for improving performance in computational-intensive code. It can be extracted and reused in any Python project requiring method-level caching.
Implements MACD indicator with incremental update. Maintains internal state (previous EMA values) to compute new MACD items in O(1) per added data point, avoiding full recalculation. The class is self-contained, takes only period parameters, and returns a CMACD_item with DIF, DEA, and MACD histogram. This pattern is reusable for any streaming indicator.
A generic combiner class that merges sequential items (K-line units, bi strokes) into combined elements based on directional logic (up/down/combine). It supports exclude_included mode and can determine fractal types (top/bottom) by comparing three consecutive combined items. The pattern is fully reusable for any time-series data that requires merging based on high/low relationships.
Implements feature sequence (įđåūåšå) detection for line segment analysis. This is a complex algorithm that processes a stream of bi strokes to identify when a line segment is terminated by a feature sequence fractal. It handles gap jumps, excluded-included merging, and supports both deterministic and probabilistic segment boundaries. The code is well-abstracted and could be reused in other domains requiring similar pattern matching on directional sequences.
A generic framework for computing buy/sell points (bsp) from a list of strokes and segments. It implements Type 1, 2, and 3 buy/sell points with configurable parameters (divergence rate, retracement ratios, etc.). The class is generic over the line type (bi or segment), allowing reuse for both stroke-level and segment-level point detection. The algorithm is optimized with incremental updates and can be easily extended with custom point types.