protecting an EA

 
hi good day can anyone tell me if i made myself an EA and i wanna sell it to someone what can i add to it that the person i sold it to cant resell it.
 
Kieyamodien Booysen: hi good day can anyone tell me if i made myself an EA and i wanna sell it to someone what can i add to it that the person i sold it to cant resell it.

Lock the code to an account number, or name or broker, or something similar. The following code is old but should work on MQL5 or MQL4, but use it as a reference for your own code.

Forum on trading, automated trading systems and testing trading strategies

EA limit code

Fernando Carreiro, 2017.09.10 22:17

Here is a sample of my own code that I use, but it is up to you to implement properly. I'm not offering any support on it. It is up to you to do the necessary research and learning.

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

   // ...

   return( INIT_SUCCEEDED );  // Initialisation complete
}
//+------------------------------------------------------------------------------------+
// Tick Event Function
//+------------------------------------------------------------------------------------+
void OnTick()
{
   // Check Licensing Restrictions
   if( boolRestrictOnTick() ) return;
   
   /// ...
}
//+------------------------------------------------------------------------------------+
// Function to Test Restrictions during Initialisation
//+------------------------------------------------------------------------------------+
bool boolRestrictOnInit()
{
   boolRestrictions =
      boolRestrictExpiration     ||
      boolRestrictAccountNumber  ||
      boolRestrictAccountName    ||
      boolRestrictAccountServer  ||
      boolRestrictAccountCompany ||
      boolRestrictDemoAccount    ||
      boolRestrictSymbols;

   if( boolRestrictions )
   {
      boolRestrictionsUnverified = true;

      if( (bool) TerminalInfoInteger( TERMINAL_CONNECTED ) )
      {
         long longAccountNumber = AccountInfoInteger( ACCOUNT_LOGIN );
         if( longAccountNumber > 0 )
         {
            if( boolRestrictAccountNumber )
               { if( longAccountNumber                        != longRestrictAccountNumber )
                  { return( boolRestrictAlert() ); } }
            if( boolRestrictAccountName )
               { if( AccountInfoString( ACCOUNT_NAME )        != strRestrictAccountName    )
                  { return( boolRestrictAlert() ); } }
            if( boolRestrictAccountServer )
               { if( AccountInfoString( ACCOUNT_SERVER )      != strRestrictAccountServer  )
                  { return( boolRestrictAlert() ); } }
            if( boolRestrictAccountCompany )
               { if( AccountInfoString( ACCOUNT_COMPANY )     != strRestrictAccountCompany )
                  { return( boolRestrictAlert() ); } }
            if( boolRestrictDemoAccount )
               { if( AccountInfoInteger( ACCOUNT_TRADE_MODE ) != ACCOUNT_TRADE_MODE_DEMO   )
                  { return( boolRestrictAlert() ); } }
            if( boolRestrictSymbols() )
               { return( boolRestrictAlert() ); }

            boolRestrictionsUnverified = false;
         }
      }
   }
   return( false );
}
//+------------------------------------------------------------------------------------+
// 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] ) != WRONG_VALUE ) 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  ) != WRONG_VALUE ) &&
             ( StringFind( _Symbol, strRight ) != WRONG_VALUE )    )
            return( false );
      }
      return( true );
   }
   return( false );
}
//+------------------------------------------------------------------------------------+
// Function to Test Expiration during Tick Events
//+------------------------------------------------------------------------------------+
bool boolRestrictOnTick()
{
   if( boolRestrictions )
   {
      if( boolRestrictionsUnverified )
         return( boolRestrictOnInit() );
      if( boolRestrictExpiration && ( TimeCurrent() >= dtRestrictExpiration ) )
         return( boolRestrictAlert()  );
   }
   return( false );
}
// Function to Alert User of Licensing Restrictions and Remove Code from Execution
bool boolRestrictAlert()
{
   if( boolRestrictAlert )
      MessageBox( strRestrictAlertMessage, strRestrictAlertCaption, MB_ICONERROR );
   ExpertRemove();
   return( true );
}
//+------------------------------------------------------------------------------------+
//      Variables for Handling of Licensing Restrictions
//+------------------------------------------------------------------------------------+
bool
   boolRestrictExpiration     = false, // Set to true, to use an Experation 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
   boolRestrictionsUnverified = false, // DO NOT CHANGE. For internal use only!
   boolRestrictions           = false; // DO NOT CHANGE. For internal use only!
datetime
   dtRestrictExpiration       = D'2017.03.31';  // Restricted by Expration 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
//+------------------------------------------------------------------------------------+

 
Fernando Carreiro #:

Lock the code to an account number, or name or broker, or something similar. The following code is old but should work on MQL5 or MQL4, but use it as a reference for your own code.


sir thanks in advance i need to ask: do all the scripts have to be copied? and in which position/section? thank you
 
JossuaBli_ STMJ #: sir thanks in advance i need to ask: do all the scripts have to be copied? and in which position/section? thank you

As I stated ... "use it as a reference for your own code".

If you don't know how to code, then that means you are attempting to protect someone else's code to sell it. That is illegal and a crime.

 
Kieyamodien Booysen:
hi good day can anyone tell me if i made myself an EA and i wanna sell it to someone what can i add to it that the person i sold it to cant resell it.
Metatrader Account Num Lock and Expiry is the best. Or you can try Remote License via your website. If you don't have a website you can license for MT4 with Google Drive and for MT5 with Git. 
 

As mentioned before – the easiest way is to set limited time or account numbers/name on which the EA is allowed to work. You should define the account number and time in the code.
In the OnTick function just use checkLicense function:

int account = 12345;
datetime time = D’2030.12.11 00:00;
bool checkLicense(){
   If(AccountInfoInteger(ACCOUNT_NUMBER)!=account){
                Print(“Wrong account”);
                return(false);
   }
   If(TimeCurrent()>=time){
                Print(“Time is after ”);
                return (false);
   }
   return(true);
} 

Just remember to compile the code with proper values and then send to the user the execution file only – so he didn’t have access to the code to change the limited variables.

 
Marzena Maria Szmit #: As mentioned before

Please edit your (original) post and use the CODE button (or Alt+S)! (For large amounts of code, attach it.)
          General rules and best pratices of the Forum. - General - MQL5 programming forum (2019)
          Messages Editor

 
Fernando Carreiro #: Lock the code to an account number, or name or broker, or something similar. The following code is old but should work on MQL5 or MQL4, but use it as a reference for your own code.

Thanks a zillion Fernando for this comprehensive code on placing restrictions! Please I'm trying to make my EA to run on a limited number of accounts and not just a single account as contained in your code. For instance I want the EA to run on Three different account numbers viz 1234567, 246810, 1001005. So I decided to create an array to capture all three account numbers as shared below in the code section but the EA still shows the restriction message when one of the accounts in the array is correct. Please your help will be highly appreciated. Thanks

long longRestrictAccountNumber[] = { 979890, 436290, 225690, 48405166 }; 

 {
         long longAccountNumber = AccountInfoInteger( ACCOUNT_LOGIN );
         if( longAccountNumber > 0 )
         {
         for (int i=0; i<ArraySize(longRestrictAccountNumber); i++)
            if( boolRestrictAccountNumber )      
               { if( longAccountNumber       !=  longRestrictAccountNumber[i] ) //                              
                  { return( boolRestrictAlert() ); } }
 
@Gabriel Nsor #: Thanks a zillion Fernando for this comprehensive code on placing restrictions! Please I'm trying to make my EA to run on a limited number of accounts and not just a single account as contained in your code. For instance I want the EA to run on Three different account numbers viz 1234567, 246810, 1001005. So I decided to create an array to capture all three account numbers as shared below in the code section but the EA still shows the restriction message when one of the accounts in the array is correct. Please your help will be highly appreciated. Thanks
// Untested, uncompiled, simply typed out

long longRestrictAccountNumber[] = { 979890, 436290, 225690, 48405166 }; 

if( boolRestrictAccountNumber ) {
   bool boolAccountValid = false;
   for( int i = ArraySize( longRestrictAccountNumber ) - 1; i >= 0; i-- )
      if( longAccountNumber == longRestrictAccountNumber[ i ] ) boolAccountValid = true;
   if( !boolAccountValid ) return boolRestrictAlert();
};
 
Fernando Carreiro #:
Thanks so very much sir! The code is working perfectly!!
 
I like these discussions, I'm not very good at using arrays at the moment, this is very helpful to me.
Reason: