Exclusive locking files / Executing only one EA per Symbol

 

I was trying to execute only one EA per _Symbol, i tried mutex and winapi, but that was very ugly...

Asking to MQL5 support they report me this interesting condition:

FileOpen when not used with shared write or shared read will open file with exclusive lock, including the FILE_COMMON flag you could use a lock file to block two EA :) check it:

int FP;
int OnInit(){
   FP=FileOpen(_Symbol+".lock",FILE_WRITE | FILE_COMMON);
   if(FP<0){
      Alert("More than one EA using this Symbol ["+_Symbol+"]");
      return(INIT_FAILED);
   }
   return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason){
   FileClose(FP);
}
 

Forum on trading, automated trading systems and testing trading strategies


Hello,

Please use the SRC button when you post code. Thank you.


This time, I edited it for you.


 
Alain Verleyen:

thanks :)