基于Python的本地量化交易框架,提供数据获取、策略回测、机器学习预测和风险管理等功能。
datadownMeizhu/ ├── src/ # 源代码 │ ├── data/ # 数据层 │ │ ├── downloader.py # 数据下载(股票/ETF/指数) │ │ ├── checker.py # 数据检查 │ │ ├── migrator.py # 数据迁移 │ │ ├── preprocess.py # 数据预处理 │ │ ├── feature_engineer.py # 特征工程 (85+技术指标) │ │ ├── chan_theory.py # 缠论特征生成器 │ │ ├── alpha101.py # Alpha101因子计算 │ │ ├── alpha_ops.py # Alpha操作函数 │ │ ├── alpha021_040.py # Alpha021-040 │ │ ├── alpha041_060.py # Alpha041-060 │ │ ├── alpha061_080.py # Alpha061-080 │ │ ├── alpha081_101.py # Alpha081-101 │ │ └── technical_indicators.py │ ├── models/ # 模型层 │ │ ├── factory.py # 模型工厂 │ │ ├── trainer.py # 训练器 │ │ ├── predictor.py # 预测器 │ │ ├── tcn.py # TCN模型 │ │ ├── cnnst.py # CNN+ST Transformer │ │ ├── small_cnn.py # 小特征CNN │ │ └── cnn_transformer.py │ ├── strategy/ # 策略层 │ │ ├── base_strategy.py # 策略基类 │ │ ├── ml_strategy.py # 统一ML策略 │ │ └── traditional/ # 传统策略 │ ├── backtest/ # 回测层 │ │ ├── backtest.py # 统一回测引擎 │ │ ├── backtest_bt.py # Backtrader适配器 │ │ ├── metrics.py # 收益指标计算 │ │ └── pbo.py # PBO分析 │ ├── risk/ # 风控层 │ └── utils/ # 工具 ├── config/ # 配置文件 ├── data/ # 数据存储 ├── sql/ddl/ # 数据库建表脚本 ├── results/ # 回测结果 ├── logs/ # 日志文件 ├── docs/ # 文档 └── tests/ # 测试 └── test_alpha101.py # Alpha因子测试
pip install -r requirements.txt
# 下载股票数据
python src/main.py data download --source yfinance --limit 100
# 下载ETF数据(akshare,无频率限制)
python src/main.py data download_etf --source yfinance --start 2015-01-01
# 下载指数数据
python src/main.py data download_index --source yfinance --start 2015-01-01
# 检查数据
python src/main.py data check
# 默认85个技术指标
python src/main.py preprocess --stocks 500
# 仅15个关键特征
python src/main.py preprocess --stocks 500 --small_features
# 仅5个OHLCV基础特征
python src/main.py preprocess --stocks 500 --basic_features
# TCN模型
python src/main.py train --model tcn --stocks 500 --epochs 80
# CNN+ST Transformer
python src/main.py train --model cnnst --stocks 500 --basic_features
# 小特征CNN(15个特征)
python src/main.py train --model small_cnn --stocks 500 --small_features
# 单股票独立回测(每只10万)
python src/main.py backtest --model_dir checkpoints_tcn --stocks 100 --threshold 0.5 --mode single
# 组合回测(总资金10万,均分)
python src/main.py backtest --model_dir checkpoints_tcn --stocks 100 --threshold 0.5 --mode combined
# 精选回测(按置信度排序,每日最多N只)
python src/main.py backtest --model_dir checkpoints_tcn --stocks 100 --threshold 0.5 --mode limited --max_stocks 5
# 全部模式
python src/main.py backtest --model_dir checkpoints_tcn --stocks 100 --threshold 0.5 --mode all
# 测试集验证
python src/main.py validate --mode test
# 验证集验证
python src/main.py validate --mode val
基于缠论数学量化的技术分析特征:
from src.data.chan_theory import ChanTheory
ct = ChanTheory()
features = ct.generate_features(df)
# 输出22个特征:
# - 分型: fractal_type, fractal_strength
# - 笔: bi_number, bi_direction, bi_length
# - 中枢: zs_exists, zs_zg, zs_zd, zs_height
# - 背驰: beichi_type, beichi_strength
# - 买卖点: maimai_signal, maimai_strength
WorldQuant经典量化因子库,101个公式化Alpha因子:
from src.data.alpha101 import Alpha101
a101 = Alpha101()
# 计算全部101个
factors = a101.compute_all(df)
# 计算指定因子
factors = a101.compute_selected(df, ['alpha_001', 'alpha_050', 'alpha_101'])
# 单个因子
factor = a101.alpha_001(df)
因子类型:动量、量价相关、反转、波动率、交易量等
三种特征可叠加使用:
| 特征集 | 数量 | 来源 |
|---|---|---|
| 技术指标 | 85+ | feature_engineer.py |
| 缠论特征 | 22 | chan_theory.py |
| Alpha101 | 101 | alpha101.py |
合计: 208个特征
| 表名 | 说明 |
|---|---|
daily_data | 股票日线数据 |
stock_info | 股票信息 |
technical_indicators | 技术指标 |
etf_data | ETF日线数据 |
etf_info | ETF信息 |
a_share_index_daily_data | A股指数日线数据 |
a_share_index_info | A股指数信息 |
运行 python sql/run_ddl.py 创建所有表。
回测结果自动保存到 results/ 目录,每次回测创建独立文件夹(带时间戳):
results/ └── backtest_2025-01-01_2026-03-20_20250401_153045_single/ ├── trades.csv # 交易记录 ├── capital_curve.csv # 资金曲线 ├── summary.json # 汇总指标 └── metadata.json # 配置参数
| 模型 | 特征数 | 参数量 | 说明 |
|---|---|---|---|
| TCN | 85 | ~100K | 时序卷积网络 |
| LightweightTCN | 85 | ~50K | 轻量级TCN |
| CNN-Transformer | 85 | ~500K | 混合架构 |
| CNN_ST_Transformer | 5 | ~46K | CNN+时空Transformer |
| SmallCNN | 15 | ~286K | 小特征CNN(残差连接) |
# 运行Alpha因子测试
python tests/test_alpha101.py
MIT License