
How to Protect Your MT4/MT5 EA from Piracy: 5 Methods Compared
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:
- Unauthorized use. Someone who never paid runs your EA, because a buyer passed the file on or kept using it after their rental ended.
- 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:
- You have to compile a separate build for every buyer, and again whenever a buyer changes broker or account.
- Once the file is delivered, you can't change or cancel the lock. A chargeback or a refund leaves the buyer with a working EA.
- The account number sits inside the compiled file, so it's only as strong as that file's resistance to tampering.
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:
- Each customer gets a unique license key (serial key).
- When the EA starts, and again while it runs, it sends the key to a licensing server.
- The server checks that the key is valid for that product, within its activation limit, and not expired.
- 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
| Method | Stops file sharing | Change access after delivery | One build for all buyers | Protects your code |
|---|---|---|---|---|
| Hardcoded account lock | Partly | No | No | No |
| Hardcoded expiry date | Only after the date | No | No | No |
| MQL5 Market | Yes, for Market sales | Within Market rules | Yes | Yes |
| MQL5 Cloud Protector | No | No | Yes | Yes |
| Server-checked license keys | Yes | Yes | Yes | No (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:
- Create a product. Add your EA or indicator in the dashboard. It gets a unique Product Code.
- Add the license code. Copy the MetatraderValidation library into your MQL4 or MQL5
Librariesfolder, and paste the short License My Bot snippet above and insideOnInit,OnTickandOnDeinit, with your Product Code. - Compile and deliver. Give your client the compiled .ex4 or .ex5 file together with the MetatraderValidation library. Your source code never leaves your computer.
- 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.
- 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:
- Add a license key to an MT4 Expert Advisor
- Add a license key to an MT5 Expert Advisor
- MT4 indicators and MT5 indicators
Practical tips for EA sellers
- Never send the .mq4 or .mq5 source to a customer. Only send compiled files.
- Give trials as short, time-limited keys instead of a separate "demo build". When the trial ends, the key stops working on its own, and you issue a full license once the customer pays.
- Set activation limits to match what you sell. If your price covers one live account, don't hand out an unlimited key.
- Write down your licence terms (number of accounts, renewal, refunds) on your sales page, so customers know what they're buying before a support ticket comes in. Our post on common EA licensing mistakes covers the usual pitfalls.
- Protect the code as well as the licence for anything you consider valuable, using Cloud Protector or similar, and test the final build before release.
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.

