Sadly most traders optimize their systems completely the wrong way. When learning Amibroker formula language (AFL) it is so tempting to dive right into complex formulas and advanced functionality.

In reality you will make faster progress if you keep your trading system code simple and learn to correctly optimize your trading system to avoid curve fitting.

This video shows you how to do exactly this – Watch it and then leave me a comment below if you have any questions and I will help you out!

Show Full Transcript

Did you know that most traders optimize their systems completely the wrong way, resulting in heavily curve fit systems look great in backtesting, but in real time trading they actually lose money? My name’s Adrian Reid and I’m the founder of Enlightened Stock Trading. And over the last 15 years, I’ve been a profitable systematic stock trader. And during that time I’ve backtested hundreds of trading systems, taken thousands of systematic trades and say I make my living by trading systematically in the stock market.

What I want to share with you in today’s video is how you can correctly optimize parameters in your trading system using Amibroker to make sure that you develop systems that are likely to be profitable in the future as well as look great in a backtest because let’s face it, we can only trade in the future. We can’t trade in a backtest. And too many traders spend time doing things to make their backtest look amazing, but that affect realtime future trading performance.

If you get this right, then chances are you’re going to be able to make a lot of money by trading systematically in the future. But get this wrong and optimize your systems incorrectly, then your chances of making money in the stock market are basically zero because when you’re optimizing correctly, you curve fit your systems to pass data. And when you do that, your ability to make money in the future is dramatically reduced. So wanting to share with you today how to optimize correctly. And I’m going to do that using a simple example of a trading system in Amibroker. So let’s jump into Amibroker right now and I’ll show you how we go about doing this to make sure you can start optimizing correctly.

Okay, so I’m just going to pull up the system here. Now what I’ve got is a simple breakout system to illustrate how to optimize correctly. I’ll spare you the boring details of the setup of the trading system, but basically we’re starting with a $100,000 of equity. We’re taking 20 positions and we’re going to account for commissions because I want this to be somewhat realistic. The system enters at the open and it exits at the open. We’re going a couple of really simple rules just to make this system work and to make it realistic.

We’ve got a liquidity filter, which is requiring the stocks that you trade to trade over $500,000 worth of volume per day or more. So these are quite liquid stocks. Now as a breakout system, I like to take breakouts in low volatility stocks. Now you may not know this, but not all conventional trading wisdom is actually true. And if you’re assuming that you have to trade high volatility stocks to make money, you’re dead wrong.

So I’ve got this rule here to select for low volatility stocks. And as you’ll see, it makes the system performance look pretty reasonable. So there’s just a little tip, something for you to test out in your own trading. Now what is the system? The system is a breakout system. So the entry rule, the entry trigger here is when the closing price crosses above the previous day’s highest high. So which high is high? Well, that’s the variable that I’m going to optimize. So you’ll see here I’ve got this parameter called breakout bars and I’ve defined it in a previous line. And breakout bars is the number of bars we’re going to look back to see if today’s close is the highest close. And I’m going to vary this between 20 bars and 400 bars in steps of 20. I’m going to default value of 50 here, but that doesn’t matter too much. We’re just going to look at the optimization results today.

So this system enters on a new highest close, a breakout. So the stock clearly has upward momentum. It’s in an uptrend and we’re going to look at different lengths of breakout. The exit rule is also very simple. It holds the stock until the closing price drops below the 200 day moving average. There’s a trend following system. It’s quite a longterm system. It’s not a system I necessarily recommend you trade in of itself, but it’s a pretty good starting point if you’re new to systematic trading. You might take this idea and play with it yourself and come up with some other ideas. But for the purposes of learning how to optimize correctly, perfect example.

Okay, so our buy conditions. We need the liquidity filter to be true. We need the volatility filter to be true and we need the entry trigger to be true. Our sell conditions is just when the stock price crosses below the 200 day moving average. So I’ve got no stop loss, no profit targets, no time stop exits. The system is very simple. And for position sizing, I’m saying every position should be 5% of equity. So when we’re fully loaded, we’re going to have 20 positions each worth about 5% or exactly 5% of our total equity.

So this is the Amibroker code to optimize the system. And if you haven’t done an optimization in Amibroker before, then this line particularly is the one that you want to optimize. So whenever you’re going to optimize a variable or parameter in Amibroker, you’re first of all going to define that parameter value as a function like this. So on line 22 here, you can see breakout bars equals this optimize.

Now the syntax is pretty simple. You type optimize, then you give it a name in inverted commas. And then the first argument here is the default value. So if you press back test, it’ll use the default value. If you press optimize, it’ll use these other values here. So the next one is the minimum value, then the maximum value, and then the step size. So this goes from 20 to 400 in steps of 20.

Now let’s go across to the Amibroker analysis window and see how this works in a backtest. So I’m just going to open it up here. Just ignore all of my systems that I’ve got there. So I’ve opened it up. I’m going to backtest from 1993 to present and I’m going to backtest on daily bars. And I’m testing on the Australian Stock Exchange. Now, if we’re designing a system, I’d use in sample and out of sample data and all of that, but for the purposes of demonstrating how to optimize a parameter, I’m not going to bother doing that.

So the actual mechanics of doing this is simple. We’re just going to load the system like we have here, and then we’re going to press optimize and you’ll see the results come up pretty quickly. Amibroker is a great platform because it’s super fast to vary the parameters and to run the test. So here we’re running, you’ll see the first line is for 20 bars, a 20 bar breakout, and if you look all the way over on the right hand side, and I’ll just expand this out a little bit, you’ll see here the highest high bar of 20, 40, 60, 80, 100. So it’s stepping through each of those values.

So from a mechanical point of view, the process of optimizing is dead easy. But that’s where the challenge is because if you just press optimize and then choose the highest or the most profitable result, that’s when you’re going to end up with a curve fit system that actually loses you money.

So look here, I’ve just sorted these back in order of the breakout size, and you can see that the net profit varies dramatically as you move through the whole optimization range. So it starts at $345,000, gets as high as $1.134 million here, and it’s up and down, up and down.

So how do you choose the best value, the optimum value? Now this is where most people make a mistake because most people optimize to get the best historical profit, the maximum historical profit or the maximum historical compound annual growth rate. And if you’re doing it that way, then you definitely choose this value here, the fifth step, which is over on the right hand side we’ll see is a hundred bars. So is that the correct answer? Well, maybe, maybe not. It’s too early to tell.

So let me show you how we actually make this decision. And in actual fact, optimizing for the highest compound annual return is not necessarily the best thing you can do because high returns typically come with high drawdowns. And if you want a very volatile equity curve, then that’s what you’re going to get when you optimize for the highest compound growth rate. I actually prefer to optimize for this parameter here, which is the compound annual return divided by the max drawdown. That gives you an idea of the smoothness of the equity curve.

In my trading, I want the smoothest equity curve possible so that a performance measure is a better one to use. There’s several other performance measures to use, but to make this optimization decision, I’m going to take this data and put it into a spreadsheet and just plot the results for you so that you can see how to actually choose the best parameter value.

So I’m just going to paste them over here, and let’s say we’re optimizing for compound annual return over max drawdown. And I’ve drawn a chart just a little further over here. I did this earlier just to illustrate graphically the optimization results.

Now look at this. What this shows is on the horizontal axis, it’s the number of bars in the breakout. On the vertical axis, it shows the compound annual return divided by the max drawdown that was generated by the optimization backtest. Now, if you were just looking for the best value, then you’d probably choose this one. I think this is a little higher than this one over here actually. So yeah, 200 bars is a little higher than 100 bars.

But what you’ll notice is as you step through this, the results are quite volatile. And this is what you need to look out for because when you’re optimizing, if you just blindly choose the best value, then you’re not taking into account the behavior of that optimization variable in the surrounding areas. Because in the future, the optimum value is not likely to be exactly the same as the optimum value for the past 10 or 20 years. The future will be a little bit different. And so if you choose blindly the best one from the past, chances are you’re not going to get a good result in the future. Because if the market shifts just a little bit, you’ll see that when you drop from 200 down to 180, yeah, that’s what that is there 180, the performance drops off quite a lot.

And imagine if you chose this one here or this one here, the performance drops off quite a lot on either side. What that means is that value is a very sensitive value. It’s an area of instability. It may be good performance in the past 10, 20 years, whatever that backtest was, but it’s an area of instability.

Now, if you want a trading system that’s profitable in the future, you need to select parameter values that are in the middle of a broad area of stable good results. So where is that on this Amibroker chart? Well, have a look. As you increase the number of bars in the breakout, the performance here is quite unstable. As you move up in 20 bar increments, the performance varies quite a lot. And over this entire period here it’s up and down.

As you keep increasing, you’ll see that in this area here, the performance is all pretty stable. Yeah, sort of from about 120 bars all the way up to about 300 bars, the performance is in a fairly tight narrow range. Now, if I was choosing a parameter value, I’m more interested in the stability of that parameter over a wide range, so I am in the absolute performance that each parameter setting resulted in.

So I want to choose something a parameter value right in the middle of this big stable area of good performance. Now if we choose this one, then we’re going to get overly optimistic backtest results because we know either side of it the performance is less. It’s not as good. So I’m going to choose and I also noticed that here it’s quite unstable from value to value. It increases and decreases quite a lot. But this area out here as the number of bars in the breakout continues to get bigger and bigger is really nice and stable. The results don’t jump around that much.

So I’m going to choose a parameter value in this area. It doesn’t matter much because remember your optimization results from the past and not going to give you the absolute best results in the future. The future is uncertain. So whether you choose this one or this one or this one probably won’t affect your realtime trading performance all that much, right? But the key is you choose the parameter value that’s in the middle of a broad range of stable similarly well-performing parameter values. So something around to the 240, 260, 280, 300 mark, even 320 is going to give you good solid, stable results.

So choosing the optimization variables in the middle of that broad stable area of good performance is going to give you a much more reliable backtest, the backtest, which is more predictive of future performance. So if you do it like that, if you optimize each of your parameters like that, you can avoid curve fitting to performance peaks like this and this. Because when you do that over several different parameters in your system, you’re going to result in a really heavily curve fit system that looks great in the backtest, but it’s absolutely useless for future performance because it’s very unstable.

You want stable parameters. That’s the purpose of optimization. It’s not actually to find the best results. Optimization, the correct purpose of optimization and trading system is to find the most stable area of good performance. That’s far more important than finding the best area of performance or the best parameter value. The trouble with most trading software is it pushes you down the path of choosing the best performing parameter value from past history, and that’s not going to give you stable realtime trading performance.

So optimize this way instead. Do it visually. Choose a parameter value in the middle of a broad area of solid performance, and your trading systems will improve dramatically in their ability to profit in future and seeing data. You’ll be less curve fit and you can be more confident in your trading system.

If you like this, if you value this information, then click the link below and download my Trading System Confidence Cheat Sheet, which shows you the steps that I follow every single time I develop a trading system to make sure I’ve got rock solid confidence in the profitability and performance of that trading system. Click the link below and download my Trading System Confidence Cheat Sheet, and it give you more tips like this to make sure that your trading is profitable in future in the real world when you’re actually using your money in your brokerage account to try it with.

My name is Adrian Reid. This is Enlightened Stock Trading. Don’t forget to click the link below and grab your Trading System Confidence Cheat Sheet. That’s all for this video. I’ll see you in the next one. Bye for now.

Pin It on Pinterest

Share This

Share This

Share this post with your friends!