| [← Utils](../README.md)
|
|
|
| # Utils/SetFiles
|
|
|
| Programmatic generation and editing of MetaTrader `.set` files (the parameter files used to run/optimize EAs). Main class: `TSN::CSetFile`.
|
|
|
| ## Main features
|
| - **Build**: create a `.set` from scratch with typed parameters: string, number, real, bool, enum, datetime, color (`AddParamLine*`). Giving `start/step/stop` makes the parameter optimizable.
|
| - **Load**: read an existing one from a file (`InitByFile`) or from a string / `#resource` (`InitBySrc`).
|
| - **Modify**: change values (`ModifyParamValue*`), optimization ranges (`ModifyParamOpt*`) and names (`ModifyParamName`).
|
| - **Read**: get a parameter back as a typed value (`ReadParamValueString/Bool/Datetime/Color`) or as a raw `SetFileParam` (`GetParam`).
|
| - **Insert / remove**: add or delete lines at a given position (`InsertParamLine*`, `RemoveLine`) and keep free-text lines such as comments (`AddNoParamsLine`, `InsertNoParamLine`, `ModifyNoParamLine`).
|
| - **Export**: write to a file (`Save`) or to a string / string array (`GetAsString`, `GetAsStringArray`).
|
| - **Debug**: `Imprimir`, `Summary` and the `Imprimir*Array` helpers.
|
|
|
| ## Basic usage
|
|
|
| ### Create from scratch and save
|
| ```mql5
|
| void OnStart()
|
| {
|
| TSN::CSetFile sf;
|
| sf.Init("BotSimple"); // expert name, writes the .set header
|
|
|
| sf.AddParamLineString("InpName", "Asia");
|
| sf.AddParamLineNumber("InpMagic", 12345, 10000, 1, 99999); // value, start, step, stop
|
| sf.AddParamLineReal("InpRisk", 1.0, 2, 0.1, 0.1, 10.0); // value, precision, start, step, stop
|
| sf.AddParamLineBool("InpUseFilter", true);
|
| sf.AddParamLineDatetime("InpStart", D'2024.01.01', D'2020.01.01', 86400, D'2025.12.31'); // step in seconds
|
| sf.AddParamLineColor("InpLineColor", clrRed);
|
|
|
| sf.Save("MQLArticles\\BotSimple.set", FILE_COMMON);
|
| }
|
| ```
|
|
|
| ### Load, modify, save
|
| ```mql5
|
| TSN::CSetFile sf;
|
| sf.InitByFile("MQLArticles\\BotSimple.set", FILE_COMMON, 16); // file, flags, reserve
|
|
|
| sf.ModifyParamValueNumber("InpMagic", 999);
|
| sf.ModifyParamValueRealNumber("InpRisk", 5.5, 2);
|
| sf.ModifyParamValueBoolean("InpUseFilter", false);
|
| sf.ModifyParamOptNumberStop("InpMagic", 5000); // change the optimization range
|
| sf.ModifyParamOptDatetimeStep("InpStart", 3600);
|
|
|
| if(sf.ParamExist("InpUseFilter"))
|
| Print(sf.ReadParamValueBool("InpUseFilter"), " ", sf.GetParam("InpMagic").value); // GetParam returns the raw strings
|
|
|
| sf.Save("MQLArticles\\BotSimple_v2.set", FILE_COMMON);
|
| ```
|
|
|
| ### Load from a `#resource`
|
| ```mql5
|
| #resource "SetFile.set" as const string set_file
|
|
|
| TSN::CSetFile sf;
|
| sf.InitBySrc(set_file, '\n', 16); // source text, line separator, reserve
|
| sf.Summary();
|
| ```
|
|
|
| ### Insert and remove lines
|
| ```mql5
|
| sf.InsertParamLineBool(5, "InpDebug", true); // idx = line index in the FILE
|
| sf.AddNoParamsLine("; -------| Risk |-------");
|
| sf.RemoveLine(4); // idx also refers to the file line
|
| ```
|
|
|
| ## Good to know
|
| - Line indexes (`InsertParamLine*`, `RemoveLine`, `GetLine`) refer to **lines of the file**, header lines included. Right after `Init` the header takes the first 4 lines, so the first parameter is at index 4.
|
| - Parameters are looked up by name through a hash map: `ParamExist`, `GetParamIdx`.
|
| - Methods return `false` on failure. Add `sf.AddLogFlags(LOG_ALL)` to see why.
|
| - Working examples live in [`Test/`](./Test): `Init`, `CustomCreation`, `LoadSrc`, `Modify` and `Remove`.
|
|
|
| > Only the most representative methods are shown here.
|