get_instrument_detail
Retrieve detailed information for a specific stock using its code, with options to fetch complete data fields, powered by XTQuantAI’s integration with quantitative trading platforms.
Instructions
获取指定股票的详细信息
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| code | Yes | 股票代码,例如 000001.SZ | |
| iscomplete | No | 是否获取全部字段,默认为False |
Implementation Reference
- src/xtquantai/server.py:669-707 (handler)The primary handler function for the 'get_instrument_detail' tool. It validates input, initializes XTQuant if needed, calls xtdata.get_instrument_detail, formats the result into a serializable dict, and handles errors.async def get_instrument_detail(input: GetInstrumentDetailInput) -> Dict[str, Any]: """ 获取指定股票的详细信息 Args: code: 股票代码,例如 "000001.SZ" iscomplete: 是否获取全部字段,默认为False Returns: 股票详细信息 """ try: # 确保XTQuant数据中心已初始化 ensure_xtdc_initialized() if xtdata is None: return {"error": "xtdata模块未正确加载"} print(f"调用xtdata.get_instrument_detail({input.code}, {input.iscomplete})") # 直接使用用户输入的股票代码,不做任何格式处理 detail = xtdata.get_instrument_detail(input.code, input.iscomplete) # 处理返回值为None的情况 if detail is None: return {"message": f"未找到股票代码 {input.code} 的详细信息"} # 将结果转换为可序列化的字典 result = {} for key, value in detail.items(): if isinstance(value, (str, int, float, bool, type(None))): result[key] = value else: result[key] = str(value) return result except Exception as e: print(f"获取股票详情出错: {str(e)}") traceback.print_exc() return {"error": str(e)}
- src/xtquantai/server.py:145-147 (schema)Pydantic model defining the input schema for the tool, with required 'code' string and optional 'iscomplete' boolean.class GetInstrumentDetailInput(BaseModel): code: str # 股票代码,例如 "000001.SZ" iscomplete: bool = False # 是否获取全部字段,默认为False
- src/xtquantai/server.py:237-255 (registration)Tool registration in the list_tools handler, defining name, description, and JSON schema matching the Pydantic input model.types.Tool( name="get_instrument_detail", description="获取指定股票的详细信息", inputSchema={ "type": "object", "properties": { "code": { "type": "string", "description": "股票代码,例如 000001.SZ" }, "iscomplete": { "type": "boolean", "description": "是否获取全部字段,默认为False", "default": False } }, "required": ["code"] } ),
- src/xtquantai/server.py:435-451 (registration)Dispatch logic in the @server.call_tool() handler that parses arguments, creates input model, calls the tool handler, and returns JSON response.elif name == "get_instrument_detail": if not arguments or "code" not in arguments: return [types.TextContent(type="text", text="错误: 缺少必要参数 'code'")] code = arguments["code"] # 从参数中获取iscomplete,如果不存在则默认为False iscomplete = False if arguments and "iscomplete" in arguments: # 确保iscomplete是布尔值 if isinstance(arguments["iscomplete"], bool): iscomplete = arguments["iscomplete"] elif isinstance(arguments["iscomplete"], str): iscomplete = arguments["iscomplete"].lower() == "true" input_model = GetInstrumentDetailInput(code=code, iscomplete=iscomplete) result = await get_instrument_detail(input_model) return [types.TextContent(type="text", text=json.dumps(result, ensure_ascii=False, indent=2))]
- src/xtquantai/server.py:75-77 (helper)Mock implementation of get_instrument_detail in MockXtdata class used when real xtquant import fails.def get_instrument_detail(self, code, iscomplete=False): print(f"模拟调用get_instrument_detail({code}, {iscomplete})") return {"code": code, "name": "模拟股票", "price": 100.0}