Metatrader 5: Backtest e Otimização de Robô Trader | MT5


When we have
an Expert Advisor (EA), the first thing we do
is to run it in the past, run it with history data. The first goal would be to analyze
if it does what it should do. Then to analyze the results. If it can’t perform in the past,
let alone in the future. So in this video we will be taking a look
on how to execute backtests and optimizations. Let’s go. So this is the
MetaTrader’s Strategy Tester. Please, select the Expert Advisor
you want to test, and I am using here
the “Moving Average Crossover”. Select the asset you want,
I am using the mini Bovespa, in which I am using the continuous
contract without adjustment, which is the “WIN$N”. I chose to test with “M5”
in the year of “2016”. You must set each of these
settings accordingly. The rest, please, set like
it is on this screen, especially here as “Disabled”
and here checked as “Preview”. I’m about to run the test. This is our test running.
It is the Moving Average Crossover EA. Over here it crossed down,
a sale was made. Crossed up, a purchase was made.
Crossed down, a sale was made. It has reached the time
to end the positions, it closed the positions. Everything according
to what we expected. And that’s the goal
of this type of test. It’s intended to
let us analyze if the EA really does
what we expect it to do. We can pause the test here, we can resume,
we can control the speed. Everything to understand if our strategy
does what we expect it to do. This was the visual backtest. If I am satisfied,
if I’m confident with my strategy, I know it does
what I expect it to do, I can turn off the “Preview”
and run the backtest. In this case, the backtest will be
performed with no chart or anything, in the fastest speed as possible. All that matters to us now are
the results displayed on this tab. On this screen,
we can see many statistics about our strategy. It is possible to see we had
a $ 16.400,00 gross profit. However,
we had a $ 17.400,00 gross loss. We ended up with a loss
of almost $ 1.000,00. We can see that, at some point,
the strategy reached $ 2.000,00 negative. And we got only 33%
of the operations right. There are statistics
to suit all tastes there. But there is one question left: What if we had ran it
in another timeframe? What if we had used
another period of “Fast Average”? Or perhaps another period
of “Slow Average”. Or perhaps another
type of average. Or I can set
all these kinds of things, and run the test again. In this specific case,
our $ 1.000,00 loss dropped to $ 445,00 Now we are in a not-so-bad situation
compared to the last one. But what if we want to test hundreds of
different combinations? We will set up the EA, run again, set up again,
run again, set up again, run again?
No. For that, we have the Optimization,
which we will take a look right now. To optimize a parameter,
we have to check it, here I am checking
the “Fast Average Period”, we have to tell in which value
we want to start the test, up to which value
we want to test, and in which range
we will be performing the test. In this case I’m testing all averages
from 1 to 100 periods going up one by one. I’m going to do the same
to the “Slow Average”. I’m going to test
different timeframes too. Testing from 1 minute up to 1 hour. And testing every possible
type of average. I’m going to switch the option “Disabled”
to “Slow Complete Algorithm”, so it can test all the combinations
I have created. Then I’m going to
click on “Start”. The “Slow Complete Algorithm”
will test all possible combinations that we selected
in the parameters settings. In this case, it will have to test
1.080.000 combinations. This is a very large number
of combinations. We can take a look
at the input parameters in order to understand why
there are so many combinations. For the “Fast Average”,
we selected 100 combinations. We’re starting at 1 going up to 100,
one by one. So 100 combinations. We’ll do
the same thing for “Slow Average”. So for each period
of “Fast Average”, we have 100 combinations
of “Slow Average” to test. So 100 times 100,
10,000 combinations. Each one of the 10,000 pairs
of both “Fast and Slow Averages” has to be tested
in 12 different timeframes. 10,000 times 12,
120,000. So we have reached
120,000 combinations. Now each one of these
120,000 configurations has to be tested using 9 different
types of average, so 120,000 times 9, we have the
1,080,000 possible combinations. It would be great if we could test
all possible combinations, but we are always short on resources.
In this case, the time. So for this kind of situation,
we have the Genetic Algorithm. So I am going to stop my test here. And I am going to switch
to “Fast Genetic Based Algorithm”. By selecting this option,
it won’t test all combinations, it will test only a few of them, but it helps us to obtain
a result closer to the best one. I’m going to set it to run. Now our optimization dropped
from 1,000,000 to 10,000 combinations, which is way better. We will certainly let this one
run until it finishes. Our test came to an end here. It took 5 minutes and 56 seconds
to finish. It generated some errors
that are more than expected, this is normal. In this specific case of
Moving Average Crossover EA it results an error every time
it tries to execute a combination, where the “Fast Average” period
is bigger than the “Slow Average” period. So every time this happens,
it will ignore the test and generate an error. This is more than expected,
there is no problem at all. If we look at the optimization results, our best result was a little higher
than R$ 6.000,00 for the period, which was using
a “Fast Average” of 49 periods, a “Slow Average” of 84 periods, both using “Linear Weighted Average”
calculation mode, running in 1 minute. Here we can double click
the best result. And, with that,
we can watch our EA running. Let me speed it up.
Let’s take a look. As soon as it crosses the averages up,
a purchase will be made. A purchase was made. Here it is using exactly
the best result’s setting. If we watch it until the end, we will get that result that was
a little higher than $ 6.000,00. But we don’t need to watch it run,
we already know it, we can turn off
the “Preview” mode. I’ll double click on our
best result once again. Here it is generating the chart. And here it is generating the results. Here we have the same $ 6.000,00
we had seen on the results tab. If any of these statistics
does not please me, or any other reason I think
this strategy doesn’t suit me, I can go back
to the optimization results and select another strategy,
for example, this one. So here I have the results
of this new strategy. Let’s say this one pleased me.
I want to use this strategy. I’ll come back here
to the input parameters. Due to the fact that I have
double clicked and ran it, the parameters have already been altered,
according to the selected strategy. So that strategy running in 1 minute, with a 47 period in the “Fast Average”
and 81 in the “Slow Average”, with the “Average”
set as “Linear Weighted”. So let’s take a look
on what we can do on this screen. We can right-click
and choose “Save Version”. So we save this setting. We can right-click
and choose “Default” to go back
to factory default settings. Here the “Fast Average” is 12,
“Slow Average” is 26, the “Type of Average”
is “Exponential”. Everything like it comes
by default in this EA. We can right-click
and choose “Load Version”. So here is the version
we have saved, with the date and time
it was saved. So I’ll click here. The values from the setting we generated
were loaded again. And besides being very useful,
this “Load Version” and “Delete Version”, we can’t give a name
to our setting. We can’t export
somewhere else either. So it is interesting
using the “Save” option. In “Save” we’ll give a name
to our setting, I’ll call it “Test”. “Save”. And we can use “Open”
to open this new setting. Now that we have the setting saved,
we can open the chart, drag the EA to the chart,
click “Open” here, go to the folder where we
saved the file. It was… right here. And done,
we have loaded our settings and our EA is running
with the desired setting. This video’s goal
was to demonstrate the process, the mechanics of the optimization. Nothing that was done here
was a suggestion. Quite the opposite, most things I did here
are serious mistakes. For example, testing a moving average
with a period from 1 to 100 is a huge mistake. It means we don’t know
what we are looking for. An average of 1 period is really different
from 100 periods. So, from now on, I recommend that you take a look
at the Robustness Test. For those who are new in the market,
everything will seem very complicated, very complex. But, still, I think it is important
to take a look. See you next video.
Regards.

16 Replies to “Metatrader 5: Backtest e Otimização de Robô Trader | MT5”

  1. obrigado pelo ajuda, um amigo que indicou e falou muito bem de vc amigo, parabens pelo trabalho esta se tornando realmente referencia aqui em minas gerais

  2. Bom dia amigo ! e parabens pelo trabalho. Uma duvida quando entro no parametro de de entrada e configuro alguns parametros no robo e saio e faço o backtest e dps q volto o parametro esta todo resetado devo configurar tudo de novo isso è normal ? assim fica complicado

  3. A velocidade que o seu Backtest executa no vídeo é real ou está acelerado para ficar legal no vídeo?
    O meu roda em velocidade normal, exemplo, tempo grafico de M5, leva 5 minutos cada candle no Back test
    Por favor me ajude!

  4. Devo estar fazendo algo errado, pois quando abro a aba de parâmetros de entrada não aparece as opções para que eu possa modifica-las. Alguem pode me ajudar?

  5. a seleção do indicadores resetam ,eu seleciono mas quando eu clico na configuração para fazer o teste a seleção q eu fiz nos indicadores somem e nao acontece nada !

  6. no meu meta tarder 5, e, parâmetros de entrada não aparece este monte de op~]ao de configura, como eu faço pra ver isso tudo:

  7. Vilela eu tenho uma dúvida, por que utilizar OHLC por 1 minuto? É o que reflete melhor o mercado real? Quando faço back teste com Tick em vez de OHLC da um resultado diferente, devo me preocoupar?

  8. Vilela esse robosq vc fala é são aqueles indicadores q tem no metatrade? quais indicadores vc usa quando vai analisa os graficos? vc tem aulas explicando cada um desses indicadores?

  9. Ola Boa noite. Estou começando agora com o metatrader e quando eu abro o meu metatrader na conta demo da XP. O meu testador de estratégia não me traz exatamente como no vídeo, no item de parâmetros de entrada. Alguem pode me ajudar a como resolver esta questão?

  10. estou tentando testar o robo de cruzamento de medias, primeiro que nao localizo em lugar nenhum, quais os campos posso parametrizar e o que cada um desses campos representa, por exemplo, em qual campo é a media lenta, em qual a media rapida, qual o campo do grafico etc etc etc,,,, mesmo com esta dificuldade de parametrizar, tento iniciar o teste e aparece apenas uma tela vilela one a esquerda do grafico, com varios botoes e nenhum funcional,,,, alguem pode me ajudar, ou me passar link onde eu possa ver mais detalhadamente o robo de cruzamento de medias, seus parametros e formas de preenche-los ????

Leave a Reply

Your email address will not be published. Required fields are marked *