TRAILING
TRAILING sets a trailing stop in ProBacktest and ProOrder via SET STOP TRAILING x, moving the stop with price at a fixed distance of x points from market.
Syntax
SET STOP TRAILING xParameters
| Name | Type | Default | Description |
|---|---|---|---|
x | numeric | none | Distance in points between the stop and the most favorable price reached. |
How it works
A fixed stop stays where it was placed. A trailing stop follows the trade. SET STOP TRAILING x places the stop x points away from price and then ratchets it as the position gains: for a long the stop rises as new highs are made, for a short it falls as new lows are made. The stop only tightens, an adverse move never pushes it back.
The result is a built-in exit that lets a winning trade run while capping how much open profit can be given back. Once price reverses by x points from its best level, the position closes. No manual stop management code is needed, the engine maintains the level bar by bar.
The distance is expressed in points. Sibling instructions cover other units, PTRAILING trails in pips, %TRAILING in percent of price, and $TRAILING in account currency. All of them occupy the same internal stop slot as SET STOP LOSS and BREAKEVEN, so the most recent SET STOP statement is the one in force.
Examples
Example 1, Pullback entry protected by a 10-point trail (ProOrder)
// Trend and pullback filters
i1 = average(close)[100]
i2 = average(close)[5]
// Enter on a pullback inside a longer uptrend, exit on strength
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
// Safety net: trail the stop 10 points behind the best price
SET STOP TRAILING 10The strategy has its own exit rule, and the trailing stop acts as a permanent safety net 10 points behind the most favorable price. This preserves the reference example for the instruction.
Example 2, ATR-scaled trailing distance (ProBacktest)
atr = AverageTrueRange[14](close)
// Trail at twice the ATR, converted into points
trailPoints = ROUND(2 * atr / pointsize)
IF NOT onmarket AND close crosses over average[50](close) THEN
BUY 1 CONTRACT AT MARKET
ENDIF
SET STOP TRAILING trailPointsThe trailing distance adapts to volatility. In quiet markets the stop hugs price, in volatile markets it stays far enough away to avoid being shaken out by noise.
Example 3, Trail activated only after the trade is in profit (ProOrder)
IF NOT onmarket AND close > highest[20](high)[1] THEN
BUY 1 CONTRACT AT MARKET
SET STOP LOSS 30
ENDIF
// Switch from fixed stop to a tight trail once 20 points in profit
IF longonmarket AND close - tradeprice >= 20 * pointsize THEN
SET STOP TRAILING 10
ENDIFThe trade starts with a wide fixed stop. Once it has earned 20 points, the code replaces it with a 10-point trailing stop, locking in most of the gain while leaving room to run.
Common errors and gotchas
- Points, not currency or percent.
SET STOP TRAILING 10means 10 points. For pips, percent, or cash distances usePTRAILING,%TRAILING, or$TRAILING, mixing up the units misplaces the stop by orders of magnitude on some instruments. - One stop slot. Trailing stops,
SET STOP LOSS, andBREAKEVENoverwrite each other, only the most recent statement is active. Code that issues a fixed stop on every bar after the trailing statement silently disables the trail. - Too tight for the timeframe. A distance smaller than normal bar-to-bar noise gets tagged almost immediately. Scale the distance to volatility, for example with ATR, rather than picking a small round number.
- Backtest fills are idealized. ProBacktest exits exactly at the trailed level. Live ProOrder execution can slip past it in fast markets, so trailing-stop systems usually perform slightly worse live than in the backtest.
Related instructions
PTRAILING, trailing stop expressed in pips.%TRAILING, trailing stop expressed in percent of price.$TRAILING, trailing stop expressed in account currency.STOP, fixed stop-loss configuration with SET STOP LOSS.BREAKEVEN, moves the stop or target to the entry price.TARGET, take-profit configuration for the position.TRADEPRICE, entry price of the current position.LONGONMARKET, true while a long position is open.
