|
| 1 | +# TigerOpen MCP 服务器快速入门指南 |
| 2 | + |
| 3 | +此 MCP(Model Context Protocol)服务器封装了 TigerOpen SDK 的行情和交易 API,使您能够通过简单的接口调用轻松访问 Tiger API 的功能。 |
| 4 | + |
| 5 | +## 安装和配置 |
| 6 | + |
| 7 | +### 前提条件 |
| 8 | + |
| 9 | +- Python 3.10 或更高版本 |
| 10 | +- 已安装 TigerOpen Python SDK |
| 11 | +- 有效的 Tiger 账户和 API 凭证 |
| 12 | + |
| 13 | +### 安装依赖 |
| 14 | + |
| 15 | +```bash |
| 16 | +pip install "mcp[cli]" tigeropen |
| 17 | +``` |
| 18 | + |
| 19 | +### 配置 |
| 20 | + |
| 21 | +在使用之前,您需要在 `server.py` 文件中更新您的 Tiger API 凭证: |
| 22 | + |
| 23 | +```python |
| 24 | +client_config = get_client_config( |
| 25 | + private_key_path='your_private_key_path', # 私钥文件路径 |
| 26 | + tiger_id='your_tiger_id', # Tiger ID |
| 27 | + account='your_account' # 交易账户 |
| 28 | +) |
| 29 | +``` |
| 30 | + |
| 31 | +您可以参考项目根目录下的 `client_config.py.template` 文件了解如何配置。 |
| 32 | + |
| 33 | +## 启动 MCP 服务器 |
| 34 | + |
| 35 | +使用以下命令启动 MCP 服务器: |
| 36 | + |
| 37 | +```bash |
| 38 | +mcp dev server.py |
| 39 | +``` |
| 40 | + |
| 41 | +服务器成功启动后,您将看到类似以下输出: |
| 42 | + |
| 43 | +``` |
| 44 | +INFO: Started server process [12345] |
| 45 | +INFO: Waiting for application startup. |
| 46 | +INFO: Application startup complete. |
| 47 | +INFO: Uvicorn running on http://127.0.0.1:5000 |
| 48 | +``` |
| 49 | + |
| 50 | +## 服务器功能 |
| 51 | + |
| 52 | +MCP 服务器提供了以下主要功能: |
| 53 | + |
| 54 | +### 行情接口 |
| 55 | + |
| 56 | +- **市场和符号** |
| 57 | + - `get_market_status`: 获取指定市场的状态 |
| 58 | + - `get_symbols`: 获取市场的所有交易符号列表 |
| 59 | + - `get_symbol_names`: 获取市场的所有交易符号及其名称 |
| 60 | + - `get_trading_calendar`: 获取交易日历 |
| 61 | + |
| 62 | +- **股票行情** |
| 63 | + - `get_briefs`: 获取股票摘要信息 |
| 64 | + - `get_stock_details`: 获取股票详情信息 |
| 65 | + - `get_bars`: 获取K线数据 |
| 66 | + - `get_timeline`: 获取分时数据 |
| 67 | + - `get_trade_ticks`: 获取逐笔成交数据 |
| 68 | + - `get_depth_quote`: 获取深度行情 |
| 69 | + |
| 70 | +- **期权相关** |
| 71 | + - `get_option_expirations`: 获取期权到期日列表 |
| 72 | + - `get_option_chain`: 获取期权链 |
| 73 | + - `get_option_briefs`: 获取期权最新行情 |
| 74 | + - `get_option_bars`: 获取期权K线数据 |
| 75 | + |
| 76 | +- **期货相关** |
| 77 | + - `get_future_exchanges`: 获取期货交易所列表 |
| 78 | + - `get_future_contracts`: 获取交易所下的可交易合约 |
| 79 | + - `get_future_bars`: 获取期货K线数据 |
| 80 | + - `get_future_brief`: 获取期货最新行情 |
| 81 | + |
| 82 | +- **财务数据** |
| 83 | + - `get_corporate_dividend`: 获取公司派息数据 |
| 84 | + - `get_corporate_split`: 获取公司拆合股数据 |
| 85 | + - `get_financial_daily`: 获取日级的财务数据 |
| 86 | + - `get_industry_list`: 获取行业列表 |
| 87 | + - `get_stock_industry`: 获取股票的行业 |
| 88 | + |
| 89 | +### 交易接口 |
| 90 | + |
| 91 | +- **账户信息** |
| 92 | + - `get_managed_accounts`: 获取管理的账号列表 |
| 93 | + - `get_assets`: 获取账户资产信息 |
| 94 | + - `get_positions`: 获取账户持仓情况 |
| 95 | + - `get_prime_assets`: 获取Prime账户资产信息 |
| 96 | + |
| 97 | +- **订单管理** |
| 98 | + - `get_contracts`: 获取合约信息 |
| 99 | + - `place_order`: 下单 |
| 100 | + - `preview_order`: 预览订单(不实际下单) |
| 101 | + - `cancel_order`: 取消订单 |
| 102 | + - `get_orders`: 获取所有订单列表 |
| 103 | + - `get_open_orders`: 获取未成交订单列表 |
| 104 | + - `get_filled_orders`: 获取已成交订单列表 |
| 105 | + |
| 106 | +- **资金管理** |
| 107 | + - `get_segment_fund_available`: 获取可用的分段资金 |
| 108 | + - `get_segment_fund_history`: 获取分段资金历史 |
| 109 | + - `transfer_segment_fund`: 转移分段资金 |
| 110 | + |
| 111 | +## 调用示例 |
| 112 | + |
| 113 | +### 从 MCP CLI 调用 |
| 114 | + |
| 115 | +您可以使用 MCP CLI 工具直接与服务器交互: |
| 116 | + |
| 117 | +```bash |
| 118 | +# 检查服务器状态 |
| 119 | +mcp call --url http://localhost:5000 server://status |
| 120 | + |
| 121 | +# 获取美股市场状态 |
| 122 | +mcp call --url http://localhost:5000 get_market_status market=US |
| 123 | + |
| 124 | +# 获取特定股票的行情 |
| 125 | +mcp call --url http://localhost:5000 get_briefs symbols=["AAPL","MSFT"] |
| 126 | +``` |
| 127 | + |
| 128 | +### 从 Python 代码调用 |
| 129 | + |
| 130 | +```python |
| 131 | +from mcp.client import McpClient |
| 132 | + |
| 133 | +# 创建 MCP 客户端 |
| 134 | +client = McpClient("http://localhost:5000") |
| 135 | + |
| 136 | +# 获取市场状态 |
| 137 | +status = client.call("get_market_status", market="US") |
| 138 | +print(status) |
| 139 | + |
| 140 | +# 获取股票行情 |
| 141 | +briefs = client.call("get_briefs", symbols=["AAPL", "MSFT"]) |
| 142 | +print(briefs) |
| 143 | + |
| 144 | +# 下单 |
| 145 | +order = client.call("place_order", |
| 146 | + symbol="AAPL", |
| 147 | + action="BUY", |
| 148 | + order_type="LMT", |
| 149 | + quantity=1, |
| 150 | + limit_price=150.0) |
| 151 | +print(order) |
| 152 | +``` |
| 153 | + |
| 154 | +## 常见问题解答 |
| 155 | + |
| 156 | +### Q: 如何查看所有可用的 API? |
| 157 | +**A**: 在服务器启动后,访问 http://localhost:5000/docs 查看完整的 API 文档。 |
| 158 | + |
| 159 | +### Q: 如何处理错误? |
| 160 | +**A**: 所有 API 调用返回的结果中,如果包含 `"error"` 键,则表示发生错误。错误信息将在该键的值中提供。 |
| 161 | + |
| 162 | +### Q: 如何修改服务器端口? |
| 163 | +**A**: 启动时指定端口:`mcp dev server.py --port 8000` |
| 164 | + |
| 165 | +### Q: 如何使用不同的配置文件? |
| 166 | +**A**: 修改 `server.py` 中的配置加载代码,指向您自己的配置文件。 |
| 167 | + |
| 168 | +## 更多资源 |
| 169 | + |
| 170 | +- [TigerOpen API 文档](https://quant.itigerup.com/openapi/zh/python/overview/intro.html) |
| 171 | +- [MCP 框架文档](https://microsoft.github.io/mcp/) |
0 commit comments