Skip to main content

CCXT-style unified API for prediction markets

Project description

dr-manhattan

CCXT-style unified API for prediction markets. Simple, scalable, and easy to extend.

Logo 1 Logo 2

Architecture

dr-manhattan provides a unified interface to interact with multiple prediction market platforms, similar to how CCXT works for cryptocurrency exchanges.

Core Components

dr_manhattan/
├── base/           # Core abstractions
│   ├── exchange.py # Abstract base class
│   └── errors.py   # Exception hierarchy
├── exchanges/      # Exchange implementations
│   ├── polymarket.py
│   └── limitless.py
├── models/         # Data models
│   ├── market.py
│   ├── order.py
│   └── position.py
└── utils/          # Utilities (future)

Design Principles

  1. Unified Interface: All exchanges implement the same Exchange base class
  2. Scalability: Adding new exchanges is straightforward - just implement the abstract methods
  3. Simplicity: Clean abstractions with minimal dependencies
  4. Type Safety: Full type hints throughout

Key Features

  • Fetch markets and market data
  • Create and cancel orders
  • Query positions and balances
  • Standardized error handling
  • Exchange-agnostic code

Installation

uv venv
uv pip install -e .

Usage

Basic Usage (Public API)

import dr_manhattan

# Initialize exchange without authentication
polymarket = dr_manhattan.Polymarket({'timeout': 30})
limitless = dr_manhattan.Limitless({'timeout': 30})

# Fetch markets
markets = polymarket.fetch_markets()
for market in markets:
    print(f"{market.question}: {market.prices}")

Advanced Usage (With Authentication)

The implementations use symbolic links to integrate with existing market maker implementations:

import dr_manhattan

# Polymarket with poly-mm integration
polymarket = dr_manhattan.Polymarket({
    'private_key': 'your_private_key',
    'condition_id': 'condition_id',
    'yes_token_id': 'yes_token',
    'no_token_id': 'no_token',
    'dry_run': False
})

# Limitless with limitless-mm integration
limitless = dr_manhattan.Limitless({
    'private_key': 'your_private_key',
    'timeout': 30
})

# Create order
order = polymarket.create_order(
    market_id="market_123",
    outcome="Yes",
    side=dr_manhattan.OrderSide.BUY,
    price=0.65,
    size=100,
    params={'token_id': 'token_id'}
)

# Fetch balance
balance = polymarket.fetch_balance()
print(f"USDC: {balance['USDC']}")

Unified API Pattern

import dr_manhattan

# Works with any exchange
for exchange_id in dr_manhattan.exchanges:
    exchange = dr_manhattan.exchanges[exchange_id]()
    print(f"{exchange.name}: {exchange.id}")

Adding New Exchanges

To add a new exchange, create a class that inherits from Exchange:

from dr_manhattan.base import Exchange

class NewExchange(Exchange):
    @property
    def id(self) -> str:
        return "newexchange"

    @property
    def name(self) -> str:
        return "New Exchange"

    def fetch_markets(self, params=None):
        # Implement API call
        pass

    # Implement other abstract methods...

Register in dr_manhattan/__init__.py:

from .exchanges.newexchange import NewExchange

exchanges = {
    "polymarket": Polymarket,
    "limitless": Limitless,
    "newexchange": NewExchange,
}

Using Symbolic Links

The implementations leverage existing market maker codebases through symbolic links:

  1. poly-mm: Full Polymarket market maker implementation
  2. limitless-mm: Full Limitless market maker implementation

When initialized with authentication credentials, the exchange classes use these implementations directly, providing access to production-ready trading functionality.

Data Models

Market

  • Question and outcomes
  • Prices and volume
  • Close time and status

Order

  • Market and outcome
  • Side (buy/sell), price, size
  • Status tracking

Position

  • Current holdings
  • PnL calculation
  • Average entry price

Error Handling

All errors inherit from DrManhattanError:

  • ExchangeError - Exchange-specific errors
  • NetworkError - Connectivity issues
  • AuthenticationError - Auth failures
  • InvalidOrder - Invalid order parameters
  • MarketNotFound - Market doesn't exist

Examples

Check out the examples/ directory for working examples:

  • spread_strategy.py - Arbitrage trading strategy for binary markets
  • simple_test.py - Basic market data fetching
  • test_strategy.py - Strategy testing framework

Run an example:

uv run python examples/spread_strategy.py

See examples/README.md for detailed documentation.

Dependencies

  • Python >= 3.10
  • requests >= 2.31.0

Development:

  • pytest
  • black
  • ruff

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dr_manhattan-0.0.1.tar.gz (216.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dr_manhattan-0.0.1-py3-none-any.whl (47.5 kB view details)

Uploaded Python 3

File details

Details for the file dr_manhattan-0.0.1.tar.gz.

File metadata

  • Download URL: dr_manhattan-0.0.1.tar.gz
  • Upload date:
  • Size: 216.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for dr_manhattan-0.0.1.tar.gz
Algorithm Hash digest
SHA256 c3ee4b3c11cf43038fe29212fcc9689fa517adf961e503a0877f6ff293528422
MD5 dfb942d8ca6e6d87ed8c24efd17a7ff2
BLAKE2b-256 b6369035e637e6706f12b4b79cb43fb91965cdd0933c337634716d72c0d6afd5

See more details on using hashes here.

File details

Details for the file dr_manhattan-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: dr_manhattan-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 47.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for dr_manhattan-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d32b942f5fbb70f3b906c66f9a899e620744d7c9591e207e2fc441f3ab0b34d8
MD5 00c83b8f545e96f3ebe34f4d6be2e02a
BLAKE2b-256 e12ef3593e80215f994623c1f24b6aeaf25ad01ff8b602f15ed53e0079eea264

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page