Features of the mql5 language, subtleties and tricks - page 298

 
amrali #:
The STR macro with indirection can be used to debug (print) macro expansion to the logs:
#define PRINT(A) Print(#A, " = ", A)

Do you know how to use it to print any amount of different variables to the logs this way, but without typing PRINT for each variable?

e.g. there are x1, x2, x3, x4, x5 variables

I want to quickly print them all with PRINT, but I don't want to type this:

Print(PRINT(x1),", ",PRINT(x2),", ",PRINT(x3),", ",PRINT(x4),", ",PRINT(x5));

instead I'd like to type this, which will give me the same result as the above Print:

prt(x1,x2,x3,x4,x5)

and I'd like this macro to work with any amount of variables:

prt(x1,x2,x3,x4)
 
I'll leave the answer for @fxsaber who wrote a nice macro TO_STR
 
amrali #:
nice macro TO_STR

Where can I see this macro?

 
Andrei Iakovlev #:

Where can I see this macro?

#define TOSTR(A) (#A + " = " + string(A) + " ")

// usage
Print (TOSTR(x1), TOSTR(x2), TOSTR(x3));
 
amrali #:

ahh, it's that, ok

I only came to solution, when I create multiple macros with different amount of variables:

#define cs                      ", "	// comma + space
#define pr                      Print
#define s(x)                    string(x)
#define P(x)                    #x + " = " + s(x)
#define P2(x1,x2)               P(x1),cs,P(x2)
#define P3(x1,x2,x3)            P(x1),cs,P(x2),cs,P(x3)
#define P4(x1,x2,x3,x4)         P(x1),cs,P(x2),cs,P(x3),cs,P(x4)
#define P5(x1,x2,x3,x4,x5)      P(x1),cs,P(x2),cs,P(x3),cs,P(x4),cs,P(x5)
#define PR(x)                   pr(P(x));
#define PR2(x1,x2)              pr(P2(x1,x2));
#define PR3(x1,x2,x3)           pr(P3(x1,x2,x3));
#define PR4(x1,x2,x3,x4)        pr(P4(x1,x2,x3,x4));
#define PR5(x1,x2,x3,x4,x5)     pr(P5(x1,x2,x3,x4,x5));

So if I want to quickly print 5 variables I do:

PR5(x1,x2,x3,x4,x5)

If I want to print 9 variables I combine:

pr(P4(x1,x2,x3,x4),cs,P5(x5,x6,x7,x8,x9));

So I don't know how to create universal macro to print any amount of variables with P(x) macro way so it would be like PR5 for instance.

 
If there is a sample code how to make api query, for example, to binance, please share.
 
Andrei Sokolov #:
If there is a code example of how to make an api request, for example, to binance, please share it.

The moderator once wrote that only in the market you can find something working with bybit.(https://www.mql5.com/en/forum/440740)

You can do it yourself, it is done via WebSocket. If someone will post an example (for example me) - all intrigue will stop. In general - it is not difficult, it uses webquest.

https://www.mql5.com/ru/articles/8196

The second option is to take a Python code for bybit, and slightly modify it (DeepSeek and ChatGPT can cope with this) so that it directs the entire stream received from bybit to the socket of the local PC. Where to read it from in the EA is not difficult at all if you use examples from the articles. IMHO

WebSocket для MetaTrader 5
WebSocket для MetaTrader 5
  • www.mql5.com
До появления сетевых функций в обновленном MQL5 API, приложения MetaTrader были ограничены в возможности подключаться и взаимодействовать с сервисами на основе протокола WebSocket. Сейчас ситуация изменилась. В этой статье мы рассмотрим реализацию библиотеки WebSocket на чистом MQL5. Будут представлены краткое описание протокола WebSocket и пошаговое руководство по использованию полученной библиотеки.
 

surprisingly this macro works

#define del(x)  /*x*/
 
Andrei Iakovlev #:

surprisingly this macro works

And old bug and strange behavior of MQL preprocessor unlike the C language preprocessor which properly deletes all code comments before any macro expansion takes place.
 
Andrei Iakovlev #:

ahh, it's that, ok

I only came to solution, when I create multiple macros with different amount of variables:

So if I want to quickly print 5 variables I do:

If I want to print 9 variables I combine:

So I don't know how to create universal macro to print any amount of variables with P(x) macro way so it would be like PR5 for instance.

seems like it's impossible to create such macro to deal with any amount of variables, like Print function does