logo
0
0
WeChat Login

LocalQuant - 量化交易系统

基于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因子测试

快速开始

1. 安装依赖

pip install -r requirements.txt

2. 数据下载

# 下载股票数据 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

3. 数据预处理

# 默认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

4. 训练模型

# 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

5. 回测策略

# 单股票独立回测(每只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

6. 验证模型

# 测试集验证 python src/main.py validate --mode test # 验证集验证 python src/main.py validate --mode val

特色功能

缠论特征 (Chan Theory)

基于缠论数学量化的技术分析特征:

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

Alpha101因子

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
缠论特征22chan_theory.py
Alpha101101alpha101.py

合计: 208个特征

数据库表

表名说明
daily_data股票日线数据
stock_info股票信息
technical_indicators技术指标
etf_dataETF日线数据
etf_infoETF信息
a_share_index_daily_dataA股指数日线数据
a_share_index_infoA股指数信息

运行 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 # 配置参数

支持的模型

模型特征数参数量说明
TCN85~100K时序卷积网络
LightweightTCN85~50K轻量级TCN
CNN-Transformer85~500K混合架构
CNN_ST_Transformer5~46KCNN+时空Transformer
SmallCNN15~286K小特征CNN(残差连接)

测试

# 运行Alpha因子测试 python tests/test_alpha101.py

技术栈

  • 数据处理:pandas, numpy
  • 机器学习:PyTorch, scikit-learn
  • 金融数据:yfinance, akshare
  • 回测框架:backtrader(可选)
  • 数据库:psycopg2 (PostgreSQL)
  • 日志:loguru
  • 测试:unittest

License

MIT License

About

大A数据下载。

1.67 GiB
0 forks0 stars1 branches0 TagREADMEMIT license
Language
Python100%