Skip to content

Commit 5d5798e

Browse files
committed
Initial commit: XTQuantAI MCP server for quantitative trading
0 parents  commit 5d5798e

11 files changed

+2327
-0
lines changed

.gitignore

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Python-generated files
2+
__pycache__/
3+
*.py[oc]
4+
build/
5+
dist/
6+
wheels/
7+
*.egg-info
8+
9+
# Virtual environments
10+
.venv
11+
12+
# Editor directories
13+
.cursor/
14+
.vscode/
15+
.idea/
16+
17+
# Backup files
18+
*.bak
19+
*.bak2
20+
*.backup
21+
22+
# Lock files
23+
uv.lock
24+
25+
# Local configuration
26+
.env

.python-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.11

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2025 dfkai
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
# XTQuantAI
2+
3+
XTQuantAI 是一个基于 Model Context Protocol (MCP) 的服务器,它将讯投 (XTQuant) 量化交易平台的功能与人工智能助手集成,使 AI 能够直接访问和操作量化交易数据和功能。
4+
5+
[![Python Version](https://img.shields.io/badge/python-3.11+-blue.svg)](https://www.python.org/downloads/)
6+
[![License](https://img.shields.io/badge/license-MIT-green.svg)](LICENSE)
7+
8+
## 功能特点
9+
10+
XTQuantAI 提供以下核心功能:
11+
12+
### 基础数据查询
13+
- **获取交易日期** (`get_trading_dates`) - 获取指定市场的交易日期
14+
- **获取板块股票列表** (`get_stock_list`) - 获取特定板块的股票列表
15+
- **获取股票详情** (`get_instrument_detail`) - 获取股票的详细信息
16+
17+
### 行情数据
18+
- **获取历史行情数据** (`get_history_market_data`) - 获取股票的历史行情数据
19+
- **获取最新行情数据** (`get_latest_market_data`) - 获取股票的最新行情数据
20+
- **获取完整行情数据** (`get_full_market_data`) - 获取股票的完整行情数据
21+
22+
### 图表和可视化
23+
- **创建图表面板** (`create_chart_panel`) - 创建股票图表面板,支持各种技术指标
24+
- **创建自定义布局** (`create_custom_layout`) - 创建自定义的图表布局,可以指定指标名称、参数名和参数值
25+
26+
## 安装
27+
28+
### 前提条件
29+
- Python 3.11 或更高版本
30+
- 讯投 (XTQuant) 量化交易平台
31+
- [uv](https://github.com/astral-sh/uv) 包管理工具 (推荐)
32+
33+
### 使用 pip 安装
34+
```bash
35+
pip install xtquantai
36+
```
37+
38+
### 使用 uv 安装
39+
```bash
40+
uv pip install xtquantai
41+
```
42+
43+
### 从源码安装
44+
```bash
45+
git clone https://github.com/dfkai/xtquantai.git
46+
cd xtquantai
47+
uv pip install -e .
48+
```
49+
50+
## 使用方法
51+
52+
### 直接启动服务器
53+
```bash
54+
# 使用 Python 直接运行
55+
python -m xtquantai
56+
57+
# 或使用安装的命令行工具
58+
xtquantai
59+
```
60+
61+
### 使用 MCP Inspector 进行调试
62+
```bash
63+
npx @modelcontextprotocol/inspector uv run xtquantai
64+
```
65+
66+
### 与 Claude Desktop 集成
67+
68+
在 Claude Desktop 中配置 MCP 服务器:
69+
70+
#### Windows
71+
编辑 `%APPDATA%/Claude/claude_desktop_config.json` 文件:
72+
73+
```json
74+
{
75+
"mcpServers": {
76+
"xtquantai": {
77+
"command": "uv",
78+
"args": [
79+
"run",
80+
"xtquantai"
81+
]
82+
}
83+
}
84+
}
85+
```
86+
87+
#### macOS
88+
编辑 `~/Library/Application Support/Claude/claude_desktop_config.json` 文件:
89+
90+
```json
91+
{
92+
"mcpServers": {
93+
"xtquantai": {
94+
"command": "uv",
95+
"args": [
96+
"run",
97+
"xtquantai"
98+
]
99+
}
100+
}
101+
}
102+
```
103+
104+
## 工具使用示例
105+
106+
### 获取交易日期
107+
```python
108+
# 获取上海市场的交易日期
109+
dates = get_trading_dates(market="SH")
110+
```
111+
112+
### 获取股票列表
113+
```python
114+
# 获取沪深A股板块的股票列表
115+
stocks = get_stock_list(sector="沪深A股")
116+
```
117+
118+
### 创建图表面板
119+
```python
120+
# 创建包含MA指标的图表面板
121+
result = create_chart_panel(
122+
codes="000001.SZ,600519.SH",
123+
period="1d",
124+
indicator_name="MA",
125+
param_names="period",
126+
param_values="5"
127+
)
128+
```
129+
130+
## 开发
131+
132+
### 构建和发布
133+
134+
准备发布包:
135+
136+
1. 同步依赖并更新锁文件:
137+
```bash
138+
uv sync
139+
```
140+
141+
2. 构建包分发:
142+
```bash
143+
uv build
144+
```
145+
146+
3. 发布到 PyPI:
147+
```bash
148+
uv publish
149+
```
150+
151+
### 调试
152+
153+
由于 MCP 服务器通过标准输入/输出运行,调试可能具有挑战性。我们强烈建议使用 [MCP Inspector](https://github.com/modelcontextprotocol/inspector) 进行调试。
154+
155+
## 项目结构
156+
157+
```
158+
xtquantai/
159+
├── src/
160+
│ └── xtquantai/
161+
│ ├── __init__.py # 包初始化文件
162+
│ └── server.py # MCP 服务器实现
163+
├── main.py # 启动脚本
164+
├── server_direct.py # 直接 HTTP 服务器实现
165+
├── pyproject.toml # 项目配置
166+
└── README.md # 项目文档
167+
```
168+
169+
## 许可证
170+
171+
本项目采用 MIT 许可证 - 详见 [LICENSE](LICENSE) 文件。
172+
173+
## 贡献
174+
175+
欢迎贡献!请随时提交问题或拉取请求。
176+
177+
## 致谢
178+
179+
- [讯投科技](https://www.thinktrader.net/) 提供的量化交易平台
180+
- [Model Context Protocol](https://modelcontextprotocol.io/) 提供的 AI 集成框架

clear_cache_and_run.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/usr/bin/env python
2+
"""
3+
清除缓存并运行xtquantai
4+
"""
5+
import os
6+
import shutil
7+
import subprocess
8+
import sys
9+
10+
def clear_cache():
11+
"""清除uv缓存"""
12+
cache_dir = os.path.expanduser("~/.local/share/uv")
13+
if os.path.exists(cache_dir):
14+
print(f"清除缓存目录: {cache_dir}")
15+
shutil.rmtree(cache_dir, ignore_errors=True)
16+
17+
cache_dir = os.path.expanduser("~/.cache/uv")
18+
if os.path.exists(cache_dir):
19+
print(f"清除缓存目录: {cache_dir}")
20+
shutil.rmtree(cache_dir, ignore_errors=True)
21+
22+
cache_dir = os.path.expanduser("~/AppData/Local/uv/cache")
23+
if os.path.exists(cache_dir):
24+
print(f"清除缓存目录: {cache_dir}")
25+
shutil.rmtree(cache_dir, ignore_errors=True)
26+
27+
def install_dependencies():
28+
"""安装依赖"""
29+
print("安装anyio模块...")
30+
try:
31+
# 尝试使用uv安装
32+
subprocess.run(["uv", "pip", "install", "anyio"], check=True)
33+
except (subprocess.CalledProcessError, FileNotFoundError):
34+
try:
35+
# 如果uv不可用,尝试使用pip
36+
subprocess.run([sys.executable, "-m", "pip", "install", "anyio"], check=True)
37+
except subprocess.CalledProcessError:
38+
print("警告: 无法安装anyio模块,某些功能可能无法正常工作")
39+
40+
print("安装xtquantai包...")
41+
try:
42+
# 尝试使用uv安装
43+
subprocess.run(["uv", "pip", "install", "-e", "."], check=True)
44+
except (subprocess.CalledProcessError, FileNotFoundError):
45+
try:
46+
# 如果uv不可用,尝试使用pip
47+
subprocess.run([sys.executable, "-m", "pip", "install", "-e", "."], check=True)
48+
except subprocess.CalledProcessError:
49+
print("警告: 无法安装xtquantai包,某些功能可能无法正常工作")
50+
51+
def run_xtquantai():
52+
"""运行xtquantai"""
53+
print("运行xtquantai...")
54+
# 添加当前目录到Python路径
55+
sys.path.insert(0, os.path.abspath('.'))
56+
57+
# 清除可能的缓存
58+
import importlib
59+
try:
60+
import xtquantai
61+
importlib.reload(xtquantai)
62+
63+
# 运行xtquantai
64+
xtquantai.main()
65+
except ImportError:
66+
print("错误: 无法导入xtquantai模块")
67+
except Exception as e:
68+
print(f"错误: {str(e)}")
69+
70+
if __name__ == "__main__":
71+
clear_cache()
72+
install_dependencies()
73+
run_xtquantai()

0 commit comments

Comments
 (0)