getOrders
Retrieve order history from TradeStation with status filtering options including Open, Filled, Canceled, or Rejected orders.
Instructions
Get order history with optional status filter
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| accountId | No | Account ID (optional, uses TRADESTATION_ACCOUNT_ID from env if not provided) | |
| status | No | Filter orders by status | All |
Input Schema (JSON Schema)
{
"properties": {
"accountId": {
"description": "Account ID (optional, uses TRADESTATION_ACCOUNT_ID from env if not provided)",
"type": "string"
},
"status": {
"default": "All",
"description": "Filter orders by status",
"enum": [
"Open",
"Filled",
"Canceled",
"Rejected",
"All"
],
"type": "string"
}
},
"type": "object"
}
Implementation Reference
- src/index.ts:551-587 (handler)The handler function for the 'getOrders' tool. It constructs the API endpoint based on accountId and optional status filter, calls makeAuthenticatedRequest to fetch orders from TradeStation API, and returns JSON-formatted response or error.async (args) => { try { const accountId = args.accountId || TS_ACCOUNT_ID; const { status } = args; if (!accountId) { throw new Error('Account ID is required. Either provide accountId parameter or set TRADESTATION_ACCOUNT_ID in .env file.'); } let endpoint = `/brokerage/accounts/${encodeURIComponent(accountId)}/orders`; if (status && status !== 'All') { endpoint += `?status=${status}`; } const orders = await makeAuthenticatedRequest(endpoint); return { content: [ { type: "text", text: JSON.stringify(orders, null, 2) } ] }; } catch (error: unknown) { return { content: [ { type: "text", text: `Failed to fetch orders: ${error instanceof Error ? error.message : 'Unknown error'}` } ], isError: true }; } }
- src/index.ts:112-117 (schema)Zod schema defining input parameters for the getOrders tool: optional accountId and status filter.const ordersSchema = { accountId: z.string().optional().describe('Account ID (optional, uses TRADESTATION_ACCOUNT_ID from env if not provided)'), status: z.enum(['Open', 'Filled', 'Canceled', 'Rejected', 'All']) .default('All') .describe('Filter orders by status') };
- src/index.ts:547-588 (registration)MCP server registration of the 'getOrders' tool, specifying name, description, input schema, and handler function.server.tool( "getOrders", "Get order history with optional status filter", ordersSchema, async (args) => { try { const accountId = args.accountId || TS_ACCOUNT_ID; const { status } = args; if (!accountId) { throw new Error('Account ID is required. Either provide accountId parameter or set TRADESTATION_ACCOUNT_ID in .env file.'); } let endpoint = `/brokerage/accounts/${encodeURIComponent(accountId)}/orders`; if (status && status !== 'All') { endpoint += `?status=${status}`; } const orders = await makeAuthenticatedRequest(endpoint); return { content: [ { type: "text", text: JSON.stringify(orders, null, 2) } ] }; } catch (error: unknown) { return { content: [ { type: "text", text: `Failed to fetch orders: ${error instanceof Error ? error.message : 'Unknown error'}` } ], isError: true }; } } );
- src/index.ts:179-230 (helper)Shared helper function used by getOrders (and other tools) to handle token refresh and make authenticated requests to TradeStation API.async function makeAuthenticatedRequest( endpoint: string, method: AxiosRequestConfig['method'] = 'GET', data: any = null ): Promise<any> { const userTokens = tokenStore.get(DEFAULT_USER); if (!userTokens) { throw new Error('User not authenticated. Please set TRADESTATION_REFRESH_TOKEN in .env file.'); } // Check if token is expired or about to expire (within 60 seconds) if (userTokens.expiresAt < Date.now() + 60000) { // Refresh the token const newTokens = await refreshToken(userTokens.refreshToken); tokenStore.set(DEFAULT_USER, newTokens); } try { const options: AxiosRequestConfig = { method, url: `${TS_API_BASE}${endpoint}`, headers: { 'Authorization': `Bearer ${tokenStore.get(DEFAULT_USER)?.accessToken}`, 'Content-Type': 'application/json', 'Accept': 'application/json' }, timeout: 60000 }; if (data && (method === 'POST' || method === 'PUT' || method === 'PATCH')) { options.data = data; } const response = await axios(options); return response.data; } catch (error: unknown) { if (error instanceof AxiosError) { const errorMessage = error.response?.data?.Message || error.response?.data?.message || error.message; const statusCode = error.response?.status; console.error(`API request error [${statusCode}]: ${errorMessage}`); console.error('Endpoint:', endpoint); throw new Error(`API Error (${statusCode}): ${errorMessage}`); } else if (error instanceof Error) { console.error('API request error:', error.message); throw error; } else { console.error('Unknown API request error:', error); throw new Error('Unknown API request error'); } } }