How to Implement Account Authentication in MetaTrader EAs: MQL4 & MQL5 Secure Programming Guide

Introduction

This article explains how to implement account authentication based on account numbers using MQL4 and MQL5 in automated trading programming for MetaTrader 4 (MT4) and MetaTrader 5 (MT5). Security for automated trading systems (EAs) is an essential factor for successful trading. Here, we introduce a basic yet effective method to strengthen EA security—account authentication—in a way that’s easy for beginners to understand.

Account authentication is the process of restricting an EA to run only on specific trading accounts. This approach helps prevent unauthorized use or copying of EAs and serves as an effective means to protect the developer’s intellectual property. By following this article, you’ll learn how to implement account authentication with MQL4 and MQL5, enhancing the safety and efficiency of your automated trading systems.

Basics of MQL4 and MQL5

MQL4 and MQL5 are programming languages for the MetaTrader platforms, widely used to develop automated trading systems, also known as Expert Advisors (EAs), in the foreign exchange (FX) market. MQL4 is designed for MetaTrader 4 (MT4), while MQL5 was created for the more advanced MetaTrader 5 (MT5). These languages provide powerful tools for traders to automate their trading strategies and build custom market analysis tools.

Although MQL4 and MQL5 have different features and characteristics, both allow you to code trading rules and develop EAs that automatically execute trades in the market. MQL4 is known for its intuitive syntax and ease of use, making it accessible for beginners. On the other hand, MQL5 offers more advanced functions and multi-threading capabilities, suitable for complex strategies and multi-asset trading.

This section introduced the basic features of MQL4 and MQL5 and how these languages help in developing FX automated trading systems, especially for beginners. The next section focuses on the benefits of account authentication.

Benefits of Account Authentication

Enhanced EA Security

Account authentication is a key feature for significantly improving the security of your EA (Expert Advisor). With this authentication system, you can restrict the EA to operate only on specified trading accounts. As a result, risks of unauthorized access and hacking are reduced, ensuring a secure trading environment. This is a crucial aspect for anyone using EAs with peace of mind.

Advantages of Restricting EA to Specific Accounts

Limiting EA usage to specific accounts via authentication offers several advantages. First, EA developers can protect their products and provide services exclusively to specific customers. Users also benefit by running EAs tailored to their own accounts, enabling more effective execution of customized trading strategies. This can lead to improved trading efficiency and performance.

Preventing Unauthorized Use

Account authentication helps prevent unauthorized use and duplication of EAs. This function is especially important for commercially sold EAs, as it protects copyright and prevents loss of revenue. Users can also have confidence that authenticated EAs are genuine products, which increases trust and reliability in the EA market.

This section has explained how account authentication is an effective means to enhance the security and efficiency of EAs. By utilizing account authentication, you can create a safer trading environment and prevent unauthorized use.

How to Retrieve the Account Number in MQL4

How to Use the AccountNumber() Function

When developing an EA (Expert Advisor) in MQL4, obtaining the account number is a basic yet important step. For this purpose, the AccountNumber() function is provided. This function returns the account number of the currently logged-in trading account. It is very easy to use and does not require any additional parameters. Here is a basic example:

void OnStart()
  {
   // Retrieve the current account number
   int myAccountNumber = AccountNumber();
   // Output the account number
   Print("Current account number: ", myAccountNumber);
  }

By using this function, EA developers can restrict the EA to run only on specific trading accounts.

Example of Using the Retrieved Account Number

The retrieved account number is especially useful for account authentication. For example, a developer can use a simple conditional statement to allow the EA to run only on a designated account:

int OnInit()
  {
   if(AccountNumber() == 1234567)  // Enter your authorized account number here
     {
      Print("Authentication successful: This EA will run on this account");
      return(INIT_SUCCEEDED);
     }
   else
     {
      Print("Authentication failed: This EA will not run on this account");
      return(INIT_FAILED);
     }
  }

This code allows the EA to initialize only if it is linked to a specific account number. This helps prevent unauthorized use and enhances EA security.

How to Retrieve the Account Number in MQL5

How to Use the AccountInfoInteger(ACCOUNT_LOGIN) Function

The process of retrieving an account number in MQL5 is slightly different from MQL4. In MQL5, you use the AccountInfoInteger(ACCOUNT_LOGIN) function to obtain the account number of the currently logged-in trading account. Here is a basic usage example:

void OnStart()
  {
   // Retrieve the account number
   long myAccountNumber = AccountInfoInteger(ACCOUNT_LOGIN);
   // Output the account number
   Print("Current account number: ", myAccountNumber);
  }

In this code snippet, the account number is stored in a long type variable and then output. This information can be used to limit EA usage to specific accounts.

Explaining the Differences from MQL4

The main differences between MQL4 and MQL5 lie in the functions used to obtain the account number and the data types returned. In MQL4, you use AccountNumber() to directly get the account number as an integer. In MQL5, you use AccountInfoInteger() with the ACCOUNT_LOGIN parameter, and the account number is returned as a long value.

Understanding this difference is important when programming in MQL5, especially when integrating account authentication features into your EA. It’s essential to know how to correctly retrieve the account number in each version.

[Implementing Account Authentication] MQL4 Sample Code

Sample Code

When implementing account authentication in MQL4, you can use the following sample code. This restricts the EA to run only on a specific account number.

// EA initialization function
int OnInit()
  {
   // Get the current account number
   int accountNumber = AccountNumber();

   // Allow initialization only for a specific account number
   if(accountNumber == 1234567) // Enter the authorized account number here
     {
      Print("Account authentication successful: ", accountNumber);
      return(INIT_SUCCEEDED);
     }
   else
     {
      Print("Account authentication failed: ", accountNumber);
      return(INIT_FAILED);
     }
  }

Detailed Explanation of Each Part

  • int OnInit(): This is a special function that initializes the EA. It is automatically called when the EA is attached to a chart.
  • int accountNumber = AccountNumber();: This line retrieves the current trading account number.
  • if(accountNumber == 1234567): This conditional statement allows the EA to initialize only if the account number matches the specified value (1234567 in this example). Initialization is denied for other account numbers.
  • return(INIT_SUCCEEDED); and return(INIT_FAILED);: These lines notify MetaTrader whether the EA initialization succeeded or failed, depending on the condition.

This sample shows the basic way to set up your EA to work only on specific accounts using MQL4. The next section will explain a similar approach for MQL5.

[Implementing Account Authentication] MQL5 Sample Code

Sample Code

To implement account authentication in MQL5, refer to the following sample code. This controls EA initialization based on a specific account number.

// EA initialization function
int OnInit()
  {
   // Get the current account number
   long accountNumber = AccountInfoInteger(ACCOUNT_LOGIN);

   // Allow initialization only for a specific account number
   if(accountNumber == 1234567) // Enter the authorized account number here
     {
      Print("Account authentication successful: ", accountNumber);
      return(INIT_SUCCEEDED);
     }
   else
     {
      Print("Account authentication failed: ", accountNumber);
      return(INIT_FAILED);
     }
  }

Detailed Explanation of Each Part

  • int OnInit(): This function is automatically called when the EA is attached to a chart and manages the initialization process.
  • long accountNumber = AccountInfoInteger(ACCOUNT_LOGIN);: In MQL5, you use AccountInfoInteger(ACCOUNT_LOGIN) to get the current account number as a long value.
  • if(accountNumber == 1234567): This condition allows initialization only if the account number matches the specified value. Otherwise, initialization fails.
  • return(INIT_SUCCEEDED) and return(INIT_FAILED): These notify MetaTrader whether the EA initialization succeeded or failed.

This sample shows an easy-to-understand method for limiting EA usage to specific accounts in MQL5, strengthening EA security and preventing unauthorized use.

Testing and Troubleshooting Account Authentication

After adding account authentication, it is essential to thoroughly test and troubleshoot to ensure everything works as intended. This section outlines how to test your EA’s account authentication and solve common issues you may encounter.

Testing Overview

  1. Test on Different Accounts: First, run the EA on the authorized account number to confirm successful authentication. Then, try running it on a different account number to verify that authentication is denied.
  2. Check Error Messages: Make sure the error messages displayed on authentication failure are clear and easy for users to understand.
  3. Review Log Files: Check MetaTrader’s log files to identify any problems that occur during the authentication process.

Common Issues and Solutions

  • Incorrect Account Number: The most common cause of authentication failure is entering the wrong account number. Double-check that you have the correct number set in your code.
  • Platform Compatibility Issues: Because of differences between MQL4 and MQL5, your EA may not work as expected. Make sure you are using the correct code for your MetaTrader version.
  • Error Messages Not Displaying: If error messages are not appearing properly, review the error handling section of your code and make necessary fixes.

Accurately testing and troubleshooting your account authentication feature will improve the safety and reliability of your EA. This allows traders to use your EA with greater confidence.

Conclusion

This article explored the importance of account authentication in MQL4 and MQL5 for MetaTrader platforms. We discussed how account authentication strengthens EA security and helps prevent unauthorized use. Implementing this feature is essential, especially if you plan to sell your EA commercially or protect your personal trading strategies.

If you are an EA developer, we recommend applying the account authentication techniques you’ve learned today in your projects. This process will keep your EAs secure and allow you to provide more reliable products to your users. For traders already using EAs, understanding how this feature protects your trading environment can help you trade with greater peace of mind.

Account authentication is becoming increasingly important in the world of automated trading. Use this knowledge to build a safer and more effective trading environment.