How to share information synchronously between 2 MT4 instances via File?

 

Hello,


I would like to send information for one MT4 instance to another MT4 instance in the same computer as fast as possible, an synchronously


I try this (pseudocode)


MT4 sender instance:


1) Open the file"shared" with open_flags = FILE_WRITE|FILE_READ|FILE_BIN|FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_COMMON;


2) while (true) { FileWrite (information); FileFlush(); }


MT4 receiver instance:


1) Open the file"shared" with open_flags = FILE_READ|FILE_BIN|FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_COMMON;


2) FileSeek ( handle, 0, SEEK_END);


3) while (true)  FileRead  (information); 


BUT this does not work. The receiver instance does not receive new information written by MT sender instance.


 


The only work around I found is to open/close the file in the MT4 receiver instance every time it want to read, but this is very time consuming.


Could anybody help me?

Thanks in advance,

Jose 

 

Please see this example:

https://www.mql5.com/en/code/10865

The example of using the Named Pipes in MetaTrader 4
The example of using the Named Pipes in MetaTrader 4
  • votes: 10
  • 2013.01.15
  • MetaQuotes Software Corp.
  • www.mql5.com
Many developers face the same problem - how to get to the trading terminal sandbox without using unsafe DLLs. One of the easiest and safest method is to use standard Named Pipes that work as normal file operations.
 
Moderator's example of named pipes is *much* more appropriate to the task. Another common method is to use a DLL with shared memory, but that's more of a C/C++ thing. The actual bug in your logic was missing the use of FileFlush, which is required.
 
Marco vd Heijden:

Please see this example:

https://www.mql5.com/en/code/10865

Hi Marco,

Thanks, but named pipes is a server - client architecture.

It is possible to write the server part in mql4, and run the server into a MT4 instance?

Regards,

Jose 

 
Jose:

Hi Marco,

Thanks, but named pipes is a server - client architecture.

It is possible to write the server part in mql4, and run the server into a MT4 instance?

Regards,

Jose 

No. You can't code a pipe server with mql.
 
Jose:

Hello,


I would like to send information for one MT4 instance to another MT4 instance in the same computer as fast as possible, an synchronously



Working with file(s) is working.

Of course it depends of what is "as fast as possible for you". If you need help with this approach please post real code.

 
I would recommend - even if I haven't worked with it yet - MMF.
 
Carl Schreiber:
I would recommend - even if I haven't worked with it yet - MMF.
I worked with C based Memory mapping in Python, C and C++. It is a very solid and robust shared file system. Since temporary files are often held in memory, the normal file operations will have the same speed, with an occasional stall from the I/O of commit to disk. Unless the file size is very large, that won't matter at all to trading. I did memory maps with the output of a real time driver getting 64MB data/sec, digesting it and then writing to mmap, read by python and then websocket push to Amazon Cloud. It was very fast and worked very well. In this situation, the file method and the Named Pipes (With MQL as the Server) will both work just as well.
 
Ian Smith:
I worked with C based Memory mapping in Python, C and C++. It is a very solid and robust shared file system. Since temporary files are often held in memory, the normal file operations will have the same speed, with an occasional stall from the I/O of commit to disk. Unless the file size is very large, that won't matter at all to trading. I did memory maps with the output of a real time driver getting 64MB data/sec, digesting it and then writing to mmap, read by python and then websocket push to Amazon Cloud. It was very fast and worked very well. In this situation, the file method and the Named Pipes (With MQL as the Server) will both work just as well.
Don't mislead people. mql can't work as a server for Named Pipes. You probably mean you are using DLL call from mql to create the server.
 
Alain Verleyen:

Working with file(s) is working.

Of course it depends of what is "as fast as possible for you". If you need help with this approach please post real code.

Hi Alan,

File(s) do not work for me :(

 I have made 3 tests:

1) Receiver.mq4 with read_ok() function and Sender.mq4

1-a) If File "Fichero" do not exist, test is OK

1-b) If File "Fichero"  exist, test fails. I think it will work as documented

2)  Receiver_optimus.mq4 with read_optimus() function and Sender.mq4

Test fails in both scenarios.

Please, find attached sources and ExpertLog.txt.

Thanks in advance,

Jose

EDIT: in read_optimus() FileClose is commented

 Sender.mq4

// INIT MT4 SENDING INSTANCE
int  Writer_handle = 0;
int Value = 0;

int OnInit()
{
   int open_flags = FILE_READ|FILE_WRITE|FILE_BIN|FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_COMMON;

   Writer_handle = FileOpen( "Fichero", open_flags);
   if (Writer_handle < 0) return INIT_FAILED;

   for (Value=0; Value<=1; Value++){
      FileWriteInteger (Writer_handle,Value, INT_VALUE);
      Print("Written: "+(string)Value);
   }
      
   FileFlush(Writer_handle);
  
   return 0;
}

void OnTick()
{
   Sleep(60000);
   Comment("Writing...");
   FileWriteInteger (Writer_handle, Value, INT_VALUE);
   FileFlush(Writer_handle);
   Print("Written: "+(string)Value);

   Value++;

}


 

 Receiver_optimus.mq4

// INIT MT4 READING INSTANCE
int  Reader_handle = 0;
ulong Pointer_read = 0;
int OnInit()
{
   int open_flags = FILE_READ|FILE_WRITE|FILE_BIN|FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_COMMON;
  
   Reader_handle = FileOpen( "Fichero", open_flags);
   if (Reader_handle < 0) return INIT_FAILED;

   Pointer_read = FileSize(Reader_handle);
  
   Print("Pointer_read: "+(string)Pointer_read);
   //FileClose(Pointer_read);
   return 0;
}

void OnTick()
{
   Sleep(1000);
   read_optimus();

}

void read_optimus()
{
  
   ulong size_fichero = FileSize(Reader_handle);
   if (size_fichero <= Pointer_read){
      //FileClose(Reader_handle);
      return;
   }
   Print("----> Pointer_read: "+(string)Pointer_read);
  
   FileSeek( Reader_handle, Pointer_read, SEEK_SET);
   Pointer_read = size_fichero;
  
   int read = 0;
   while(!FileIsEnding(Reader_handle))
   {
      read = FileReadInteger(Reader_handle, INT_VALUE);
      Print("Read value:"+(string)read);
   }// while(!FileIsEnding(Trade_log_handle))
  
   Print("<---- Pointer_read: "+(string)Pointer_read);
}


 

Receiver.mq4

//............................
//............................
// INIT MT4 READING INSTANCE
int  Reader_handle = 0;
ulong Pointer_read = 0;
int OnInit()
{
   int open_flags = FILE_READ|FILE_WRITE|FILE_BIN|FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_COMMON;
  
   Reader_handle = FileOpen( "Fichero", open_flags);
   if (Reader_handle < 0) return INIT_FAILED;

   Pointer_read = FileSize(Reader_handle);
  
   Print("Pointer_read: "+(string)Pointer_read);
   FileClose(Pointer_read);
   return 0;
}

void OnTick()
{
   Sleep(1000);
   read_ok();

}

void read_ok()
{
   int open_flags = FILE_READ|FILE_WRITE|FILE_BIN|FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_COMMON;
  
   Reader_handle = FileOpen( "Fichero", open_flags);
   if (Reader_handle < 0) {
      Print ("FAILED!!" );
      Sleep(10000);
      return;
   }
  
   ulong size_fichero = FileSize(Reader_handle);
   if (size_fichero <= Pointer_read){
      FileClose(Reader_handle);
      return;
   }
   Print("----> Pointer_read: "+(string)Pointer_read);
  
   FileSeek( Reader_handle, Pointer_read, SEEK_SET);
   Pointer_read = size_fichero;
  
   int read = 0;
   while(!FileIsEnding(Reader_handle))
   {
      read = FileReadInteger(Reader_handle, INT_VALUE);
      Print("Read value:"+(string)read);
   }// while(!FileIsEnding(Trade_log_handle))
  
   FileClose(Reader_handle);
   Print("<---- Pointer_read: "+(string)Pointer_read);
}

 

Files:
 

Hello again,

No comment on my last post...

I would like to know at least if this is a problem with my code or is a MT4 bug 

Thanks,

Jose 

Reason: