Trading System Backtest: 30 Mean Reversion Exits – Which Works Best? (Full Video)

Adrian Reid backtests a Canadian mean-reversion system with 30 different exit rules. Next-bar-open exits outperformed same-bar-close exits, and very short-term exits often delivered higher risk-adjusted returns. The original EMA(3) exit performed well among many viable options.
Questions this video answers
RSI, period 3; entry when RSI crosses below 5.
Close must be above the 200-bar (day) moving average.
Average True Range / Close must be above 0.08 (8%).
Average turnover ≥ $1,000,000 per day.
40% stop loss (next-bar-on-open stop).
Chapters
Hey, Adrian Reed here, founder of Enlightened Stock Trading. Would you like to know which out of the 30 exit rules I tested works best for my mean reversion trading system? In this video I'm going to share with you the results of a backtest where I go through one of my existing trading systems and I tested 30 different exits the to see which one works best because I've been wondering, I've been using had this system out there for a while, but I never really played with a whole bunch of different exits and I thought this would be a great opportunity to share some ideas with you and go through the code and the rules and just show you how I, how I do this, how I do this sort of investigation using my backtesting methodology. So what I'm going to do first is show you the rules for the system. Now before we go any further, if you'd like to download the rules for everything I'm talking about in this video, click the link in the description. Because when, when you click your link, enter your name and email, you'll get my Trader Acceleration bundle which includes a whole bunch of awesome goodies to improve your trading, one of which is the complete code for this core system and also the complete code for the exit test that I'm going to be doing in this video. So click the link in the description, enter your name and email and grab all of those goodies today to help improve your trading immediately. All right, let's jump in. What I want to do first is show you the rules for the system. Now I'm going to test this today in amibroker. So I've got the amibroker formula language code. If you don't know amibroker, don't stress. This code is super simple so it's very easy to understand. So let's have a look at the code. I'll zoom in so you can actually read it. Now this system, before I go any further, this system is a mean reversion system for the Canadian stock market. You of course can test it on any stock market you want.
Kind of works on the Australian market, doesn't really work so well on, on some other markets. But test it for yourself. And see today I'm the Canadian, Canadian market. Alright, so let's just jump in and have a quick look at the rules. I'm not going to go through all the detail the code because we'll be here for hours, but it's super simple. You can see basically the crux of the system is contained here in this section. So what we've got is a trend filter. And the trend filters just the close must be the close of the stock price has to be above the long term moving average and that's a 200 bar moving average. We've got a volatility filter. We want the average true range divided by the closing price to be above 0.08, so above 8%. So we want fairly volatile stocks. And the reason for that is that volatile stocks tend to perform better when it comes to mean reversion systems because when they move, they move a greater distance. They've got a better chance of overcoming slippage and commission because the bouncing from one day to the next is much higher. We've got a liquidity filter, of course, with mean reversion we've got to be careful to make sure that the stocks we buy and sell are sufficiently liquid so that when we're trying to extract a small profit from these mean reversion type moves, we don't get too much slippage that erodes the profit. Now luckily this system has a pretty big average profit per trade, so we should be okay. But I've got a million dollar per day turnover filter for this system. So it needs to be fairly liquid in order to trade this stock. The next system is, the next rule is the entry trigger. This is a simple RSI based entry trigger and the entry is when the RSI, it's a three period RSI crosses below a value of 5. So it's very oversold on the three period RSI. Now we've got two sell rules in the core system. Before we get into the 30 rules that we're going to test, the first sell rule is that the close is above the three period exponential moving average. So that's basically a short term rally that gets the price back above the three period EMA and then you'll get out. And the second cell rule is the close is below the long term moving average. So basically if the stock dips and then keeps dipping and then it ultimately ends up in a downtrend we want to get out. So they're the, they're the main rules. We've also got a fail safe stop, stop loss here and that stop loss is.
40% wide. So it's a really wide stop loss. It's a next bar on open stop. And for position sizing we're using a percentage percent equity per trade model and it's 10% equity per trade. And we're ranking these on the most oversold gets the first, gets the capital first. So if we have more signals than we can take, the most oversold on the RSI gets the signal. So this is a pretty simple mean reversion system. And I've got the results of the standard backtest right here that I wanted to show you before we go any further because we need to know what we're dealing with. So here is the backtest. You can see here the system has an annual return of 6.7%, risk adjusted return of 634%. And the reason the risk adjusted return is so high is because this system is highly selective. It only takes a trade when the rule when the stock is very oversold, that 3 period RSI crossing below 5 doesn't happen all of that all that often. So you can see here that it's only in the market about 1% of the time. So we can make 6% or this system made 6% return in the backtest or 6.7%, but it was only in the market 1% of the time, which is pretty good. Had 279 trades, 72% winners, and the maximum drawdown all the way down the bottom here was 8%. So not too shabby. This is the back tested equity curve. You can see there are a couple of outlier like big positive moves in here in 2000. The market was really volatile back then, so a couple of really good moves. But there's a fairly stable gentle curve up since then. So that equity curve is looking fairly stable. Now if you've got a keen eye, you probably notice that I've stopped this in 2018. This tab here is 2015. So it goes up to 2018. And the reason I did that is because as a result of this investigation, you can, if I found something interesting, I want to have some out of sample data to validate my system decisions on. I didn't want to burn all of my data just to demonstrate this system to you on the video. So I've only tested up to 2018 and if I make some changes, then I've got some out of sample data to test it on, which is kind of useful. All right, drawdown profile is quite tolerable as one big drawdown here. The drawdown touched 8% a few times, but the profile is fairly stable up until 2018 anyway, so it's looking hopeful. So that's the core system and the core system's performance. Now the whole point of this video is I wanted to show you 30 different exits that.
I tested. And so there's two types of exits for this system that I'M playing with. One is the typical next bar on open exit. That's where you get the signal today, and then tomorrow at the open, you exit. Now, that's a very simple, easy way to trade. The second type is exiting at the close today if certain things happen. So I've got two different types of exits, and some of these are sort of duplicates, but the first one is the next bar and open. The second one is the exiting at the same close. But the trick when you're exiting at the close on the signal bar is, is you've got to make sure that you know all of the information required to calculate that exit on the signal bar before the closing price actually happens. So typically that means you'd have to exit with a limit on close order or something like that, where you predetermined that exit level based on other price patterns. So that's what we're doing here. Now remember, if you want the code for this system and if you want a whole bunch of free resources to improve your trading, immediately click the link in the description, enter your name and email to get my Trader Acceleration bundle and the complete code and rules for this system that I'm testing. Okay, so what's next? We've done the core system code. Let's have a look at all of the exits that I'm testing. So I've got a different copy of the code here. Let me zoom in and I'll scroll down. You can see that the bulk of it is the same, except I've added 30 different exit triggers here. The first 18 of them are next bar on open exits and exit 19 to 30 are exiting at the close. Now, I'll go through these rules in a minute, but let me sort of explain how this works. The way I've set this code up is I have specified all of the different exit criteria as separate parameters. And then what I'm going to do is I'm going to pull them together using Amy Broker's case switch function to allow my backtest to automatically test the first one, then the second one, then the third one, all the way up to number 30. So the optimization is going to run 30 times and give me the backtest results for all 30 exit conditions with one button click. Which is kind of cool because I don't want to sit there copying and pasting and changing rules 30 times to test all of these different exits. So here's the case switch function. I'm not going to go through it. If you really want that, click the link in the description Enter your name and email and grab the Trader Acceleration bundle. You can download this code and go through it. You can see here it takes a few lines of code to define it. There's a special optimization parameter in here and then the switch function which changes between all of the different exit rules for each of the back tests. Let's have a look at the sort of rules that I've tested. So the first one is the base rule that we had in the previous backtest that I showed you. The closing price is above the three day moving average. When the closing price is above the three day moving average, this exit gets out on the next bar at the open. Now the second rule that I want to test is, well, let's just see if I just get out immediately so I get in to the trade and then the next bar and open, I just exit. So just exit next day on open. Doesn't matter what the stock price does. Let's see if that is a profitable exit. That's what exit trigger 2 is. Exit trigger 3 is the close is above yesterday's close. Then I've got the close is above yesterday's high. The close is above the open. So it's a green candle. The close is higher than the previous three day highest high. Highest close. Sorry. The closest higher than the previous three day highest high. So you can see I've got all of these different exits here. I won't read through them all. You'll get bored out of your brain. You'll click off the video and then you won't get to the gold at the end. If you want all of those rules, click the link in the description. You know the drill. Enter your name and email. Get the code.
Now for the exits at the close. There's a few different ones. You see here I've. I've got. The first one is just exit at the first close. So in this situation, what the system would do is it would enter when the three period RSI crosses below five at the open and then immediately exit at the same close. The second exit is.
Exit if today's close is greater than the previous day's close. Now this is a little bit tricky. If you wanted to do this in real life, the way you execute it is you put in a limit on close order.
For where the limit is the previous day's close price. So let's say we get into the trade today at the open and then immediately we put in a limit on close order that says if the closing price is above the previous day's close, then Sell on close. And then each day you're in the trade, you would adjust that limit on close order. So you'd make that a good for day order and then adjust it each day. That way, if the price keeps trending down, you only have to get above the previous day's close in order to exit. So the trick for exiting at the close on the signal bar is to make sure that you know the information needed to generate that exit. Right? So in this case we had to know the previous day's close. We know that as soon as yesterday closes. So today we can place that limit on close order and we know all of the information we need to exit. So it's really easy way to trade and to exit at the closing price. Now I've got a bunch of other examples here of ways to exit at the close if today's close is above yesterday's high. Again, that would be a limit on close order if today's close is above the open. Now this one's a little more tricky because you would have to wait for the price for the stock to start trading today, look at what the official opening price is, and then place the limit on close order with the limit at the opening level. So that way if it's up, if it's a green candle, you'll get out at the close. So you can do that. Kind of hard to automate, but if you're there at the open, you see the opening price for trades for stocks that you're holding, then you could actually do this. And then there's a bunch of other ones. We've got the close is above the previous 3 day highest close. The close closes above the previous 3 day highest high. The closes above yesterday's 3 day EMA. We've got some candlestick patterns here. We've got the close is more than half an ATR above yesterday's close. Now there's a trick to this one, and this is really, really important. If you want to measure the distance that today's close is above the previous day's close. So what we're after here is a bounce, right? So with mean reversion, we enter on a dip and then we want to exit after a bounce. So we get a small profit from that mean reversion behavior. Now when you're placing a limit on close order, that limit distance, that limit price level, you need to be able to calculate that on the signal bar on the day you're actually going to exit. So to calculate a distance, you can't use today's Average true range because you don't know it yet. You won't know today's average true range until today's bar is closed. But you need the.
Limit level before the close. So if you use a stretch to say okay, exit. If the close is more than half an ATR above the previous close, you actually need to use yesterday's average true range. Otherwise your backtest will be cheating and you won't actually be able to do it in real life. So this code here, you'll see here, I've got ref atr minus one that's referring to yesterday's average true range. Okay, so we've got 30 different exits and this case switch function that I've got down the bottom. What that's going to do is allow me to cycle through all 30 exits in one optimization and give me a nice set of results to compare and analyze. Now I've got the backtest. Let me just see if I still have the results here. Yep, I've got the backtest. So I ran this earlier because I didn't want you to have to wait for my computer to run. It didn't take too long, only about two or three minutes. But no sense boring you to tears while we wait for that. So here's the results. But to make it a little bit easier to investigate, I've put it into a spreadsheet. And incidentally, I'll include this spreadsheet for you in the Ultimate Trader. Sorry, in the trader acceleration bundle. So you'll get the code get this spreadsheet. You'll also get a whole bunch of those other goodies, two ebooks, three different trading courses, four cheat sheets, and a trading quiz to help you improve your results. So entername Nemo below, grab that and you'll get this spreadsheet as well. So you don't have to worry about taking notes furiously while I talk. Alrighty. So here's the results of my 30 different backtests, 30 different exit tests. I've got the grouping of the exits for next bar on open and the exits.
On the close on the same bar. And what's interesting is the exit that I'd already designed and I had put quite a bit of work into this system. The exit that I'd already designed performs really, really well. The close above the 3 bar exponential moving average is a pretty good exit for a short term mean reversion system. You can see here, it's got a compound annual return of 7.9. Actually, this is a different time period, so those numbers are a little Bit different to what you saw before, but you'll get the picture. The relativity of the different exits is the same. And the max drawdown there is 8.05%. The car over MDD is 0.99. So the big question is, do any of my 30 exits outperform my base level, my base system? And the way we're going to do that, we're going to look for a couple of different performance metrics. We'll look at compound annual return because obviously we're interested in making money. We're going to look at risk adjusted return. Risk adjusted return is the.
Compound return divided by the exposure, the average exposure of the system. And that's interesting because we want to make money.
We want to make more money per unit of time. We're invested in the system, in the market. So a risk adjusted return will be much higher if the system is invested for a very low, very short period of time. A system that is always in the market 100% of the time will have a compound annual return. A risk adjusted return being exactly the same. A system that is very rarely in the market will have a much higher risk adjusted return than compound annual return. And you can see that here on my first line for the default exit rule that I've got, the Compound annual return is 8%, but the risk adjusted return is 744 because the exposure is only 1%. So we're going to look at compound return, risk adjusted return. We're also going to look at drawdown. We're going to look at smoothness of equity curve because that's important. We want to make good returns for a low level of risk. So the compound annual return divided by the max drawdown is the smoothest measure that I like to use. And then we're also going to look at the average profit per per trade. One of the traps with mean reversion systems is that very small average profit per trade. Systems that have a really fine edge can look great in backtesting. But in real time trading, as soon as you get any slippage, the system gets destroyed. Now, in this system, I've already got an allowance for slippage and commission. I've got. Let me have a look. How much have I allowed for slippage and commission? I've already allowed actually half a percent for slippage and commission for entry and exit. So it's quite a generous allowance considering we've got a million dollars average turnover per trade per day required. So we're trading quite liquid stocks and we've got half a percent for slippage and commission. So I think we're pretty safe here. But it's important when you analyze any mean reversion system, especially to make sure your average profit per trade isn't so low that it won't work in real time. Okay, so we've got our exits, we've backtested them all and we know we're going to look at certain performance metrics to evaluate them. So let's have a look through and see what we can find. You can see.
The default exit has a pretty high compound return. There's a couple that are in the same vicinity. So you can see here.
Here. So let's highlight the rules that are performing really well on compound return.
Yeah, there we go. So close above the ema close above the previous day's high works pretty well. That's a nice and simple one.
The close is greater than the previous three day high. That works nice, closes above the five day ema. So a slightly longer term one, the close is above is more than half an ATR above the previous day's close. So it's kind of like how to jump up then. Actually I'm going to highlight this one. And then we've got the RSI exits which is a pretty typical mean reversion exit. If the RSI crosses above 20 or above 50, it performs fairly well. This one is interesting because this is rsi. The RSI is higher than the RSI was yesterday. So what that means is in a mean reversion system the, the RSI is typically falling and our entry is The RSI crosses below 5 and this exit that we just looked at is it exits the first day that the RSI moves up. Yeah, and what's interesting about this one is have a look at the risk adjusted return. The risk adjusted return is off the charts here. It's like 10, 88%. Why is it so high? Because these trades are not in the market very long. Because typically when the rsi crosses below 5 the very next day it will tick up and then will get out straight away. So the average duration of the trade is really, really low. And we should see that. If I scroll across here, we'll see.
Number of bars in trade.
Average bars held, average bars held only 2.6. Right. So that's a really short term system. And this is on daily bars, of course. Now the other thing that's interesting about these results is if you look at all the next bar on open exits, on average they definitely outperform the same bar on close exits. Now this Is kind of interesting. I was a little surprised, but there is actually an overnight edge in the market. And if you look at The S&P 500 chart over long term, if you took that data and you analyze the percentage of the total profit that the S and P generates that happens from open to close, as in when the market is open, compared to the amount of profit that happens from closed to open when the market is closed. Most of the positive movements in the S and P actually happen overnight. So it actually kind of makes sense that when we look at these systems, these exits, that the exiting on the same close we get the signal is not as good as exiting the following open because there's a bit of an edge holding overnight. The market pays us for taking that risk. And so therefore, you know, I thought it might be better to exit more quickly, but it's actually better to hold one more night and get out in the open the next day. Okay, there's a couple of other things that are interesting here. The first one is that we've got a few that have a very high risk adjusted return. So let's highlight these ones. Here we go. 7, 17 and 21.
Okay, so the close is above the previous day's close. The RSI ticks up. And this one, I've got a typo in here. So I'm gonna have to look at the code to see what that actually is. Let me just pull up the code. Exit 17.
Exit 17. Ah, here it is. The RSI is the highest it's been in the last three bars. That's kind of cool because I've never tested that one before.
So this exit actually looks pretty nice. So let's have a look at the car over mdd. Let's look at the smoothness of these equity curves. You can see here, the default rule is actually the best one. But the other smooth equity curves came from.
Here we go. Basically looks like a lot of the shortest term strategies.
Here. Okay, so we've got quite a few results where the COVID MDD is close to 1, just under 1 at 0.9 something. So this shows the system is actually pretty good. Right, because there's lots of different exits that perform well. All we're doing with a mean reversion system is looking for a little bounce. And there's quite a few different ways to calculate that the price has bounced a little bit so that we can get out. So there's lots of different exits that work. The performance actually doesn't vary that much. You can see that the average profit per Trade of this system, sorry, not the average profit. The compound return basically for most of these exits is in the vicinity of 6 to 7% per annum. Now I know that sounds low, but remember it's only invested in the market about 1 to 2% of the time. So the risk adjusted return, which is your compensation for when you're actually exposed to market risk is really, really high. Now there's a couple of exits here that you would definitely not use and that's. Let me just highlight them and I'll explain why.
Here we go. So these ones here, the reasons, the reason I wouldn't use them is because the risk adjusted return is much lower. And what's happening is the. I think you'll find that the trade length is a lot longer. So let me just go across here and find average bars held. Yeah, look, we've got average bars held of 8.8 days, 11 days, 11 days and 9.7 days. But the thing about mean reversion is the very best mean reversion trades happen very, very quickly. So we want exits that catch the first jump in the price. And the reason that's powerful is because if you wait for too long, waiting for confirmation or waiting for more profit on mean reversion, you might get that little bounce, but then often the price falls away and you end up giving up all of your profit and more. So the best mean reversion exits are often the very quick ones, the ones that just get you out on the first sign of a little bounce and then you take your profit and you run. They're the ones that tend to work best. And the exit test that we've done here absolutely confirm that. So anyway, I just want to wrap this up. This has been a long video, but I wanted to share because there's a couple of really interesting insights in this. The first one is that little, that extra overnight hold is really powerful. It bumps up the profit. The best mean reversion exits are the short term ones that give you an exit signal as soon as there's a little bit of a bounce. And there's quite a few ways of calculating that. So this approach to trading is quite robust. You don't need to have a very precise exit because many of them generated stats around the same sort of level. Now one other thing I didn't show you is the average profit per trade because with mean reversion one of the constant challenges and a lot of the systems you'll see online, on websites and everything don't have slippage and commission in their backtest and the average profit trade is really, really small. But in this backtest I've included half a percent slippage and commission for entry and half a percent for exit. And it's a million dollars average turnover per day as the liquidity requirement. And if we look at the average profit or loss which is in this column here, you see that the average profit or loss for the first set of exits is between 6 or between 5 and 8%. So we've actually got quite a big buffer to play with here. So that'll allow, even if we have a little more slippage, this system should still work, should still hold up in the future. So we've been through 30 different exits, we've tested them all, we found that a bunch of them actually work really well. And we've also learned that we should exit next bar and open with this system rather than trying to exit at the close because that overnight hold adds some value to our system. We also learned that the shorter term exits that exit more quickly give us better risk adjusted returns, which is great because we want to be compensated more highly for, for the amount of time that we're exposed to market risk. If we were fully invested the whole time, 100% invested, we would need much more than the small compound annual returns that this system is generating. But because we're only in the market 1 to 2% of the time, generating 5 to 6 to 8% compound annual return is actually really good because most of the time you're not using that cash. So what you can do is take that cash and apply it to other systems. And, and then when you get a signal in this system, you just take that, take that money, put the trade on, and then you'll close it out one to two days later. And then, and then the money can go back to the other systems that you were trading. So just because the compound annual return is low doesn't mean the system is no good. This has a very high risk adjusted return. So it's really quite, quite beneficial when you combine it with other strategies in your portfolio. So if you want to get the code for this, these exits, these tests, click the link in the description, enter your name and email, and you'll get the Trader Acceleration bundle, which includes the code and the rules for these 30 exit tests. It includes two ebooks to help you improve your trading system confidence. It includes four different cheat sheets to help you improve your results immediately, a trading quiz to show you the flaws and the gaps in your trading and give you some pointers to improve, and a whole bunch of other important things or useful things to make sure that you can improve your trading results. So I hope that's helpful to you. My name is Adrian Reid. This is Enlightened Stock Trading. Enter your name in the email below, get the code Study this and if you've got any questions, there's a forum in the Resource hub that you'll get access to. You can ask me questions. I'll help you out there. That's all for this video. Thanks for watching. Make sure you give it a thumbs up and subscribe so you can see next time I go live. Bye for now.











