If these articles become a series, it will be just great!!!!
How much I missed such articles about four years ago when I started learning to write code.
And now I still find things in them that I didn't know yet.
Especially the previous one about StringFormat and PrintFormat is very informative, considering that the help on these functions is very poor.
Thanks to the author for interesting and necessary articles!
- www.mql5.com
Files are not attached to the article, as it was conceived as a kind of extended reference information. You can simply use Copy-Paste codes from the article in the MetaEditor code editor.
Forum on trading, automated trading systems and testing trading strategies
Vladimir Karputov, 2016.10.15 08:38 AM
Please write in Russian in the Russian part of the forum.Forgot it, I guess. Thanks.
//+------------------------------------------------------------------+ //| Logs the description of all fields of the MqlRates structure | //+------------------------------------------------------------------+ void MqlRatesPrint(const string symbol,const ENUM_TIMEFRAMES timeframe,const MqlRates &rates, const bool short_entry=true,const uint header_width=0,const uint indent=0,int index=WRONG_VALUE) { //--- Declare a variable to store the result string res=""; //--- Get the number of decimal places and the string value of the timeframe int dg=(int)SymbolInfoInteger(symbol,SYMBOL_DIGITS); string tf=StringSubstr(EnumToString(timeframe==PERIOD_CURRENT ? Period() : timeframe),7); string num=(index==WRONG_VALUE ? ":" : StringFormat("[%ld]:",index)); //--- If short record - output bar data to the log in Symbol TF[index] format: Time, O, H, L, C, S, V/VR if(short_entry) { res=StringFormat("%s %s%s %s, O: %.*f, H: %.*f, L: %.*f, C: %.*f, S: %2ld, V: %5lld, RV: %lld", symbol,tf,num,(string)rates.time,dg,rates.open,dg,rates.high,dg,rates.low,dg,rates.close,rates.spread,rates.tick_volume,rates.real_volume); Print(res); } /* Example output: GBPUSD H1: 2023.07.21 08:00:00, O: 1.28794, H: 1.28848, L: 1.28772, C: 1.28772, S: 4, V: 448, RV: 0 */ //--- Otherwise. else { //--- create a string describing all data of the structure with indentation and specified width of the header field res=StringFormat("%s %s%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s",symbol,tf,num, MqlRatesTime(rates,header_width,indent), MqlRatesOpen(symbol,rates,header_width,indent), MqlRatesHigh(symbol,rates,header_width,indent), MqlRatesLow(symbol,rates,header_width,indent), MqlRatesClose(symbol,rates,header_width,indent), MqlRatesTickVolume(rates,header_width,indent), MqlRatesSpread(rates,header_width,indent), MqlRatesRealVolume(rates,header_width,indent) ); //--- Output the received string to the logbook Print(res); } /* Example output (if Last is not null): Time: 2023.07.20 14:42:33 Bid: 1.28958 Ask: 1.28962 Last: 1.28947 Volume: 33 Time msc: 2023.07.20 14:42:33.401 Flags: BID|ASK Volume Real: 33.45 Example output (if Last is null): Time: 2023.07.20 14:42:33 Bid: 1.28958 Ask: 1.28962 Time msc: 2023.07.20 14:42:33.401 Flags: BID|ASK */ } //+------------------------------------------------------------------+
I propose to complete the functionality.
string GetPrintSource( const string Source );
Returns the source of the print function by the source of the structure.
string GetPrintSource( const string Source ) { return("string ToString( const STRUCT &Data ) { return(\"i = \" + (string)Data.i); }"); } void OnStart() { Print(GetPrintSource("struct STRUCT { int i; };")); }I did something similar here, but with inputs.
- www.mql5.com
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Check out the new article: Structures in MQL5 and methods for printing their data.
In this article we will look at the MqlDateTime, MqlTick, MqlRates and MqlBookInfo strutures, as well as methods for printing data from them. In order to print all the fields of a structure, there is a standard ArrayPrint() function, which displays the data contained in the array with the type of the handled structure in a convenient tabular format.
The MqlParam and MqlTradeRequest structures transmit technical information for creating indicators and sending trading requests to the server. We fill in the required fields of the structures in accordance with the required result of sending data in the completed structure. In other words, these structures do not particularly need to print the data the fields of these structures were filled with by a programmer.
But the remaining structures return query results, and each field is filled in either by the terminal subsystem or by the trade server. Obtaining data from these structures, analyzing programmatically filled fields of structures, or printing them to a log for subsequent manual analysis is very convenient and necessary both for making decisions programmatically and for understanding and finding the location of a logical error.

In order to print all the fields of a structure, there is a standard ArrayPrint() function, which displays the data contained in the array with the type of the handled structure in a convenient tabular format. But sometimes we need to print data from the structure in another format, which may be more convenient than a tabular representation. For example, we may need to display all the fields of the structure in one line - with headers and corresponding data. This may be more convenient for analyzing large amounts of data. At the same time, sometimes we need to see a more detailed view - with a description of the structure fields and a different representation of the corresponding data.Author: Artyom Trishkin