Skip to main content

A typed Python client for the Kalshi prediction markets API with WebSocket streaming, automatic retries, and ergonomic interfaces

Project description

pykalshi logo

PyKalshi

PyPI version Python versions Tests License Open In Colab

The Python client for Kalshi prediction markets. WebSocket streaming, automatic retries, pandas integration, and clean interfaces for building trading systems.

Jupyter notebook rich display

from pykalshi import KalshiClient, Action, Side

client = KalshiClient()

# Place a trade
order = client.portfolio.place_order("KXBTC-25MAR15-B100000", Action.BUY, Side.YES, count_fp="10", yes_price_dollars="0.45")
order.wait_until_terminal()  # Block until filled/canceled

Features

  • WebSocket streaming - Real-time orderbook, ticker, and trade data with typed messages
  • Automatic retries - Exponential backoff on rate limits and transient errors
  • Domain objects - Market, Order, Event with methods like order.cancel(), market.get_orderbook()
  • pandas integration - .to_dataframe() on any list of results
  • Jupyter support - Rich HTML display for markets, orders, and positions
  • Local orderbook - OrderbookManager maintains state from WebSocket deltas
  • Type safety - Pydantic models and typed exceptions throughout

Installation

pip install pykalshi

# With pandas support
pip install pykalshi[dataframe]

Get your API credentials from kalshi.com and create a .env file:

KALSHI_API_KEY_ID=your-key-id
KALSHI_PRIVATE_KEY_PATH=/path/to/private-key.key

Quick Start

Interactive demo: examples/demo.ipynb or Open In Colab

Browse Markets

from pykalshi import MarketStatus, CandlestickPeriod

client = KalshiClient()

# Search markets
markets = client.get_markets(status=MarketStatus.OPEN, limit=100)
btc_markets = client.get_markets(series_ticker="KXBTC")

# Get a specific market
market = client.get_market("KXBTC-25MAR15-B100000")
print(f"{market.title}: ${market.yes_bid_dollars} / ${market.yes_ask_dollars}")

# Market data
orderbook = market.get_orderbook()
trades = market.get_trades(limit=50)
candles = market.get_candlesticks(start_ts, end_ts, period=CandlestickPeriod.ONE_HOUR)

Trading

from pykalshi import Action, Side, OrderStatus

# Check balance
balance = client.portfolio.get_balance()
print(f"${balance.balance / 100:.2f} available")

# Place an order
order = client.portfolio.place_order(market, Action.BUY, Side.YES, count_fp="10", yes_price_dollars="0.50")

# Manage orders
order.wait_until_terminal()  # Block until filled/canceled
order.modify(yes_price=45)   # Amend price
order.cancel()               # Cancel

# View portfolio
positions = client.portfolio.get_positions()
fills = client.portfolio.get_fills(limit=100)
orders = client.portfolio.get_orders(status=OrderStatus.RESTING)

Real-time Streaming

from pykalshi import Feed, TickerMessage, OrderbookSnapshotMessage

async with Feed(client) as feed:
    await feed.subscribe_ticker("KXBTC-25MAR15-B100000")
    await feed.subscribe_orderbook("KXBTC-25MAR15-B100000")
    await feed.subscribe_trades("KXBTC-25MAR15-B100000")

    async for msg in feed:
        match msg:
            case TickerMessage():
                print(f"Price: ${msg.price_dollars}")
            case OrderbookSnapshotMessage():
                print(f"Book: {len(msg.yes)} yes levels, {len(msg.no)} no levels")

Local Orderbook

from pykalshi import Feed, OrderbookManager

manager = OrderbookManager()

async with Feed(client) as feed:
    await feed.subscribe_orderbook(ticker)

    async for msg in feed:
        manager.apply(msg)
        book = manager.get(ticker)
        best_bid = book["yes_dollars"][0] if book["yes_dollars"] else None

pandas Integration

# Any list result has .to_dataframe()
positions_df = client.portfolio.get_positions().to_dataframe()
markets_df = client.get_markets(limit=500).to_dataframe()
fills_df = client.portfolio.get_fills().to_dataframe()

# Candlesticks and orderbooks too
candles_df = market.get_candlesticks(start, end).to_dataframe()
orderbook_df = market.get_orderbook().to_dataframe()

Error Handling

from pykalshi import InsufficientFundsError, RateLimitError, KalshiAPIError

try:
    order = client.portfolio.place_order(...)
except InsufficientFundsError:
    print("Not enough balance")
except RateLimitError:
    pass  # Client auto-retries with backoff
except KalshiAPIError as e:
    print(f"{e.status_code}: {e.error_code}")

Examples

See the examples/ directory:

Web Dashboard

A real-time web dashboard is included for browsing markets, viewing orderbooks, and monitoring your portfolio. It serves as both a development tool and a reference implementation.

pip install pykalshi[web]
uvicorn web.backend.main:app --reload

See web/ for details.

Why pykalshi?

pykalshi kalshi-python (official)
WebSocket streaming
Automatic retry/backoff
Rate limit handling
Domain objects
pandas integration
Jupyter display
Local orderbook
Typed exceptions
Pydantic models
Full API coverage

The official SDK is auto-generated from the OpenAPI spec. pykalshi adds the infrastructure needed for production trading: real-time data, error recovery, and ergonomic interfaces.

Links


This is an unofficial library and is not affiliated with Kalshi.

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

pykalshi-1.0.4.tar.gz (92.2 kB view details)

Uploaded Source

Built Distribution

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

pykalshi-1.0.4-py3-none-any.whl (79.6 kB view details)

Uploaded Python 3

File details

Details for the file pykalshi-1.0.4.tar.gz.

File metadata

  • Download URL: pykalshi-1.0.4.tar.gz
  • Upload date:
  • Size: 92.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.23

File hashes

Hashes for pykalshi-1.0.4.tar.gz
Algorithm Hash digest
SHA256 687a4611c9379fa0bf9c5277346f5daae14f52cd08e7324c2296733e28a6e0c4
MD5 6e2bdc0a1b7bfdca7845d1528000555d
BLAKE2b-256 9447f6d1db20484dee3d69ba5d358b53f4bf0185bb05ea5253e0218cc6d3f605

See more details on using hashes here.

File details

Details for the file pykalshi-1.0.4-py3-none-any.whl.

File metadata

  • Download URL: pykalshi-1.0.4-py3-none-any.whl
  • Upload date:
  • Size: 79.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.23

File hashes

Hashes for pykalshi-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 7de6a809e1cf4f0a42f1f0495d6350566fa91a16427bd4d504e46c58753fb8de
MD5 726254d0a9446e7cfc8dafa68ed49b24
BLAKE2b-256 e229bd2f7e051622f4b1b40b5b0754bbfe99dfd2ef11b66d6d5fd0a195dee4c1

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