Amibroker Tutorial: Learn How to Optimize Amibroker Trading Strategy the right way!

Adrian Reid (founder of Enlightened Stock Trading) demonstrates how to correctly optimize trading-system parameters in Amibroker to avoid curve-fitting. Using a simple breakout system (entries on new highs, exits below the 200-day moving average) he shows an Optimize sweep (breakout bars 20→400 step 20) and explains why you should select parameter values in the middle of broad, stable regions of good performance (rather than the single best historical peak). He emphasizes stability (e.g., optimizing for CAGR / max drawdown) and gives practical setup details for reproducible backtests.
Questions this video answers
$100,000
20 positions
$500,000 per day
breakoutBars, Optimize(...) from 20 to 400 in steps of 20 (default shown as 50)
Exit when closing price drops below the 200-day moving average
Chapters
Did you know that most traders optimize their systems completely the wrong way, resulting in heavily curved fit? Systems look great in backtesting, but in real time trading they actually lose money. My name is 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 back tested hundreds of trading systems, taken thousands of systematic trades, and today 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 real time 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 optimize incorrectly, you curve fit your systems to past data. And when you do that, your ability to make money in the future is dramatically reduced. So what I'm going to share with you today is 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 Broker 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 $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've got 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 make 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, is when the closing price crosses above the previous day's highest high. So which highest high? Well that's the variable that I'm going to optimize. So you see here I've got this parameter called breakout bars and I've defined it on 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've got a default value of 50 here but that doesn't matter too much. We're just going to 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. This is a trend following system. It's quite a long term 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. 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 moving average crosses below. Sorry 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 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, 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 and 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 back Test from 1993 to present, and I'm going to back test 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 purpose 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. Amiborker 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 and if a 20 bar breakout, 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 for 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. 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 100 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 over 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.
Performance measure is a better one to use. There are 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. 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 is 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 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. So 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 than I am in the absolute performance that each parameter setting resulted in. So I want to choose something right, 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. Though the performance is less, it's not as good. So I'm going to choose. And I also notice 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 are 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 real time trading performance or 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 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. A 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're going to 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 in a 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, best performing parameter value from past history. And that's not going to give you stable real time 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 unseen 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 and 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 I give you more tips like this to make sure that your trading is profitable in future in the real world where when you're actually using your money in your brokerage account to trade with. My name's 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.











