Trading bot risk management: the part that actually keeps you alive
A trading bot's edge decides whether you win slowly; its risk management decides whether you survive to find out. Automation makes risk failures catastrophic — a bug that overrides a stop, a strategy that quietly leverages up, a flash crash hitting a bot with no kill switch, and the account is gone before you wake up. This is the discipline that separates a bot that compounds from one that detonates. Here are the rules, the math, and the code.
Why automation raises the stakes
A human trader hesitates, second-guesses, walks away. A bot does exactly what it's told, instantly, forever — including the wrong thing, at scale, at 4am. Risk management isn't a feature you add later; it's the frame the whole bot lives inside.
The 1% rule
Risk no more than ~1% of the account on any single trade. Not 1% position size — 1% of account at risk if the stop is hit. This single rule means a brutal losing streak dents you instead of ending you.
Position sizing from the stop
Size flows backward from your stop. Decide the dollar risk (1% of account) and the stop distance, and the position size falls out:
python · size.pydef position_size(account, risk_pct, entry, stop):
risk_amount = account * risk_pct # e.g. 1%
per_unit_risk = abs(entry - stop)
return risk_amount / per_unit_risk # units to buy
position_size(10000, 0.01, 100, 95) # 20 units
Do it for every trade — or just use the position sizing calculator.
Hard stop-losses, enforced server-side where possible
Place the stop as a resting order on the exchange, not just logic in your code. If your bot crashes (see deployment), a server-side stop still protects the position. A stop that only exists in a dead process protects nothing.
The max-drawdown kill switch
The most important safety system: if the account draws down past a hard threshold (say 15–20% from peak), the bot flattens everything and stops. No exceptions, no "it'll come back." A kill switch turns a slow bleed or a runaway bug into a bounded, survivable loss.
python · kill_switch.pyif equity < peak_equity * (1 - max_dd):
flatten_all_positions()
halt_trading()
alert('KILL SWITCH: max drawdown breached')
Exposure and leverage caps
- Total exposure cap — limit how much of the account can be in the market at once.
- Per-asset cap — don't let correlated positions secretly become one big bet.
- Leverage ceiling — keep it low; leverage multiplies the very losses risk management exists to bound.
Validate that your sizing and stops actually produce a survivable drawdown on our backtester before risking a cent. Then read the common mistakes that defeat even good risk rules.
Frequently asked questions
What is the 1% rule in trading?
The 1% rule means risking no more than about 1% of your account on any single trade — measured as the loss you'd take if the stop is hit, not the position size. It ensures that even a long losing streak only dents the account rather than ending it, making survival a function of sizing rather than the signal.
How do I size a position for a trading bot?
Work backward from your stop. Decide the dollar risk (e.g. 1% of the account) and the distance from entry to stop, then divide: position size equals risk amount divided by per-unit risk. This automatically gives smaller positions for wider stops and larger ones for tighter stops, keeping risk constant.
What is a kill switch for a trading bot?
A kill switch is a hard rule that flattens all positions and halts trading when the account draws down past a set threshold from its peak, such as 15–20%. It converts a slow bleed or a runaway bug into a bounded, survivable loss instead of a blown account, and it's the single most important safety system a bot can have.
Should stop-losses be on the exchange or in my code?
Place stops as resting orders on the exchange whenever possible, not only as logic in your code. If your bot crashes or loses connectivity, a server-side stop still protects the position, whereas a stop that exists only in a dead process protects nothing.