%TRAILING
The %TRAILING instruction sets a trailing stop as a percentage of the position price with SET STOP %TRAILING in ProRealTime™ ProBacktest and ProOrder code.
Syntax
SET STOP %TRAILING xParameters
| Name | Type | Default | Description |
|---|---|---|---|
x | numeric | none | Percentage distance maintained between the stop and the most favorable price reached since entry. Decimals such as 1.5 are accepted. |
How it works
%TRAILING combines with SET STOP only. Unlike the static SET STOP %LOSS, the trailing variant ratchets. For a long position the stop starts x percent below the position price and rises whenever price makes a new high since entry, always keeping the x percent gap to that high. It never moves down. For a short position the logic mirrors: the stop trails x percent above the lowest price reached.
Once the instruction executes, the setting persists and applies to the current position and to all future positions until another SET STOP call replaces it. A value of 0 disables the trailing stop.
Because the distance is a percentage, the absolute gap widens as price rises and narrows as it falls, which keeps the protection proportional on instruments with large price ranges. When the trailed level is hit, the position closes at market like an ordinary stop.
Examples
Example 1, Trend entry protected by a 1.5 percent trail (ProBacktest)
// Long-term and short-term moving averages
i1 = Average[100](close)
i2 = Average[5](close)
// Enter on a pullback inside an uptrend, after three rising lows
tradeinitiate = Close > i1 AND Close < i2 AND Low[3] > Low[2] AND Low[2] > Low[1] AND Low[1] > Low
tradeclose = Close > Close[1]
IF NOT LongOnMarket AND tradeinitiate THEN
BUY 1 CONTRACTS AT MARKET
ENDIF
IF LongOnMarket AND tradeclose THEN
SELL AT MARKET
ENDIF
// Trail the stop 1.5 percent below the highest price since entry
SET STOP %TRAILING 1.5The strategy buys pullbacks in an uptrend and exits on strength. If the exit signal never fires, the trailing stop closes the position after a 1.5 percent retreat from the best price reached.
Example 2, Percent trail on a short breakdown (ProOrder)
support = Lowest[20](low)
IF NOT ShortOnMarket AND close crosses under support[1] THEN
SELLSHORT 1 CONTRACT AT MARKET
ENDIF
// The stop trails 2 percent above the lowest price since entry
SET STOP %TRAILING 2On the short side the stop follows price downward and only a 2 percent bounce from the low closes the trade.
Example 3, Fixed stop first, trail after the trade is in profit (ProOrder)
DEFPARAM CumulateOrders = false
IF NOT OnMarket AND RSI[14](close) < 30 THEN
BUY 1 CONTRACT AT MARKET
// Static protection while the trade develops
SET STOP %LOSS 1
ENDIF
// Switch to a tighter trailing stop once price is 1 percent ahead
IF LongOnMarket AND close >= TRADEPRICE * 1.01 THEN
SET STOP %TRAILING 0.5
ENDIFA common two-phase pattern: a static percent stop caps the initial risk, then a tighter %TRAILING takes over once the position shows a profit. The switch is one-way for the rest of the trade because the condition stays true once reached, and each execution keeps the same value.
Common errors and gotchas
- Wrong SET pairing.
%TRAILINGis a stop instruction, so it combines withSET STOPonly.SET TARGET %TRAILINGis not a valid form; targets use%PROFIT. - One stop slot.
%TRAILING,%LOSS,$TRAILING,PLOSSandBREAKEVENall write to the same internal stop. The most recentSET STOPcall wins, so mixing a static stop and a trailing stop in per-bar logic replaces one with the other rather than combining them. - Bar-based trailing in backtests. ProBacktest updates the trailed level from bar data, so intrabar spikes may be handled optimistically compared with live tick-by-tick execution. Expect small differences between backtest and live fills.
- Too tight a percentage. A trail below the instrument's typical bar range gets tagged almost immediately. Compare
xagainst average volatility, for example a percentage derived fromAverageTrueRange, before fixing the value.
Related instructions
$TRAILING, trailing stop as a fixed money amount.PTRAILING, trailing stop as a distance in points.TRAILING, the trailing stop family overview.%LOSS, static stop loss as a percentage of the position price.%PROFIT, take profit as a percentage of the position price.BREAKEVEN, moves the stop or target to the entry price.STOP, pending stop orders and the SET STOP family.TRADEPRICE, entry price of a given trade.LONGONMARKET, true while a long position is open.
