Watch how to download trading robots for free

Interesting script?
So post a link to it -
let others appraise it

You liked the script? Try it in the MetaTrader 5 terminal


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

Stanislav Korotky

votes: 10
2020.01.16 20:13
TypeToBytes.mqh (20.54 KB)view
Resource.mqh (0.64 KB)view
Convert.mqh (3.42 KB)view
Expert.mqh (13.27 KB)view

This indicator is a pseudo-indicator w/o buffers. Its only purpose is to demonstrate multi-threaded calculations technique. The indicator can issue commands for lenghty calculations which are performed in separate threads, created for chart objects with worker expert adviser. The classes provided in the included header file - MultiThreadedObjectWorker.mqh - will automatically create the chart objects and apply template with specified worker expert adviser in each object. After calculation is done the object is automatically removed from the chart.

This example is intended for users with programming background.

The idea comes from the article about Multi-Threaded Asynchronous WebRequests. Please, find all technical details on how the messages (events) are dispatched, and how resources are used to pass data between charts in the article.

The indicator utilizes more "compact" implementation of the techique than in the article by using chart objects instead of full-fledged chart windows. You may find details on the forum.

Make sure the worker expert adviser - MultiThreadedObjectWorker - is compiled before you compile and run the indicator itself - MultiThreadedIndicator.

Input parameters

  • sinput int _Cores = 1; - number of threads to utilize (don't use more than available physical cores)
  • sinput int _Tasks = 1; - number of pseudo-tasks to execute in parallel (use number larger than number of cores to see parallel performance)
  • input string MultiThreadedScriptPath = "Experts\\MultiThreadedObjectWorker.ex5"; - must indicate the pathname of the worker expert adviser

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 a chart, please, click the chart. All important phases of the process (both in client and worker MQL5 programs) are logged.

The client MQL programm issuing parallel calculations requests can be not indicators only but expert advisers as well. Scripts and services are not supported by the platrform.

Using the header file MultiThreadedObjectWorker.mqh you should implement your own data marshalling between MQL5-client and the worker expert adviser.

Here is 2 examples of the logs.

Execution of 4 tasks on 2 cores (2 threads in parallel)

Execution of 4 tasks on 2 cores (2 threads in parallel)

When running 4 tasks on 2 cores there are 2 parallel threads, that provides twice as fast execution as on 1 cores (compare 3354ms above and 6240ms below).

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

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

More cores - more speed.

    Price in polar coordinate system Price in polar coordinate system

    Just for experiment and to demonstrate the possibilities of Canvas. The price is drawn in the polar coordinate system by winding circles.

    Cross Cross

    Simple code for Candle Cross above or below Conditions..

    3D Spiral Quotes 3D Spiral Quotes

    Demonstration of Canvas's capabilities using 3D spiral quotes as an example.

    CHistoryPositionInfo Class CHistoryPositionInfo Class

    A class for easy access to the closed position properties.