position_remove
Remove a stock position from your portfolio by specifying the stock code and market, helping you manage your investment holdings.
Instructions
删除持仓记录
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| code | Yes | 股票代码 | |
| market | Yes | 市场 |
Implementation Reference
- src/index.ts:347-361 (handler)The handler function that processes position_remove tool calls. It validates input using RemovePositionSchema, calls position.removePosition(), and returns a success/error response.if (name === 'position_remove') { const params = RemovePositionSchema.parse(args); const success = position.removePosition(params.code, params.market as Market); if (!success) { throw new Error('Position not found'); } return { content: [ { type: 'text', text: JSON.stringify({ success: true }, null, 2), }, ], }; }
- src/position.ts:78-89 (handler)Core implementation of removePosition function. Loads positions from JSON file, finds the position by code and market, removes it using splice(), saves changes, and returns success status.export function removePosition(code: string, market: Market): boolean { const positions = loadPositions(); const index = positions.findIndex(p => p.code === code && p.market === market); if (index === -1) { return false; } positions.splice(index, 1); savePositions(positions); return true; }
- src/index.ts:42-45 (schema)Zod schema definition for position_remove tool input validation, requiring 'code' (string) and 'market' (enum: sh/sz/hk/us) parameters.const RemovePositionSchema = z.object({ code: z.string().describe('股票代码'), market: z.enum(['sh', 'sz', 'hk', 'us']).describe('市场'), });
- src/index.ts:172-183 (registration)Tool registration in ListToolsRequestSchema handler that exposes position_remove to MCP clients with description and input schema.{ name: 'position_remove', description: '删除持仓记录', inputSchema: { type: 'object', properties: { code: { type: 'string', description: '股票代码' }, market: { type: 'string', enum: ['sh', 'sz', 'hk', 'us'], description: '市场' }, }, required: ['code', 'market'], }, },
- src/position.ts:8-23 (helper)Helper functions loadPositions() and savePositions() used by removePosition to read/write position data from positions.json file.function loadPositions(): Position[] { try { if (fs.existsSync(DATA_FILE)) { const data = fs.readFileSync(DATA_FILE, 'utf-8'); return JSON.parse(data); } } catch (error) { console.error('Failed to load positions:', error); } return []; } // 保存持仓数据 function savePositions(positions: Position[]): void { fs.writeFileSync(DATA_FILE, JSON.stringify(positions, null, 2)); }