How do I add a kill switch to an AI trading bot?
A kill switch is a control the agent can read and cannot write. When it is on, the process that sends orders refuses new ones. Closing an open position is a separate step you run, not a step you leave to the agent that failed.
Checked on 2026-10-18. Broker pages below were checked Oct 2026. No business relationship with these companies.
Paste an order the agent proposed into the Agent Order Check. It runs in your browser. The reject list is the guardrails guide. The paper host for each broker is the paper trading guide. The file that already names the operator as the switch is the Desk Kit.
A switch the agent cannot write
The switch is not a sentence in the prompt. The agent can rewrite a prompt. Put the switch where the sender can read it and the agent cannot change it.
Flag file. You create a file, for example one named HALT, in a directory the sender can read and the agent user cannot write. Before every send, the sender checks that file. If the file is there, it does not send. Do not give the agent a tool that deletes the file or writes that directory.
Environment variable. You set TRADING_HALT=1 in the service config or the process environment, from outside the agent. The sender reads it before every send. Do not give the agent a tool that changes its own environment or restarts itself with a new one.
The Desk Kit install writes KILL_SWITCH: operator in limits.md and says you are the only editor of that file. A seat does not change the line. Same idea: the agent reads the file, you write it.
Daily max loss and max order count
Both numbers live in the file you write. The agent does not get a tool that raises them.
Max loss for the day. The Desk Kit limits file has a line MAX LOSS PER DAY. The Risk seat reads that number from limits.md. The realized result for today is an input you supply, not a field copied off the order. When that result has reached the number you wrote, the sender sets the halt flag or refuses the next order. The order does not get to name a higher number.
Max order count for the day. The Desk Kit limits file does not have this line. Add one you write, in the same file, and have the sender read it from that file. Keep a counter in a file the agent cannot write. Increment it on each attempt to send, including a retry. When the count reaches your number, refuse. You pick the integer. This page does not pick one.
A trip writes the halt flag or refuses the send. It does not ask the agent to decide.
Flatten-all and stop-new-orders
Stop-new-orders leaves positions open and refuses the next send. Flatten-all sends closing orders. Wire stop-new-orders as the trip the sender enforces. Run flatten from a separate script you start, or from a broker control, because the process that failed should not be the process that closes the book.
Stopping new orders leaves positions open. Flattening sends orders while you may not be watching the fills. Decide which one you want before you need it, and test both on paper.
What each broker documents
Each item is a control the broker page describes. Checked Oct 2026. A control that is not on the page is left out.
Alpaca
The Alpaca Trading API documents a block on new orders, a cancel-all, and a close-all. The configuration page and the close-all page both list a paper server and a live server.
- PATCH
/v2/account/configurationswithsuspend_tradeset to true. The field text is: "If true, new orders are blocked." Servers listed:https://paper-api.alpaca.marketsandhttps://api.alpaca.markets. (Account configurations) - The account object has
trade_suspended_by_user: "User setting. Iftrue, the account is not allowed to place orders."trading_blockedis a separate flag. The same page says an account may be blocked for trades if Alpaca identifies suspicious activity. That flag is not your switch. (Trading account) - DELETE
/v2/orders"Attempts to cancel all open orders." If an order is no longer cancelable, the server responds with status 500 and rejects the request. (Delete all orders) - DELETE
/v2/positions"Closes (liquidates) all of the account's open long and short positions." Querycancel_orders: "If true is specified, cancel all open orders before liquidating all positions." Paper serverhttps://paper-api.alpaca.markets. (Close all positions)
The same key that can set suspend_trade to true can set it back to false. Keep that key off the agent. The halt the agent cannot undo is the flag file or the env var, plus a broker setting changed by a key or a login the agent does not have.
Interactive Brokers
Interactive Brokers documents a block on API orders, a cancel-all, and a close-all in TWS. The pages below do not document one switch that both flattens the book and then refuses every later order. Connection steps and the paper username are on the Interactive Brokers guide.
- Read-Only API. In Global Configuration, under API, then Settings: "The setting 'Read-Only' is enabled by default and will block all API orders. It must be unchecked to allow any orders from the API." The same lesson says paper accounts work with the TWS API, and to test programs in a paper environment before production trading. (Installing and configuring TWS)
- The API cannot turn Read-Only off by itself. "Read-Only access to the system must be first disabled manually through the GUI." (Settings introduction)
reqGlobalCancel"will cancel ALL open orders including those placed directly from TWS." That cancels what is open. It is not a lasting block on the next order. (Cancel all open orders)- TWS Users' Guide, Trade menu: Close All Positions "Transmits multiple opposite-side orders to close out all positions in your portfolio." The Order menu entry adds that if no market data is available, it creates an opposite side market order, and you must transmit it manually. (TWS Users' Guide)
Read-Only blocks new API orders. That page does not say it cancels orders already working or closes positions. Cancel and close are the other two controls.
TradeStation
TradeStation platform help documents operator commands that cancel all open orders and close all open positions. The Web API pages checked do not document an account-level halt.
- Command line
.CancelAllOrders: "Cancels all open orders.".CloseAllPositions: "Closes all open positions." Both are listed under Order Execution. (Command line reference) - TradeManager, Positions tab: right-click, Close All Open Positions, then Send Now. "If you choose this option to close out your position, it will be sent as a market order using the Intelligent order route. The Confirm dialog is not displayed when using the Send Now Exit All [order details] option." (Closing a position)
- The published Web API documents canceling one open order: DELETE
/v2/orders/{order_id}, "Cancels an open order. You cannot cancel an order that has been filled." That spec does not document an account-level halt or a close-all call. The current docs index recommends v3 and, on the pages read for this check, does not publish a halt. (API docs, Order execution OpenAPI)
These platform commands are for you to run. They are not a tool to hand the agent. The simulator host is on the paper trading guide.
Test the switch on paper
Use the paper or sim host from the paper trading guide. A paper fill is not a live result.
- Point the sender at the paper host. Leave live keys out of the process.
- Turn the switch on. Create the halt file, or set the env var. Ask for an order the checker would otherwise allow. Confirm the sender refuses, and confirm the paper account shows no new order.
- Turn the switch off. Send one paper order. Confirm it appears on the paper account.
- Set the daily order count to a number you have already reached, including 1 if you want the next send to refuse. A retry counts.
- Set the daily max loss at or under the realized result you already recorded on paper. The next send must refuse. Do not raise the limit to let the order through.
- If you use a broker control, call it on the paper host and repeat step 2. The Alpaca configuration and close-all pages list
https://paper-api.alpaca.markets. The Interactive Brokers lesson says to log in to the paper account and test there. The TradeStation simulator base URL is on the paper trading guide.
Then read the guardrails guide and run the Agent Order Check on the order you did not send.
Questions people ask
How do I add a kill switch to an AI trading bot?
A kill switch is a control the agent can read and cannot write. When it is on, the process that sends orders refuses new ones. Closing an open position is a separate step you run, not a step you leave to the agent that failed.
Where does the kill switch live?
Outside the agent. A flag file the sender can read and the agent user cannot delete, or an environment variable you set from outside the process. The Desk Kit names this KILL_SWITCH: operator in limits.md, and the install says you are the only editor of that file. A line in the prompt is not a switch, because the agent can rewrite the prompt.
What is the difference between flatten-all and stop-new-orders?
Stop-new-orders leaves positions open and refuses the next send. Flatten-all sends closing orders. Wire stop-new-orders as the trip the sender enforces. Run flatten from a separate script you start, or from a broker control, because the process that failed should not be the process that closes the book.
Do brokers provide a kill switch?
Some document a control. None of those controls replaces a switch the agent cannot write. Alpaca can block new orders with suspend_trade. Interactive Brokers blocks API orders while Read-Only is on, and that setting is turned off in the GUI. TradeStation platform help documents commands that cancel all open orders and close all open positions. The Web API pages checked do not document an account-level halt. Sources are on this page. Checked Oct 2026.
How do I test a kill switch?
Point the sender at the broker paper or sim host and leave live keys out. Turn the switch on and confirm no new paper order appears. Turn it off and confirm one paper order does appear. Then trip the daily order count and the daily loss number and confirm the next send refuses. A paper fill is not a live result.
Sources checked
Checked on 2026-10-18. Checked Oct 2026.
- Alpaca account configurations
- Alpaca trading account
- Alpaca delete all orders
- Alpaca close all positions
- Interactive Brokers, configuring TWS
- Interactive Brokers settings introduction
- Interactive Brokers cancel all open orders
- Interactive Brokers TWS Users' Guide
- TradeStation command line reference
- TradeStation close a position
- TradeStation API docs
- TradeStation order execution OpenAPI
- Guardrails
- Paper trading
- Agent Order Check
- Desk Kit
Educational only. Not financial advice.