Get Top Losers
get_top_losersRetrieve the top 10 losing stocks from the Colombo Stock Exchange for the current trading day. Get real-time data on price declines and change percentages.
Instructions
Get the top 10 losing stocks in the CSE for the current trading day.
Tool call template
This tool declares no required parameters. Add optional parameters from the input schema as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {},
"name": "get_top_losers"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- src/index.ts:233-263 (handler)The actual handler function `getTopLosers()` that fetches top losing stocks from the CSE API endpoint 'https://www.cse.lk/api/topLooses' and returns mapped stock data (symbol, price, change, changePercentage).
async function getTopLosers() { try { const response = await axios.post( 'https://www.cse.lk/api/topLooses', {}, { headers: { 'Content-Type': 'application/json' }, timeout: 10000 } ); const data = response.data || []; return data.map((item: any) => ({ symbol: item.symbol, price: item.price, change: item.change, changePercentage: item.changePercentage })); } catch (error: any) { if (error.code === 'ECONNABORTED') { throw new Error('Request timed out. Please try again.'); } else if (error.response) { throw new Error(`API error: ${error.response.status} - ${error.response.statusText}`); } else { throw new Error(`Network error: ${error.message}`); } } } - src/index.ts:511-515 (schema)Schema definition for the tool. Input schema is empty (no inputs required).
{ title: "Get Top Losers", description: "Get the top 10 losing stocks in the CSE for the current trading day.", inputSchema: {} }, - src/index.ts:509-552 (registration)Tool registration via `server.registerTool('get_top_losers', {...})` with the handler that formats and returns top losers data.
server.registerTool( "get_top_losers", { title: "Get Top Losers", description: "Get the top 10 losing stocks in the CSE for the current trading day.", inputSchema: {} }, async () => { try { const topLosers = await getTopLosers(); const formattedLosers = topLosers.map((stock: any, index: number) => { const priceChangeSymbol = stock.change >= 0 ? '+' : ''; return { rank: index + 1, symbol: stock.symbol, price: `Rs. ${stock.price.toFixed(2)}`, change: `${priceChangeSymbol}${stock.change.toFixed(2)}`, changePercentage: `${priceChangeSymbol}${stock.changePercentage.toFixed(2)}%` }; }); return { content: [{ type: "text", text: JSON.stringify({ title: "Top 10 Losers", count: formattedLosers.length, losers: formattedLosers, lastUpdated: new Date().toISOString() }, null, 2) }] }; } catch (error: any) { return { content: [{ type: "text", text: `Error fetching top losers: ${error.message}` }], isError: true }; } } );