Few ways to close/exit positions in Pine Script

Good timing for exiting your positions is essential for good trading strategies. There are a few ways to close/exit positions in Pine Script. In this article, I’ll show you examples of all of them.

First of all, in Pine Script, we can create a strategy without separated exits. If we create a strategy with longs and shorts, then Pine Script will close an opposite position when a new signal appears. For example, if we have a long signal short position will be automatically closed at the same time as the long position will be initiated. Here is an example of the MACD strategy, which has only entries without any exits:

//@version=5
strategy("MACD Strategy", overlay=true)

MACD = ta.ema(close, 12) - ta.ema(close, 26)
aMACD = ta.ema(MACD, 9)
delta = MACD - aMACD

if (ta.crossover(delta, 0))
	strategy.entry("MacdLE", strategy.long, comment="MacdLE")
	
if (ta.crossunder(delta, 0))
	strategy.entry("MacdSE", strategy.short, comment="MacdSE")

Closing Pine Script positions using a condition

If you want to close a position with a market order when a certain condition is true, you can use strategy. close function:

strategy.close(id, comment, qty, qty_percent, alert_message, immediately) → void

To call it, you have to specify at least the first argument – id of your position. Also, you can choose to close your positions partially by specifying the qty parameter for absolute quantity or qty_percent for closing percent of your open positions. Here is the same MACD strategy but with the long-only side together with the strategy.close function:

//@version=5
strategy("MACD Strategy", overlay=true)

MACD = ta.ema(close, 12) - ta.ema(close, 26)
aMACD = ta.ema(MACD, 9)
delta = MACD - aMACD

if (ta.crossover(delta, 0))
	strategy.entry("MacdLE", strategy.long, comment="MacdLE")
	
if (ta.crossunder(delta, 0))
	strategy.close("MacdLE")

Also, you can use strategy.close_all function to close all current positions.

Exiting your positions with a stop loss/profit target

Another option to close your strategies is to use strategy.exit function:

strategy.exit(id, from_entry, qty, qty_percent, profit, limit, loss, stop, trail_price, trail_points, trail_offset, oca_name, comment, comment_profit, comment_loss, comment_trailing, alert_message, alert_profit, alert_loss, alert_trailing) → void

As you can see, this strategy is pretty complicated, but it’s quite powerful. With this function, you can set up the following exits:

  • Limit order at a specified price (limit parameter)
  • Stop order at a specified price (stop parameter)
  • Exit if X ticks profit reached (profit parameter)
  • Exit if X ticks loss reached (loss parameter)
  • Exit using trailing stop loss with offset (trail_points, trail_offset parameters)

It’s pretty essential to know how this function works because it’s really easy to add quite advanced exit logic to your strategies with it. Here is the same function example but with static SL and PT exit:

//@version=5
strategy("MACD Strategy", overlay=true)

MACD = ta.ema(close, 12) - ta.ema(close, 26)
aMACD = ta.ema(MACD, 9)
delta = MACD - aMACD

if (ta.crossover(delta, 0))
	strategy.entry("MacdLE", strategy.long, comment="MacdLE")

strategy.exit("SL_PT", "MacdLE", loss = (0.03 * close) / syminfo.mintick, profit = (0.05 * close) / syminfo.mintick)


Follow me on TradingView and YouTube.

This image has an empty alt attribute; its file name is wide.png

Leave a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.