Protect expert advisor

 

Good day


Can anyone please tell me how I can add protection to an expert. I want my expert advisor to be able to only work on one specific broker account number so that my expert cant be coppied to a different pc and used. I am using MT5 please thus it needs to be on MQL5. How would I code my expert to only work on one Account number

 

Forum on trading, automated trading systems and testing trading strategies

Account restriction

Fernando Carreiro, 2016.05.14 13:13

Here is some sample code for various restrictions to be applied on an EA, including that of a Account number. Just set the variables according to your requirements:

//+------------------------------------------------------------------------------------+
//      Variables for Handling of Licensing Restrictions
//+------------------------------------------------------------------------------------+
bool
   boolRestrictExpiration     = false, // Set to true, to use an Expiration Date
   boolRestrictAccountNumber  = false, // Set to true for Restricting by Account Number
   boolRestrictAccountName    = false, // Set to true for Restricting by Account Name
   boolRestrictAccountServer  = false, // Set to true for Restricting by Account Server
   boolRestrictAccountCompany = false, // Set to true for Restricting by Account Company
   boolRestrictDemoAccount    = false, // Set to true, to only allow Demo Accounts
   boolRestrictSymbols        = false, // Set to true, to only allow certain Symbols
   boolRestrictAlert          = true;  // Display Alert Message when Restrictions apply
datetime
   dtRestrictExpiration       = D'2016.12.31';  // Restricted by Expiration Date
long
   longRestrictAccountNumber  = 123456789;      // Restricted by Account Number
string
   strRestrictAccountName     = "Client Name",  // Restricted by Account Name
   strRestrictAccountServer   = "Server Name",  // Restricted by Account Server
   strRestrictAccountCompany  = "Company Name", // Restricted by Account Company
   strRestrictSymbols[]       = { "EURUSD", "GBPJPY", "NZDCAD" }, // Restricted Symbols
   strRestrictAlertCaption    = "Restrictions", // Alert Message Box Caption
   strRestrictAlertMessage    =
      "ATTENTION! Due to Licensing Restrictions, code execution has been blocked!";
      // Message to be used when Restrictions have been detected

//+------------------------------------------------------------------------------------+
// Function to Test Restrictions during Initialisation   
//+------------------------------------------------------------------------------------+
bool boolRestrictOnInit()
{
   if( boolRestrictAccountNumber )
      { if( AccountInfoInteger( ACCOUNT_LOGIN )      != longRestrictAccountNumber )
         { voidRestrictAlert(); return( true ); } }
   if( boolRestrictAccountName )
      { if( AccountInfoString( ACCOUNT_NAME )        != strRestrictAccountName    )
         { voidRestrictAlert(); return( true ); } }
   if( boolRestrictAccountServer )
      { if( AccountInfoString( ACCOUNT_SERVER )      != strRestrictAccountServer  )
         { voidRestrictAlert(); return( true ); } }
   if( boolRestrictAccountCompany )
      { if( AccountInfoString( ACCOUNT_COMPANY )     != strRestrictAccountCompany )
         { voidRestrictAlert(); return( true ); } }
   if( boolRestrictDemoAccount )
      { if( AccountInfoInteger( ACCOUNT_TRADE_MODE ) != ACCOUNT_TRADE_MODE_DEMO   )
         { voidRestrictAlert(); return( true ); } }
   if( boolRestrictSymbols() )
      { voidRestrictAlert(); return( true ); }
   return( boolRestrictOnTick() );
}

//+------------------------------------------------------------------------------------+
// Function to Test Variations of Restricted Symbols
//+------------------------------------------------------------------------------------+
bool boolRestrictSymbols()
{
   if( boolRestrictSymbols )
   {
      int intSymbolCount = ArraySize( strRestrictSymbols );
      if( intSymbolCount == 0 ) return( false );
      for( int i = 0; i < intSymbolCount; i++ )
      {
         if( StringFind( _Symbol, strRestrictSymbols[i] ) != EMPTY ) return( false );
         int
            intLen  = StringLen( strRestrictSymbols[i] ),
            intHalf = intLen / 2;
         string
            strLeft  = StringSubstr( strRestrictSymbols[i], 0, intHalf ),
            strRight = StringSubstr( strRestrictSymbols[i], intHalf, intLen - intHalf );
         if( ( StringFind( _Symbol, strLeft  ) != EMPTY ) && 
             ( StringFind( _Symbol, strRight ) != EMPTY )    )
            return( false );
      }
      return( true );
   }
   return( false );
}

//+------------------------------------------------------------------------------------+
// Function to Test Expiration during Tick Events
//+------------------------------------------------------------------------------------+
bool boolRestrictOnTick()
{
   if( boolRestrictExpiration )
      { if( TimeCurrent() >= dtRestrictExpiration )
         { voidRestrictAlert(); return( true ); } }
   return( false );
}

//+------------------------------------------------------------------------------------+
// Function to Alert User of Licensing Restrictions and Remove Code from Execution
//+------------------------------------------------------------------------------------+
void voidRestrictAlert()
{
   if( boolRestrictAlert )
      MessageBox( strRestrictAlertMessage, strRestrictAlertCaption, MB_ICONERROR );
   ExpertRemove();
}

//+------------------------------------------------------------------------------------+
// Initialisation Event Function
//+------------------------------------------------------------------------------------+
int OnInit()
{
   // Check Licensing Restrictions
   if( boolRestrictOnInit() ) return( INIT_FAILED );

   // ... other code ...

   return( INIT_SUCCEEDED );  // Initialisation complete
}

//+------------------------------------------------------------------------------------+
// Tick Event Function
//+------------------------------------------------------------------------------------+
void OnTick()
{
   // Check Licensing Restrictions
   if( boolRestrictOnTick() ) return;

   // ... other code ...
}

//+------------------------------------------------------------------------------------+
 
Thank you. How do I attach it as I tried but it had no effect
 
Fernando Carreiro #:

Forum on trading, automated trading systems and testing trading strategies

Account restriction

Fernando Carreiro, 2016.05.14 16:14

I could be wrong, but according to the "Uninitialization Reason Codes", I don't think that will happen because MT4 will reload the EA when there is a Change in Account, Chart Symbol or Time-frame. So, in essence the OnInit() function will be called again with the new Account Number.

However, if I am wrong, then he just needs to add the verification to the OnTick() event.

For Indicators, however, it seems this does not apply and they do not undergo a OnDeinit/OnInit cycle with a change in Account and in this case, the verification of the account number will have to be checked in one of the other event functions such as OnCalculate(), OnTimer() or OnChartEvent().

Can you pleace tell me what else does not lead to the deinitialization of the indicator?

Should I check for the existence of class objects before creating them in OnInit()? I have seen such a check in someone else's code, but I don't understand why they do it, since the objects will be deleted in OnDeinit(). Example:

class CExample1
  {
   //...
  };

CExample1* name;

void OnInit()
  {
   if(CheckPointer(name) == POINTER_DYNAMIC)
      delete name;
   name = new CExample1;
  }