Tradingview: Plotting Stop Losses and Take Profits

In this article, we will take a quick look at plotting stop losses and take profits. This technique has actually been covered in a couple of posts on this site already but there has never been a dedicated article on the subject and requests for this subject still occasionally come in!

Plotting Stop Losses and Take Profits

In Pine Script send stop loss and take profit orders by using the stopandlimit parameters for the strategy.exit() and strategy.order()functions. In fact, the strategy.entry()function also supports sending stopandlimitorders. However, that would be considered an entry rather than a stop loss or take profit and thus out of the scope of this article. Having said that, the techniques used in this article can be used for entries and just about any line you want to track/plot.

Sending Orders

There are a couple of the reasons why people can sometimes find plotting the stop loss and take profit lines a little challenging:

  1. They are using the profitandlossparameters instead of stopandlimit
  2. They calculate the stop loss and/or profit level once at the time of entry.

The profitand loss are measured in ticks. As such, the numbers we use to feed these parameters do not provide a natural price level we can plot. As such, a bit more work would be required to plot the level. Note: For more information on the various stop-loss options, see this article.

The second item in the list above is often utilized when users wish to use a more complex calculation for their stop and some parts of that calculation can change between bars. For example, one common request is to set the stop of a long entry at lowATR (Average True Range).

If you don’t place strategy.exit() function inside an ifstatement, you can find that the stop line bounces around every bar as the ATRvalue is updated.

Example

Before we move into a solution, let’s run through a quick example of placing strategy.exit()inside an ifstatement in case you are having trouble visualizing it. The example below will calculate the stop level and only send the order once when a longConditionorshortConditionis met.

This will result in a chart that looks something like this:

Setting Stop losses and Take Profit example - Showing sending a stop / take profit once only

Plotting Solution

The key to plotting stop losses and take profits is to create a series (new line/variable) that is updated only when your stop-loss is updated. For example, if you only send a stop loss order once when the longConditionis met, then we similarly we only update our new series at that time too. Otherwise, we keep the same value as the previous bar. We can do this using something called a Ternary Conditional Operator. Click on that link for a more detailed tutorial on them.

The example below will expand on the first example to both plot the lines and fix the levels.

Commentary

Before we start, It is worth noting that the example above could be achieved using valuewhen(). This is outlined in the Save a variable / store a value for later post. However, it is a good idea to become familiar with ternary conditional operators as they will allow you to create some complex and unique trailing stop losses.

Although the code appears to be longer, there are actually only a few changes between the first example and this one. The first is that strategy.exit()is no longer to be placed inside an ifstatement. When we do this, we will start updating the order on every bar. However, since our value is not changing, then it doesn’t matter.

Next, we calculate our levels. When we do this, we must declare each series with long_stop_level = na first. This is because the code is written in version 3 of pinescript and if we want to reference a previous value of the same series (e.g long_stop_level[1]), we will receive an error if we do not declare it first.

The ternary conditional operators might seem daunting at first but they are essentially saying (for a long example):

  • If a longConditionis met, set the line at lowATR.
  • If not, keep the previous value.

Finally, we just do a little creative plotting to make sure we stop plotting the lines when we don’t want to see them. This just helps to keep things nice and tidy. Again we use a ternary conditional operator to do this.

On the Charts

Once plotted, the chart should look like this:

A Simple Alternative for Fixed % Stops / Profits

If you just simply want to set a stop loss or take profit at x% distance away from your entry level and you are NOT pyramiding (entering in the same direction multiple times), then you can just use the variable strategy.position_avg_priceto calculate the lines.

(Note this example also features in the stop loss tutorial for Tradingview)

Mini Commentary

When we don’t have a position in the market the strategy.position_avg_pricevalue equals na. This is helpful for plotting because we are able to plot the stop loss line only when we are in a position. nais never plotted and therefore we don’t need to add a ternary conditional operator like in the example above. .

Note: To ensure our lines are plotted tidily, the linebrstyle should be used. This will ensure that you don’t have long lines joining up gaps between when the value is na. You will see the difference if you just use a normal line style.

Stop Losses: Simple Stop Loss

Support the site

If you like this article and are thinking to become a Tradingview subscriber, you can support the site by clicking on the affiliate link below before signing up!

https://tradingview.go2cloud.org/SHpB