anchor-demo / options trader bot / manual

ZDT autopilot: SPY 0DTE opening-range breakout on Interactive Brokers

A trading script, finished and tested. The original arrived cut off mid-line at the PUT trigger check (elif last_price < orb_lo); every change from it is listed below, and every value in zdt_autopilot.py that is not from the original says so beside it.

Run it on a paper account first

  1. Start TWS (or IB Gateway) logged in to the PAPER account. In TWS: Global Configuration, API, Settings: tick "Enable ActiveX and Socket Clients", port 7497.
  2. The account needs live market data for SPY and for US options (OPRA). Without it the bot stops at startup and says so.
  3. pip install -r requirements.txt
  4. py zdt_autopilot.py

The bot refuses a live port (7496, 4001) or a live account (IBKR paper account IDs start with "D") unless you add --live.

It sends no orders until the two exit rules are set (STOP_LOSS_PCT, TAKE_PROFIT_PCT near the top of the file). The original stops before its exit rules, so they are not known. Until they are set it runs SIGNALS ONLY: it does everything else (opening range, signals, the contract it would buy, how many) and prints the order it would send.

The use case it follows

Actor: the trader, running the script on their own PC beside TWS or IB Gateway. Goal: on a trading day, buy a SPY 0DTE option when SPY breaks out of its first 15-minute range, sized to the balance, let IB hold the exits, write every trade to the CSV, and never leave an order or a position behind. Preconditions: TWS or IB Gateway is running with the API on PORT; the account has live SPY and OPRA data.

Main path, each step with what you see:

  1. You start the script. It connects and prints PAPER or LIVE with the account ID.
  2. It reads the account's USD TotalCashBalance (the original's field) and its own budget: STARTING_BUDGET_USD ($100) before its first trade, then the last ending balance in its log. It prints [ACCOUNT] and [BUDGET].
  3. It reads today's session hours from IB, waits for the first 15-minute bar to close, and reads that bar. It prints [ORB ESTABLISHED] and the two triggers (high + ORB_BUFFER, low - ORB_BUFFER).
  4. It streams SPY's last price and prints a status line every second.
  5. SPY trades above the CALL trigger or below the PUT trigger. It prints [SIGNAL].
  6. It picks today's expiry and the listed strike nearest spot +/- 1.50 rounded to the dollar (the original's rule), always out of the money, a tie going to the one nearer spot +/- 1.50. It prints [CONTRACT].
  7. It reads that option's ask and sizes the order with the original's tier table, from its budget and never from more than the account's cash. It prints [ORDER].
  8. It buys with a limit at the ask and waits for the fill. It prints [FILLED].
  9. It places the stop and the target at IB as one OCA pair, so they stay working even if the PC or the script dies. It prints [EXITS AT IB].
  10. One exit fills, or the flatten time arrives (10 minutes before the close) and it sells at market. It prints [TRADE CLOSED].
  11. It writes one row to trade_execution_log.csv in the original's columns, with the net P/L after the commissions IB reports.
  12. It waits for SPY to come back between the triggers before it takes another signal, and stops for the day after MAX_CONSECUTIVE_LOSSES losses in a row. It prints [REARMED] or [CIRCUIT BREAKER].
  13. At the flatten time, or on Ctrl+C, it cancels its own orders, sells what it bought, and disconnects.

Alternate paths:

What is from the original and what is new

Kept as the original wrote it: the configuration, the CSV columns, the tier table, the strike rule (spot +/- 1.50, nearest within $5), the ORB buffer and triggers, the consecutive-loss breaker, the prints.

Changed, each one a defect in the original as it arrived:

New, because the file ends before them: steps 7 to 13, the live guard (1a), the session hours (3b), the re-arm rule (12), and the cleanup (13). Every value that is not from the original is marked in the file.

From the owner's answer (2026-11-08): start it with $100 and trade 0DTE within that budget, buying cheap calls when the market is going up and cheap puts when it is going down. The bot now sizes from its own budget, STARTING_BUDGET_USD to start and the log's last ending balance after that, and never from more than the account holds (2, 7, 2b). Calls on an up move and puts on a down move is the breakout rule it already had (5).

Open questions for the owner

  1. The original ends at the PUT check. Is there more of it? If not:
  2. What stop and target do you want (STOP_LOSS_PCT, TAKE_PROFIT_PCT)?
  3. What does TIME_STOP_PCT = 0.25 do? The original sets it but uses it after the point where it ends, so the bot does not use it yet.
  4. After a trade, should it trade again the same day? It now waits for SPY to come back inside the range first, then takes the next breakout, until two losses in a row.
  5. The trend: the bot reads it as SPY breaking out of the first 15-minute range, up for calls and down for puts. Is that the read you mean, or another one (VWAP, a moving average)?
  6. Cheap lotto options: the strike is the original's fixed $1.50 from spot (about 0.20 delta in its comment). When that option costs more than the budget, should it go further out to a cheaper one, or skip the day? And keep the fixed offset, or pick by the delta IB reports?
  7. Answered 2026-11-08: start with $100 and trade within it. The budget carries over from day to day in the log; say if it should start again at $100 each day instead.

The light loop

spec_check/ holds a check of the code against a written spec, spec_check/SPEC.md: the use case above, the original script's own comments, and ib_async's documentation, each copied word for word. Every rule in rules.json quotes it, every suspect in suspects.json names a rule and a code line, and a fault counts only when that suspect's repro (repros/) fails its own assertion on the code.

It now holds 19 rules and 18 suspects. The first run proved 3 faults (REPORT_before_fixes.md): a tie between two listed strikes went to the one farther from spot +/- 1.50; with the strikes just above spot unlisted it could buy an in-the-money call, against the original's "slightly OTM"; and Ctrl+C while waiting for IB's commission report left a closed trade out of the CSV. All three are fixed and each has a test. The second run proved none (REPORT.md).

Run any repro on its own: python spec_check/run_repro.py S1.

Epics and use cases

model/ is the bot written as two epics: trading one day (seven use cases) and getting it ready for real money (four). Each use case gives its actor, trigger, success and minimal guarantee, its steps, and every alternate path with where it leads. Requirements (model/requirements.yaml) never name code: each is carried by steps, and only the steps name code. model/step_tests.yaml names the tests that run each step and path; py tests/step_trace.py runs them under a tracer and writes the code each one ran (model/step_trace.json), and tests/test_model.py fails any step whose named code its tests never ran. py make_epics_page.py OUT writes the page that shows it all. The model also passes the closed-loop kit's use case checker (usecase_model.py, rules U1 to U19) with no holes; set USECASE_MODEL to that file and tests/test_model.py runs it too.

Tests

pip install -r requirements-dev.txt, then py -m pytest tests (84 tests) runs the bot through whole simulated trading days against an in-memory TWS (tests/fake_ib_async.py), with tests for every step and alternate path of the trading epic. No TWS, network or account is involved. tests/test_api_contract.py reads the real ib_async source and checks that every ib. call, keyword and field the bot uses exists there. It runs when ib_async is installed, or when IB_ASYNC_SRC points at its source, and is skipped otherwise.

py tests/plant_check.py plants 30 defects, one at a time, in copies of the bot and of the model (the original's five among them), and each must make a test fail. A check that has only ever passed cannot be told from one that is switched off.

What the tests cannot show: how IB itself fills, prices and reports on a given day. The first paper run is that check.

py tests/demo_day.py prints one simulated day the way the terminal shows it (tests/demo_day_signals_only.txt); py tests/demo_day.py --exits 0.5 1.0 does the same with a stop and a target set, test values only (tests/demo_day_with_exits.txt).

Replay on real sessions

sim/ runs the bot's own code, unchanged, through SPY's real one-minute bars: py sim/market_data.py fetches the latest five sessions (Yahoo Finance's public chart data), py sim/replay.py replays them with the budget carried day to day (--reset-daily starts each at $100; --stop and --target set the test exits), and py sim/make_replay_page.py OUT writes the page that plays each day back. Option prices there are modeled (Black-Scholes, VIX as the volatility, the time left to the close), because past 0DTE quotes are not in free data; sim/README.md says what is real and what is modeled. The replay has its own light loop (sim/spec_check/): six rules from its use case, among them that no decision uses a price from the future, none broken, and a planted look-ahead caught.

How others do it

Searched on 2026-11-08. Each item links to a public page; where that page blocks readers, a readable summary is linked beside it. This is what others built and what their authors report, not advice.

On Hugging Face

A search of Hugging Face's model, dataset and Space names for "0dte", "opening range breakout" and "spy options" finds no model or dataset for this strategy: three Spaces have 0DTE in their names, and the two datasets it matches are unrelated. The nearest data:

Tools that run this kind of bot

The same signal in research

0DTE options and their costs in research

AI agents that trade

How this bot differs

Talk to the page

The bot's pages carry Living Page, a small package any page can use: one script and a Cloudflare Worker that holds the Claude key. Press Talk to this page and Claude answers aloud, from this README, the epics model and the bot's settings. On the bot page you can also tell it how to change the page: reword a part, add or remove one, reorder them, or change the colour and the text size. The change is made at once, in your own browser only, and Undo puts it back; only the page's owner can keep a change for everyone. Claude cannot change the bot, its code or anyone's account, and it gives no trading advice. relay/build_docs.py writes what the relay tells Claude about the bot.


Software, not financial advice. Options can lose their whole premium, and 0DTE options expire the same day. Run it on a paper account first.