get_order_history
Retrieve filled order history for specific instruments within defined time periods to analyze trading activity and performance.
Instructions
Get a list of filled orders for a given date range and optional symbol filter
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| begin | No | the beginning of the time range in timestamp format | |
| end | No | the end of the time range in timestamp format | |
| instId | Yes | Instrument ID (symbol), e.g. BTC-USDT |
Implementation Reference
- src/tools/get_order_history.ts:31-50 (handler)The main execution handler for the get_order_history tool. It invokes the OKX API client to retrieve order history and formats the response as text content.export default async function get_order_history({ instId, begin, end, }: InferSchema<typeof schema>) { try { const orderHistory = await okxApiClient.getOrderHistory(instId, begin, end); return { content: [{ type: "text", text: JSON.stringify(orderHistory, null, 2) }], }; } catch (error) { const message = error instanceof Error ? error.message : "An unknown error occurred"; return { content: [ { type: "text", text: JSON.stringify({ error: message }, null, 2) }, ], }; } }
- src/tools/get_order_history.ts:7-17 (schema)Zod-based input schema defining parameters: instId (required string), begin and end (optional numbers for timestamp range).export const schema = { instId: z.string().describe("Instrument ID (symbol), e.g. BTC-USDT"), begin: z .number() .optional() .describe("the beginning of the time range in timestamp format"), end: z .number() .optional() .describe("the end of the time range in timestamp format"), };
- src/tools/get_order_history.ts:19-29 (registration)Tool metadata registration, specifying the name 'get_order_history', description, and annotations for MCP tool protocol.export const metadata = { name: "get_order_history", description: "Get a list of filled orders for a given date range and optional symbol filter", annotations: { title: "Get Order History", readOnlyHint: true, destructiveHint: false, idempotentHint: true, }, };
- src/services/okxApiClient.ts:75-95 (helper)Supporting helper method in OkxApiClient class that interfaces with the OKX REST API to fetch order history for SPOT instruments, processes the response, and handles errors.async getOrderHistory(instId: string, begin?: number, end?: number) { try { const response = await client.getOrderHistory({ instType: "SPOT", instId, begin: begin?.toString(), end: end?.toString(), }); return response.map((order) => ({ orderId: order.ordId, symbol: order.instId, price: parseFloat(order.avgPx), amount: parseFloat(order.sz), side: order.side, realizedPnl: parseFloat(order.pnl), })); } catch (error) { console.error("Error fetching order history:", error); throw error; } }