Asynchronous and multi-threaded programming in MQL

 
There is a need to write mql code in asynchronous or multithreaded mode.
I am familiar with asynchronous and multi-threaded code writing from the asyncio module of Python.

As you know, all EAs and scripts in mql are executed in a single thread.
Therefore, there is some confusion about it.
The WinAPI library ported to mql has functions for working with threads via WinAPI.
If I use these multi-threading functions in my mql code, then the mql passed functions will also be multi-threaded?
And the Expert Advisor/Script is single threaded by default, won't it hinder to use multi-threading in WinAPI?

If it is possible to use WinAPI multithreading in mql programs, please suggest me a guide or articles on how to handle these WinAPI threads in mql.
I understand the basic principle of asyncio, but lack the illustrative examples for mql.
I am aware of Microsoft's help, but I have not studied it, because I still don't have a clear understanding of the logic of using the WinAPI flow in mql, and what exactly is to be found in the help.
Point me in the right direction, if there are any examples or articles for mql, that would be awesome.
 

Try it, there's no point in guessing. If the pointers to the functions are valid, maybe something will work. Have you thought about another question - how to initialize the standard µl library for a new thread?

Examples are easy to google - "winapi multithreading". You should understand (perhaps you know it without me) that multithreaded programming is another level, you may lose your legs. I doubt that Python (though I'm 0 in Python) could reveal all sides of this activity. It's unlikely to get a decent multi-threaded non-code at random.

 
Vict:

Have you thought about another question - how to initialise the standard µl library for a new thread?

Yes, the standard WinAPI library, ported from Windows API, which interacts via standard dlls.
Accordingly the standard dll must be initialized, and for this in WinAPI also are ported functions.
The examples in C++ for dll initialization should be able to work in mql as well.
Since the initialization functions are the same from Windows API, the main thing is to find the right examples which are not outdated, that is the problem. A Google search is not always the right result.
There are a lot of professional programmers on this forum, they can show you a piece of code, like initialization, memory allocation, deinitialization, etc.
And then you have to work with threads. It's very strange that there is no article on this subject in the "Articles" section so far.

Vict:

In general, you need to understand (maybe you know it without me) - multithreaded programming is another level, you can be left without legs.
I doubt that Python (though I'm 0 in Python) could reveal all sides of this activity. It's unlikely to get a decent multi-threaded non-code at random.

What do you mean by "no legs"?
If you work properly with flows or asynchrony, there should be no disabilities.
That's the challenge, to learn how to properly use Windows API threads in mql, or async.
In Python I worked mostly with async, everything flies fine, with threads in Python because of GIL there are problems. For this reason threads in Python are better not to be used.

 
Roman:
There is a need to write mql code in asynchronous or multithreaded mode.
I am familiar with asynchronous and multi-threaded code writing from the asyncio module of Python.

As you know, all EAs and scripts in mql are executed in a single thread.
Therefore, there is some confusion about it.
The WinAPI library ported to mql has functions for working with threads via WinAPI.
If I use these multi-threading functions in my mql code, then the mql passed functions will also be multi-threaded?
And the Expert Advisor/Script is single threaded by default, won't it hinder to use multi-threading in WinAPI?

If it is possible to use WinAPI multithreading in mql programs, please suggest me a guide or articles on how to handle these WinAPI threads in mql.
I understand the basic principle of asyncio, but lack the illustrative examples for mql.
I am aware of Microsoft's help, but I have not studied it, because I still don't have a clear understanding of the logic of using the WinAPI flow in mql, and what exactly is to be found in the help.
Point me in the right direction, if there are any examples or articles for mql, that would be awesome.
mt5 has an openCL for multithreading. Look for articles on this subject here.
 

You cannot call MQL functions from a DLL.

But it is possible to run multiple threads through DLL calls, so that they can work independently from MQL5 in their environment.

For example, you prepare data from MQL and place somewhere or pass it to DLL, which in turn will work with multiple threads and return the result through an additional function.

 
Roman:

Yes, I did, the standard WinAPI library, ported from Windows API, which interacts via standard dlls.
Correspondingly, the standard dlls must be initialized, and there are ported functions in WinAPI for this purpose as well.

No, I was talking specifically about the standard µl library (you know, the trading functions there, for example, and all the others). There must be global data there, and each thread must have its own copy, your new thread via CriatTrade() will remain uninitialized properly.

What do you mean by "left without legs"?

There are a lot of rakes out there. But if you don't have a problem with a read like this https://en.cppreference.com/w/cpp/atomic/memory_order, you're ready.
 
MetaQuotes Software Corp.:

But it is possible to run multiple threads via DLL calls, so that they work independently from MQL5 in their environment.

This is what we are talking about, use the standard ported WinAPI library, it just implies working through standard Windows dlls.

Vict:

No, I was talking exactly about standard MQL5 library (e.g. trading functions and all the rest). There must be global data there and each thread must have its own copy, your new thread via CREATTrade() will remain uninitialized properly.

There are a lot of rakes in there. But if you don't have a problem with a read like this https://en.cppreference.com/w/cpp/atomic/memory_order, you're good to go.

As far as I know, when you initialise the dll, you can also initialise the exported functions that will be called from the dll to the mql program.
So this is not the main problem, the problem is how to do it? As I wrote above, perhaps C++ examples will work in mql as well, but I thought the mql code examples could help here.
So to say, to not rack my brains about portability, may be someone who already worked with WinAPI will be able to give me a hint.

p.s.
Ah, I see what you mean, but it's about the ported WinAPI, not about mql functions,
maybe the mql functions could be initialized through objects, or put in their dll and initialize as described above.

 

Something we don't understand each other.

But don't bother - you won't create a thread via CREATTrade() inside µl, you need to pass a pointer to a function (where the thread will start), but as noted above - kukush.

 

I will translate what you have already been told by a representative of the MT development company:

You cannot create threads from MQL. Not even with WinAPI.

You can only do this by writing your own DLL.


 
Roman:
There is a need to write mql code in asynchronous or multithreaded mode.

Not the right thing to do, but it's good to learn

Многопоточный асинхронный WebRequest на MQL5 своими руками
Многопоточный асинхронный WebRequest на MQL5 своими руками
  • www.mql5.com
Реализация торговых алгоритмов часто требует анализа информации из различных внешних источников, в частности из Internet. MQL5 предоставляет функцию WebRequest для отправки HTTP-запросов во "внешний мир", но она, к сожалению, обладает одним заметным недостатком. Эта функция является синхронной, а потому блокирует работу эксперта на все время...
 
Vict:

Something we don't understand about each other.

But don't get bogged down - you won't create a thread via CREATTrade() inside µl, you need to pass a pointer to a function (with which the thread will start), but as noted above - kukish.

What about this answer then?

Forum on trading, automated trading systems and strategy testing

Asynchronous and Multi-threaded Programming in MQL

MetaQuotes Software Corp., 2019.07.24 16:31

But it is possible to run multiple threads via DLL calls, so that they work independently of MQL5 in their environment.

Vict, perhaps as you described there is a problem with threads working inside mql, but as I understand calling functions from WinAPI using CriatTrade() shouldn't be a problem.
I need multithreading with WinAPI functions, not with mql functions.

Reason: