Overview
Thebot.py module implements a multi-strategy paper trading bot that monitors Bitcoin prediction markets on Kalshi. It tracks 7 different trading strategies simultaneously, recording all trades to CSV for backtesting analysis.
Configuration Constants
Base URL for Kalshi trade API
Market series to monitor (15-minute Bitcoin prediction markets)
Polling interval in seconds between market checks
Stake amount in USD per trade for simple strategies
Lookback window for momentum strategy (1 minute)
Lookback window for 15-minute momentum strategy
Initial bankroll for consensus strategies (from env)
Risk percentage per trade for consensus strategies (from env)
Core Functions
get_btc_price()
Fetch the current Bitcoin spot price from Coinbase.Current BTC/USD spot price
get_open_market()
Get the next expiring open KXBTC15M market from Kalshi.Market object with ticker, yes_ask, no_ask, close_time, or None if no open markets
get_market(ticker)
Get a specific market by ticker symbol.Market ticker (e.g., “KXBTC15M-24DEC05-T1030”)
Market object including result field if settled
get_settled_side(market)
Check if a market is settled and return the winning side.Market object from Kalshi API
Returns “yes”, “no”, or None if not settled
Trade Management
load_trades()
Load all trades from the CSV file.List of trade records with all strategy data
save_trades(trades)
Save all trades to CSV with complete field set.List of trade dictionaries to persist
calc_stats(trades, strategy=None)
Calculate performance statistics for trades.List of trade records
Optional strategy name to filter by (e.g., “CONSENSUS”, “MOMENTUM”)
Statistics dict with keys: total_staked, total_profit, wins, losses, pending
Consensus Strategy Functions
consensus_bankroll(trades)
Calculate current consensus strategy bankroll from realized P&L.All trade records
Initial bankroll plus realized consensus profits
consensus_period_pnl(trades, now)
Calculate daily and weekly P&L for consensus strategies.All trade records
Current datetime for period calculation
Today’s realized P&L
This ISO week’s realized P&L
rolling_consensus_metrics(trades)
Calculate rolling performance metrics and break-even win rate.All trade records
Dict with keys: sample_size, win_rate, break_even_win_rate
Trading Strategies
The bot implements 7 simultaneous paper trading strategies:- PREVIOUS: Buy the same side as the previous market’s result
- MOMENTUM: Buy based on BTC price direction over 60 seconds
- CONSENSUS: Only trade when PREVIOUS and MOMENTUM agree (with risk management)
- MOMENTUM_15: Buy based on BTC direction over 15 minutes
- PREVIOUS_2: Wait for PREVIOUS side at price ≤ $0.45
- CONSENSUS_2: Wait for CONSENSUS agreement at price ≤ $0.45
- ARBITRAGE: Buy immediately then hedge opposite side if profitable
Main Loop
main()
Main bot loop that monitors markets and executes all strategies.- Polls for open markets every
POLL_SECONDS - Tracks BTC price history for momentum signals
- Executes trades when strategy conditions are met
- Checks for settled markets and updates P&L
- Enforces risk limits for consensus strategies
- Load historical trades from CSV
- Print initial statistics for all strategies
- Monitor markets continuously
- Execute trades according to each strategy’s rules
- Update and save trades as markets settle