What is a trailing-stop order?

A trailing-stop order is a stop that follows price in your favour but never moves against you. As a long position rises, the stop ratchets up to stay a fixed distance below the highest price seen; if price then reverses by that distance, the order triggers and closes the trade — locking in most of the run instead of giving it all back. It is the single cleanest way a bot resolves the eternal exit tension between cutting losers fast and letting winners run, because the rule is mechanical and emotion-free. This guide explains exactly how a trailing-stop order works, how a bot tracks and updates it in code, and how to size the trail so noise does not knock you out of good trades.

On this page
  1. What it is
  2. How it ratchets
  3. Implementing it in code
  4. Choosing the trail distance
  5. Common mistakes
  6. Getting started
  7. FAQ

What a trailing-stop order is

A trailing-stop is a stop order whose trigger price is not fixed — it trails the market by a set distance, expressed as a percentage or an absolute amount. For a long, the stop sits below price; every time price makes a new high, the stop moves up to maintain the gap. Crucially it only ever moves up, never down, so it converts unrealised profit into a protected floor. It is the dynamic cousin of the static stop-and-target bracket in stop-loss vs take-profit, and the engine behind every trailing-stop strategy and trend-following bot.

exit on reversal trailing stop ratchets up, never down price
The trailing stop (dashed) follows price up and holds its highest level. When price reverses into it, the position closes — banking most of the move.

How the ratchet works

On each new candle the bot records the highest price reached since entry. The stop level is simply that high minus the trail distance. If the new high is higher than the last, the stop moves up; if not, the stop stays put. The position closes the moment price falls to the stop. Some exchanges offer a native trailing-stop order type so the venue does the tracking; otherwise the bot maintains the high-water mark itself and updates a normal stop order each cycle.

Implementing it in code

The logic is short — track the peak, recompute the stop, exit on a breach. This is the same pattern used in the trailing-stop strategy guide, wired into the loop from the crypto bot guide.

python · trailing_stop.pytrail_pct = 0.05            # 5% trailing distance
peak = entry_price            # highest price seen since entry

def update_stop(price):
    global peak
    if price > peak:
        peak = price          # new high → ratchet up
    stop = peak * (1 - trail_pct)
    return stop

stop = update_stop(current_price)
if current_price <= stop:
    ex.create_market_sell_order('BTC/USDT', amount)  # trailing stop hit

Choosing the trail distance

Too tight and ordinary market noise knocks you out at the first wobble; too wide and you give back a large chunk of the gain before exiting. Tie the distance to volatility — an ATR multiple adapts to each market far better than a fixed percentage, just like the volatility-based position sizing approach. Test several trail widths on the backtester and pick the one with the best risk-adjusted return, not the highest single backtest, to avoid overfitting.

A trailing stop guarantees you give some back

By design a trailing stop never exits at the exact top — it exits one trail-distance below the peak. That is the price of letting winners run. In a choppy, sideways market a trailing stop also tends to underperform a fixed take-profit, because every minor pullback risks an early exit. Match the tool to the regime: trailing stops shine in trends, not chop.

Common mistakes

Using the same fixed-percent trail across wildly different volatilities; placing the trail inside the market's normal noise band; letting the stop move down (a bug that defeats the entire purpose); and switching a trailing stop on in a ranging market where a static target would have banked more. Always confirm which regime your strategy lives in before choosing a trailing exit.

Getting started safely

Backtest several trail distances on the strategy backtester, paper trade the ratchet logic against live data to confirm it only moves up, then deploy at the smallest size with the trail tied to volatility rather than a fixed number.

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 trailing-stop order?

A trailing-stop order is a stop whose trigger price follows the market in your favour by a fixed distance but never moves against you. For a long position it sits below price and ratchets up every time price makes a new high; if price then reverses by the trail distance, the order triggers and closes the trade, locking in most of the gain.

How is a trailing stop different from a normal stop-loss?

A normal stop-loss is fixed at one price and stays there. A trailing stop is dynamic — it moves up as price rises (for a long) to protect accumulating profit, but it never moves down. So a static stop only ever caps a loss, whereas a trailing stop converts an open winner into a protected floor that keeps rising as the trade works.

How does a bot implement a trailing stop?

The bot tracks the highest price reached since entry (the high-water mark) and sets the stop to that peak minus the trail distance. On each new candle it updates the peak only if price made a new high, recomputes the stop, and exits the moment price falls to it. Some exchanges offer a native trailing-stop order type that does this tracking for you.

What trail distance should I use?

Tie the distance to volatility rather than using a fixed percentage everywhere. An ATR multiple adapts to each market: too tight and normal noise stops you out early, too wide and you give back too much before exiting. Backtest several trail widths and choose the one with the best risk-adjusted return. Trailing stops work best in trending markets, not choppy ranges.

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.