analysis.compare
Compare up to five Polymarket markets at once. View price, spread, order book depth, volume, and quality score to identify the optimal market for your trade.
Instructions
Compare 2-5 Polymarket markets side by side. Shows price, spread, order book depth, volume, and quality score for each market. Useful for choosing the best market to trade among similar options.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| condition_ids | Yes | 2-5 condition IDs to compare |
Implementation Reference
- src/tools/compare-markets.ts:10-78 (handler)The main handler function that executes the analysis.compare tool logic. It takes 2-5 condition IDs, fetches price, order book depth, spread, and quality metrics for each, then formats a comparison table with a best-liquidity recommendation.
export async function handleCompareMarkets(input: z.infer<typeof compareMarketsSchema>): Promise<string> { log("info", `Comparing ${input.condition_ids.length} markets`); const results = await Promise.all(input.condition_ids.map(async (cid) => { const priceInfo = await getMarketPriceByCondition(cid); if (!priceInfo) return { cid, error: true }; const [book, quality] = await Promise.all([ getMarketPrice(priceInfo.tokenId), checkMarketQuality(priceInfo.tokenId), ]); return { cid, error: false, price: priceInfo.price, bid: book?.bid ?? 0, ask: book?.ask ?? 0, spread: book?.spread ?? 0, quality: quality.pass, bidDepth: quality.metrics.bidDepth, askDepth: quality.metrics.askDepth, }; })); const valid = results.filter((r) => !r.error); if (valid.length === 0) return "Could not resolve any of the provided markets."; let output = `## Market Comparison (${valid.length})\n\n`; output += `| Metric |`; for (const r of valid) output += ` ${r.cid.slice(0, 10)}.. |`; output += `\n|--------|`; for (const _ of valid) output += `------------|`; output += `\n`; output += `| Price |`; for (const r of valid) output += ` $${(r as any).price.toFixed(4)} |`; output += `\n`; output += `| Bid |`; for (const r of valid) output += ` $${(r as any).bid.toFixed(4)} |`; output += `\n`; output += `| Ask |`; for (const r of valid) output += ` $${(r as any).ask.toFixed(4)} |`; output += `\n`; output += `| Spread |`; for (const r of valid) output += ` ${((r as any).spread * 100).toFixed(1)}% |`; output += `\n`; output += `| Bid Depth |`; for (const r of valid) output += ` $${(r as any).bidDepth.toFixed(0)} |`; output += `\n`; output += `| Ask Depth |`; for (const r of valid) output += ` $${(r as any).askDepth.toFixed(0)} |`; output += `\n`; output += `| Quality |`; for (const r of valid) output += ` ${(r as any).quality ? "PASS" : "FAIL"} |`; output += `\n`; // Best pick const best = valid.reduce((a, b) => ((a as any).spread < (b as any).spread ? a : b)); output += `\n**Best liquidity:** ${(best as any).cid.slice(0, 12)}.. (tightest spread)\n`; return output; } - src/tools/compare-markets.ts:6-8 (schema)Zod schema defining the input for analysis.compare: an array of 2-5 condition IDs (strings).
export const compareMarketsSchema = z.object({ condition_ids: z.array(z.string()).min(2).max(5).describe("2-5 condition IDs to compare"), }); - src/index.ts:463-468 (registration)Registration of the 'analysis.compare' tool with the MCP server, including description, schema, and handler invocation via safe().
server.tool( "analysis.compare", "Compare 2-5 Polymarket markets side by side. Shows price, spread, order book depth, volume, and quality score for each market. Useful for choosing the best market to trade among similar options.", compareMarketsSchema.shape, safe("markets.compare", async (input) => ({ content: [{ type: "text" as const, text: await handleCompareMarkets(compareMarketsSchema.parse(input)) }] })) ); - src/tools/compare-markets.ts:1-15 (helper)Imports of helper utilities: zod for validation, getMarketPriceByCondition/getMarketPrice from price-service, checkMarketQuality from market-filter, and logger.
import { z } from "zod"; import { getMarketPriceByCondition, getMarketPrice } from "../services/price-service.js"; import { checkMarketQuality } from "../services/market-filter.js"; import { log } from "../utils/logger.js"; export const compareMarketsSchema = z.object({ condition_ids: z.array(z.string()).min(2).max(5).describe("2-5 condition IDs to compare"), }); export async function handleCompareMarkets(input: z.infer<typeof compareMarketsSchema>): Promise<string> { log("info", `Comparing ${input.condition_ids.length} markets`); const results = await Promise.all(input.condition_ids.map(async (cid) => { const priceInfo = await getMarketPriceByCondition(cid); if (!priceInfo) return { cid, error: true };