HorizonAI
ToolPro

Structure Trailing Stop Loss

Stop losing profits to arbitrary trailing stops. This tool trails your stop loss based on actual market structure—swing lows in uptrends, swing highs in downtrends. Let the market tell you where to place your stop.

Full Source Code
Alert Templates
Build on with HorizonAI

What's Included

Structure-based trailing stop
Follows swing highs/lows
Customizable buffer/offset
Visual stop level display
Works with any strategy
Alert on stop level changes

How Structure Trailing Stop Loss works

A trailing stop follows a winning trade to lock in profit as price moves in your favour, but the method you use to trail matters. A fixed percentage or ATR-based trail moves mechanically regardless of what price is actually doing, which can pull your stop too tight in a healthy trend or leave it too loose in a choppy one. A structure-based trailing stop instead anchors to market structure itself.

In an uptrend, price advances by making higher highs and higher lows; the swing lows are the structural footholds that define the trend. A structure trailing stop trails beneath the most recent confirmed swing low, so the stop only ratchets up when the market prints a new higher low. In a downtrend the logic mirrors: the stop trails above the latest lower high. As long as structure holds, you stay in the trade; when structure breaks, you are taken out.

The result is a stop that breathes with the trade's own rhythm rather than an arbitrary distance. You give the trend room to pull back and continue, while still exiting decisively the moment the structure that justified the trade is violated.

When to use it

  • Riding trending moves where you want to capture the bulk of a run without micromanaging the exit.
  • Trend or swing setups where the invalidation logically sits at the last swing low or high.
  • Letting winners run past a fixed target while keeping the exit tied to real price behaviour.
  • Positions where you want the stop to tighten automatically as new structure forms.

When it's not the right tool

  • Tight ranges or heavy chop, where swing points form erratically and the stop can be whipsawed on noise.
  • News-driven gaps or fast spikes that can blow through the structural level before the stop executes.
  • Very short scalps where structural swings are too infrequent to trail meaningfully.

Source Code Preview

Get the full PineScript v6 source code—use it as a foundation and build on top of it with HorizonAI.

structure-tsl.pine
1//@version=6
2indicator("Structure Trailing Stop Loss", overlay=true)
3// ─────────────────────────────────────
4// Structure Trailing Stop Loss - Full Source Code
5// ─────────────────────────────────────
6
7// Input Settings
8pivotLen = input.int(5, "Pivot Length")
9showLabels = input.bool(true, "Show Labels")
10
11// Core Logic
12pivotHigh = ta.pivothigh(high, pivotLen, pivotLen)
13pivotLow = ta.pivotlow(low, pivotLen, pivotLen)
14
15// Structure Detection
16var float lastHigh = na
17var float lastLow = na
18
19if not na(pivotHigh)
20 lastHigh := pivotHigh
21 // ... more logic
22
23// Break of Structure
24bosUp = close > lastHigh

200+ lines of source code

Unlock premium to access the full source code

Unlock Source Code

Frequently asked questions

How is this different from an ATR trailing stop?

An ATR stop trails at a volatility-derived distance from price and moves regardless of structure. A structure trailing stop only moves when the market forms a new swing low or high, so your exit is tied to the actual reason you are in the trade rather than to a statistical band.

Won't a structure stop sometimes sit further from price than I'd like?

Yes, and that is by design. Anchoring to the last valid swing gives the trend room to breathe, which usually means a wider but more logical stop. If that distance is too large for your risk, reduce position size rather than tightening the stop into the noise.

Does it work in both directions?

Yes. In uptrends it trails below rising swing lows; in downtrends it trails above falling swing highs. It is a trade-management tool, so you still need your own entry logic and a plan for how much you are willing to risk per position.

Get access to all 23+ scripts

Full source code, alert templates, and the perfect base to build on with HorizonAI. Join 10,000+ traders.