MQL4 Expert Advisor Read Text from Terminal Journal - page 2

 

I've just realised it is actually the experts log that I need to read in values/text from, and not the journal - I had some manual alerts set up in my terminal along with an indicator so I got the two mixed up.

And yes after some further digging I've found many say the logs are not flushed in real time. The only way to force an update is to right click the logs in the terminal and click open , and it then updates. Someone here has written some code to force the terminal to do this process on every new bar - https://www.mql5.com/en/forum/131720 - I've not tested this out yet though. This isn't really an ideal solution as I imagine opening closing the log file repeatedly is going to be resource intensive. I would want to be doing this more than every bar, probably once every few ticks!

However this is the closest I've found to a solution so far so might have to work with it and try it out.

What are the "winapi functions"?

 
James Parker:

I've just realised it is actually the experts log that I need to read in values/text from, and not the journal - I had some manual alerts set up in my terminal along with an indicator so I got the two mixed up.

And yes after some further digging I've found many say the logs are not flushed in real time. The only way to force an update is to right click the logs in the terminal and click open , and it then updates. Someone here has written some code to force the terminal to do this process on every new bar - https://www.mql5.com/en/forum/131720 - I've not tested this out yet though. This isn't really an ideal solution as I imagine opening closing the log file repeatedly is going to be resource intensive. I would want to be doing this more than every bar, probably once every few ticks!

However this is the closest I've found to a solution so far so might have to work with it and try it out.

What are the "winapi functions"?

That's why I asked why you want to read the Journal log ;-)

WinApi is API from Microsoft which is the base of Windows, you can use these functions as a workaround for MT4 limitations, but it's most of the time useless now, with new MT4 build.

For your problem it could be a solution, but in my opinion it's better to proceed otherwise to control your self what is written in the Expert logs, code your own alerts, don't use MT4 built-in, same for indicators.

 
Alain Verleyen:

That's why I asked why you want to read the Journal log ;-)

WinApi is API from Microsoft which is the base of Windows, you can use these functions as a workaround for MT4 limitations, but it's most of the time useless now, with new MT4 build.

For your problem it could be a solution, but in my opinion it's better to proceed otherwise to control your self what is written in the Expert logs, code your own alerts, don't use MT4 built-in, same for indicators.

Thanks, I'll do some research into the WinApi.

I've read through the File Operations section in the MQL4 book today and managed to write a basic function to read in the information that I need from the log file if it was stored in the MQL4/Files folder...

bool ReadDataFromLogFile()
{

//Initialise variables
int Handle = 0;                           // File descriptor
string File_Name = "20160109.log";        // Name of the file
string LogText= " ";                      // Text read from last line of the log

//Open the file
Handle=FileOpen(File_Name,FILE_READ,";");       

//Check if the file opening process fails
if(Handle<0)                                          
{
   if(GetLastError()==4103)                           
      Print("No file named ",File_Name);              
   else                                              
      Print("Error while opening file ",File_Name);
   return(false);                                       
}

else {
//Read first line from log file
   while(FileIsLineEnding(Handle)==false)       
  {                                          
   LogText = FileReadString(Handle);           
   if(FileIsLineEnding(Handle)==true)        
   break;                                
   }
     
//Close the file
FileClose(Handle);                        

//Print the text from the first line of the log as a test
Print("Text read from last entry to the log file = ",LogText);
   
return(true);
}
}


However my question now is how do I get it to search for the file in the MQL4/Logs folder instead?

I'm guessing this is where the WinApi comes in... I'll have a look into it...

 

I've moved a stage further with this now. I used the information in this article - https://www.mql5.com/en/articles/1540 - to help me write some code to read the log file using functions from the "kernel32.dll".

I'm having a problem with it though. It's giving me an error when it tries to open the file at the first "_lopen" command. Can anyone see where this might be going wrong?...

 

// constants for function _lopen
#define OF_READ               0
#define OF_WRITE              1
#define OF_READWRITE          2
#define OF_SHARE_COMPAT       3
#define OF_SHARE_DENY_NONE    4
#define OF_SHARE_DENY_READ    5
#define OF_SHARE_DENY_WRITE   6
#define OF_SHARE_EXCLUSIVE    7
 
#import "kernel32.dll"
   int _lopen  (string path, int of);
   int _lcreat (string path, int attrib);
   int _llseek (int handle, int offset, int origin);
   int _lread  (int handle, string buffer, int bytes);
   int _lwrite (int handle, string buffer, int bytes);
   int _lclose (int handle);
#import

string FilePath = "...";

string TextFromLog=ReadDataFromLogFile(FilePath);  
Print("Text from Log = ",TextFromLog);

string ReadDataFromLogFile(string path) 
  {
    int handle=_lopen (path,OF_READ);           
    if(handle<0) 
      {
        Print("Error opening file ",path); 
        return ("");
      }
    int result=_llseek (handle,0,0);      
    if(result<0) 
      {
        Print("Error placing the pointer" ); 
        return ("");
      }
    string buffer="";
    string char1="x";
    int count=0;
    result=_lread (handle,char1,1);
    while(result>0) 
      {
        buffer=buffer+char1;
        char1="x";
        count++;
        result=_lread (handle,char1,1);
     }
    result=_lclose (handle);              
    if(result<0)  
      Print("Error closing file ",path);
      
    Print("Bytes counted:",count);
    return (buffer);
  }
File Operations via WinAPI
File Operations via WinAPI
  • 2008.08.15
  • MetaQuotes Software Corp.
  • www.mql5.com
Environment MQL4 is based on the conception of safe "sandbox": reading and saving files using the language are allowed in some predefined folders only. This protects the user of MetaTrader 4 from the potential danger of damaging important data on the HDD. However, it is sometimes necessary to leave that safe area. This article is devoted to the problem of how to do it easily and correctly.
 

Got this working now :)

The _lopen command doesn't work in the latest builds of MT4.

Here someone has written a ReadFile function that uses the latest commands - https://www.mql5.com/en/forum/147939 - I've modified this to do what I need.

#import "kernel32.dll"
   int CreateFileW(string, uint, int, int, int, int, int);
   int GetFileSize(int, int);
   int ReadFile(int, uchar&[], int, int&[], int);
   int CloseHandle(int);
#import

//Number of bytes to read from first line of log file
int BytesToRead=10;

//path where file is stored
string path = "...";

string ReadFile(string Filename)
{

string strFileContents = "";
int Handle = CreateFileW(Filename, 0x80000000 /*GENERIC_READ*/, 3 /*SHARE READ|WRITE*/, 0, 3 /*OPEN_EXISTING*/, 0, 0);
   
if (Handle == -1)
   {
     //error opening file
     Print("Error opening log file ",path); 
     return ("");
   }  
      
else 
   {
   int LogFileSize = GetFileSize(Handle, 0);
   if(LogFileSize<=0)
      {
      //File empty
      Print("Log file is empty ",path); 
      return ("");
      }
   
   else
      {
      uchar buffer[];
      ArrayResize(buffer, BytesToRead);
      int read[1];
      ReadFile(Handle, buffer, BytesToRead, read, 0);
         if (read[0] == BytesToRead) 
            {
            strFileContents = CharArrayToString(buffer, 0, read[0]);
            } else 
                 {
                 // Read failed
                 Print("Error reading log file ",path); 
                 return ("");
                 }
      }
CloseHandle(Handle);
}
return strFileContents;
}

 

 Now I just need to find an efficient way to have the log files refresh in real time!

How to get file's modifycation time? - MQL4 forum
  • www.mql5.com
How to get file's modifycation time? - MQL4 forum
 
Alain Verleyen:
Can someone tell me one reason to read the Journal log from an EA ? Thanks.

I want to do this so that I can get the ms that a BUYSTOP order was executed

 
Trevor Schuil:

I want to do this so that I can get the ms that a BUYSTOP order was executed

Then it's time to switch to mql5 which allow that without difficulty.
 
Trevor Schuil:

I want to do this so that I can get the ms that a BUYSTOP order was executed

Hi Trevor, i came across your EA banana pip 1.25, can you share with me its source codes? i want to improve it, thanks

 
Alain Verleyen:
Can someone tell me one reason to read the Journal log from an EA ? Thanks.
My VPS-Provider restarts my Expert every view Days and all local and global Data/Files will be deleted. Same effect appears when re-uploading the Expert to the VPS.  The Server-Log seems to to be the only persistent channel. Is there a better (working) way to preserve Data? 
 
sonaht:
My VPS-Provider restarts my Expert every view Days and all local and global Data/Files will be deleted. Same effect appears when re-uploading the Expert to the VPS.  The Server-Log seems to to be the only persistent channel. Is there a better (working) way to preserve Data? 
Use a better VPS provider.
Reason: