MT5 optimization giving unexpected result

 
I have created a basic Expert Advisor which triggers a buy signal each time the short term EMA crosses above the long term EMA, when the buy is triggered a trailing stop loss at 2*ATR is set which gets updated each time a new candlestick is formed. I tested this strategy on EURUSD H1 timeframe. When I optimized the expert advisor for best settings, I ran it on the period 1st August 2023 to 1st November 2023, the output came gave the best results where the short term EMA period > long term EMA period which is unconventional. I have attached the code for EA and the optimization result. Can anyone help if I am missing anything?
 
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 

Usually, one would put in a condition in the EA to reject inputs where the "fast" period would be longer than the "slow" period, to prevent such cases from being included in the optimisations.

However, in this case due to not having that filter, what was discovered was simply that the strategy does better at "fading" then by following the crossover trend. By "fading", I mean trading in the opposite direction of the crossover trend, which is essentially what happens when you switch the fast and slow periods.

However, this realisation could be misleading and depends on what modelling was used for the testing. So run it again, using "Every tick based on real ticks", should that not have been the case for these results.

EDIT: On a side note, in order for optimisation results to have some statistical significance, make sure that your test period has at least 100 trades or so. Otherwise, your optimisation results may just be a "fluke".

How to choose a sample size (for the statistically challenged)
How to choose a sample size (for the statistically challenged)
  • Piroska Bisits Bullen
  • tools4dev.org
One of the most common questions I get asked by people doing surveys in international development is “how big should my sample size be?”. While there are many sample size calculators and statistical guides available, those who never did statistics at university (or have forgotten it all) may find them intimidating or difficult to use. If this...
 
Fernando Carreiro #:

Usually, one would put in a condition in the EA to reject inputs where the "fast" period would be longer than the "slow" period, to prevent such cases from being included in the optimisations.

However, in this case due to not having that filter, what was discovered was simply that the strategy does better at "fading" then by following the crossover trend. By "fading", I mean trading in the opposite direction of the crossover trend, which is essentially what happens when you switch the fast and slow periods.

However, this realisation could be misleading and depends on what modelling was used for the testing. So run it again, using "Every tick based on real ticks", should that not have been the case for these results.

EDIT: On a side note, in order for optimisation results to have some statistical significance, make sure that your test period has at least 100 trades or so. Otherwise, your optimisation results may just be a "fluke".

Hi Fernando Carreiro, Thank you for the response, I just started coding EAs and am learning how to automate strategies. I will try to optimize the EA on a bigger dataset and include a minimum of 100 trades with recommended settings.


Additionally, please let me know how to add the condition to exclude cases where fast EMA > slow EMA during optimization. Do I need to add some condition in the .mq5 file only or do I need to add it somewhere else for optimization.