How sswadkar/kalshi-interface works
Purpose and Entry Points
This repository is a lightweight Kalshi trading integration: a FastAPI service exposes status and trading operations, while shared Python logic authenticates Kalshi API calls and calculates position metrics. The operational entry point is server.py; core API/client and accounting logic resides in kalshi_positions.py. A minimal browser-facing page is available at static/index.html.
Service Runtime and Deployment
The service runs as Python/FastAPI, locally via python server.py or in a container defined by Dockerfile. Runtime configuration is environment-driven, selecting demo versus production credentials and a monitored event ticker. Dependencies are declared in requirements.txt.
Relevant paths: server.py, Dockerfile, requirements.txt.
Kalshi Authentication Boundary
Outbound requests to Kalshi pass through PSS/RSA signing logic in the core client layer. Private keys are environment-isolated, with distinct demo and production key locations. Key IDs and active environment are supplied externally through environment variables rather than embedded application logic.
Relevant paths: kalshi_positions.py, api_keys/demo/private_key.pem, api_keys/prod/private_key.pem.
Market and Position Polling
The server maintains real-time, in-memory state through two background polling loops: market/position refreshes run at roughly 0.5-second intervals, while resting-order refreshes run roughly every three seconds. This state is the source for the status API, creating a boundary between Kalshi’s remote API responses and locally served read status.
Relevant paths: server.py, kalshi_positions.py.
Trading API Flow
FastAPI endpoints accept buy, sell, and cancellation requests, delegate execution to the signed Kalshi client, and return the resulting order payload. Trading is an externally side-effecting boundary; orders are submitted to Kalshi rather than persisted in a local database.
Relevant paths: server.py, kalshi_positions.py.
Position Accounting
The core logic derives per-market liquidation value, realized PnL, and net exposure from fetched positions and market prices. It also applies the repository’s taker-fee model, 0.07 × P × (1 − P), when interpreting order costs and fees.
Relevant paths: kalshi_positions.py.
External Interfaces
Primary external dependency: Kalshi’s trading API, accessed with signed HTTP requests. Consumers can use the HTTP API on port 8000 or the included static status/UI asset. The repository has no indicated persistent datastore, queue, or streaming transport; process memory is the live state store.
Relevant paths: server.py, static/index.html, kalshi_positions.py.
Main entry points: kalshi_positions.py Signed Kalshi client · server.py Server runtime · index.html Status page · Dockerfile Container runtime
Components 15
FastAPI Service
- Server runtime · Operational entry point and HTTP API host.
server.py - Status API
server.py - Trading API · Accepts buy, sell, and cancellation requests.
server.py - Market/position poller · Refreshes live market and position state about every 0.5 seconds.
server.py - Resting-order poller · Refreshes resting orders about every three seconds.
server.py - In-memory live state · Local read model for status responses.
server.py - Status page
static/index.html - Container runtime
Dockerfile - Python dependencies
requirements.txt
Trading Core
- Signed Kalshi client · Signs outbound Kalshi HTTP requests with RSA/PSS.
kalshi_positions.py - Position accounting · Calculates liquidation value, realized PnL, net exposure, and taker fees.
kalshi_positions.py
External Boundaries
- Runtime environment · Selects demo or production credentials and monitored ticker.
- Demo private key
api_keys/demo/private_key.pem - Production private key
api_keys/prod/private_key.pem - Kalshi Trading API
Connections 19
- Container runtime → Server runtime: runs
- Python dependencies → Server runtime: provides runtime packages
- Status page → Server runtime: uses HTTP API
- Server runtime → Status API: hosts
- Server runtime → Trading API: hosts
- Server runtime → Market/position poller: starts
- Server runtime → Resting-order poller: starts
- Market/position poller → Signed Kalshi client: fetches markets and positions
- Resting-order poller → Signed Kalshi client: fetches resting orders
- Signed Kalshi client → Position accounting: supplies positions and prices
- Market/position poller → In-memory live state: updates
- Resting-order poller → In-memory live state: updates
- Position accounting → In-memory live state: derives metrics
- Status API → In-memory live state: reads
- Trading API → Signed Kalshi client: submits and cancels orders
- Signed Kalshi client → Kalshi Trading API: signed HTTP requests
- Runtime environment → Signed Kalshi client: selects environment and key ID
- Demo private key → Signed Kalshi client: demo signing key
- Production private key → Signed Kalshi client: production signing key