$ show ryanfrigo/kalshi-ai-trading-bot
This repository implements an automated trading bot for Kalshi prediction markets, leveraging AI models (primarily via OpenRouter) to make directional trading decisions. The system is built as an async Python application with a CLI entry point (`cli.py`), a Streamlit dashboard, and a paper trading mode. It supports multiple strategies: an AI-driven directional strategy using a multi-agent debate pipeline (bull/bear researchers, risk manager, trader), a conservative NO-side safe compounder, market making, and a unified trading system that dynamically allocates capital. The core data flow involves ingesting market data from the Kalshi REST and WebSocket APIs, persisting to SQLite, running AI inference through OpenRouter with fallback chains and cost tracking, executing trades via Kalshi's signed REST API, and monitoring positions with stop-loss/take-profit logic. Risk management is enforced through Kelly criterion position sizing, edge filtering, category scoring, portfolio enforcer, and cash reserves. The codebase also includes performance analysis jobs, a paper trading signal tracker with static HTML dashboard, and a comprehensive test suite. External dependencies include Kalshi API, OpenRouter (with optional xAI and Grok models), SQLite, Streamlit, and various async libraries (httpx, aiohttp, websockets).
The LLM identified 6 files worth highlighting for reuse:
Canonical Kelly Criterion implementation used by all strategies. Computes optimal bet size based on probability, payout, and a fractional Kelly multiplier. Self-contained, no internal imports beyond standard library, and can be extracted for any binary-option or gambling application.
Adaptive stop-loss and take-profit calculator that adjusts exit levels based on market volatility and confidence. Implements dynamic thresholds (e.g., 5-10% stop, 15-30% target) with optional trailing stops. Isolated utility with clear inputs/outputs, reusable in any trading system.
Edge-based trade filter that enforces a minimum probability difference between AI confidence and market price, with confidence-adjusted thresholds. Pure logic, no external dependencies, and can be reused to qualify any prediction market trade.
Abstract base class for LLM-powered agents with robust JSON extraction and repair from model outputs. Handles malformed JSON, missing fields, and retries. Demonstrates a reusable pattern for building structured LLM agents with fallback parsing.
Async publish/subscribe event bus for decoupling real-time data distribution (e.g., WebSocket market updates). Supports typed events, multiple subscribers, and error handling. Self-contained and can be extracted for any async application needing event-driven architecture.
OpenRouter API client with model fallback chain, retry/backoff, daily cost persistence, and budget enforcement. Handles model pricing, token counting, and error recovery. Demonstrates a robust pattern for integrating multiple LLM providers with cost control.