Skip to main content

Python client for the Prediction Markets API

Project description

Prediction Markets API - Python Client

Official Python client library for the Prediction Markets API.

Installation

pip install predictmarket

Quick Start

from predictmarket import Client

# Initialize client with your API key
client = Client(api_key="pk_live_your_key_here")

# Get markets from Kalshi
markets = client.get_markets(venue="kalshi", limit=50)
for market in markets["items"]:
    print(f"{market['venue_ticker']}: {market['title']}")

# Get recent trades
trades = client.get_trades(
    venue="kalshi",
    start_date="2025-01-01",
    limit=1000
)

# Get factor returns with summary
factor_data = client.get_processed_factor_returns(
    factor_name="Macro",
    days_back=90
)
print(f"Returns: {factor_data['returns']}")
print(f"Summary: {factor_data['summary']}")

Features

  • Complete API Coverage: Access all raw and processed endpoints
  • Automatic Pagination: Fetch all results automatically
  • Type-Safe: Full type hints for better IDE support
  • Error Handling: Detailed exceptions for different error types
  • Easy to Use: Intuitive Pythonic interface

Getting Your API Key

  1. Contact us at info@predictmarket.ai to request API access
  2. Provide your name, organization, and use case
  3. Once approved, you'll receive an API key (starts with pk_live_)
  4. Keep your API key secure - treat it like a password!

Available Methods

Raw Data Endpoints

# Markets
client.get_markets(venue=None, factor_category=None, tags=None)

# Trades
client.get_trades(venue=None, venue_ticker=None, start_date=None, end_date=None)

# Daily Prices
client.get_market_daily_prices(venue=None, venue_ticker=None, start_date=None, end_date=None)

# Factor Data
client.get_factor_returns(factor_name=None, start_date=None, end_date=None)
client.get_factor_summary(factor_name=None)

# Stock-Factor Relationships
client.get_stock_factor_betas(stock_ticker=None)

# Correlations
client.get_thematic_correlations(theme=None, stock_ticker=None, market_ticker=None)

# Markets by Tags
client.get_markets_by_tags(tag=None, venue=None)

Processed Analytics Endpoints

# Enhanced daily prices with analytics
client.get_processed_market_prices(venue_ticker="KXBTC-24DEC29", days_back=365)

# Factor returns with summary statistics
client.get_processed_factor_returns(factor_name="Macro", days_back=90)

# Filtered correlations
client.get_processed_correlations(theme="crypto", min_correlation=0.5)

# Stock betas with R² filtering
client.get_processed_stock_betas(stock_ticker="AAPL", min_r_squared=0.3)

# Factor trade impacts - see which markets drive stock factor betas
client.get_factor_trade_impacts(stock_ticker="NVDA", top_n=10, factor_filter="Tech")

Advanced Usage

Automatic Pagination

# Fetch all markets across all pages
markets = client.get_markets(venue="kalshi", auto_paginate=True)
print(f"Total markets: {len(markets['items'])}")

Context Manager

# Automatically close connection
with Client(api_key="pk_live_xxx") as client:
    markets = client.get_markets(limit=100)
    # Process markets...
# Connection automatically closed

Error Handling

from predictmarket import (
    Client,
    AuthenticationError,
    ValidationError,
    RateLimitError
)

try:
    client = Client(api_key="pk_live_xxx")
    markets = client.get_markets(limit=50)
except AuthenticationError:
    print("Invalid API key")
except ValidationError as e:
    print(f"Invalid parameters: {e.message}")
except RateLimitError:
    print("Rate limit exceeded - please wait")

Examples

Example 1: Track Market Sentiment

from predictmarket import Client
from datetime import datetime, timedelta

client = Client(api_key="pk_live_xxx")

# Get markets about AI
ai_markets = client.get_markets_by_tags(tag="ai", limit=100)

for market in ai_markets["items"]:
    # Get recent price history
    prices = client.get_market_daily_prices(
        venue_ticker=market["venue_ticker"],
        start_date=(datetime.now() - timedelta(days=30)).strftime("%Y-%m-%d")
    )

    if prices["items"]:
        latest = prices["items"][0]
        print(f"{market['title']}: {latest['close_price']:.2f}")

Example 2: Analyze Factor Performance

from predictmarket import Client

client = Client(api_key="pk_live_xxx")

# Get factor returns with summary
factors = ["Macro", "Tech", "Crypto"]

for factor in factors:
    data = client.get_processed_factor_returns(
        factor_name=factor,
        days_back=365
    )

    summary = data["summary"]
    print(f"\n{factor} Factor:")
    print(f"  Mean Return: {summary['mean_return']}")
    print(f"  Sharpe Ratio: {summary['sharpe_ratio']}")
    print(f"  Observations: {summary['num_observations']}")

Example 3: Find Stock-Market Correlations

from predictmarket import Client

client = Client(api_key="pk_live_xxx")

# Find strong correlations for AAPL
correlations = client.get_processed_correlations(
    stock_ticker="AAPL",
    min_correlation=0.7  # Only strong correlations
)

print(f"Found {len(correlations['items'])} strong correlations for AAPL:")
for corr in correlations["items"][:10]:
    print(f"  {corr['market_ticker']}: {corr['pearson_correlation']:.3f}")

Example 4: Analyze Which Markets Drive Stock Factor Exposures

from predictmarket import Client

client = Client(api_key="pk_live_xxx")

# Get the top markets driving NVDA's factor exposures
impacts = client.get_factor_trade_impacts(
    stock_ticker="NVDA",
    top_n=10
)

print(f"\nStock: {impacts['stockTicker']}")
print(f"Factor Betas: {impacts['betas']}")
print(f"\nTop {len(impacts['topImpacts'])} Contributing Markets:")

for impact in impacts["topImpacts"]:
    print(f"\n{impact['marketTitle']}")
    print(f"  Factor: {impact['factorCategory']}")
    print(f"  Beta: {impact['beta']:.6f}")
    print(f"  Relative Weight: {impact['relativeWeight']:.2%}")
    print(f"  Contribution: {impact['contribution']:.6f}")
    if impact['recentPrice']:
        print(f"  Recent Price: {impact['recentPrice']:.2f}")
    if impact['priceChange']:
        print(f"  Price Change: {impact['priceChange']:.2f}%")

# Get impacts for a specific factor only
tech_impacts = client.get_factor_trade_impacts(
    stock_ticker="NVDA",
    top_n=5,
    factor_filter="Tech"
)

print(f"\nTop Tech Factor Markets for NVDA:")
for impact in tech_impacts["impactsByFactor"]["Tech"]["topImpacts"]:
    print(f"  - {impact['marketTitle']}: {impact['relativeWeight']:.2%}")

API Reference

For complete API documentation and data schemas, visit the GitHub repository

Requirements

  • Python 3.8+
  • httpx (automatically installed)

License

MIT License - see LICENSE file for details

Support

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

predictmarket-0.2.0.tar.gz (11.3 kB view details)

Uploaded Source

Built Distribution

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

predictmarket-0.2.0-py3-none-any.whl (9.7 kB view details)

Uploaded Python 3

File details

Details for the file predictmarket-0.2.0.tar.gz.

File metadata

  • Download URL: predictmarket-0.2.0.tar.gz
  • Upload date:
  • Size: 11.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.12

File hashes

Hashes for predictmarket-0.2.0.tar.gz
Algorithm Hash digest
SHA256 4a049a17e48a3fb1351bcc6b5a97fbc9f9d8c57ddcf927aab97ca18ddbdb8d1a
MD5 b071a8a0f2e51b17e9f9c87d836c0829
BLAKE2b-256 20fbe14597ad1db252d58b12d9ad1884802a69608c5ffe1f595042d01ba0cc9f

See more details on using hashes here.

File details

Details for the file predictmarket-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: predictmarket-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 9.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.12

File hashes

Hashes for predictmarket-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1ba23cf61acce1f2e29ecfd1b69e06baab096f58ef15c9ede053d80a464b0e3c
MD5 a76dd7be0ab7379298c5dfa223e5f11c
BLAKE2b-256 f4d4de94513bd3f22966d205c0f71ae74a48b63fda62abb7e40f785617956eb5

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