Documentation/AlgoCode/Function reference

trail

Trailing protective stop that closes the simulated trade.

Reference/Language/Language

trail 5 points

trail N points subtracts N from the running high on a long and adds N to the running low on a short. trail N percent uses that same extreme: long extreme × (1 − N/100), short extreme × (1 + N/100). trail N atr converts N × ATR(14) of the second-to-last loaded bar into points once for the series. The level on a bar includes that bar's high or low. The entry bar can raise or lower the level and cannot close the trade. A later bar closes the position when a long's low tags the trail or a short's high tags it. The trail only moves in the favorable direction. With a fixed stop, the tighter price is the one that protects the trade. A partial exit leaves the trail running for the remaining size. Going flat, including a flip from long to short, clears the trail. The chart Trail line is this same price. The fill is the bar close for the quantity still open. The trail level is the trigger, not the P&L price. This is simulated OHLC logic, not a broker order.

Return value

—

Example

when ema(9) crosses above ema(21)
  buy
stop 10 points
trail 5 points

See also