Trend following bot (strategy, code and cutting losers)

Trend following is the oldest, most-studied edge in markets and the philosophy behind the entire managed-futures industry. The rule is almost insultingly simple: get long things that are going up, get out (or short) when they turn down, and let the rare huge trend pay for the many small losses. A trend-following bot automates that discipline — which is the hard part, because the strategy is designed to be wrong most of the time. This guide covers the entries, the all-important exit, the code, and the psychology a bot removes.

On this page
  1. The philosophy
  2. Entries
  3. The exit is everything
  4. The code
  5. Low win rate, fat tails
  6. Sizing & risk
  7. FAQ

The philosophy: cut losses, let winners run

Trend following makes no prediction. It reacts: when a market starts trending, it climbs aboard; when the trend ends, it gets off. The entire edge is asymmetry — many small, quick losses against a handful of enormous winners. Almost nobody can run it by hand because it forces you to sit through long, boring drawdowns and let profits run far past where instinct screams “take it.” That's precisely what a bot does without flinching.

Entries — how a trend bot gets in

The entry barely matters; studies repeatedly show trend systems are robust across entries. What matters is the exit and the sizing.

The exit is the whole strategy

trailing stop ratchets up trend tops stop hit = exit
A trailing stop locks in trend profit, only exiting after the trend genuinely turns.

A trailing stop — a moving exit that ratchets up beneath a rising trend and never moves down — is what lets winners run while capping the damage when a trend dies. ATR-based trailing stops are the standard: they widen in volatile markets and tighten in calm ones.

The code

python · trend_follow.pydef atr(highs, lows, closes, n=14):
    trs = [max(highs[i]-lows[i], abs(highs[i]-closes[i-1]),
               abs(lows[i]-closes[i-1])) for i in range(-n,0)]
    return sum(trs)/n

# ratcheting trailing stop, 3x ATR below the highest close since entry
trail = max(trail, highest_close - 3*atr(highs,lows,closes))
if closes[-1] < trail: signal = 'exit'

The signature: low win rate, fat-tailed wins

Judge it by expectancy, not win rate

Trend followers win 30–45% of trades and still print money because the average winner can be 5–20× the average loser. A losing year happens when no big trend appears. Measure a trend bot by profit factor, expectancy and the size of its biggest winners — use the profit calculator to see how a 35% win rate with a 5:1 payoff compounds.

Sizing and risk

Because trend following endures long losing streaks before a winner lands, sizing is survival: risk a small fixed fraction per trade with the position calculator so no streak buries you. Then validate across many markets and out-of-sample in the backtester — a trend system that only works on one cherry-picked chart is overfit.

Not financial advice. This content is educational. Automated and algorithmic trading carries a real risk of financial loss. Never trade money you cannot afford to lose. Review the SEC investor.gov and CFTC resources before trading.

Frequently asked questions

What is a trend following bot?

A trend following bot buys markets that are rising and exits or shorts when they turn down, letting the rare large trend pay for many small losses. It makes no prediction — it reacts to price — and automates the discipline humans struggle to hold.

What win rate does trend following have?

Trend following typically wins only 30–45% of trades, yet remains profitable because its winners are far larger than its losers. It should be judged by expectancy and profit factor rather than win rate, since a few big trends carry the whole result.

What is a trailing stop in trend following?

A trailing stop is an exit that ratchets in the trend's favour — rising beneath a long position as price climbs and never moving against you. ATR-based trailing stops are standard because they widen in volatile markets and tighten in calm ones, letting winners run while capping losses.

When does trend following lose money?

Trend following has losing periods when markets chop sideways with no sustained trend, generating repeated small losses with no big winner to offset them. These drawdowns can last months, which is why small position sizing and diversification across markets are essential.

MB

Mustafa Bilgic

Algorithmic trading practitioner · Founder, AITradingBot.us

Mustafa builds and backtests automated trading systems and writes about them without the hype. Every tool on this site is free and runs entirely in your browser.