No trades open during strategy optimization

 

Hi,

I created an EA which makes trades if some conditions are satisfied and I'd like to optimize two parameters - the SL (in pips) and the risk-to-reward ratio - which do not affect those conditions.

However, when I perform backtest, I see that for most of the combinations no trades are actually opened, as can be seen from the first of the attached figures.

This is unexpected since, as I said, the opening of a particular position is independent of the quantities I'm trying to optimize.

In addition, if I try one of those combinations (e.g., the first one) on a single backtest without optimization, the trades are correctly opened, and I get the result shown in the second attached figure.

Why does that happen?

I got the same result with different optimization strategies and modelling (i.e., every tick, 1m OHLC, etc.).

Thanks,

Alessandro

The Fundamentals of Testing in MetaTrader 5
The Fundamentals of Testing in MetaTrader 5
  • www.mql5.com
What are the differences between the three modes of testing in MetaTrader 5, and what should be particularly looked for? How does the testing of an EA, trading simultaneously on multiple instruments, take place? When and how are the indicator values calculated during testing, and how are the events handled? How to synchronize the bars from different instruments during testing in an "open prices only" mode? This article aims to provide answers to these and many other questions.
Files:
img_1.png  38 kb
img_2.png  17 kb
 

What are you expecting as answer with the information provided ?

Fix your code.

 
Alain Verleyen #:

What are you expected as answer with the information provided ?

Fix your code.

Clever, useful and polite answer.

Since I exposed a problem in a (I think) clear way, contextualizing it and giving details and proofs of what I am saying, and since replying to posts is not mandatory, if you don't have time or you don't find a question useful for the community, you can simply avoid giving arrogant answers.
 

Do you really expect an answer? There are no mind readers here and our crystal balls are cracked. Always post all relevant code (using Code button) or attach the source file.

We can't see your broken code.

Fix your broken code.

With the information you've provided — we can only guess. And you haven't provided any useful information for that.

 
Alessandro Davoli: I created an EA which makes trades if some conditions are satisfied and I'd like to optimize two parameters - the SL (in pips) and the risk-to-reward ratio - which do not affect those conditions. However, when I perform backtest, I see that for most of the combinations no trades are actually opened, as can be seen from the first of the attached figures. This is unexpected since, as I said, the opening of a particular position is independent of the quantities I'm trying to optimize. In addition, if I try one of those combinations (e.g., the first one) on a single backtest without optimization, the trades are correctly opened, and I get the result shown in the second attached figure. Why does that happen? I got the same result with different optimization strategies and modelling (i.e., every tick, 1m OHLC, etc.).

One common cause for optimisation passes to be different from individual back-test runs, is globally scoped variables that are not properly initialised in functions and only initialised in their global declarations.

The reason is that the EA's global variables are not reinitialised based on their declarations on every new optimisation pass (I don't know if this is a bug or intentional, but it is what happens).

So, make sure you initialise all your globally scoped variables in the OnInit() event handler if they are not initialised elsewhere in other functions

However, that is just one possible cause. It is only a guess. If you want an informed answer, then you will have to provide the actual source code of the EA that has this issue.

 
Fernando Carreiro #:

One common cause for optimisation passes to be different from individual back-test runs, is globally scoped variables that are not properly initialised in functions and only initialised in their global declarations.

The reason is that the EA's global variables are not reinitialised based on their declarations on every new optimisation pass (I don't know if this is a bug or intentional, but it is what happens).

So, make sure you initialise all your globally scoped variables in the OnInit() event handler if they are not initialised elsewhere in other functions

However, that is just one possible cause. It is only a guess. If you want an informed answer, then you will have to provide the actual source code of the EA that has this issue.

Thanks a lot Fernando,

that was precisely the cause.

It solved my problem, thanks!

 
William Roeder #:

Do you really expect an answer? There are no mind readers here and our crystal balls are cracked. Always post all relevant code (using Code button) or attach the source file.

We can't see your broken code.

Fix your broken code.

With the information you've provided — we can only guess. And you haven't provided any useful information for that.

I didn't expect anything but advice or comments from people more expert than me in the field and this is exactly what happened with Fernando, well, look at that.

Enjoy repairing your crystal balls.

 
Fernando Carreiro #:

One common cause for optimisation passes to be different from individual back-test runs, is globally scoped variables that are not properly initialised in functions and only initialised in their global declarations.

The reason is that the EA's global variables are not reinitialised based on their declarations on every new optimisation pass (I don't know if this is a bug or intentional, but it is what happens).

So, make sure you initialise all your globally scoped variables in the OnInit() event handler if they are not initialised elsewhere in other functions

However, that is just one possible cause. It is only a guess. If you want an informed answer, then you will have to provide the actual source code of the EA that has this issue.

Do I understand correctly ?

You are saying that the above code when optimized (custom max), should (could ?) return different values ?

input int SomethingToOptimize = 0;
int       AGlobalVariable     = 17;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   AGlobalVariable++;
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Tester function                                                  |
//+------------------------------------------------------------------+
double OnTester()
  {
//---
   return(AGlobalVariable);
  }

If yes, in what circumstances ? Because I can't reproduce it.

Maybe I misunderstood your point.

 
Enrique Dangeroux #:

Fernando means this:


It's not what he wrote though :

is globally scoped variables that are not properly initialised in functions and only initialised in their global declarations.

We will see.

 

It is not what he meant indeed, hence the reason for me to delete my post. You are fast and beat me to it.


 

 
Alessandro Davoli #: Thanks a lot Fernando, that was precisely the cause. It solved my problem, thanks!

You are welcome!