Kill Switch & Position Limits for Kalshi and Polymarket US Bots

KalshiPolymarket US Paper / demo first

PASS WITH FIXES Tested on Composer 2.5, , result: PASS WITH FIXES. We built the limits, kill switch and daily-loss stop from the prompt as a standalone kit (2 tests passing). Kalshi and Polymarket US cancel-all were mocked: no real keys, orders or money. The prompt below includes the fixes from that run.

Short answer: A good kill switch for a prediction-market bot has three layers: a local KILL file the bot checks before every order, the venue's cancel-all endpoint (Kalshi DELETE /portfolio/events/orders, Polymarket US POST /v1/orders/open/cancel), and Kalshi order groups as a server-side safety net. Position limits belong in code and are checked before every order.

What are the key facts?

  • Layer 1, local: the bot checks for a KILL file (or BOT_KILL=1) before every order and refuses to trade if it's there.
  • Layer 2, venue cancel-all: Kalshi DELETE /portfolio/events/orders (cancels resting orders across shards; Kalshi notes newly placed orders may also be cancelled during the minute after the request). Polymarket US POST /v1/orders/open/cancel.
  • Layer 3, server-side: Kalshi order groups cancel every resting order in the group and block new ones when fills exceed a contracts limit in a rolling 15-second window (limit range 1–1,000,000).
  • Polymarket US note: pure cancels are never rejected by its 5-second latency stopgap, so cancel-all works even when new orders are being rejected.
  • Limits live in a config file and are enforced in code before every order, not just described in a prompt.
DifficultyBeginner
Time1 hour
Cost$0
Works withClaude Code, Cursor, ChatGPT / Codex, Grok

What will you build?

Retrofit any existing bot with hard limits checked in code, a one-file kill switch, a cancel-all command for each venue, and Kalshi's server-side order-group safety net, then prove they work in demo/paper.

Who is it for?

Anyone with a bot (theirs or from GitHub) that doesn't yet have a fast, tested way to stop.

What prompt do you paste into your AI coding tool?

Works in Claude Code, Cursor, Codex/ChatGPT, Grok or any agent that can write files. Paste it into an empty folder. Read what it builds before you run anything.

Add hard risk limits and a three-layer kill switch to this existing trading
bot. Read the codebase first and list every place an order can be created.
(If there is no bot in this folder yet, stop and tell me. Don't invent one.)
Use only endpoints documented at https://docs.kalshi.com and
https://docs.polymarket.us. Ask me if unsure.

1) limits.json + risk.py
   - max_contracts_per_order, max_open_exposure_usd, max_daily_loss_usd,
     max_orders_per_minute, allowed_tickers (allowlist).
   - check_order(order, state) raises RiskError on any breach. EVERY code
     path that creates an order must call it first. Add a test that fails if
     any order function is reachable without check_order.
2) Local kill switch
   - If a file named KILL exists in the project root, or BOT_KILL=1, refuse
     all new orders and log "KILL ACTIVE" each loop. Checked inside
     check_order AND at the top of the main loop.
3) Venue cancel-all (cancel_all.py)
   - Kalshi: DELETE /portfolio/events/orders (signed request).
   - Polymarket US: POST https://api.polymarket.us/v1/orders/open/cancel
     (signed request).
   - Print what was cancelled; then re-query open orders to confirm.
   - If keys are missing (or BROKER_MOCK=1), print the exact request it
     would send and send nothing.
4) Kalshi server-side safety net (if the bot trades Kalshi)
   - On startup, POST /portfolio/order_groups/create with contracts_limit
     from limits.json (order_group_contracts_limit), store the id, and pass
     order_group_id on every order.
   - On shutdown, leave the group (don't delete it automatically) and log
     its id so I can inspect it.
5) Daily loss stop: track daily loss as a non-negative dollar amount
   (max(0, -realized PnL today)). When it reaches max_daily_loss_usd,
   create the KILL file automatically and run cancel-all.
6) Tests for every limit, the KILL file, and the daily loss stop. Run them.

Don't change the strategy logic. Show me a diff summary when done.

How do you build it, step by step?

  1. Commit your bot first so you can see exactly what changes. This recipe retrofits an existing bot. Don't have one? Build the Kalshi AI Trading Bot first and run this prompt in that folder.
  2. Paste the prompt into your AI coding tool. Make sure it lists every order path before changing anything.
  3. Set limits you'd be fine losing entirely. Then halve them.
  4. Run the tests. They should cover every limit, the KILL file and the daily-loss stop.
  5. Drill in demo/paper (no keys yet? BROKER_MOCK=1 python cancel_all.py shows the requests without sending them): start the bot, touch KILL, confirm no new orders, run python cancel_all.py, then confirm in the venue UI that nothing is resting.
  6. Drill the Kalshi order group (demo): set order_group_contracts_limit very low, let a couple of tiny demo fills happen, and confirm Kalshi triggered the group and cancelled the rest.
  7. Write your stop procedure on a sticky note: touch KILL → python cancel_all.py → check the venue UI → revoke the API key if anything looks wrong.

How do you test it in paper or demo mode first?

Test every layer before any live use:

  • Kalshi: run the drills on the demo exchange (https://external-api.demo.kalshi.co/trade-api/v2) with demo keys. Order groups and cancel-all behave like production there.
  • Polymarket US: there's no public demo exchange, so drill the local layers (KILL file, limits, daily-loss stop) against your paper ledger. Test cancel-all live only with a single tiny resting limit order far from the market, when you're ready.

Time yourself. You want "something's wrong" → "everything cancelled" in under 30 seconds.

What safety checks does it need before real money?

Tick every box before the bot touches real money. The full rules file, .gitignore and pre-commit hook are in the Safety Pack.

Which official docs did we check?

Endpoints and rules were checked against these pages on Oct 19, 2026. APIs change, so if something here disagrees with the official docs, trust the docs and tell us.

Frequently asked questions

What is a kill switch for a trading bot?

A fast, reliable way to stop a bot from placing new orders and cancel the ones already resting. Good setups have a local switch (a file or flag the bot checks before every order), a cancel-all command using the venue's API, and if available, a server-side limit like Kalshi order groups.

What are Kalshi order groups?

A Kalshi feature that tracks fills for a group of orders in a rolling 15-second window. If fills exceed the group's contracts limit, Kalshi cancels all resting orders in the group and rejects new ones until you reset it.

How do I cancel all orders on Kalshi via the API?

Send a signed DELETE /portfolio/events/orders request. It cancels resting event-market orders across exchange shards, and Kalshi notes orders placed in the following minute may also be cancelled.

How do I cancel all orders on Polymarket US via the API?

Send a signed POST https://api.polymarket.us/v1/orders/open/cancel, which cancels all your open orders, optionally filtered by market. Pure cancels aren't affected by the 5-second latency stopgap that can reject new orders.

Related recipes