Optimization results are not matching with results from a single test run - page 2

 

I have found same problem. I tested and the it seems to be missing ticks:

https://www.mql5.com/en/forum/360252

Bug on Tester Build 2747 - Missing ticks
Bug on Tester Build 2747 - Missing ticks
  • 2021.01.14
  • www.mql5.com
While running this minimal piece of code, I have different results on tester...
 

I was having the same issue when optimizing symbol WIN$D in Brazilian market, which has tick size of 5.

But it was intermittent. At the result table, some results when I ran single test, the output was the same, some were different. Sometimes they were all correct.

I did several fixes but none worked, like deleting some cache files, only using local farm, or only local processor, checking my code for uninitialized variables or structures, etc.

Until I saw on test log that some tests had this error of invalid price, due to price no multiple of 5 (tick size):

failed exchange buy 5 WIN$D at 136712, close #2 sell 5 WIN$D.15R 136500 [Invalid price]

The weird part was this didn't happen all the times, considering same symbol, same EA, same parameters. I don't understand why sometimes MT5 sends correct price and other times don't. Testing with market closed, in history, with no prices changing. So seems like a bug to me.

Well, the fix was to adjust the price to me multiple of tick size, to use in all calls, like position open, position modify, etc. A function as simple as this:

double AdjustPriceTick(double price)
{
   double tick_size = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE);
   return MathRound(price/tick_size)*tick_size;
}
  

I always used this fix so I didn't understand why this issue was happening. I use CTrade class, so all prices in Buy, BuyLimit, BuyStop, Sell, SellLimit, SellStop, PositionModify functions I pass it adjusted.

The problem was on PositionClose function, which doesn't have a price argument, and it was sending orders with incorrect price.

So to fix it, I copied Trade/Trade.mqh to Include folder with a different name and modified PositionClose function to adjust price to close positions.

And DONE, no more errors or different results!!