In TradingView, writing a stop loss in Pine Script can be very simple or very not-so-simple depending on if you’re using a Pine strategy or a pine study. In a strategy, TradingView will manage your positions for you, and do the tracking of price, and triggering your buy and sell entries and exists. However in a study, you’re on your own.

So, if you want a stop loss in your script, read on to learn how they work and what may be required to get yours to work as you expect. Before reading on, make sure you know if you’re using a study or a strategy, why you’re using that type of script, and what you plan to do with it. If you ever need help getting started, just reach out in our Discord. If you find yourself using a strategy but want to change to a study, please read this article on how to switch.

Pine Script Strategy: Stop Orders

In a strategy, little code is required to set a stop for your entry orders. Assuming you have a strategy in place…let’s take a quick look at TradingView’s documentation on how the entry function works…

strategy.entry(id, long, qty, limit, stop, oca_name, oca_type, comment, when) → void

See the 5th parameter? All you have to do is set that param to the stop price you want when calling stratety.entry and Pine will do the rest for you. Here’s more from the Pine Script manual about using this parameter:

stop (float) An optional parameter. Stop price of the order. If it is specified, the order type is either 'stop', or 'stop-limit'. 'NaN' should be specified for any other order type.

Pine Script Strategy: Stop Losses

When using the strategy.exit function, the stop parameter is used in the same way with the additional option of using either number of ticks (use the loss param) or the specific price (use the stop param) you want the stop loss to be set to. Note that when using the loss param to specify in ticks, this is number of ticks away from your entry price…do not confuse this with a trailing stop specified in ticks. Pine Script has specific parameters for setting trailing stops.

Let’s take a quick look at TradingView’s documentation for strategy.exit to see how stops work.

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

loss (float) An optional parameter. Stop loss (specified in ticks). If it is specified, a stop order
is placed to exit market position when the specified amount of loss (in ticks) is reached. The
default value is 'NaN'.

stop (float) An optional parameter. Stop loss (requires a specific price). If it is specified, a stop
order is placed to exit market position at the specified price (or worse). Priority of the parameter
'stop' is higher than priority of the parameter 'loss' ('stop' is used instead of 'loss', if its value
is not 'NaN'). The default value is 'NaN'.

Make sure you know which one you’re using and why you’re using it! In programming, the computer will do exactly what you tell it to do…even when you don’t know what you did. And then you won’t know why it did what it did…and then you go down the rabbit hole of figuring out why. Don’t get me wrong, its a great way to learn…but if you can take just a couple minute to study and plan before executing your code, it’ll be 1) easier to tell when things aren’t working as they should, and 2) easier to find the issue.

To learn more about how to set a trailing stop loss in Pine Script, read this post.

Pine Script Study: Stop Loss

In a study it’s not so simple. You might want to add a stop loss to a study for an alert or just to print a symbol on the chart. Because studys don’t manage positions or track trades, you’re going to have to manage this yourself. But its not so bad.

(If you’re looking for how to convert a strategy to a study, read this article.)

You’re going to need a few pieces of information.

  1. You’re going to have to know your stop price.
  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.

Assuming you have logic in place for when you would go long and when you would go short, go ahead and keep track of two new variables in case you don’t have them yet.

  • var isLong – set to false at instantiation and true when your long condition happens. (If using Pine Script v3, set to isLong[1] after instantiation.)
  • var isShort – set to false at instantiation and true when your short condition triggers. (If using Pine Script v3, set to isShort[1] after instantiation.)

DEV NOTE: Using the var keyword in v4 or assigning to the ...[1] value in Pine v3 makes it so the variable carries over the value from the previous candle. Since when you’re long, you’re long until you exit or go short, we need the isLong variable to keep its value after being set to true until we set it otherwise.

Once all that is in place and working properly, now we can add our stop loss logic. Define another variable toward the top of your script called stopLoss. And when your long or short conditions trigger, set it to be the price you want. And just like our isLong variable, you want to either use the var keyword or the := ...[1] syntax to ensure the value carries over from the previous candle.

Now, all you have to do is check if you’re long or short, check if price has gone below or above your stop price, and if it has, do something. That something depends on what exactly you’re trying to achieve with your script. It could be sending an alert. It could be printing something on the chart. It could be changing some value. Up to you.

KEEP IN MIND when you want your stop loss alert to fire or symbol to print. It might happen at the close of the bar. If you want it to happen mid-bar you’ll have to specifically test that scenario to make sure it works exactly as you would expect.

When you’re all done, you should be able to print your stops on your chart as they would in a strategy. Keep in mind though you might not draw lines at the exact price the stop should have triggered.

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