Power Language’s SetExitOnClose: How To Use It in Backtest and Live Trading

by Andrea Unger

Need More Help? Book Your FREE Strategy Session With Our Team Today!

We'll help you map out a plan to fix the problems in your trading and get you to the next level. Answer a few questions on our application and then choose a time that works for you.

BOOK YOUR FREE STRATEGY SESSION NOW >>

The Power Language keyword "setexitonclose" is a reserved word that allows you to close all open trades at the end of the day. 

This makes it particularly useful when we want to backtest intraday strategies.

In today's video, we'll give you an interesting insight into this keyword and how to use it correctly.

By watching the video you’ll discover:

- What is and how to use the reserved keyword "setexitonclose"

- How to insert it in a strategy in order to backtest it (with an example of a breakout system on Gold)

- How to use it to compare the performance of a system in intraday and multiday mode

Moreover, since many people ask us if it is possible to use "setexitonclose" also for live trading, we'll reveal a trick to properly use this keyword also live!

Enjoy the video!

Transcription

Hey everyone and welcome back to our channel! One of the coaches of Unger Academy here, and today we’re going to be talking about the keyword "setexitonclose" which is used a lot to test intraday strategies.

Since so many people ask us how to use the keyword "setexitonclose" in live trading, today we’re going to be reviewing the use of this reserved word that will be very useful when developing a strategy that closes all trades at the end of the day.

Setexitonclose Definition

As said, the keyword "setexitonclose" is very useful if we want to test intraday strategies. So, what are the peculiarities of this keyword? And what we must pay attention to when using it?

So the definition by MultiCharts that can be found in the Power Language Editor under the index entry "setexitonclose" comes to our aid. So, we're going to search for "setexitonclose". Here it is below.

And right here we’re told that this command will close the open trades, whether long or short, at the last tick of the session bar generating an exit market order whether the open trade is long or short.

It is then specified that this keyword can be used only inside a signal, namely a strategy which sends orders to the market. And it would be impossible to use this type of keyword, for example, on an indicator.

In order to apply it to a strategy, as we can see below, we only need to rewrite this word inside our signal in order to send the orders to close the trades at the very last bar, the very last tick of the session.

The session that MultiCharts will use to close the open positions at the last tick will be the one set in the Quote Manager. And this is also specified in the description found in the Power Language Editor dictionary.

Strategy

Now let's go and create a strategy to see how to use the keyword "setexitonclose" for our tests. Here I've created a very simple strategy which will buy and sell inside a specific time window.

We use this time window to simply eliminate the first two bars of the session and the last two bars of the session from the backtest.

We're going to do our testing on the 15-minute Gold Future market. As we know, this market has a session that goes from 6 pm to 5 pm, so in this way by starting at 6:30 pm and ending at 4:30 pm, we can eliminate the first two and the last two bars from the backtest.

After that, always inside the time window, we'll send stop orders on the high and low of the previous day.

At this point, our keyword "setexitonclose" comes into play. We're going to be using a condition. So, if "ID", which in this case is an input, is equal to 1, then we'll close our trades at the end of the session.

If, instead, the value of "ID" isn’t equal to 1, for example, 0, 2, 3, 4, then this means that the trades will remain open beyond the end of the session, transforming the strategy from intraday to multiday.

Testing and Analysis

Let's go back to MultiCharts, where you can see that I've uploaded the Gold data at 15 minutes and I've inserted our strategy, changing from multiday to intraday. Let's now take a quick look at the equity line of the multiday strategy.

As you can see, this is it. In the very first few years of backtest, namely a backtest that starts from 2008 and lasts until today, we'll see that in the very first years, the breakout was more consistent or more constant, if you want.

Then there was a very long phase that started in 2016 and lasted until 2018-2019, where, instead, the trend-following approach didn't work as well as in the past. After that, another rally and now a sort of new pause.

If we modify our signal from 0 to 1, we'll close our positions at the end of the day. We'll see that the results will change and we'll see it later when we'll compare the intraday and multiday versions.

You'll see that in this case, also the intraday was much more consistent until 2016 but unlike the multiday strategy, by now the intraday strategy no longer reached the equity peaks.

Well, here it touched the equity peaks at this very point for a moment but then fell back again. We'll see if the intraday strategy will also improve in the future.

In order to see the results and also compare a multiday strategy to an intraday strategy, what we can do is directly optimize the value of  "ID", which is our input, from 0 to 1.

Because in this case, as we've said, if "ID" equals zero, then we will keep the trades open even at the end of the session, while if we use 1, we'll close them at the end of the day.

By optimizing these values, we can see very clearly the differences between the two strategies. Strategies that at the moment are still rather basic, because stop loss and many other things are missing, such as for example the filtering conditions.

Anyway, what we can see now is that with ID equal to zero, thus holding trades open longer, the net profit is higher in the time period considered.

This is because the strategy has more time to make good profits. Indeed, this is reflected by the average trade, which you see reaches $140.

While when switching to the intraday strategy, the average trade collapses. This is because there are less bars in the market. So, also, the strategy will have more difficulty to achieve higher average trades.

Moreover, we can see a big difference in the number of entries. This is because the intraday strategy will open and close trades from day to day. So, every day it will open and close a trade while the multiday strategy will keep it open for more days. Consequently, from a statistical viewpoint, there will be a much lower number of trades.

What is interesting, however, is that in the case of a gain, which from a purely absolute value perspective is lower, the percent profitable, namely the percentage of profitability of the intraday strategy, is higher than the multiday strategy. Even 48% compared to 38,5% of the multiday strategy.

So, we can say that the intraday strategy will be always less profitable than a multiday strategy. But also intraday strategies can have very good results, and the peculiarity is that the percent profitable could be higher, because in any case at the end of the session we'll close our trades.

Let's select "1", namely the active intraday input, to see on the chart what kind of trades the strategy does and how it closes them.

Looking closer we can see that at the very last bar, namely the 5 pm bar, at the very last tick, MultiCharts will send a trade close order.

But that should set off an alarm bell! Because if you listened closely to what I've just said, if we were to send a close order or any other order right after the session closes, it means that the market is closed. And as a result, our order would go be suspended and not executed until the next open session. And well, we clearly don't want that to happen.

This means that the keyword "setexitonclose" is usable only in backtest as is, because otherwise at the end of the day, it would send an order but that order wouldn't be executed because the market would be closed at the moment in time.

So, it's very useful in the backtest phase because, as we've seen before, we can decide whether to close or not to close trades at the end of the day just in a line of code.

Use in Live

However, in order to use it in live, it's necessary to create a sort of trick, a ploy, to deceive MultiCharts and then create a sort of "fake session" that cuts the last minute of the session in order to send that order of setexitonclose one minute before the official session, and then actually close our trades.

To do this, just open the Quote Manager and click on "Session Templates”.

As you can see, I've already created it. Click on "Add" and create a session that is the same as this one. So, instead of opening the market at 6 pm and closing at 5 pm, we'll close one minute earlier.

So, for MultiCharts, if I apply this type of session to the chart on which I had put the strategy, according to MultiCharts, the Gold session will close at 4:59 pm instead of 5:00 pm.

In this way, when setexitonclose sends the exit order at 4:59 pm, the market will still be open for one minute and consequently we'll be able to close our trades.

What we’re going to do is quickly go back to our chart and reset our session, which instead of being the default session will be "Comex_cut". Here it is. And we are going to click on “OK".

At a graphical level, nothing is going to change. We'll simply hide the last minute of each session from the backtest calculations, but this shouldn't change too much.

If you want to use this trick, I'd suggest that you do it only in the most liquid markets, because in this way you won't run the risk of not getting orders filled in the last minute. Because it isn't said that on all markets the last trading minute of a session is actually quite liquid. Therefore, use this trick only on the most liquid markets, where you're sure that even at the very last bar, even in the very last minute, there will be buyers or sellers who can trade with you to close your trade.

So, we have seen that the "setexitonclose" can be very useful in backtests, but it could be used also for live trading. As we said, though, just be careful because it doesn't work properly on all markets.

And if there is someone interested in the world of systematic trading, you’ll find a link in the description of this video. From there you'll be able to see a presentation by Andrea Unger or get our best-selling book, "The Unger Method", covering only the shipping costs. Or why not, book a free call with a member of our team.

If you liked the video, please leave us a Like, subscribe to our channel and click on the notification bell to stay updated with the release of all our new videos.

And with that, thank you so much for watching. And we're going to be seeing you in our next videos soon, bye-bye!

Need More Help? Book Your FREE Strategy Session With Our Team Today!

We’ll help you map out a plan to fix the problems in your trading and get you to the next level. Answer a few questions on our application and then choose a time that works for you.

BOOK YOUR FREE STRATEGY SESSION NOW >>

Andrea Unger

Andrea Unger here and I help retail traders to improve their trading, scientifically. I went from being a cog in the machine in a multinational company to the only 4-Time World Trading Champion in a little more than 10 years.

I've been a professional trader since 2001 and in 2008 I became World Champion using just 4 automated trading systems. 

In 2015 I founded Unger Academy, where I teach my method of developing effecting trading strategies: a scientific, replicable and universal method, based on numbers and statistics, not hunches, which led me and my students to become Champions again and again.

Now I'm here to help you learn how to develop your own strategies, autonomously. This channel will help you improve your trading, know the markets better, and apply the scientific method to financial markets.

Becoming a trader is harder than you think, but if you have passion, will, and sufficient capital, you'll learn how to code and develop effective strategies, manage risk, and diversify a portfolio of trading systems to greatly improve your chances of becoming successful.