Adding License Code to MT5 Expert Advisor

A step-by-step guide on adding a license code to your MT5 Expert Advisor.

Overview

Welcome to the guide on adding a license code to your MT5 Expert Advisor (EA). This tutorial will walk you through the process of integrating the license code to secure your EA and ensure it connects properly with the application.

Prerequisites

Before you begin, make sure you have the following:

  • MetaTrader 4 (MT5) application installed
  • Basic knowledge of MetaEditor
  • A license code file (MetatraderValidation.ex4)

Installation Steps

Step 1: Copy the MetatraderValidation.ex5 file

Open your download folder and copy the MetatraderValidation.ex5 file.

Alt Text

Step 2: Open MT5 Application

Open the MetaTrader 5 (MT5) application.

Alt Text

Step 3: Open Data Folder

Go to "File" in the top menu and click "Open Data Folder."

Alt Text

Step 4: Navigate to MQL5 Folder

In the Data Folder, click on the "MQL5" folder.

Alt Text

Step 5: Open Libraries Folder

Within the MQL5 folder, locate and open the "Libraries" folder.

Alt Text

Step 6: Paste MetatraderValidation.ex5

Paste the MetatraderValidation.ex5 file you copied earlier into the "Libraries" folder.

Alt Text

Step 7: Navigate to Experts Folder

Go back to the "MQL5" folder and click on the "Experts" folder.

Alt Text

Step 8: Open the Expert Advisor (EA)

Locate and open the Expert Advisor (EA) you want to license using the MetaEditor.

Alt Text

Step 9: Add License Code (Above OnInit)

Place the following code above the OnInit() function of your Expert Advisor:

//-------------------License Code Start----------------------//

#import "MetatraderValidation.ex5"
bool Validate(string,string);
void updateConnectionStatus(string);
void updateHardwareId(string);
void updateConnectionStatusConnected(string);
void updateConnectionStatusDisconnected(string);
#import
bool auth = false;   

input string strMA1="---------------------------------------- License Input ----------------------------------------";//<<<<<<<< License Input >>>>>>>>

input string serialkey = ""; //Serial Keys
string ProductCode = "";//Product Code

//----------------License Code End--------------------------//

Alt Text

Step 10: Add License Code (OnInit)

Place the following code inside the OnInit() function of your Expert Advisor:

//-------------------License Code Start----------------------//

if(!TERMINAL_DLLS_ALLOWED)
     {
      Alert("Please Allow DLL Imports!");
     }
   if(MQLInfoInteger(MQL_OPTIMIZATION) == 0)
     {
      updateHardwareId(serialkey);
      auth = Validate(serialkey,ProductCode);
      if(auth == true)
        {
         Comment("Active");
         updateConnectionStatusConnected(serialkey);
        }
      if(auth == false)
        {
         Comment("Inactive. Contact Provider to Activate");
         ExpertRemove();
        }
     }

//----------------License Code End--------------------------//

Alt Text

Step 11: Add License Code (OnDeinit)

Place the following code inside the OnDeinit() function of your Expert Advisor:

//-------------------License Code Start----------------------//

  if(MQLInfoInteger(MQL_OPTIMIZATION) == 0)
     {
      updateConnectionStatusDisconnected(serialkey);
     }

//----------------License Code End--------------------------//

Alt Text

Step 12: Add License Code (OnTick)

Place the following code inside the OnTick() function of your Expert Advisor:

//-------------------License Code Start----------------------//

 if(MQLInfoInteger(MQL_OPTIMIZATION) == 0 && MQLInfoInteger(MQL_TESTER) == 0)
     {
      auth = Validate(serialkey,ProductCode);
      if(auth == true)
        {
         Comment("Active");
        }
      if(auth == false)
        {
         Comment("Inactive. Contact Provider to Activate");
         return ;
        }
     }

//----------------License Code End--------------------------//

Alt Text

Step 13: Replace the Product Code

Replace the string "" with the your generated Product Code, the Expert Advisor will use this Product Code to validate the license key provided by the client using the MetaTraderValidation library.

Alt Text

Alt Text

Step 14: Compile and Provide to Client

Compile the Expert Advisor and provide your client the resulting .ex5 files (yourExpert.ex5 and MetatraderValidation.ex5).

Step 15: Instruct the Client

Instruct your client to enter the Serial Key in the "serial key" input field of the Expert Advisor when placing the Expert Advisor on the chart.

Alt Text

Step 16: Validation

The Expert Advisor will validate the Serial Key with the MetaTraderValidation library and allow the client to use the Expert Advisor if the validation is successful.

Alt Text