Model 1.12 of WFO-library introduces an attention-grabbing and highly effective characteristic – a chance to pause and resume an optimization. Till now WFO-library supported solely separate uninterrupted optimizations.
From the viewpoint of EA developer, new mode will be enabled by new flag WFO_FLAG_RESUME_OPTIMIZATION (32) handed to the operate wfo_setAdvancedOptions. However other than the coding, the characteristic requires some particular process for correct optimization setup. Right here I will present some directions. But earlier than we start, it is vital to refresh your data on how optimization course of is organized in MetaTrader 5 technically.
Principle
The tester helps 2 sorts of optimization:
- sluggish optimization with full iteration by all mixtures of enter parameters, and
- quick optimization based mostly on genetic algorithm;
Genetics pace up the method through choice of a small a part of doable mixtures – after all the choice isn’t random however backed up with a classy logic (mendacity far past the scope of this quick publication), invented for locating quasi-optimal answer with greatest ratio of “optimality” and time spent to seek out it.
I remind you that even when you choose sluggish optimization, the tester could mechanically change to genetics if the variety of mixtures is just too massive (for instance, over 100 mln in case of utilizing 64-bit terminal).
Each optimization produces a cache file – an opt-file situated within the subfolder /tester/cache/ of your MT5 folder. The identify of the file begins with a prefix equal to EA identify and likewise consists of work image, timeframe, date vary, and a hash quantity, uniquely figuring out present “EA construct” and settings. For instance,
WalkForwardDemo.EURUSD.H1.20230101.20231026.11.EF7BF3C92D7F53F512CC1E61A72B9448.decide
What’s the “EA construct” – I will describe a couple of strains under.
Now it is vital to notice, that the cache can be utilized not solely to assessment outcomes of latest optimizations simply within the tester UI, however to pause and resume a prolonged optimization. Certainly, as quickly as you begin an optimization, the tester gathers incoming leads to the opt-file, which is adequate to proceed the method with respect to already made efforts. Chances are you’ll cease optimization at any second, after which resume it by urgent Begin at any time later, on condition that the opt-file nonetheless exists, and the hash matches “EA construct” and settings.
“EA construct” is a hash of binary ex5-file (and all binary libraries, if they’re used, corresponding to WalkForwardOptimizer.ex5). Each time you recompile EA (or change a library) the hash is modified, which invalidates the opt-file cache.
Please observe, that MetaEditor generates a singular ex5-file on account of any compilation, even when the supply code was not modified. This is part of safety towards decompilation.
Modifications within the tester settings or enter parameters may even make opt-file cache inconsistent.
Sadly, there is no such thing as a approach to uncover which ex5-file variant and settings are encoded within the hash (hash calculation is a one-way algorithm). So, it is a bit troublesome for a consumer to differentiate one opt-cache from the opposite and determine if it nonetheless corresponds for the present atmosphere, and therefore legitimate for optimization resumption. The one attribute a consumer can use for investigation is the timestamp of the opt-file (final time optimization knowledge was up to date). A consumer ought to make an observation someplace, which settings and inputs had been used for particular opt-cache (marked by particular date/time). Truly a consumer can choose caches one after the other within the tester UI from the dropdown listing with all cached optimizations, learn their settings and inputs, however UI doesn’t present the opt-file names, so if there are lots of caches for a similar EA, image, timeframe and date ranges, the consumer want in some way deduce, which hash corresponds to optimization run of his/her curiosity.
If the cache grew to become invalid (on account of adjustments in a minimum of one of many components: ex5-file, or settings, or inputs), subsequent time you begin an optimization will probably be a brand new optimization, not a continuation of a earlier one. If ex5-file was modified, the tester removes opt-file (if exists for present settings/inputs) and outputs a message about this into the log.
For genetic optimizations there exists much more attention-grabbing chance. After a genetic optimization is completed, you’ll be able to restart it and proceed the method, considering all cached outcomes, that’s not from scratch.
You may restart genetic optimization many times. Each restart will almost definitely discover an increasing number of higher outcomes, all summed up within the opt-file.
This works easily till you do not customise optimization in any peculiar means, corresponding to WFO. The issue arises from a number of points.
MQL5 API doesn’t present any means to let MQL-program know, if present optimization is a brand new one or a continuation based mostly on pre-existing opt-cache. In different phrases, neither your EA nor WFO library can mechanically detect whether or not to collect their particular knowledge (complementing customary optimization knowledge) to a brand new file or append to an previous one (if it exists). Furthermore, it is inconceivable to determine, if a sound opt-cache exists for present “EA construct” and settings. And furthermore, it is inconceivable to detect a state of optimization in line with an current opt-file: it might belong to a completed sluggish optimization (it could possibly’t be resumed), to a paused optimization (regardless of sluggish or quick, each will be resumed), or to a completed genetic – the latter will be restarted/refined. Strictly talking there are some “hacks”, which aren’t components of MQL5 API, and permitting to learn opt-files – they’re inadequate and unreliable to be used in a product for MQL5 market.
For this reason WFO library didn’t permit optimization suspension/continuation till model 1.12. The library begins amassing its particular knowledge from very starting upon every optimization begin. It implies that if a sound opt-file exists, it ought to have been eliminated manually by a consumer earlier than new optimization.
Observe
Ranging from model 1.12 it’s possible you’ll set the flag WFO_FLAG_RESUME_OPTIMIZATION in your code. Consequently WFO library will preserve current WFO knowledge throughout optimization begin and append new passes into it, that’s in sync with filling opt-file by the tester.
To allow/disable the flag one can use the next method:
#embody <WalkForwardOptimizer.mqh> sinput ulong wfo_advancedOptions = 0; int OnInit() { ... wfo_setAdvancedOptions(wfo_advancedOptions); } void OnTesterInit() { ... wfo_setAdvancedOptions(wfo_advancedOptions); }
The operate wfo_setAdvancedOptions is named twice in OnInit and OnTesterInit for generality right here, as a result of completely different flags are utilized in completely different contexts: a part of the flags are relevant on the brokers, and a part of the flags – within the terminal (see https://www.mql5.com/en/blogs/submit/754712).
Particularly the flag WFO_FLAG_RESUME_OPTIMIZATION is smart in OnTesterInit solely.
Remember that altering wfo_advancedOptions enter itself adjustments the hash and invalidates opt-cache (if it exists). Watch out:
- while you change 0 to 32 in wfo_advancedOptions, ensure there is no such thing as a WFO-related recordsdata, as a result of the tester ought to usually create a brand new opt-file;
- but if you happen to did already use 32 some day in the past, an opt-file corresponding to those inputs should still exist – then it’s good to ensure (verify your aspect notes, for instance) that correct WFO-related recordsdata do additionally exist;
- while you change 32 to 0 in wfo_advancedOptions, ensure there is no such thing as a an opt-file appropriate for continuation, as a result of WFO-files can be deleted;
In case you are completely positive that you simply at all times wish to proceed optimization (that’s WFO-related recordsdata, corresponding to csv and gvf, in the event that they exist, correspond to an opt-file eligible for continuation), then you’ll be able to hardcode the brand new mode within the following means:
#embody <WalkForwardOptimizer.mqh> void OnTesterInit() { ... wfo_setAdvancedOptions(WFO_FLAG_RESUME_OPTIMIZATION); }
It is also secure method, if there is no such thing as a caches in any respect (neither an acceptable opt-file, nor WFO-related recordsdata) – it is best to verify this (or clear up) manually earlier than optimization.
The brand new mode is particularly helpful for a number of successive runs of genetic optimizations. As a result of genetic optimization selects very restricted variety of passes from total optimization house, it produces WFO-reports with a variety of holes in ahead steps (keep in mind – steps are organized by increments of wfo_stepOffset, and genetics could take into account to probe solely a small a part of the steps).
By restarting genetic optimization an increasing number of occasions, you may more and more fill within the gaps in WFO-report.