Breakout trading bot (strategy, code and false breakouts)
A breakout bot waits for price to escape a defined range — a prior high, a channel, a consolidation — and trades in the direction of the escape, betting that a coiled market releases energy in one direction. It's the engine behind trend-following and behind the famous Turtle traders. The whole game is one problem: the false breakout, where price pokes out, traps the eager, and snaps back. This guide gives the rules, the Donchian and volatility variants, the code, and how to filter the fakes.
The core idea
Markets alternate between coiling (low-volatility consolidation) and releasing (a trending expansion). A breakout bot tries to be on board the instant the release begins, by buying when price closes above a recent resistance or selling when it breaks support. It accepts that it will be wrong often — that's the cost — in exchange for catching the occasional large trend that pays for everything.
Two ways to define a breakout
- Donchian channel — buy when price closes above the highest high of the last N bars (the classic Turtle rule, e.g. N=20); exit on the opposite N-bar low.
- Volatility breakout — buy when price moves more than a multiple of the recent average true range (ATR) from the open or prior close.
The code
python · breakout.pydef donchian_signal(highs, lows, closes, n=20):
upper = max(highs[-n-1:-1]) # prior N-bar high (no lookahead)
lower = min(lows[-n-1:-1])
if closes[-1] > upper: return 'buy'
if closes[-1] < lower: return 'sell'
return 'flat'
The false-breakout problem
Ranges are where stops cluster, so price often spikes just past the boundary to trigger orders, then reverses — trapping breakout traders at the worst price. The defences: require a close beyond the level (not just a wick), demand a buffer (a fraction of ATR past the line), or wait for a retest. None of these eliminate fakeouts; they trade some missed real breakouts for fewer fake ones.
Volume and volatility filters
A real breakout usually arrives with a surge in volume and an expansion in range; a fake one is quiet. Gating entries on above-average volume, or on a volatility expansion (today's range > recent average), filters out a meaningful share of the fakes. This is the same squeeze-then-expand logic behind the Bollinger squeeze.
Sizing and risk
Place the stop back inside the range (a failed breakout should be a small, fast loss) and size from it with the position calculator. Because breakouts win a minority of the time, expectancy comes from letting winners run — pair the entry with a trailing exit, and validate the N and the filter out-of-sample in the backtester. Breakout is the entry engine of every trend-following bot.
Frequently asked questions
What is a breakout trading bot?
A breakout bot waits for price to escape a defined range — such as a 20-bar high or a volatility threshold — and trades in the direction of the escape, betting that consolidation releases into a trend. It catches big moves but takes many small losses on failed breakouts.
What is a Donchian channel breakout?
A Donchian channel marks the highest high and lowest low over the last N bars. The classic breakout rule, used by the Turtle traders, buys when price closes above the N-bar high and sells when it breaks the N-bar low, exiting on the opposite channel.
How do you avoid false breakouts?
Require a candle to close beyond the level rather than just wick past it, demand a buffer of a fraction of ATR, wait for a retest of the broken level, or filter on a volume/volatility surge. These reduce fakeouts but cost some genuine breakouts in exchange.
Where do breakout bots fail?
They bleed in choppy, range-bound markets full of false breakouts, where price repeatedly pokes past a level and snaps back through clustered stops. They rely on the occasional large trend to pay for many small losses, so cutting losers fast is essential.