Pine Script Strategy: Trailing Stops

Trailing stops are great because you can lock in more profits on the way up than with simply a regular stop loss. But you already know that or you wouldn’t be here. So how do we use trailing stop losses in Pine Script?

Again let’s take a peak at the TradingView documentation about exiting a position with a trailing stop loss:

strategy.exit(id, from_entry, qty, qty_percent, profit, limit, loss, stop, trail_price, trail_points, trail_offset, oca_name, comment, when) → void

trail_price (float) An optional parameter. Trailing stop activation level (requires a specific price).
If it is specified, a trailing stop order will be placed when the specified price level is reached. The
offset (in ticks) to determine initial price of the trailing stop order is specified in the 'trail_offset'
parameter: X ticks lower than activation level to exit long position; X ticks higher than activation
level to exit short position. The default value is 'NaN'.

trail_points (float) An optional parameter. Trailing stop activation level (profit specified in ticks).
If it is specified, a trailing stop order will be placed when the calculated price level (specified
amount of profit) is reached. The offset (in ticks) to determine initial price of the trailing stop
order is specified in the 'trail_offset' parameter: X ticks lower than activation level to exit long
position; X ticks higher than activation level to exit short position. The default value is 'NaN'.

trail_offset (float) An optional parameter. Trailing stop price (specified in ticks). The offset in ticks
to determine initial price of the trailing stop order: X ticks lower than 'trail_price' or 'trail_points'
to exit long position; X ticks higher than 'trail_price' or 'trail_points' to exit short position. The
default value is 'NaN'.

Lots of information! Ok, basically, to set a stop price for exiting your trade, you’ll want to set the stop param to your stop loss price. Then TradingView will take care of the rest for you. Strategies in Pine Script manage the positions, orders, trades, etc for you, so you don’t have to worry about it. Just set the stop when you call strategy.exit and you’ll be good to go.

Pine Script Study: Trailing Stops

Trailing stops in Pine Script studies are trickier. Why? Because you’re going to have to do the work yourself. You have to set the price, check if its hit, and update it every candle as you go along.

Assuming you have logic in place for when you would go long and when you would go short, we’re going to have to add a few more variables in order to get the behavior we want. These are the basic ones for a study that goes both long and short.

  1. You’re going to have to know your trailing stop amount.
  2. You’re going to have to know if you are currently in a trade.
  3. You’re going to have to know if you’re either long or short.
  4. And you’re going to have to track the current stop price.

Go ahead and keep track of these new variables in case you don’t have them yet. You can use isLong and isShort as explained in this post to tackle both #2 and #3 above.

Ok, so now we know if we’re in a position or not and what our stop distance is, we can calculate our stop price at every candle. Our calculation will run once every tick, but the final value for that candle will be whatever it was on the last tick of that candle. Keep this in mind as it could change when you get your alerts of whatever else you’re trying to trigger based on this trailing stop.

In Pine Script, close will give you the current price of the asset as the candle is being painted. To set your stop price, let’s assign a new variable if we’re in a position:

float stopDist = 50 // $50
float stopLoss = na
if isLong
    stopLoss := max(stopLoss, close - stopDist)

DEV NOTE: This example uses the simple case having a stopDist value for how var from price we want to trail in dollars.

See how we use the max function to ensure the stopLoss price only increases? That’s key for the trailing stop! Now all you have to do is check if its been hit. This logic will depend on if you’re long or short, and will also depend on if you want to use close, or high and low based on when you want alerts to fire.

Trailing stops are tricky in Pine Script!

If you’re looking for help writing an indicator or script, jump in our Discord. We’re happy to help.