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
- Contact us at info@predictmarket.ai to request API access
- Provide your name, organization, and use case
- Once approved, you'll receive an API key (starts with
pk_live_) - 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)
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}")
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
- Documentation: GitHub README
- Issues: GitHub Issues
- Email: info@predictmarket.ai
Project details
Release history Release notifications | RSS feed
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.1.0.tar.gz
(10.1 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file predictmarket-0.1.0.tar.gz.
File metadata
- Download URL: predictmarket-0.1.0.tar.gz
- Upload date:
- Size: 10.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5e483d5874e9fbc0b529719ea34ae6a5b06e2bb90af9543992e759a8167d65ca
|
|
| MD5 |
80734b90fa0abad616f32fcc31f79180
|
|
| BLAKE2b-256 |
86289f42ea33653a99d09276e7818e85e43339355a98476a912cf8b000839de4
|
File details
Details for the file predictmarket-0.1.0-py3-none-any.whl.
File metadata
- Download URL: predictmarket-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
766baa4b386b1cd92fa80d80610dbfb56c7a698533204d71f8966a310a3ac156
|
|
| MD5 |
cc760ac99f9af42c5452298db5fb331f
|
|
| BLAKE2b-256 |
1b08e896d27235e77c008609e3204a88846a9e46e3396d075ca21493797204a2
|