Tradingview: strategy.close(), strategy.close_all() Vs strategy.exit()

Today’s tutorial comes straight from a readers suggestion. It was received some time ago but a bug in Pine Script meant that the article could not be published at that time. Now it is resolved, we can dive right in!
In summary, our reader was confused about the difference between close(),close_all()and exit()as the meaning of the three functions is quite similar. Furthermore, he noted that they all do provide a method of closing a position. Hopefully, this article will help clear up some of that confusion as we will have a little look at each function and its ideal usage.

The Documentation:

Before we do that though, we should take a look at the official documentation. Don’t forget you can control + click (or command + click for Mac users) on any function whilst in the Pine editor to bring up the relevant info. In general, the documents do tend to provide enough information. However, if you are a beginner, you might find some terms used or the large blocks of text a little daunting to read and decipher.

Official docs describing the close function

As such, it can sometimes ease digestion if we break down the key statements into simple bullet points.

strategy.close

Is a command to exit from the entry with the specified ID.
  • If there were multiple entry orders with the same ID, all of them are exited at once.
  • If there are no open entries with the specified ID by the moment the command is triggered, the command will not come into effect.
  • The command uses a market order.
  • Every entry is closed by a separate market order.

Usage: strategy.close(id, when) → void

strategy.close_all
Is a command to exit from current market position making it flat:
  • If there is no open market position by the moment the command is triggered, the command will not come into effect.
Usage: strategy.close_all(when) → void

strategy.exit

It is a command to exit either a specific entry or whole market position.
  • If an order with the same ID is already pending, it is possible to modify the order.
  • If an entry order was not filled, but an exit order is generated, the exit order will wait till entry order is filled and then the exit order is placed.
    • To deactivate an exit order, the command strategy.cancel or strategy.cancel_all should be used.
  • If the function strategy.exit is called once, it exits a position only once.
  • If you want to exit multiple times, the command strategy.exit should be called multiple times.
  • If you use a stop loss and a trailing stop, their order type is ‘stop’, so only one of them is placed (the one that is supposed to be filled first).
  • If all the following parameters ‘profit’, ‘limit’, ‘loss’, ‘stop’, ‘trail_points’, ‘trail_offset’ are ‘NaN’, the command will fail.

To use a market order to exit, the command strategy.close or strategy.close_all should be used.

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

Summary

After breaking the documentation down into bullet points, we can see the key takeaways are:

  • strategy.close() should be used if you want to use a market order to exit a specific entry of a position.
  • strategy.close_all()should be used if you want to use a market order to exit the WHOLE position.
  • strategy.exit()should be used if you want to use a stoporlimitorder to exit a position.

The tutorial doesn’t end there though. After all, it wouldn’t be much of a tutorial if this article just regurgitated the documentation and told you how to read it! Instead, we shall scratch beneath the surface and show an example of each.

Before continuing it is worth noting that strategy.exit()has been covered in some detail previously as part of the Stop Losses tutorial.

Example Notes:

All of the following examples are designed to run on the daily timeframe. We will simply use the dayofweekvariable to determine whether to enter a position. Furthermore, we will enter different positions on different days of the week, allowing us to have more than one long position open at a time. Finally, for the eagle-eyed readers, it should be pointed out that we don’t actually enter on the day of the week in which we triggered the trade. For example, in the snippets below, we refer to a Monday trade, hower this actually enters on Tuesday morning because the entry order is sent after the close on Monday.

close() vs close_all()

Let’s start simple. strategy.close()will close a single entry whereas strategy.close_all()will close all entries for the whole position. In practical terms, if you do not allow more than one entry in the same direction, the two functions will do the same thing. It is only if we have multiple entries where we can start to see the difference.

Take the following example:

The example contains both close methods. However, strategy.close_all()is commented out. We will un-comment that in a moment for comparison. Our code will just buy on Monday and sell on Friday. It shall only enter one position. As such, you will be able to see that the two methods essentially do the same thing.

Running The Example

With the default code above, we should see a chart that looks like this (of course, the price data will change according to the stock/asset you are looking at!)

Close() Example 1

Next, comment out the strategy.close()line and replace it with the strategy.close_all()line like so:

Then run the script again:

Close All Example

You will see that only the purple text actually changes between the two. One is telling us that it is an order to close the Monday entry whilst the other is telling us it is an order to close the position. Again, because we have only one entry, they are essentially the same.

Adding a second entry

When we add a second entry, we can begin to see the difference between the two methods. In the following example, we add a second entry on Wednesday and allow pyramiding. We then compare the results between the two we can see that strategy.close()only ever closes out our Monday order. Our Wednesday order is filled at the start and is never closed.

Note that in the code above a wedEntered variable has also been added. This is to combat the mechanics of pyramiding. Essentially, if you don’t prevent a second Wednesday entry after the first, we can end up with two Wednesday entries and no Monday entries. This is because there might be a bank holiday on a Monday. Therefore, we would close the Monday position and Friday and miss the chance to re-open it on Monday. Wednesday then rolls around and we enter our second position. Now we have two Wednesday positions and order to close Monday will never trigger because there are no Monday entries. This gives the illusion that the script is no longer buying or selling.

If you are a little lost reading the above paragraph, don’t worry too much. The key to all this is to simply understand that the Wednesday order is never closed by our strategy.close()order!

Note: You can run the example above without the wedEnteredparts and it will run as you might expect for a few weeks until a bank holiday appears.

close() example

As we can see in the first example, we only ever close a Monday position. Wednesday always remains open. Conversely, in the second example, both positions are closed at the end of the week.

Now we can switch to using close_all(). We can also remove the wedEnteredcode as it is no longer needed. This is because we expect both positions to be closed at the end of the week and therefore there is no chance of getting stuck like in the first example.

After running the above code, we can see that both entries are closed together on a Friday and are subsequently re-entered the following week.

close_all() example

strategy.exit() vs strategy.close()

As noted above, strategy.exit()should be used for sending limit and stop orders rather than market orders. Other than that, it works the same way as strategy.close()in that you can target a specific entry to be closed out by a stop/limit order.

Let’s take a look.

The code above results in the following chart:

Take Profit with exit()

Just like the chart from the strategy.close()example, we can see that the Wednesday entry is bought and held. We are simply taking out our Monday entry when the take profit is hit.

It is worth noting that the example above is for illustration only. The logic is a little flawed because when we use the highof the signal candle as the take profit level. As such, it can result in the take profit being hit immediately after entering because we are already above it. Naturally, you would not want this in real life.

That should just about cover the basic differences between the three types of close. For a detailed look at the using strategy.exit() with stop losses, you can take a look at this tutorial.

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