Account restriction

 
I want my EA to run only on account 105293. So i wrote this in the initialization function
Int init()
  {
  if(AccountNumber()!=105293)
  return(0);
  }

But still my EA is working on all accounts. Please help what mistake i made?
 

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 ...
}

//+------------------------------------------------------------------------------------+
 
FMIC:

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:


Dear what i wrote actually worked in another source code but not working here. Did i write this in wrong function? Please identify mistake.
 
sammiawan999:
Dear what i wrote actually worked in another source code but not working here. Did i write this in wrong function? Please identify mistake.
int OnInit()
{
   if( AccountNumber() != 105293 ) return( INIT_FAILED );

   // ... other code ...

   return( INIT_SUCCEEDED );
}
 
FMIC:

Thanks..
 
FMIC:

If the EA is running and you login to an other account, the EA will still continue to work.

 
angevoyageur:
If the EA is running and you login to an other account, the EA will still continue to work.

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().

 
FMIC:

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().

You are right, I was thinking about an indicator.