Welcome Forex EA downloads & MT4/MT5 auto-trading resources — EAs, Gold EAs, quant tools and real-world automation.
Sign In Sign Up

Pseudo indicator with asynchronous multi-threaded calculation demonstration - MetaTrader 5 script | Forex indicator download - MT4/MT5 resource

author EAcpu | 5 reads | 0 comments |

Pseudo-Indicator with Asynchronous Multi-Threaded Calculations Demo - indicator for MetaTrader 5

Pseudo-Indicator with Asynchronous Multi-Threaded Calculations Demo - indicator for MetaTrader 5

This indicator is a pseudo indicator without a buffer. Its sole purpose is to demonstrate multi-threaded computing techniques. The indicator can issue commands for lengthy calculations, which are performed in separate threads and are created for chart objects by working expert advisors. The class provided in the included header file - Multithreaded Object Worker.mqh - will automatically create chart objects and apply the template with the specified worker expert advisor in each object. After the calculation is completed, the object is automatically removed from the diagram.

This example is intended for users with a programming background.

The idea came from talking about multi-threaded asynchronous web requests . Please find in this article all the technical details on how to dispatch messages (events) and how to use resources to pass data between charts.

The indicator is implemented using more "compact" techniques than in this article by using chart objects instead of full-blown chart windows. You may find detailed information about this forum .

Make sure the worker expert adviser - multi-threaded object worker - compiles - multi-threaded indicator before compiling and running the indicator itself.

Each pseudo-task calculates a bunch of sqrt functions (for demo purposes only). This can be changed in your MQL projects to any other time-consuming algorithms.

To start parallel calculations after the indicator is attached to the chart, click on the chart. All important stages of the process (in the client terminal and in the working MQL5 program) are logged.

A client MQL program issuing parallel calculation requests can be not only an indicator, but also an expert advisor. Scripts and services are not supported on this platform.

Using the header file MultiThreadedObjectWorker.mqh you should implement your own data marshalling between the MQL5 client and the worker Expert Advisor.

Here are 2 examples of logs.

Pseudo-Indicator with Asynchronous Multi-Threaded Calculations Demo - indicator for MetaTrader 5

Execute 4 tasks on 2 cores (2 threads in parallel)

When running 4 tasks on 2 cores, with 2 parallel threads, they execute twice as fast as on 1 core (compare 3354 ms above to 6240 ms below).

Pseudo-Indicator with Asynchronous Multi-Threaded Calculations Demo - indicator for MetaTrader 5

Execute 4 tasks on 1 core (single thread - standard MQL program)

More cores - more speed.


Attachment download

📎 multithreadedindicator.mq5 (3.93 KB)

📎 multithreadedobjectworker.mqh (18.27 KB)

📎 custommultithreadedexample.mqh (1.1 KB)

📎 typetobytes.mqh (20.54 KB)

📎 resourcedata.mqh (1.71 KB)

📎 resource.mqh (0.64 KB)

📎 convert.mqh (3.42 KB)

📎 expert.mqh (13.27 KB)

📎 multithreadedobjectworker.mq5 (2.24 KB)

Source: MQL5 #27644

Verification code Refresh