get_price
Retrieve current cryptocurrency prices from OKX exchange for trading pairs like BTC-USDT, with options for JSON or formatted markdown output.
Instructions
Get latest price for an OKX instrument with formatted visualization
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| instrument | Yes | Instrument ID (e.g. BTC-USDT) | |
| format | No | Output format (json or markdown) | markdown |
Implementation Reference
- src/index.ts:559-664 (handler)The get_price tool handler. Fetches latest ticker data from OKX /market/ticker API endpoint using axios. Handles both JSON and markdown formats. Markdown includes price change calculations, visual price range bar, and formatted statistics.if (request.params.name === "get_price") { console.error( `[API] Fetching price for instrument: ${args.instrument}` ); const response = await this.axiosInstance.get<OKXTickerResponse>( "/market/ticker", { params: {instId: args.instrument}, } ); if (response.data.code !== "0") { throw new Error(`OKX API error: ${response.data.msg}`); } if (!response.data.data || response.data.data.length === 0) { throw new Error("No data returned from OKX API"); } const ticker = response.data.data[0]; if (args.format === "json") { // Original JSON format return { content: [ { type: "text", text: JSON.stringify( { instrument: ticker.instId, lastPrice: ticker.last, bid: ticker.bidPx, ask: ticker.askPx, high24h: ticker.high24h, low24h: ticker.low24h, volume24h: ticker.vol24h, timestamp: new Date(parseInt(ticker.ts)).toISOString(), }, null, 2 ), }, ], }; } else { // Enhanced markdown format with visualization elements const priceChange = parseFloat(ticker.last) - parseFloat(ticker.open24h); const priceChangePercent = (priceChange / parseFloat(ticker.open24h)) * 100; const changeSymbol = priceChange >= 0 ? "▲" : "▼"; const changeColor = priceChange >= 0 ? "green" : "red"; // Create price range visual const low24h = parseFloat(ticker.low24h); const high24h = parseFloat(ticker.high24h); const range = high24h - low24h; const currentPrice = parseFloat(ticker.last); const position = Math.min(Math.max((currentPrice - low24h) / range, 0), 1) * 100; const priceBar = `Low ${low24h.toFixed(2)} [${"▮".repeat( Math.floor(position / 5) )}|${"▯".repeat(20 - Math.floor(position / 5))}] ${high24h.toFixed( 2 )} High`; return { content: [ { type: "text", text: `# ${ticker.instId} Price Summary\n\n` + `## Current Price: $${parseFloat( ticker.last ).toLocaleString()}\n\n` + `**24h Change:** ${changeSymbol} $${Math.abs( priceChange ).toLocaleString()} (${ priceChangePercent >= 0 ? "+" : "" }${priceChangePercent.toFixed(2)}%)\n\n` + `**Bid:** $${parseFloat( ticker.bidPx ).toLocaleString()} | **Ask:** $${parseFloat( ticker.askPx ).toLocaleString()}\n\n` + `### 24-Hour Price Range\n\n` + `\`\`\`\n${priceBar}\n\`\`\`\n\n` + `**24h High:** $${parseFloat( ticker.high24h ).toLocaleString()}\n` + `**24h Low:** $${parseFloat( ticker.low24h ).toLocaleString()}\n\n` + `**24h Volume:** ${parseFloat( ticker.vol24h ).toLocaleString()} units\n\n` + `**Last Updated:** ${new Date( parseInt(ticker.ts) ).toLocaleString()}\n\n` + `*Note: For real-time updates, use the subscribe_ticker and get_live_ticker tools.*`, }, ], }; } } else if (request.params.name === "get_candlesticks") {
- src/index.ts:286-305 (registration)Registration of the get_price tool in the ListTools response, including name, description, and input schema definition.{ name: "get_price", description: "Get latest price for an OKX instrument with formatted visualization", inputSchema: { type: "object", properties: { instrument: { type: "string", description: "Instrument ID (e.g. BTC-USDT)", }, format: { type: "string", description: "Output format (json or markdown)", default: "markdown", }, }, required: ["instrument"], }, },
- src/index.ts:290-304 (schema)Input schema definition for the get_price tool, specifying required 'instrument' parameter and optional 'format'.inputSchema: { type: "object", properties: { instrument: { type: "string", description: "Instrument ID (e.g. BTC-USDT)", }, format: { type: "string", description: "Output format (json or markdown)", default: "markdown", }, }, required: ["instrument"], },