How to choose the parameters to optimize an automatic trading system

Strategies and tips to avoid mistakes and maximize profits.

If you are also a trader and you develop automatic trading strategies, you absolutely have to ask yourself this question: What parameters do I choose during an optimization?

This question is by no means obvious and if you don’t answer it right it will have catastrophic effects on your systems and especially on your personal account.

In this article we will see how to optimize a trading system with one variable and with two variables.

In this article I use Tradestation, for those unfamiliar with it it is a trading platform that allows you to do automatic trading in a simple and safe way, the same goes for those who use Multicharts, I remind you that the concepts I explain are the same for whatever platform you are using.

Let’s start right away…


Suppose we have a system with tm Daily that enters long on the S&P 500 when the price exceeds a moving average of variable length and we exit the market after 6 bars (6 days), here is the system in its entirety:

Now we don’t know what length of the moving average to use for better performance, so we’re going to optimize the length of the average.

Well after a good optimization we find this system:

With a net profit of $139,000, an average trade of $335 and a drawdown of $37,000, you’ll say perfect, I’ve created a system, I’ll add a filter to lower the drawdown and that’s it!

Unfortunately this is not the case, let’s take a better look at what our optimization has done…

I optimized my moving average from 5 to 200 bars with steps of 5 and the Tradestation automatically chose the best result that I showed you above, but let’s see all the results:

Let’s zoom in on the part chosen by the optimization and with better parameters:

What you notice? I’m telling you this is called OVERFITTING, meaning too much adapting your system and metrics based on the past!

Do you know what happens if a system is OVERFITTED? YOU WILL LOSE YOUR MONEY

How to understand it? Well the answer is simple when you optimize just 1 parameter: LOOK AROUND

We see that in our case moving 10 bars in length from the selected result something very interesting: The net profit drops drastically and the drawdown increases!

Do you think it is a sign of stability? Well I would say NO!

But let’s see in more detail…

This is a simple chart showing NetProfit in relation to the length of the bars..

Notice the difference in just 10 bars? Moving the indicator 10 bars would have resulted in a $30,000 lower NetProfit and an almost doubled drawdown!

So guys be careful what parameters you select and always look at the surroundings!


Let’s continue with the previous system, this time I don’t want to optimize only the length of the moving average but also the duration of the trade (i.e. the exit after tot bars), and therefore I put a new element in input: lenBar, I will optimize it to understand which parameter is the best.

So this time I proceed to optimize the length of the moving average from 5 to 200 with steps of 5 and the length of bars for the exit from 0 to 6 with steps of 1 (I don’t want to stay on the market for more than 6 days)

Let’s see the results and we notice that Tradestation offers us the same inputs I had before, therefore average length 80 and output after 6 bars!

Fine, but now that we have one more input how do we figure out which one is the best and the most stable? If optimizing 1 parameter we used a 2D graph, we just have to use a 3D graph to show 3 parameters! In this case we will have the optimized inputs in X and Y and in Z we will have the NetProfit, unfortunately Tradestation unlike Multicharts does not give us the possibility to do this, so we should do it ourselves.

For a while I used excel, but it was a tedious and graphically unappealing operation, so in the end I decided to write a small python tool and share it with all of you!

Well, by saving the report you can upload it to my site and the tool will do everything automatically.

Let’s see the results of my optimization:

What do you think? I’ll tell you, this system is not stable, the returns of the strategy after a few inputs vary considerably, this means that the system will not withstand long market variations or adverse conditions that arise after its creation, in this case if we choose the values we like the most like these:

But we will have an OVERFITTED system that at the first signs of change or even without signs of change the system will no longer perform and you WILL LOSE MONEY.


The explanation with 1 and 2 parameters is very simple, look at the surroundings, they must be stable and above all the Net Profit must not fluctuate much. Of course there is one thing to say, optimizing a moving average on a daily tm is different from optimizing it on a 5-minute tm, so choose also and above all the optimization steps with criteria, as a too narrow step does not allow it will give a complete but too fragmented vision, while a large step will give us an overall vision but not detailed enough, the secret lies in the middle.

I hope this article has been useful and can help you in building your systems, if you have any questions do not hesitate to write me, I leave you the link to my site again to upload your 2 parameter reports

See you soon!

Join the 👉 Telegram group

Follow me on 👉 LinkedIn

If you want discounts for the platform I use contact me privately or buy via this 👉 MultiCharts