量化框架
一个开放、可插拔的框架,用于可组合的定量工作流程。从FRED开始。扩展到任何东西。
灵感源自 Karpathy的自我研究 --同样的三层契约(不可变评估器、代理沙箱、人为指导),作为一个可扩展的框架应用于定量金融。
这是一个框架,而不是一个产品。 FRED是hello world连接器。其他一切都是同一模式的延伸。
______________________________________________________________________
先决条件
- Python 3.12.10+
- 紫外线 --Python包管理器
- FRED API密钥 — 从FRED免费获得一个
______________________________________________________________________
安装
# Clone the repository
git clone
cd quant_framework
# Install all dependencies
uv sync______________________________________________________________________
配置
环境变量
创建一个 .env 项目根目录中的文件(或直接导出):
# .env
FRED_API_KEY=your_api_key_here人物角色配置
编辑 configs/persona.yaml 控制MCP服务器公开的功能和连接器:
name: "Quant Research Agent"
description: "MCP server exposing quantitative research functions"
host: "127.0.0.1"
port: 8000
functions:
- run_linear
- run_random_forest
- run_svr
- run_xgboost
- run_bayesian_ridge
- run_hmm
connectors:
- fred护栏配置
编辑 configs/guardrails.yaml 定义函数输出的验证规则:
defaults:
max_records: 10000
rules:
run_linear:
max_records: 5000
required_fields: [model, r_squared, coefficients]
roles:
analyst:
redacted_fields: [model]______________________________________________________________________
用法
CLI--启动MCP服务器
# Show available commands
uv run quant --help
# Start the MCP server with SSE transport
uv run quant serve --persona configs/persona.yaml
# Use stdio transport instead
uv run quant serve --persona configs/persona.yaml --transport stdio这将:
- 从注册所有建模功能
FunctionRegistry - 初始化连接器(使用自动连接
$FRED_API_KEY) - 启动MCP服务器
127.0.0.1:8000
从克劳德桌面连接
添加到您的 claude_desktop_config.json:
{
"mcpServers": {
"quant-framework": {
"url": "http://localhost:8000/sse"
}
}
}运行示例脚本
uv run python examples/basic_usage.py这表明:
- 从FRED查询GDP数据
- 通过以下方式运行线性回归
FunctionRegistry - 通过验证结果
GuardrailEngine
______________________________________________________________________
项目结构
quant_framework/
├── pyproject.toml # Dependencies & CLI entry point
├── configs/
│ ├── persona.yaml # MCP server persona config
│ └── guardrails.yaml # Validation rules
├── examples/
│ └── basic_usage.py # End-to-end demo script
├── experiments/ # Autonomous research loop files
│ ├── evaluate.py # Evaluation harness (scalar metric)
│ ├── prepare_snapshot.py # Data snapshot caching script
│ └── strategy.py # Editable strategy sandbox
├── program.md # Human-directed research agenda
└── quant_framework/ # Package root
├── cli.py # CLI (quant serve)
├── core/
│ ├── function.py # @register_function, FunctionRegistry, FunctionResult
│ └── guardrail.py # GuardrailEngine, GuardrailViolation
├── connectors/
│ ├── connectors.py # BaseConnector, ConnectorRegistry
│ └── fred.py # FREDConnector (with 24h file cache)
├── functions/
│ └── modelling.py # Registered modelling functions
└── mcp/
└── generator.py # MCPServerGenerator______________________________________________________________________
核心组件
连接器
| 连接器 | 注册表名称 | 描述 |
|---|---|---|
FREDConnector | fred | 基于24小时文件缓存的美联储经济数据 |
from quant_framework.connectors import FREDConnector
fred = FREDConnector()
fred.connect({"api_key": "your_key"})
df = fred.query("GDP", observation_start="2020-01-01")建模功能
所有功能均已注册 @register_function 并返回a FunctionResult:
| 函数 | 注册表名称 | 型号类型 | 键输出 |
|---|---|---|---|
run_linear_regression | run_linear | 线性回归 | 系数、截距、r² |
run_random_forest | run_random_forest | 随机森林分类器 | 功能_重要性,r² |
run_svr | run_svr | SVR | r² |
run_xgboost | run_xgboost | XGBRegressor | 功能_重要性,r² |
run_bayesian_ridge | run_bayesian_ridge | 贝叶斯海脊 | 后角_std,alpha\_,lambda\_ |
run_hmm | run_hmm | 高斯HMM | 隐藏状态、转换矩阵、AIC、BIC |
from quant_framework.functions.modelling import run_linear_regression
result = run_linear_regression(df, target="GDP", features=["UNRATE", "FEDFUNDS"])
print(result.output["r_squared"]) # 0.12
print(result.trace_id) # unique trace ID护栏发动机
from quant_framework.core import GuardrailEngine
engine = GuardrailEngine("configs/guardrails.yaml")
engine.validate("run_linear", result.output) # passes
engine.validate("run_linear", result.output, role="analyst") # applies role-specific rules- 热重载:对YAML的编辑立即生效(检查文件mtime)
- 按角色覆盖:针对特定角色的更严格规则
函数注册表
from quant_framework.core import FunctionRegistry
# List all registered functions
FunctionRegistry.list() # ['run_linear', 'run_random_forest', ...]
FunctionRegistry.list_by_category("modelling") # filter by category
# Call by name
result = FunctionRegistry.call("run_linear", df=df, target="GDP")______________________________________________________________________
自主研究循环
该框架包括一个完全自主的研究循环,旨在测试假设并逐步改进定量策略。
它建立在中概述的三层合同之上 program.md:
- 固定评估线束 (
experiments/evaluate.py):在固定的历史数据集上对策略进行评分。 - 战略沙盒 (
experiments/strategy.py):代理测试功能、模型选择和信号逻辑的单个文件。 - 人类方向 (
program.md):定义代理的约束和高级研究议程。
运行循环
提供 program.md 将文件发送到任何自主编码代理(如Claude或内置系统),并指示其开始。代理人将阅读 program.md,修改 experiments/strategy.py,跑 evaluate.py,并使用保留/丢弃棘轮仅提交提高综合得分的更改。
______________________________________________________________________
扩展框架
添加连接器
from quant_framework.connectors.connectors import BaseConnector, ConnectorRegistry
@ConnectorRegistry.register("bloomberg")
class BloombergConnector(BaseConnector):
def connect(self, config): ...
def query(self, request, **kwargs): ...
def get_schema(self): ...
def health_check(self): ...添加功能
from quant_framework.core import register_function, FunctionResult
@register_function(name="my_indicator", category="technical")
def my_indicator(df, window=14):
result = ... # your logic
return FunctionResult(output={"value": result}, metrics={"window": window})该功能在 FunctionRegistry 通过将其名称添加到您的角色YAML中,可以将其作为MCP工具公开。
______________________________________________________________________
设计原则
- 连接器优先。 每个数据源都是一个
BaseConnector学习一个接口,连接任何东西。 - 作为原子发挥作用。 修饰Python函数,通过MCP自动注册和自动公开。
- 渐进式复杂性。 从FRED开始。添加你需要的东西,当你需要的时候。
- 三层合同。 不可变评估器(护栏)、代理沙盒(功能存储)、人工指导(角色配置)。
______________________________________________________________________
贡献者
阿琼·辛格
许可证
麻省理工学院
