一个系统化的因子挖掘系统,为freqtrade量化交易策略提供数据驱动的决策支持。
factor/
├── src/
│ ├── data/ # 数据管理模块
│ │ ├── collectors/ # 数据采集器
│ │ ├── processors/ # 数据处理器
│ │ └── storage/ # 数据存储
│ ├── factors/ # 因子计算模块
│ │ ├── base/ # 基础因子类
│ │ ├── technical/ # 技术因子
│ │ ├── fundamental/ # 基本面因子
│ │ └── alternative/ # 另类因子
│ ├── evaluation/ # 因子评估模块
│ │ ├── metrics/ # 评估指标
│ │ ├── backtesting/ # 回测引擎
│ │ └── analysis/ # 分析工具
│ ├── strategy/ # 策略生成模块
│ │ ├── generators/ # 策略生成器
│ │ └── freqtrade/ # freqtrade集成
│ ├── api/ # API接口
│ │ ├── routers/ # 路由模块
│ │ └── schemas/ # 数据模型
│ ├── monitoring/ # 监控模块
│ │ ├── alerts/ # 预警系统
│ │ └── reports/ # 报告生成
│ ├── config/ # 配置管理
│ └── utils/ # 工具函数
├── frontend/ # 前端界面
├── tests/ # 测试代码
├── docs/ # 文档
├── scripts/ # 脚本工具
├── docker/ # Docker配置
└── requirements.txt # Python依赖
# 创建虚拟环境
python -m venv venv
source venv/bin/activate # Linux/Mac
# 或 venv\Scripts\activate # Windows
# 安装依赖
pip install -r requirements.txt# 直接启动(使用默认配置)
python run.py
# 或者使用uvicorn启动
python -m uvicorn src.api.main:app --reload --port 8000- API文档: http://localhost:8000/docs
- 系统健康检查: http://localhost:8000/health
# 启动完整系统(包括数据库、缓存等)
docker-compose up -d
# 查看日志
docker-compose logs -f factor-mining
# 停止服务
docker-compose down启动后可访问:
- 因子挖掘API: http://localhost:8000/docs
- Grafana监控: http://localhost:3000 (admin/admin)
# 构建镜像
docker build -t factor-mining .
# 运行容器
docker run -p 8000:8000 factor-mining- ✅ 多交易所数据采集 (Binance, OKX)
- ✅ 实时市场数据获取
- ✅ 历史数据回填
- ✅ 数据质量检查
- ✅ 40+ 技术因子库
- 动量类因子 (10+): 价格动量、RSI动量、MACD动量等
- 波动率因子 (10+): 历史波动率、ATR、GARCH波动率等
- 反转类因子 (10+): 短期反转、RSI反转、布林带反转等
- ✅ 自定义因子开发框架
- ✅ 因子批量计算API
- ✅ IC分析 (信息系数)
- ✅ 因子回测引擎
- ✅ 分层回测分析
- ✅ 多空组合构建
- ✅ 性能指标计算
- ✅ 因子排名系统
- ✅ freqtrade策略生成
- ✅ 实时监控预警
- ✅ 可视化分析界面
# 运行基础功能测试
python examples/simple_test.py
# 运行API客户端测试(需要先启动服务)
python examples/api_client_demo.pyimport aiohttp
import asyncio
async def get_factor_data():
async with aiohttp.ClientSession() as session:
# 获取因子列表
async with session.get("http://localhost:8000/api/v1/factors/list") as resp:
factors = await resp.json()
print(f"可用因子: {factors['count']} 个")
# 计算动量因子
params = {"symbol": "BTC/USDT", "timeframe": "1h", "limit": 100}
async with session.post(
"http://localhost:8000/api/v1/factors/calculate/momentum_20",
params=params
) as resp:
result = await resp.json()
print(f"动量因子计算结果: {result['statistics']}")
asyncio.run(get_factor_data())# 使用curl获取BTC/USDT数据
curl -X POST "http://localhost:8000/api/v1/data/ohlcv" \
-H "Content-Type: application/json" \
-d '{"symbol": "BTC/USDT", "timeframe": "1h", "limit": 50}'src/data/- 数据采集和处理模块src/factors/- 因子计算模块,包含各种因子算法src/api/- RESTful API接口src/config/- 配置管理src/utils/- 工具函数examples/- 使用示例和测试脚本docker/- Docker相关配置
- 在
src/factors/technical/中创建新的因子文件 - 继承
TechnicalFactor基类 - 实现
calculate方法 - 在模块末尾注册因子
from src.factors.base.factor import TechnicalFactor, FactorMetadata, factor_registry
class MyCustomFactor(TechnicalFactor):
def __init__(self):
metadata = FactorMetadata(
name="my_custom_factor",
description="我的自定义因子",
category="technical",
sub_category="custom",
calculation_window=20,
update_frequency="1d",
data_requirements=["close"],
)
super().__init__(metadata)
def calculate(self, data, **kwargs):
# 实现因子计算逻辑
return data['close'].pct_change()
# 注册因子
factor_registry.register(MyCustomFactor())系统支持通过环境变量进行配置:
# 数据库配置
export DB_HOST=
export DB_PORT=
export DB_USERNAME=
export DB_PASSWORD=
# 交易所API配置
export EXCHANGE_BINANCE_API_KEY=
export EXCHANGE_BINANCE_SECRET=
# API服务配置
export API_HOST=0.0.0.0
export API_PORT=8000
export API_DEBUG=falseMIT License # factor_mining