Overview
Thefeed.py module provides a centralized, thread-safe market data feed that runs in the background and shares real-time market information across all trading bots. It continuously fetches open markets, tracks price history, and monitors settlements.
Classes
MarketSnapshot
Immutable snapshot of a single market’s state at a specific point in time. Attributes:Market ticker symbol
Parent event ticker
Market status:
"open", "closed", or "settled"Settlement result:
"yes", "no", or empty string for unsettled marketsYes side ask price in dollars (0.0 - 1.0)
No side ask price in dollars (0.0 - 1.0)
Yes side bid price in dollars (0.0 - 1.0)
No side bid price in dollars (0.0 - 1.0)
Last traded price in dollars (0.0 - 1.0)
24-hour trading volume
Current open interest
Market close time (None if not set)
Market category (e.g., “politics”, “economics”)
Market title/description
Timestamp when this snapshot was fetched
MarketHistory
Rolling price history for a single market with automatic size limiting. Attributes:Market ticker symbol
List of (timestamp, yes_ask_price) tuples. Automatically trimmed to
MARKET_HISTORY_MAX_TICKS.append(ts: datetime, yes_ask: float)
Append a new price observation to the history. Automatically trims the list to maintain the maximum size.Timestamp of the observation
Yes ask price at this timestamp
MarketDataFeed
Centralized market data fetcher that runs in its own background thread. All bots read from shared state via public read methods.__init__(client, poll_interval: float = 30.0)
Authenticated Kalshi API client
Seconds between market data fetches
start()
Start the background fetch thread.stop()
Stop the background fetch thread (blocks up to 10 seconds).get_open_markets() → dict[str, MarketSnapshot]
Return a copy of all currently open markets.Dictionary mapping ticker symbols to market snapshots
get_market(ticker: str) → MarketSnapshot | None
Get a specific market snapshot (checks both open and settled markets).Market ticker to retrieve
Market snapshot if found, None otherwise
get_history(ticker: str) → list[tuple[datetime, float]]
Get price history for a specific market.Market ticker to retrieve history for
List of (timestamp, yes_ask_price) tuples, empty list if ticker not found
get_settlement(ticker: str) → str | None
Return settlement result if market is settled.Market ticker to check
"yes" or "no" if settled, None if not settled or not foundget_categories() → list[str]
Return all known market categories.Sorted list of category names (e.g., [“crypto”, “economics”, “politics”])
get_stats() → dict
Return feed statistics and health metrics.Dictionary with keys:
open_markets: Number of currently tracked open marketssettled_markets: Number of recently settled markets in cachecategories: Number of known categoriesfetch_count: Total successful fetches since starterror_count: Total errors encountered
check_specific_tickers(tickers: set[str])
Manually check specific tickers for settlement (used for positions we hold). This method queries individual markets directly rather than relying on bulk settlement checks.Set of ticker symbols to check
Internal State
The feed maintains several internal data structures (thread-safe with RLock):markets: Currently open markets (ticker → MarketSnapshot)histories: Price histories (ticker → MarketHistory)settled: Recently settled markets (kept for 2 hours)event_categories: Event ticker to category mappingknown_categories: List of all discovered categories
Configuration
The feed respects these configuration parameters fromgenetic.config:
MARKET_CLOSE_WINDOW_HOURS: Only fetch markets closing within this windowMARKET_HISTORY_MAX_TICKS: Maximum price history length per market