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.

Frequently Asked Questions about Amibroker Optimizing Trading System

What is trading system optimization?

Trading system optimization is the process of adjusting the parameters of a trading system to improve its performance. Here’s a breakdown of what it involves:

  • Parameter Adjustment: In any trading system, there are parameters that influence how the system behaves, such as moving average periods or stop-loss levels. Optimization involves finding the best values for these parameters to maximize performance metrics like profit or minimize risk measures like drawdown .
  • Backtesting: This process typically involves running multiple backtests using different parameter combinations to see which set yields the best historical performance. Tools like AmiBroker allow for optimization across multiple symbols, testing all possible parameter combinations to find the most profitable setup .
  • Avoiding Over-Optimization: A key challenge is avoiding over-optimization, where a system is too finely tuned to past data and fails to perform well in real-time trading. The goal is to find stable parameter values that perform well across different market conditions, not just the historical data .
  • Stability and Robustness: Effective optimization seeks parameter values that are stable, meaning small changes in the parameters don’t drastically affect performance. This stability suggests the system is more likely to perform well in the future .
  • Smart Algorithms: Techniques like Particle Swarm Optimization (PSO) or Genetic Algorithms can be used to find stable parameter regions, focusing on plateaus rather than sharp peaks, which are less reliable in real trading .

How do you optimize a trading strategy in AmiBroker?

Optimizing a trading strategy in AmiBroker involves several key steps to ensure that your system is both effective and robust. Here’s a breakdown of the process:

  • Define Parameters: Start by identifying the parameters you want to optimize. These could be things like moving average periods or breakout bars. Each parameter should have a default value, a range (minimum and maximum), and a step size for testing .
  • Use the Optimize Function: In AmiBroker, the optimize function is used to vary these parameters. The syntax is straightforward: variable = optimize(“Description”, default, min, max, step);. This allows AmiBroker to test all possible combinations of parameter values within the specified range .
  • Run Backtests: Once your parameters are set, run backtests using the optimization feature. AmiBroker will evaluate the performance of your trading strategy across all parameter combinations. This is where the platform’s speed and power really shine, allowing you to quickly assess a wide range of scenarios .
  • Analyze Results: After running the optimizations, analyze the results to identify which parameter combinations yield the best performance. Look for stable regions or plateaus rather than sharp peaks, as these are more likely to be robust in real trading .
  • Avoid Overfitting: Be cautious of over-optimizing, which can lead to curve fitting. The goal is to find parameter values that perform well across different market conditions, not just in historical data .

Why is optimization important for systematic trading?

 

Optimization is crucial for systematic trading because it helps refine your trading system to improve its performance and ensure its robustness in real-time trading. Here’s why it’s important:

  • Parameter Refinement: Optimization allows you to adjust the parameters of your trading system, like moving averages or stop-loss levels, to find the most effective settings for historical data. This helps in identifying parameter values that are likely to perform well in the future .
  • Stability Over Perfection: The goal of optimization isn’t to find the perfect parameter combination for past data, but to identify stable parameter values that are less likely to degrade in future trading. Stability means that small changes in parameters don’t drastically affect performance, which is crucial for real-world application .
  • Avoiding Overfitting: A well-optimized system avoids overfitting, where a system is too finely tuned to past data and fails in real-time trading. By focusing on stable parameter regions rather than the best historical results, you reduce the risk of overfitting .
  • Confidence Building: Through optimization, you build confidence in your trading system by ensuring it can handle various market conditions. This confidence is essential for sticking to your system during challenging times .
  • Risk Management: Optimization helps in understanding the risk profile of your system, allowing you to manage risks more effectively by knowing how different parameter settings impact performance .

What are the common methods used to optimize trading systems?

Optimizing trading systems involves several common methods, each with its own strengths and applications. Here’s a rundown of the most prevalent ones:

  • Exhaustive Search: This method tests all possible combinations of parameters within specified ranges. It’s thorough but can be time-consuming, especially with multiple parameters .
  • Non-Exhaustive Search: When the parameter space is too large, non-exhaustive methods like Particle Swarm Optimization (PSO) or Genetic Algorithms are used. These smart algorithms search for stable parameter regions rather than sharp peaks, which are less reliable in real trading .
  • Single Parameter Optimization: Focusing on one parameter at a time helps reduce the risk of curve fitting and makes it easier to identify stable parameter values .
  • Walk-Forward Optimization: This involves optimizing parameters over a portion of historical data and then testing them on a subsequent period. It helps assess how well the system adapts to changing market conditions .
  • Out-of-Sample Testing: After optimizing, it’s crucial to validate the system on data not used during the optimization process to ensure robustness .

These methods aim to find parameter values that enhance system performance while avoiding overfitting, ensuring the system remains effective in real-world trading scenarios.

What is an example of an optimized trading strategy?

An example of an optimized trading strategy could be a moving average crossover system. Here’s how you might go about optimizing it:

  • Define Parameters: Let’s say you have a simple moving average crossover system with two parameters: a short-term moving average and a long-term moving average. You might start with a short-term average ranging from 10 to 50 days and a long-term average from 100 to 200 days .
  • Optimization Process: Using a platform like AmiBroker, you would set up these parameters to be optimized. For instance, you could use the optimize function to test different combinations within these ranges. The goal is to find the parameter values that maximize your chosen performance metric, such as net profit or risk-adjusted return .
  • Backtesting: Run backtests across historical data to evaluate how different parameter combinations perform. This helps identify which settings yield the best results while ensuring the system remains robust across various market conditions .
  • Stability Check: Ensure that the optimized parameters are stable. This means that small changes in the parameter values should not drastically affect the system’s performance. Stability is crucial for real-world application, as it indicates the system is less likely to degrade in future trading .

This approach helps refine the strategy to improve its effectiveness and robustness, reducing the risk of overfitting to historical data.

How do you avoid overfitting when optimizing a trading system?

Avoiding overfitting when optimizing a trading system is crucial to ensure that your strategy performs well in real-world trading, not just in historical backtests. Here are some strategies to help you avoid this common pitfall:

  • Use Out-of-Sample Testing: After optimizing your system on historical data, validate it on a separate out-of-sample dataset. This helps ensure that the system’s performance isn’t just a result of fitting to past data .
  • Walk-Forward Optimization: This technique involves optimizing your system over a rolling window of historical data and then testing it on the subsequent period. By repeating this process, you can create a continuous out-of-sample performance set, which is more indicative of future performance .
  • Focus on Stability: Instead of looking for the best-performing parameter set, aim for stable parameter regions where small changes don’t drastically affect performance. This indicates robustness and reduces the risk of overfitting .
  • Limit Complexity: Avoid using too many parameters or overly complex models. The more parameters you have, the higher the risk of fitting noise in the data rather than capturing genuine market patterns .
  • Avoid Perfection: Don’t aim for a perfect backtest result with minimal drawdowns and high returns. Such results are often a sign of overfitting. Instead, look for a strategy that performs reasonably well across different market conditions .

By following these practices, you can develop a trading system that is more likely to succeed in real-time trading.

What are the best practices for backtesting and optimization?

When it comes to backtesting and optimization, there are several best practices to ensure your trading system is robust and reliable:

  • Understand Your System: Before diving into backtesting, make sure you have a clear understanding of your trading system’s concept. Know the rules and logic behind your strategy .
  • Validate the Raw System: Check the accuracy of your system by verifying entry and exit dates and prices for several trades. Ensure it includes real-world costs like slippage and commissions .
  • Optimize Thoughtfully: Focus on finding stable parameter regions rather than the most profitable ones. This helps avoid overfitting and ensures your system is more likely to perform well in future market conditions .
  • Use Out-of-Sample Testing: After optimizing, validate your system on a separate out-of-sample dataset to ensure it can handle unseen data .
  • Incorporate Walk-Forward Optimization: This technique helps assess how well your system adapts to changing market conditions by optimizing parameters over a rolling window and testing them on subsequent periods .
  • Implement Portfolio and Risk Management: Test your system across a portfolio of stocks, not just individual ones, to understand how different instruments work together and manage risk effectively .
  • Document Everything: Keep detailed records of your backtesting and optimization process, including parameter settings, results, and any changes made. This helps in refining your strategy and understanding its performance over time .

These practices help ensure your trading system is robust, adaptable, and ready for real-world trading.

Which optimization techniques work best for algorithmic trading?

When it comes to algorithmic trading, choosing the right optimization technique is crucial for ensuring your trading system is robust and effective. Here are some techniques that work well:

  • Individual Parameter Optimization: This method involves varying one parameter at a time to find a stable area of performance. It’s particularly useful for beginners and helps avoid the pitfalls of overfitting by focusing on stability rather than peak performance .
  • Robust Brute Force Optimization: Unlike traditional brute force methods, this approach focuses on finding stable parameter regions within the multidimensional space of the entire parameter set. It’s a powerful tool for systematic trading, especially when combined with walk-forward techniques to evaluate effectiveness .
  • Walk-Forward Optimization: Developed by Robert Pardo, this technique is ideal for short-term, high-frequency systems. It involves optimizing parameters over a rolling window and testing them on subsequent periods, providing a continuous out-of-sample performance set .
  • Non-Exhaustive Methods: Techniques like Particle Swarm Optimization (PSO) and Genetic Algorithms are used to search for stable parameter regions rather than sharp peaks, which are less reliable in real trading .

Each of these methods has its strengths, and the choice depends on the nature of your trading system. For short-term, high-frequency systems, walk-forward optimization is often the best choice, while robust brute force optimization is more suited for long-term systems . 

Can optimization help increase profitability in live trading?

 

Optimization can indeed help increase profitability in live trading, but it’s crucial to approach it correctly to avoid pitfalls like overfitting. Here’s how it can be beneficial:

  • Parameter Tuning: By optimizing your trading system’s parameters, you can enhance its performance. For instance, finding the right moving average period for a crossover strategy can significantly impact profitability .
  • Stability Over Perfection: The key is to focus on finding stable parameter values rather than the single best combination from historical data. Stable parameters are more likely to perform well in future market conditions, providing a better chance of profitability in live trading .
  • Out-of-Sample Testing: After optimization, it’s essential to test your system on out-of-sample data to ensure the chosen parameters hold up in real-world scenarios. This step helps confirm that the system isn’t just curve-fitted to past data .
  • Robust Brute Force Optimization: This method, which I teach in The Trader Success System, focuses on finding stable regions of parameter values, making it a powerful tool for systematic trading .

By following these practices, optimization can indeed enhance your trading system’s profitability in live trading. It’s all about finding that balance between optimizing for past performance and ensuring future robustness. 

How often should you re-optimize your trading system?

The frequency of re-optimizing your trading system depends on the nature of the system itself. Here’s a general guideline:

  • Short-Term Systems: These systems often require more frequent re-optimization due to their sensitivity to market changes. Typically, you might look at re-optimizing every six months or even more frequently if the system is very short-term .
  • Long-Term Systems: For systems with a longer-term focus, re-optimization can be less frequent. Once a year might be sufficient, as these systems are generally more stable and less affected by short-term market fluctuations .
  • Monitoring for Changes: Regardless of the system’s timeframe, it’s crucial to monitor performance regularly. This doesn’t mean re-optimizing every time you check, but rather looking for signs that the system’s performance is deviating from expectations. If you notice significant changes, it might be time to consider re-optimization .

Remember, the goal is to ensure the system remains robust and aligned with current market conditions without overfitting to past data. 

author avatar
Adrian Reid Founder and CEO
Adrian is a full-time private trader based in Australia and also the Founder and Trading Coach at Enlightened Stock Trading, which focuses on educating and supporting traders on their journey to profitable systems trading. Following his successful adoption of systematic trading which generated him hundreds of thousands of dollars a year using just 30 minutes a day to manage his system trading workflow, Adrian made the easy decision to leave his professional work in the corporate world in 2012. Adrian trades long/short across US, Australian and international stock markets and the cryptocurrency markets. His trading systems are now fully automated and have consistently outperformed international share markets with dramatically reduced risk over the past 20+ years. Adrian focuses on building portfolios of profitable, stable and robust long term trading systems to beat market returns with high risk adjusted returns. Adrian teaches traders from all over the world how to get profitable, confident and consistent by trading systematically and backtesting their own trading systems. He helps profitable traders grow and smooth returns by implementing a portfolio of trading systems to make money from different markets and market conditions.