A Bot Kill Switch: Stopping Reliably When Something Goes Wrong
Automated trading can reach a moment when you need to stop immediately: prices look wrong, the bot repeats orders or losses grow faster than expected. Unless you decide in advance what to press, you may stop it in the worst possible way.
Stopping has three different meanings
“Stop the bot” sounds like one instruction but can mean three different actions. A stop button whose meaning is unclear can cause an accident in an emergency.
1. Block new exposure, the most common mode
· No new entries
· No averaging down or increases
· Continue stops and take-profits for existing holdings
2. Management only
· In addition to mode 1, stop signal calculations
· Monitor stop orders only
3. Close everything
· Close existing positions at market
· This cannot be undone
─────────────
Suggested default
→ Mode 1: avoid increasing exposure while continuing to protect existing positions.
If mode 3 is the default, a temporary query failure can close otherwise valid positions and realize losses. If only mode 1 exists when mode 3 is needed, positions must be closed manually. It is therefore better to use two separate buttons: a routine block on new exposure and a close-all action with an additional confirmation.
Closing the program window is a poor way to stop
In a hurry, people often close the window or terminate the program in Task Manager. That can remove monitoring rather than stop market exposure.
Open position: BTC long, 20x leverage, margin $40
Notional = 40 × 20 = $800
Planned stop at −3% = −$24
If the stop exists only inside the bot:
Bot stops → stop protection disappears
Price −8% → loss −$64
That is 1.6 times the $40 margin in this illustration.
─────────────
Difference: −24 → −64 = 2.7 times
The bot stopped; the market did not.
A position remains open after the bot stops. If the bot watches prices and closes positions instead of placing an actual exchange-side stop order, turning it off leaves the position unprotected. If forced termination is an option, the stop must be placed on the exchange.
Restarting creates another problem. A bot starting without remembering its holdings may enter again on the first cycle and double the quantity. See bot restarts and state recovery. Forced termination can create both a stopping accident and a restart accident at once.
A stop signal does not arrive instantly
A common kill-switch implementation leaves a flag, such as a file or disabled setting. The bot reads it each cycle and stops placing orders if it is present. The question is how long each cycle takes.
3-second loop with a stop flag
→ Applied within at most 3 seconds
30-second loop with a stop flag
→ Orders may remain possible for 30 seconds
→ An additional entry and an averaging-down order can still occur.
A bot reading the flag only at startup
→ The stop may never take effect.
─────────────
Rules
· Read the flag again immediately before placing an order.
· Failure to read the flag = treat as stopped.
The final rule matters. Treating an unreadable file as no flag and permission to continue lets one disk problem defeat the switch. Stop when the state is unknown is a safer default. This follows the same principle as WebSocket disconnections: silence must not be interpreted as normal market data.
Which conditions should trigger a stop?
A manually pressed switch is insufficient. The bot must also stop while you are asleep or away, so define numerical conditions.
Capital: $2,000
1. Daily loss −3% = −$60
A loss of −$24 per contract gives −$72 after 3 consecutive losses, so the limit is reached before completing that sequence.
2. 4 consecutive losses
3. Order-failure rate
5 failures among the last 20 orders = 25%
4. Quote freshness
Latest quote more than 8 seconds old
5. Bot quantity ≠ exchange quantity
→ Mismatch on 2 consecutive checks
─────────────
Conditions 1 and 2: The strategy does not fit current conditions.
Conditions 3, 4 and 5: The bot is malfunctioning.
→ Use separate switches for these groups.
The first two conditions concern markets moving against expectations; the last three concern the bot itself operating incorrectly. The latter require an immediate stop, while the former may call for taking the rest of the day off. Daily loss limits discusses the human-discipline side; here the limit is enforced by code.
Quantity mismatch is particularly easy to overlook. If the bot's known quantity differs from the account's actual quantity, subsequent orders use the wrong size. Continuing is harmful because the basis for decisions is already broken. See position reconciliation for comparison methods.
An incomplete stop: Only one bot stops
One bot may need only one switch. In practice, there may be two accounts, accidentally duplicated bot processes or separate programs for each coin.
· Separate account-level switches, but only one was pressed.
· Two copies of the same bot, but only one process was terminated.
· Only 2 of 10 coin-specific bots were stopped.
· The stop file was created in the wrong folder, so the bot cannot see it.
─────────────
Verification
Check the order history for 0 actual new orders after stopping.
Pressing the button does not prove the bot stopped.
Pressing stop and actually stopping are different things. Check that no new orders appear for several minutes afterward. Two processes place orders independently, so stopping one can leave trading active. The resulting damage resembles duplicate orders.
Forgetting to resume is also an operational failure
A kill switch can fail by not stopping or by remaining stopped and forgotten. The latter is less noticeable because it creates no trading loss, but there is still a cost.
Stop for maintenance → forget to resume
Meanwhile:
· Every entry opportunity is skipped.
· Loss = 0, profit = 0.
· The trade history contains no trace of the missed activity.
─────────────
What is needed
· Record the stop time.
· Send a reminder after a set interval: “Still stopped.”
· After resuming, verify normal operation.
A stopped bot is quiet, so days can pass without a reminder. Periodic stopped-state notifications reduce forgotten pauses. When resuming, do more than flip the switch: observe a cycle to confirm that normal order placement has returned.
How to test a kill switch
A kill switch may go unused until the most urgent moment. Test it beforehand when there is no emergency.
1. Enter in paper mode or with the minimum quantity.
2. Turn the stop switch on.
3. Check:
· How many seconds did it take?
· Are new entries 0?
· Is protection for existing holdings still active?
4. Turn the switch off.
5. Do normal orders resume?
─────────────
Additional tests
· Make the flag file unreadable.
→ The bot should stop; continued operation is a bug.
· Run two bot instances and stop once.
→ Do both stop?
The third check is easily missed. Some implementations stop stop-loss handling along with new entries, increasing risk instead of reducing it. See the exchange API integration guide for broader order-handling concerns.
Summary
2. Blocking new exposure is the safer default.
3. Closing everything needs additional confirmation.
4. Closing a window can remove monitoring rather than stop risk.
5. A bot-only stop leaves positions unprotected when the bot shuts down.
6. Stop application can lag by one loop interval.
7. An unreadable flag should mean stop.
8. Separate strategy loss triggers from failures, stale data and quantity mismatches.
9. With multiple bots, check for incomplete stops.
10. Pressing stop and stopping are different.
11. Forgetting to resume also has a cost.
12. Verify the switch by testing it before an emergency.
A stopping mechanism should be built with the bot, not improvised after something goes wrong. A prepared switch gives you a clear action when abnormal behavior appears.
Note
Capital, margin, leverage, PnL and timing figures are hypothetical illustrations, not measurements from a particular exchange. Cancellation behavior, persistence of conditional orders after shutdown, request limits and account-specific settings vary between exchanges. Consult the documentation for your exchange and test with small amounts. Leveraged trading can lose all principal, and decisions and outcomes remain your responsibility.
NOONOO TRADING invites you to follow live trading in our free chat.
Start in the bot📈 OKX trading fee discount for new registrations
Register for the OKX Fee Discount →