position_add
Add a new stock position to your portfolio by specifying code, quantity, cost price, currency, and market for tracking investments.
Instructions
添加持仓记录
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| code | Yes | 股票代码 | |
| name | Yes | 股票名称 | |
| quantity | Yes | 持有数量 | |
| costPrice | Yes | 成本价 | |
| currency | Yes | 货币单位,如 CNY、HKD、USD | |
| market | Yes | 市场 |
Implementation Reference
- src/position.ts:26-52 (handler)The core implementation of addPosition function that creates a new position record, loads existing positions from JSON file, creates a new Position object with timestamp, appends to the array, saves back to file, and returns the new position.export function addPosition( code: string, name: string, quantity: number, costPrice: number, currency: string, market: Market ): Position { const positions = loadPositions(); const now = new Date().toISOString(); const newPosition: Position = { code, name, quantity, costPrice, currency, market, createdAt: now, updatedAt: now, }; positions.push(newPosition); savePositions(positions); return newPosition; }
- src/index.ts:304-322 (registration)The request handler for position_add tool in CallToolRequestSchema that validates arguments using AddPositionSchema, calls position.addPosition with parsed parameters, and returns the result as JSON.if (name === 'position_add') { const params = AddPositionSchema.parse(args); const result = position.addPosition( params.code, params.name, params.quantity, params.costPrice, params.currency, params.market as Market ); return { content: [ { type: 'text', text: JSON.stringify(result, null, 2), }, ], }; }
- src/index.ts:26-33 (schema)Zod validation schema AddPositionSchema that defines the expected input structure for position_add with fields: code, name, quantity, costPrice, currency, and market.const AddPositionSchema = z.object({ code: z.string().describe('股票代码'), name: z.string().describe('股票名称'), quantity: z.number().positive().describe('持有数量'), costPrice: z.number().positive().describe('成本价'), currency: z.string().describe('货币单位,如 CNY、HKD、USD'), market: z.enum(['sh', 'sz', 'hk', 'us']).describe('市场'), });
- src/index.ts:142-157 (registration)Tool registration for position_add in ListToolsRequestSchema that exposes the tool to MCP clients with name, description, and input schema definition.{ name: 'position_add', description: '添加持仓记录', inputSchema: { type: 'object', properties: { code: { type: 'string', description: '股票代码' }, name: { type: 'string', description: '股票名称' }, quantity: { type: 'number', description: '持有数量' }, costPrice: { type: 'number', description: '成本价' }, currency: { type: 'string', description: '货币单位,如 CNY、HKD、USD' }, market: { type: 'string', enum: ['sh', 'sz', 'hk', 'us'], description: '市场' }, }, required: ['code', 'name', 'quantity', 'costPrice', 'currency', 'market'], }, },
- src/types.ts:81-90 (schema)The Position interface type definition that defines the structure of position objects including code, name, quantity, costPrice, currency, market, createdAt, and updatedAt fields.export interface Position { code: string; name: string; quantity: number; costPrice: number; currency: string; market: Market; createdAt: string; updatedAt: string; }