Discussion of article "DIY multi-threaded asynchronous MQL5 WebRequest"

 

New article DIY multi-threaded asynchronous MQL5 WebRequest has been published:

The article describes the library allowing you to increase the efficiency of working with HTTP requests in MQL5. Execution of WebRequest in non-blocking mode is implemented in additional threads that use auxiliary charts and Expert Advisors, exchanging custom events and reading shared resources. The source codes are applied as well.

Implementation of trading algorithms often requires analyzing data from various external sources, including Internet. MQL5 provides the WebRequest function for sending HTTP requests to the "outside world", but, unfortunately, it has one noticeable drawback. The function is synchronous meaning it blocks the EA operation for the entire duration of a request execution. For each EA, MetaTrader 5 allocates a single thread that sequentially executes the existing API function calls in the code, as well as incoming event handlers (such as ticks, depth of market changes in BookEvent, timer, trading operations, chart events, etc.). Only one code fragment is executed at a time, while all remaining "tasks" wait for their turn in queues, till the current fragment returns control to the kernel.

For example, if an EA should process new ticks in real time and periodically check economic news on one or several websites, it is impossible to fulfill both requirements without them interfering with one other. As soon as WebRequest is executed in the code, the EA remains "frozen" on the function call string, while new tick events are skipped. Even with the ability to read skipped ticks using the CopyTicks function, the moment for making a trading decision may be missed. Here is how this situation is illustrated using the UML sequence diagram:

Event handling sequence diagram featuring the blocking code in one thread

Fig.1. Event handling sequence diagram featuring the blocking code in one thread

Author: Stanislav Korotky

 

As always, strong! If the library could be generalised, you could create a ready-made toolkit to easily create asynchrony of any functions. At the same time cross-platform.

This method could be used to create OrderSendAsync for MT4, for example.

Thanks for the article!

 
fxsaber:

As always, strong! If the library could be generalised, you could create a ready-made toolkit to easily create asynchrony of any functions. At the same time cross-platform.

Using this method you could create OrderSendAsync for MT4, for example.

Yes, I started to make a group order management system for MT4 with plugin commands on messages once a long time ago, but it wasn't finished. I haven't checked how resources work in MT4, although judging by the documentation it should be like in MT5.

 
Stanislav Korotky:

I haven't checked how resources work in MT4, although according to the documentation it should be like in MT5.

Yes, everything is like in MT5. Only in the current MT4 build there is a bug that makes exchange impossible

Forum on trading, automated trading systems and testing trading strategies

Bugs, bugs, questions

fxsaber, 2018.09.17 18:38

ResourceReadImage in MT4 with such a bug that it is impossible to read the resource
#property strict

class RESOURCE
{
public:
  const string Name;

  RESOURCE( const string sName = __FILE__ ) : Name("::" + sName )
  {
  }

  ~RESOURCE( void)
  {
    ::ResourceFree(this.Name);
  }

  virtual bool Set( const uint &Data[], const uint Width = 1, const ENUM_COLOR_FORMAT ColorFormat = COLOR_FORMAT_XRGB_NOALPHA ) const
  {
    return(::ResourceCreate(this.Name, Data, Width, (Width == 0) ? ::ArraySize(Data) : ::ArraySize(Data) / Width, 0, 0, Width, ColorFormat));
  }

  int Get( uint &Data[] ) const
  {
    uint Width;
    uint Height;

    return(::ResourceReadImage(this.Name, Data, Width, Height) ? ::ArraySize(Data) : 0);
  }
};

void OnStart()
{
  RESOURCE Resource;
  
  uint DataIn[] = {0};  
  Resource.Set(DataIn);
   
  uint DataOut[];
  Resource.Get(DataOut);
  
  Print(DataOut[0]); // MT5x64 (build 1881) - 0, MT4 (build 1126) - 4278190100 (random values)
}


ResourceSave writes correctly, but ResourceReadImage is one big bug. Is it possible to fix it? I haven't checked it in MT5x32....


Maybe this method will help to make resource reading correct in MT4. I haven't tested it.

 

By the way, you can do without full-fledged charts - OBJ_CHART. Scripts are loaded on them. And WebRequest and OrderSend work there.

I gave an example of how to make an indicator trade without additional charts.

Thus, you can use iCustom-indicator as a manager, and it will launch WebRequest in its script for each WebRequestAsync.

The design should be simpler and more reliable, because there will be no additional windows.

 
fxsaber:

By the way, you can do without full-fledged charts - OBJ_CHART. Scripts are loaded on them. And WebRequest and OrderSend work there.

I gave an example of how to make an indicator trade without additional charts.

Thus, you can use iCustom-indicator as a manager, and it will run WebRequestAsync in its script for each WebRequestAsync.

The design should be simpler and more reliable, because there will be no additional windows.

can you link to this example ?

 
Maxim Kuznetsov:

can I have a link to this example?

Here and here.


ZY The user will probably be surprised if the Market Indicator starts trading....

 
fxsaber:

Here and here.


ZY Probably, the user will be surprised if Market-indicator starts trading...

jeez, it's just a transfer of events.... I thought :-)

 
Maxim Kuznetsov:

shit, it's just an event.... I thought :-)

There's no event.

 
fxsaber:

There's no event.

It's the script that executes the orders.

and how it gets the orders is another matter.

 
Maxim Kuznetsov:

the script executes the orders.

and how it receives orders is another matter.

The script does not receive orders, it is triggered by the indicator in any quantity.