Disallowing multiple EA instances in one terminal

 

Hi,

can anyone think of a way to disallow the user to run more than 1 instance of the same EA in one MT4 terminal at once (not only 2 forward testing instances, but also forward testing + backtesting)?

I don't want to get into detailed explanation about why I need to do that, but it's necessary, trust me ;-)

Thank you!

 

i have something in my mind but i think it's a little problem because the user can change the name of the EA

 
qjol:

i have something in my mind but i think it's a little problem because the user can change the name of the EA


Please tell me :-) This wouldn't matter, if the user tries to get around it although he is warned first, potential crashes aren't my responsibility any more.
 

scan all the windows that open in the terminal (by using WIN API) & check the name of the EA by using WindowExpertName() & than or close the chart or remove the EA

 
qjol:

scan all the windows that open in the terminal (by using WIN API) & check the name of the EA by using WindowExpertName() & than or close the chart or remove the EA


Can you please give me some code examples on how to do that? Thank you
 

it's a little tricky to do because WindowExpertName() Returns name of the executed expert, script, custom indicator, or library so the only way to that is to save in the EA a GlobalVariableSet() or to a file the name of the EA & use FileRead or GlobalVariableCheck() GlobalVariableGet() if there is allready open the EA once & use the following code to remove the EA

#include <WinUser32.mqh>

   int hndl = WindowHandle(Symbol(),Period());
   if (...... == "The Name of EA")
      {
      PostMessageA(hndl,WM_COMMAND,33050,0);
      }

or this code to close the chart (i'm not sure about the WM_QUIT) maybe u supposed to use DestroyWindow(int hWnd) or WM_DESTROY (bottom line do a research on this here u have all the information on handling windows http://msdn.microsoft.com/en-us/library/aa383688)

#include <WinUser32.mqh>
   
int hndl = WindowHandle(Symbol(),Period());
   if (WindowExpertName() == "GBUSTEM")
      {
      PostMessageA(hndl,WM_QUIT,0,0);
      }

but u need to know something about GlobalVariables, if the "terminal" (MT4) or "WINDOWS " crashed the GlobalVariable will not be saved

then u left with the option to use FileRead() FileWrite() but u have another problem if the user is gonna make a copy of the "terminal" (MT4) u can't read the file that located other then terminal_directory\experts\files folder (terminal_directory\tester\files if for expert testing) or in its subfolders. so, u have to use for this an external program or a dll to save the file in a specific place to be able to control if there is another copy of the "terminal" (MT4)

 

Try This: This was developed on Windows XP.

This sample EA uses winAPI to write and read a file in the C:\WINDOWS\system32 folder, which is accessible to all running MetaTrader terminals.

1) If no TestFile.dat file in system32 folder, EA continues to write TestFile.dat and EA will initialize and continue to run program.

2) EA deletes file when stopped or removed from chart.

3) If another attempt to run 2nd instance of same EA at same time while TestFile.dat exists, 2nd EA will not initialize.


You will need to put the attached WinFile2.mqh file in the include folder. C:\Program Files\MetaTrader 4\experts\include


#include <WinFile2.mqh> // "include file" provided by http://www.mt4i.com


//------------ Inputs ---------------------------------------

   int TestNumber=0;
   string TestFilename;

int init()
{
   TestFilename = "C:\windows\system32\FileTest.dat";
   
   if(DoesFileExist(TestFilename)==true) 
      {
         Print("Cannot Initialize ",WindowExpertName()," Expert Advisor, as EA is already running on this PC.");
         Alert("Cannot Initialize ",WindowExpertName()," Expert Advisor, as EA is already running on this PC.");
         return(0);
      }
   
   int fWrite = OpenNewFileForWriting(TestFilename, true);
   if (!IsValidFileHandle(fWrite)) {
   MessageBox("Unable to open " + TestFilename + " for writing");
   } else {
// Write the parameter to file
   WriteIntToFile(fWrite, 11111);
   CloseFile(fWrite);
   }
   
   int fRead = OpenExistingFileForReading(TestFilename, true);
   if (!IsValidFileHandle(fRead)) {
   MessageBox("Unable to open " + TestFilename + " for reading");
   } else {
// Read the parameter from file
   TestNumber=ReadIntFromFile(fRead);
   CloseFile(fRead);
   }
   
   if(TestNumber < 1) 
      {
         Comment("Cannot Initialize ",WindowExpertName()," Expert Advisor, as EA is already running on this PC.");
         return(0);
      }
   else
      {
         Comment("Initialization of ",WindowExpertName()," Expert Advisor was successful.");
      }
   
return(0);
}

int deinit()
{

   DeleteFile(TestFilename);

return(0);
}


int start()
{ 
//------ If FileTest deleted, re-write file --------------------      
   if(DoesFileExist(TestFilename)==false) 
      {
         //---------- Re-Write TestFile to system32 folder --------------
         int fWrite = OpenNewFileForWriting(TestFilename, true);
         if (!IsValidFileHandle(fWrite)) {
         MessageBox("Unable to open " + TestFilename + " for writing");
         } else {
      // Write the parameter to file
         WriteIntToFile(fWrite, 11111);
         CloseFile(fWrite);
         }
      }
   
      
      
   return(0);    
}
//--------- End of Start() ----------------------------------



Files:
winfile2.mqh  28 kb
 
wackena:

Try This: This was developed on Windows XP.

This sample EA uses winAPI to write and read a file in the C:\WINDOWS\system32 folder, which is accessible to all running MetaTrader terminals.

1) If no TestFile.dat file in system32 folder, EA continues to write TestFile.dat and EA will initialize and continue to run program.

2) EA deletes file when stopped or removed from chart.

3) If another attempt to run EA at same time while TestFile.dat exists, EA will not initialize.


You will need to put the attached WinFile2.mqh file in the include folder. C:\Program Files\MetaTrader 4\experts\include




great
 

Thank you very much!

 
wackena:

Try This: This was developed on Windows XP.

This sample EA uses winAPI to write and read a file in the C:\WINDOWS\system32 folder, which is accessible to all running MetaTrader terminals.

The code is fine for your personal use, but has a potential problem on other people's computers because it is assuming Administrator rights in the form of being able to create files in System32. It's particularly likely to fail on Vista, Windows 7, and Windows Server 2008. There's also the side-issue that the System32 directory isn't necessarily c:\windows\system32.

As a possible alternative, the attached code differs in three ways:

* It creates its files in the %TEMP% directory - though see the notes in the code.

* It allows the lock to be applied at three different levels: either across the whole computer, or within a single installation of MT4, or within all running copies of MT4 which are logged in to the same account number.

* Its actual locking mechanism is slightly simpler: it just opens the file and keeps it open, rather than reading or writing anything.

Files:
 
i might be wrong, but this seems to be the first scenario where i would use a GlobalVariable
Reason: