How to protect your MT4/MT5 EA from piracy

How to Protect Your MT4/MT5 EA from Piracy: 5 Methods Compared

· License My Bot Team

You spent months building and testing an Expert Advisor. You sell it to one trader, and a few weeks later the same file turns up in a Telegram group or on a "free EA" forum. It's one of the most common complaints from MT4 and MT5 developers, and it usually happens because the EA shipped with no protection at all.

This guide covers the practical ways to protect an MT4 or MT5 EA (or indicator) from piracy and unauthorized sharing: what each method does, where it falls short, and how to combine them. Code examples use MQL4 and MQL5 functions from the official MetaQuotes documentation.

Piracy is really two different problems

When developers say "protect my EA", they usually mean one or both of these:

  1. Unauthorized use. Someone who never paid runs your EA, because a buyer passed the file on or kept using it after their rental ended.
  2. Reverse engineering. Someone decompiles the .ex4 or .ex5 file to read or copy your trading logic.

Each problem needs a different tool. Licensing controls who can run the EA. Obfuscation and encryption make the code harder to read. Neither one does the other's job, so a serious seller usually needs both.

Why a compiled EA is so easy to share

When you compile in MetaEditor, you get an .ex4 (MT4) or .ex5 (MT5) file. On its own, that file isn't tied to a trading account or a computer. MetaQuotes says as much when it describes its own MQL5 Cloud Protector: protected files "can be run on any computer similar to common EX4/EX5 files" (MetaEditor Help). Anyone who has the file can copy it into their own MQL4/Experts or MQL5/Experts folder and run it, unless you add a check that says otherwise.

Method 1: Lock the EA to an account number

The most common first step is to hardcode the buyer's account number and stop the EA if it's attached to a different account. In MQL4, AccountNumber() returns the current account number (MQL4 reference). In MQL5, AccountInfoInteger(ACCOUNT_LOGIN) does the same (MQL5 reference).

// MQL5: run only on one account
input long AllowedAccount = 12345678;   // buyer's account number

int OnInit()
  {
   if(AccountInfoInteger(ACCOUNT_LOGIN) != AllowedAccount)
     {
      Alert("This EA is not licensed for this account.");
      return(INIT_FAILED);
     }
   return(INIT_SUCCEEDED);
  }

(In real use, make the allowed number a constant rather than an input, or the buyer can just type their own.)

Where it falls short:

An account lock works for a handful of hand-delivered copies. It doesn't scale to a real customer base.

Method 2: Add a hardcoded expiry date

For rentals and trials, developers often add a date after which the EA stops. Use the trade server's time, not the local PC clock, which the user can change. TimeCurrent() "returns the last known server time" (MQL5 reference).

// Stop working after the rental period
datetime ExpiryDate = D'2027.01.31 00:00';

int OnInit()
  {
   if(TimeCurrent() >= ExpiryDate)
     {
      Alert("Your licence has expired. Contact the provider to renew.");
      return(INIT_FAILED);
     }
   return(INIT_SUCCEEDED);
  }

Where it falls short: every renewal means a new build and a new file sent to the customer. You can't end a rental early, and if you forget to set the date, the "trial" runs forever.

Method 3: Sell through the MQL5 Market

The MQL5 Market handles protection for you. Under the Market rules, each purchase creates a secure version of the product for the buyer's "hardware + OS" configuration, called an activation. The seller sets the number of activations, from a minimum of 5 to a maximum of 20 per buyer.

Where it falls short: the protection only covers copies sold through the Market, under the Market's terms. If you sell from your own website, through a Telegram channel, or to private clients, or if you want your own pricing and payment flow, you need your own licensing.

Method 4: Make the code harder to decompile

Licensing doesn't stop someone from opening the file and studying your logic. For that, MetaEditor includes MQL5 Cloud Protector (Tools > MQL5 Cloud Protector). According to MetaQuotes, it applies extra protection to the compiled EX4/EX5 file, similar to what the Market does, and your source code is never sent to the service. Unlike the Market, the protected file isn't linked to the user's computer, so it still runs anywhere.

So Cloud Protector and licensing complement each other: one protects the code, the other controls who can run it. If you combine them, test the protected build together with any libraries your EA imports before you ship it.

Method 5: Use server-checked license keys

Professional sellers keep the licence decision outside the EA file. The idea is simple:

  1. Each customer gets a unique license key (serial key).
  2. When the EA starts, and again while it runs, it sends the key to a licensing server.
  3. The server checks that the key is valid for that product, within its activation limit, and not expired.
  4. If the check fails, the EA stops trading.

Because the licence lives on the server, you ship one build to every customer and control access afterwards: extend a subscription, give a 7-day trial, or let a licence expire, all without recompiling or sending a new file. A forwarded copy doesn't help anyone who doesn't have a valid key.

No protection on a compiled file is unbreakable, and a determined cracker with enough time can attack any client-side check. The goal is to make casual sharing useless and keep control of your paying customers. Server-checked keys do that far better than hardcoded locks.

The methods compared

MethodStops file sharingChange access after deliveryOne build for all buyersProtects your code
Hardcoded account lockPartlyNoNoNo
Hardcoded expiry dateOnly after the dateNoNoNo
MQL5 MarketYes, for Market salesWithin Market rulesYesYes
MQL5 Cloud ProtectorNoNoYesYes
Server-checked license keysYesYesYesNo (combine with Cloud Protector)

How to add license keys to an MT4 or MT5 EA with License My Bot

License My Bot is a licensing system built for MetaTrader 4 and MetaTrader 5 Expert Advisors and indicators. The setup follows the steps in the documentation:

  1. Create a product. Add your EA or indicator in the dashboard. It gets a unique Product Code.
  2. Add the license code. Copy the MetatraderValidation library into your MQL4 or MQL5 Libraries folder, and paste the short License My Bot snippet above and inside OnInit, OnTick and OnDeinit, with your Product Code.
  3. Compile and deliver. Give your client the compiled .ex4 or .ex5 file together with the MetatraderValidation library. Your source code never leaves your computer.
  4. Issue a serial key to each client, with the activation limit and expiry date you choose. The client enters it in the EA's inputs when attaching it to a chart.
  5. The EA validates itself on start-up and while it runs. A valid license shows "Active". An invalid or expired one shows "Inactive. Contact Provider to Activate" and the EA stops trading.

On a Mac or on MetaTrader's VPS, add https://api.licensemybot.com/ to the allowed WebRequest URLs under Tools > Options > Expert Advisors, as described in the special instructions.

Because every license can have an expiry date, the same build covers monthly, yearly and trial access. Every plan includes the Stripe payment integration so your clients can pay you directly, and every plan starts with a free trial. See pricing for the current plans.

Step-by-step guides with screenshots:

Practical tips for EA sellers

New to licensing models in general? Start with what software licensing is and how it works.

Summary

Hardcoded account numbers and expiry dates are fine for a few private copies, but they don't scale and you can't revoke them. The MQL5 Market protects sales made through the Market. For your own sales channels, server-checked license keys give you one build for every customer and full control after delivery, and Cloud Protector adds a layer against decompiling. Together they cover both halves of the piracy problem.

Frequently Asked Questions