How to Implement Account Authentication in MQL4 and MQL5: A Beginner’s Guide to Secure Automated Trading

※記事内に広告を含む場合があります。

Introduction

This article explains how to implement account authentication based on account numbers using MQL4 and MQL5 in MetaTrader 4 (MT4) and MetaTrader 5 (MT5) automated trading programming. The security of automated trading systems (EAs) is crucial for trading success. Here, we’ll introduce a basic method for strengthening that security through account authentication, explained in an easy-to-understand way for beginners.

Account authentication is a process that restricts the use of an EA to a specific trading account. This method serves as an effective way to prevent unauthorized use or copying of the EA, protecting the developer’s intellectual property. Through this article, you will learn how to implement account authentication in MQL4 and MQL5, improving the safety and efficiency of your automated trading system.

Basics of MQL4 and MQL5

MQL4 and MQL5 are programming languages used for developing automated trading systems, commonly known as Expert Advisors (EAs), on the MetaTrader platforms. MQL4 is designed for MetaTrader 4 (MT4), while MQL5 is intended for the more advanced MetaTrader 5 (MT5). These languages provide powerful tools for traders to automate customized trading strategies and create market analysis tools.

MQL4 and MQL5 each have different functions and features, but both can be used to code trading rules and develop EAs that execute trades automatically. MQL4 is known for its intuitive syntax and ease of use, making it accessible for beginners. On the other hand, MQL5 offers more advanced features and multi-threaded processing, making it suitable for complex strategies and multi-asset trading.

This section introduces the basic features of MQL4 and MQL5 and explains how these languages can be useful in developing FX automated trading systems in a way that is easy for beginners to understand. The next section will focus on the benefits of account authentication.

Benefits of Account Authentication

Enhanced EA Security

Account authentication is a key feature that significantly enhances the security of an EA (Expert Advisor). By utilizing this authentication system, you can restrict the EA to operate only on a specific trading account. As a result, the risk of unauthorized access or hacking is reduced, ensuring a secure trading environment. This is a crucial aspect for operating EAs with peace of mind.

Benefits of Operating EA on Specific Accounts

Restricting EA usage to specific accounts through account authentication has several benefits. First, EA developers can protect their products and provide services only to specific customers. Additionally, users can execute customized trading strategies more effectively by using an EA designed specifically for their account. This can improve trading efficiency and outcomes.

Prevention of Unauthorized Use

Account authentication helps prevent unauthorized use or duplication of EAs. This feature is particularly important for commercially sold EAs as it protects copyrights and prevents revenue loss. Moreover, users can be confident that the authenticated EA is legitimate. This increases the EA’s credibility in the market and earns user trust.

This section explains that account authentication is an effective means of enhancing the security and efficiency of EAs. By utilizing account authentication, you can increase the safety of the trading environment and prevent unauthorized use.

How to Retrieve Account Number in MQL4

Usage of the AccountNumber() Function

When developing an EA (Expert Advisor) in MQL4, retrieving the account number is a fundamental yet important step. The AccountNumber() function is provided for this purpose. This function returns the number of the currently logged-in trading account. Its usage is straightforward, with no additional parameters required. Here is a basic example of its use.

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 set the EA to operate only on specific trading accounts.

Examples of Using the Retrieved Account Number

The retrieved account number is particularly useful for account authentication. For instance, developers can use a simple conditional statement to restrict the EA to operate only on a specific account.

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

This code allows initialization only if the EA is associated with a specific account number, helping to prevent unauthorized use and enhance EA security.

How to Retrieve Account Number in MQL5

Usage of the AccountInfoInteger(ACCOUNT_LOGIN) Function

In MQL5, the process of retrieving the account number is slightly different from MQL4. Here, we use the AccountInfoInteger(ACCOUNT_LOGIN) function to obtain the account number. This function is used to get the number of the currently logged-in trading account. Below is the 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 restrict the EA’s usage to specific accounts.

Explanation of Differences from MQL4

The main differences between MQL4 and MQL5 are the types of functions used to retrieve account numbers and the data types returned. In MQL4, the AccountNumber() function is used to directly obtain an integer-type account number. In MQL5, the AccountInfoInteger() function is used with the ACCOUNT_LOGIN parameter to get the account number, which is returned as a long type.

Understanding this difference is important for programming in MQL5. Especially when incorporating account authentication features into EAs, knowing the correct method for retrieving account numbers is essential.

[Implementation of Account Authentication] MQL4 Sample Code

Practical Code Example

When implementing account authentication features using MQL4, you need to utilize the account number retrieval method introduced earlier. Here is a sample code for account authentication implementation.

input int authorizedAccountNumber = 1234567;  // Enter the specified account number
int OnInit()
{
 if(AccountNumber() == authorizedAccountNumber)
 {
  Print("Authentication successful: EA will operate on this account");
  return(INIT_SUCCEEDED);
 }
 else
 {
  Print("Authentication failed: EA will not operate on this account");
  return(INIT_FAILED);
 }
}

In this example, the EA will only start if the account number matches the authorized account number specified. This code demonstrates a basic implementation of account authentication in MQL4.

[Implementation of Account Authentication] MQL5 Sample Code

Practical Code Example

Similarly, for MQL5, you can implement account authentication using the method of retrieving the account number. Here is a sample code for MQL5 implementation.

input long authorizedAccountNumber = 1234567;  // Enter the specified account number
void OnStart()
{
 if(AccountInfoInteger(ACCOUNT_LOGIN) == authorizedAccountNumber)
 {
  Print("Authentication successful: EA will operate on this account");
 }
 else
 {
  Print("Authentication failed: EA will not operate on this account");
 }
}

This MQL5 code snippet checks whether the retrieved account number matches the authorized number and outputs the authentication result.

Conclusion

Account authentication is a valuable method for improving the security of your automated trading systems (EAs) on MetaTrader platforms. By implementing account authentication in MQL4 and MQL5, you can restrict EA usage to specific accounts, prevent unauthorized access, and protect your intellectual property.

This article has provided a basic guide to account authentication, including how to retrieve account numbers and sample code for implementation. By understanding and applying these methods, you can enhance the safety and reliability of your EAs and ensure a more secure trading experience.

※記事内に広告を含む場合があります。
ベアちゃん@東京シストレ: One of Japan's oldest MetaTrader FX automated trading developers 🔧. With over 15 years of professional experience, he boasts achievements such as winning first place in the Tradency Tournament '15 🥇 and being the runner-up in the 3rd EA-1 Grand Prix 🥈. He has also appeared on Radio Nikkei and is currently active as an executive of Trilogy Corporation (a registered investment advisory and agency business) [Registration number: 372, Director of the Kinki Finance Bureau, Ministry of Finance]. To join the board of Trilogy Corporation, he underwent a rigorous personal examination by the Kinki Finance Bureau, and his name is now registered with the Kinki Finance Bureau. Becoming an executive officer of a registered investment advisory and agency business requires passing this stringent personal examination. Utilizing his extensive knowledge as a former senior design engineer, he provides insights into the development of high-performance Expert Advisors (EAs), the latest trading technologies, and market analysis. He delivers professional strategies and tips to traders aiming to optimize their FX trading and maximize profits.

This website uses cookies.