How to draw stop loss and take profit in trading view

This feature lets you estimate how an order will go if you go long or short, shows the Profit & Loss (PnL) and estimates risk and closing account balance when price reaches your profit target or stop loss levels.

You can practice placing orders and seeing results without actually having to place the orders — get confident and knowledgeable before risking real money in the markets.

Calculate your Position Size and Account Balance

1. Create a Long Position or Short Position drawing.

2. In properties dialog of the instrument enter your initial account size and risk amount (either in absolute numbers or as a % of your account size). You can also enter the Lot Size. Click OK to accept.

3. Drawing tool tags will show you position size (1) and account balance when positions are closed after reaching either the Take Profit (2) or the Stop Loss (3) level.

How are the Position Size and Account Balance calculated?

Long Position:

Position Size

Qty = (RiskSize / ((EntryPrice – StopPrice) * Point value)) / Lot Size

Account Balance when position is closed after reaching the Take Profit level

Amount = AccountSize + (ProfitLevel – EntryPrice) * Qty * Point value

Account Balance when position is closed after reaching the Stop Loss level

Amount = AccountSize — (EntryPrice – StopLevel) * Qty *  Point value

Short Position:

Position Size

Qty = (RiskSize / ((StopPrice – EntryPrice) * Point Value)) / Lot Size

Account Balance when position is closed after reaching the Take Profit level

Amount = AccountSize + (EntryPrice – ProfitLevel) * Qty *  Point value

Account Balance when position is closed after reaching the Stop Loss level

Amount = AccountSize — (StopLevel – EntryPrice) * Qty * Point value

  • AccountSize — initial account size specified in the settings
  • RiskSize =
    • Risk, if risk is selected in absolute numbers,
    • Risk / 100 * AccountSize, if risk is selected as a percentage of account size.

Display performance in the Compact Mode

Try using the Compact Mode if you want the position performance tags to consume less space on the chart. You can enable by choosing the Compact checkbox in Settings.

The new Long & Short Position drawing tool is here! This highly demanded feature lets you estimate how an order will go if you go long or short, shows the Profit & Loss (PnL) and estimates risk and closing account balance when price reaches your profit target or stop loss levels.

Now you can practice placing orders and seeing results without actually having to place the orders – get confident and knowledgeable before risking real money in the markets.

Calculate your Position Size and Account Balance

  1. risk-reward_tool_en

    Create a Long Position or Short Position drawing.

 

 

 

 

 

 

  1. risk-reward_prop

    Enter your initial account size and risk amount (either in absolute numbers or as a % of your account size), and click OK to accept.

 

 

 

 

 

 

 

 

 

 

Drawing tool tags will show you position size (1) and account balance when positions are closed after reaching either the Take Profit (2) or the Stop Loss (3) level.

risk-reward

 

 

 

 

 

 

 

 

 

 

How are the Position Size and Account Balance calculated?

Long Position

Position Size

Qty = RiskSize / (EntryPrice – StopPrice)

Account Balance when position is closed after reaching the Take Profit level

Amount = AccountSize + (ProfitLevel – EntryPrice) * Qty

Account Balance when position is closed after reaching the Stop Loss level

Amount = AccountSize – (EntryPrice – StopLevel) * Qty

 

Short Position

Position Size

Qty = RiskSize / (StopPrice – EntryPrice)

Account Balance when position is closed after reaching the Take Profit level

Amount = AccountSize + (EntryPrice – ProfitLevel) * Qty

Account Balance when position is closed after reaching the Stop Loss level

Amount = AccountSize – (StopLevel – EntryPrice) * Qty

  • AccountSize – initial account size specified in the settings

  • RiskSize

    • =

      Risk, if risk is selected in absolute numbers,

    • = Risk / 100 * AccountSize, if risk is selected as a percentage of account size.

 

Display performance in the Compact Mode

Try using the Compact Mode if you want the position performance tags to consume less space on the chart. You can enable by choosing the Compact checkbox in Settings.

risk-reward_compact

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, stop of a long entry at lowATR (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.

//@version=3strategy("My Strategy", overlay=true)ATR = atr(7)longCondition = crossover(sma(close, 100), sma(close, 200))if (longCondition) stop_level = low - ATR profit_level = close + ATR strategy.entry("My Long Entry Id", strategy.long) strategy.exit("TP/SL", "My Long Entry Id", stop=stop_level, limit=profit_level)shortCondition = crossunder(sma(close, 100), sma(close, 200))if (shortCondition) stop_level = high + ATR profit_level = close - ATR strategy.entry("My Short Entry Id", strategy.short) strategy.exit("TP/SL", "My Short Entry Id", stop=stop_level, limit=profit_level)

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

//@version=3strategy("My Strategy", overlay=true)ATR = atr(7)longCondition = crossover(sma(close, 100), sma(close, 200))if (longCondition) strategy.entry("My Long Entry Id", strategy.long) shortCondition = crossunder(sma(close, 100), sma(close, 200))if (shortCondition) strategy.entry("My Short Entry Id", strategy.short) long_stop_level = nalong_profit_level = nalong_stop_level := longCondition ? low - ATR : long_stop_level[1]long_profit_level := longCondition ? close + ATR : long_profit_level[1]short_stop_level = nashort_profit_level = nashort_stop_level := shortCondition ? high + ATR : short_stop_level[1]short_profit_level := shortCondition ? close - ATR : short_profit_level[1]strategy.exit("TP/SL", "My Long Entry Id", stop=long_stop_level, limit=long_profit_level) strategy.exit("TP/SL", "My Short Entry Id", stop=short_stop_level, limit=short_profit_level) plot(strategy.position_size <= 0 ? na : long_stop_level, color=red, style=linebr, linewidth=2)plot(strategy.position_size <= 0 ? na : long_profit_level, color=green, style=linebr, linewidth=2)plot(strategy.position_size >= 0 ? na : short_stop_level, color=red, style=linebr, linewidth=2)plot(strategy.position_size >= 0 ? na : short_profit_level, color=green, style=linebr, linewidth=2)

Commentary

Before we start, It is worth noting that the example above could be achieved using valuewhen(). This is outlined in the 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

//@version=3strategy("Stop Loss Example: Simple Stoploss", overlay=true)sma_per = input(200, title='SMA Lookback Period', minval=1)sl_inp = input(2.0, title='Stop Loss %', type=float)/100tp_inp = input(4.0, title='Take Profit %', type=float)/100sma = sma(close, sma_per)stop_level = strategy.position_avg_price * (1 - sl_inp)take_level = strategy.position_avg_price * (1 + tp_inp)strategy.entry("Simple SMA Entry", strategy.long, when=crossover(close, sma))strategy.exit("Stop Loss/TP","Simple SMA Entry", stop=stop_level, limit=take_level)plot(sma, color=orange, linewidth=2)plot(stop_level, color=red, style=linebr, linewidth=2)plot(take_level, color=green, style=linebr, linewidth=2)
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

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!In Pine Script send stop loss and take profit orders by using theandparameters for theandfunctions. In fact, thefunction also supports sendingandorders. 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.There are a couple of the reasons why people can sometimes find plotting the stop loss and take profit lines a little challenging:Theandare measured in. 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.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 theof a long entry at Average True Range ). If you don’t placefunction inside anstatement, you can find that the stop line bounces around every bar as thevalue is updated.Before we move into a solution, let’s run through a quick example of placinginside anstatement in case you are having trouble visualizing it. The example below will calculate thelevel and only send the order once when aoris met.This will result in a chart that looks something like this:The key to plotting stop losses and take profits is to create a(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 theis met, then we similarly we only update our newat 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.Before we start, It is worth noting that the example above could be achieved using. 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 thatis no longer to be placed inside anstatement. 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 eachwithfirst. This is because the code is written in version 3 of pinescript and if we want to reference a previous value of the same(e.g), 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):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.Once plotted, the chart should look like this:If you just simply want to set a stop loss or take profit at x% distance away from your entry level and you arepyramiding (entering in the same direction multiple times), then you can just use the variableto calculate the lines. (Note this example also features in the stop loss tutorial for Tradingview When we don’t have a position in the market thevalue equals. This is helpful for plotting because we are able to plot the stop loss line only when we are in a position.is never plotted and therefore we don’t need to add a ternary conditional operator like in the example above. .To ensure our lines are plotted tidily, thestyle should be used. This will ensure that you don’t have long lines joining up gaps between when the value is. You will see the difference if you just use a normal line style.

Written by Jane