DEV Community

BotCookery
BotCookery

Posted on Originally published at botcookery.com

Our ChatGPT/Codex Kalshi starter would have bought the same contract 38 times in under 13 minutes

Originally published on BotCookery, free build recipes for AI-built trading and prediction-market bots. Written with AI assistance. Every log line and count below is from the Oct 25, 2026 rebuild: a dry run with no keys on Kalshi's live public demo market data. Nothing was signed with a real key and no orders were sent. Nothing here is financial advice. Demo and dry run only.

"Can ChatGPT build me a trading bot?" Yes, it can write the code. It can't make the bot profitable, and it often remembers outdated API endpoints. The BotCookery recipe is a prompt for ChatGPT (chat) or Codex (agent) that builds a small Kalshi bot on the demo exchange, with the rules in AGENTS.md and the limits in code.

On Oct 25, 2026 we pasted the Oct 22 version of that prompt into a clean folder, built every file it asks for, following it line by line, and ran the bot without keys against live Kalshi demo data that afternoon. The V2 order body, the dry run and the kill switch all worked. The trading rule did not: while the ask stayed low, it proposed a new 1-contract bid on every loop. On one ticker that was 38 orders in under 13 minutes, then 38 more after a restart.

What the prompt builds

The bot is built one file at a time, waiting for your "next" between files:

  • kalshi_client.py: signed requests (RSA-PSS with SHA-256 or Ed25519), base URL fixed to https://external-api.demo.kalshi.co/trade-api/v2. Key id and key file path come from env vars. Without them the bot dry-runs: it reads public data, logs what it would send, and sends nothing.
  • risk.py: limits.json (1 contract per order, $5 open exposure, $2 daily loss, 3 orders a minute) plus a KILL file or BOT_KILL=1. If any check errors, the order is blocked.
  • bot.py: watches one ticker, reads yes_ask_dollars and yes_ask_size_fp, and places through POST /portfolio/events/orders (Create Order V2) with side "bid", count "1" and a dollar-string price like "0.4500".
  • cancel_all.py: DELETE /portfolio/events/orders.
  • find_ticker.py, a README and a .gitignore.

If the model writes the legacy /portfolio/orders for order creation, it's working from old memory, so re-paste the docs.

Bug 1: "1 contract per order" isn't a position limit

The old build at 4:32 PM ET, no keys, ticker KXNFLTD-26OCT25KCSEA-KCAARMSTRONG85-1 (Andrew Armstrong to score a touchdown), ask $0.13, max price $0.15 (timestamps and order IDs trimmed):

{"decision": "dry_run_would_send", "body": {"ticker": "KXNFLTD-26OCT25KCSEA-KCAARMSTRONG85-1", "side": "bid", "count": "1", "price": "0.1300", ...}}
{"decision": "dry_run_would_send", "body": {... "price": "0.1300", ...}}
{"decision": "dry_run_would_send", "body": {... "price": "0.1300", ...}}
{"decision": "blocked", "reason": "max_orders_per_minute", "ask": 0.13}
{"decision": "blocked", "reason": "max_orders_per_minute", "ask": 0.13}
{"decision": "blocked", "reason": "max_orders_per_minute", "ask": 0.13}
Enter fullscreen mode Exit fullscreen mode

Only the 3-a-minute limit slowed it, and only the $5 exposure cap stopped it. The same risk code on a fake clock: one hour at ask 0.13 allowed 38 buy orders and $4.94 of exposure, and after a restart, 38 more.

Every individual order obeyed "1 contract per order". The bot just kept making them.

Bug 2: "$5 exposure" and "$2 daily loss" weren't defined

A literal build counted orders sent, in memory, and left daily loss at 0. So a restart wiped exposure, and the daily loss limit could never trip. A limit that can't trip looks exactly like a limit that's working.

Bug 3: "keep the key outside your project" doesn't hide it from Codex

We tested Codex CLI 0.164.0's sandbox on its own (no model, a throwaway file, not a key). In its normal edit mode, both cat and a Python script read ~/.config/kalshi/demo.pem. A Codex permission profile that denies that folder blocked both, and project writes still worked (paths shortened):

--- default
THROWAWAY-NOT-A-KEY
THROWAWAY-NOT-A-KEY
project write ok
--- with "~/.config/kalshi" = "deny"
cat: ~/.config/kalshi/demo.pem: Permission denied
PermissionError: [Errno 13] Permission denied: '~/.config/kalshi/demo.pem'
project write ok
Enter fullscreen mode Exit fullscreen mode

Bugs 4 to 6: docs, ticker search, the small stuff

  • The pasted docs didn't cover the code. The prompt said to use only endpoints from the pasted pages, but GET /markets/{ticker} and the cancel-all DELETE weren't on the two pages it listed. A careful model should stop and say so.
  • The ticker search read one page. That returns 1,000 markets plus a cursor, and only 19 had an ask. Following the cursor gave 217 pages and 7,348 markets with an ask, and Kalshi rate-limited us partway (HTTP 429).
  • Small things. Only risk.py had tests, so a wrong signature would show up as 401s on your first keyed run. The .gitignore was thin.

What we changed

  • One position per ticker. Before proposing, the bot checks for a resting order or a position on the ticker (in dry run, a paper order in state/paper.json) and logs "holding". It skips unless the market status is "active".
  • Exposure is computed, not counted. Before every order: resting orders (yes_price_dollars * remaining_count_fp) plus positions' market_exposure_dollars, from your demo account.
  • Daily loss from the balance. Today's first balance plus portfolio value from GET /portfolio/balance is saved in state/day_start.json. In dry run it logs "daily loss not tracked" instead of pretending it's 0.
  • Order timestamps saved to disk, so a restart doesn't reset the per-minute limit.
  • Step 3 sets up a Codex permission profile that denies ~/.config/kalshi (shown below).
  • The prompt lists every docs page it needs: Quick Start authenticated requests, Create Order (V2), Get Market, Get Markets, Get Orders, Get Positions, Get Balance, Cancel All Orders.
  • find_ticker.py follows the cursor through GET /markets?status=open&mve_filter=exclude&limit=1000 and retries on 429 (1, 2, 4, 8, 16 s).
  • Signing tests for RSA and Ed25519 against the public key, with a fake HTTP session. .gitignore now covers .env.*, *.key and state/. Without keys, cancel_all.py exits 1 and says nothing was sent.

The Codex step

Edit ~/.codex/config.toml yourself, not through Codex, and restart it:

default_permissions = "kalshi-bot"
[permissions.kalshi-bot]
extends = ":workspace"
[permissions.kalshi-bot.filesystem]
"~/.config/kalshi" = "deny"
Enter fullscreen mode Exit fullscreen mode

Permission profiles are a beta feature and don't combine with sandbox_mode or --sandbox, so leave those out. Then ask Codex to cat ~/.config/kalshi/demo.pem; it should get "Permission denied". In plain ChatGPT chat, skip this step, since chat can't read your files.

After: a fresh rebuild

A fresh rebuild of the new prompt (10 tests pass), 4:33 PM ET, same ticker, then a restart, then the kill-switch drill (timestamps and order ID trimmed):

{"decision": "dry_run", "body": {... "count": "1", "price": "0.1300", ...}, "exposure": 0, "daily_loss": "not tracked in dry run"}
{"decision": "holding", "ask": 0.13}
{"decision": "holding", "ask": 0.13}
--- restart
{"decision": "holding", "ask": 0.13}
--- KILL drill
{"decision": "blocked", "reason": "kill switch on", "ask": 0.13}
--- cancel_all
No Kalshi credentials set (KALSHI_API_KEY_ID / KALSHI_PRIVATE_KEY_PATH). Nothing was sent.
Enter fullscreen mode Exit fullscreen mode

One paper order, then "holding", and still "holding" after a restart.

Built this before Oct 25?

Do the Codex permission step above, then paste this follow-up into the same project:

Update this Kalshi demo bot. Use only the Kalshi docs pages I paste
below (Get Market, Get Markets, Get Orders, Get Positions, Get Balance,
Cancel All Orders). Never run the bot with my keys; I run it myself.
1. One position only: before proposing, skip and log "holding" if this
   ticker has a resting order (GET /portfolio/orders?ticker=...&
   status=resting) or a position (GET /portfolio/positions?ticker=...,
   position_fp != 0). In dry-run, keep paper orders in state/paper.json
   and apply the same rule. Skip unless the market status is "active".
2. Open exposure: compute it from the exchange before each order
   (resting yes_price_dollars * remaining_count_fp plus positions'
   market_exposure_dollars), not from a counter of orders sent.
3. Daily loss: save today's first balance + portfolio_value from
   GET /portfolio/balance (cents) in state/day_start.json and block when
   the current value is max_daily_loss_usd below it. No placeholder 0.
4. Save order timestamps in state/ so a restart doesn't reset the
   per-minute limit. If any risk check errors, block the order.
5. cancel_all.py with no credentials: exit 1, "Nothing was sent."
6. Add find_ticker.py: follow the cursor through
   GET /markets?status=open&mve_filter=exclude&limit=1000, wait and
   retry on HTTP 429, print markets with 0 < yes_ask_dollars < 1.
7. Add .env.*, *.key and state/ to .gitignore.
8. Tests with a fake HTTP session for each of these, plus RSA and
   Ed25519 signature checks against the public key.
Show me the diff before running anything.
Enter fullscreen mode Exit fullscreen mode

Starting fresh? The full one-shot prompt is on the recipe page.

Chat or Codex?

In chat you copy code by hand: slower, but you see every line. Codex edits files and runs commands: faster, so the rules matter more. Either way, never paste keys, key files or account screenshots into ChatGPT, and run the bot with real keys in your own terminal, not through Codex.

Limits

The page's verified line, word for word: Verified working on live data in paper mode, Oct 25, 2026. Dry run on live Kalshi demo data with no keys. Signed demo orders need your own demo keys.

So this run proves plumbing on public demo data: the V2 order body, the one-position rule, the kill switch and the paper ledger. The exposure, daily-loss and holding checks against real demo orders are covered by tests with a fake HTTP session, not by a signed run. You'll need your own demo keys for that. Kalshi notes demo prices may not reflect real markets, so judge your code there, not your strategy. There's no evidence that asking ChatGPT for trades produces reliable profits. Use it to build tools.

The full recipe, with the complete prompt, the step-by-step build, the safety checklist and every doc we checked, is at botcookery.com/recipes/chatgpt-trading-bot-codex.

API changes and new field notes: @botcookery on X (https://x.com/botcookery)

Top comments (0)