Skip to content

PolymarketDataLoader.from_market_slug() to use slug endpoint instead of client-side scanning #3302

Description

@yairt

Bug Report

Use this template only for issues that fit the Bug definition.

Term Definition
Bug Behavior that contradicts the platform's documented or intended design as per code, docs, or specs. (i.e., the implementation is incorrect.)
Expectation mismatch Behavior that follows the platform's documented or intended design but differs from what you expected. (i.e., the design/spec might be the problem.)
Enhancement request A request for new functionality or behavior that is not implied by existing design. (i.e., "It would be great if the platform could…")

Note:

  • Submitting this issue automatically applies the bug label.
  • bug-labeled issues are triaged with higher priority because they require corrective implementation work.
  • Expectation mismatches and design-level concerns should be opened as Discussions, or RFCs instead, where they can be validated and discussed to consensus before any work is scheduled.
  • The absence of a feature is typically not an expectation mismatch, and should be filed as an enhancement request.

Confirmation

Before opening a bug report, please confirm:

  • I've re-read the relevant sections of the documentation.
  • I've searched existing issues and discussions to avoid duplicates.
  • I've reviewed or skimmed the source code (or examples) to confirm the behavior is not by design.
  • I've tested this issue using a recent development wheel (dev develop or a nightly) and can still reproduce it.

Checking a recent development wheel can save time because the issue may already have been fixed.
You can install a development wheel by running:

pip install -U nautilus_trader --pre --index-url https://packages.nautechsystems.io/simple

See the development-wheels section for more details.

Expected Behavior

PolymarketDataLoader.from_market_slug() should efficiently fetch a single market by slug using the Polymarket API's built-in slug filter parameter, resulting in a single O(1) API call.

The API supports direct slug filtering:

curl "https://gamma-api.polymarket.com/markets?slug=<market-slug>"
# Returns exactly 1 market (filtered server-side)

or:

curl "https://gamma-api.polymarket.com/markets/slug/{slug}"

Actual Behavior

PolymarketDataLoader.from_market_slug() uses an inefficient lookup chain that fetches and iterates through potentially thousands of markets:

Call chain:
from_market_slug()find_market_by_slug()fetch_markets() → iterates through all markets in-memory

Two bugs identified:

  1. Inefficient market lookup: The implementation fetches batches of markets and searches client-side instead of using the API's slug query parameter for server-side filtering. This results in O(n) performance where n = total number of markets.

  2. Originally hardcoded limit=100: fetch_markets() had a hardcoded limit=100 with no pagination, meaning markets beyond position 100 were unreachable, causing polymarket_simple_quoter.py to fail with "Market with slug not found" errors.

Current inefficient code (in find_market_by_slug()):

        markets = await PolymarketDataLoader.fetch_markets(
            limit=100, ## No pagination support
            http_client=http_client,
        )
        for market in markets:
            if market.get("slug") == slug:
                return market ## Client side fitlering

Steps to Reproduce the Problem

  1. Try to load a market that's positioned beyond the first 100 in the API results:
import asyncio
from nautilus_trader.adapters.polymarket.loaders import PolymarketDataLoader

async def test():
    # This market is at position ~150
    loader = await PolymarketDataLoader.from_market_slug("kamala-harris-divorce-in-2025")
    print(f"Found: {loader.instrument.id}")

asyncio.run(test())
  1. Observe multiple API calls with pagination instead of a single targeted call.

  2. Run examples/backtest/polymarket_simple_quoter.py with a market slug that's not in the first 100 results - it fails with the original code.

Code Snippets or Logs

Affected files:

  • nautilus_trader/adapters/polymarket/loaders.py (lines ~70, ~252, ~310)

Specifications

  • OS platform: macOS 26.1 (also affects all platforms)
  • Python version: 3.12.12
  • nautilus_trader version: develop branch (December 2025)

Metadata

Metadata

Assignees

Labels

improvementImprovement to existing functionality

Type

No type

Projects

Status
Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions