If you’ve built an MT4 Expert Advisor, indicator, or script that you want to share or sell, you need a way to control who runs it. This guide covers five copy-paste MQL4 code methods — account binding, trial periods, demo-only restrictions, symbol restrictions, and how to combine them — plus MQL5 equivalents for each. No programming background required.
Explore LicenseShield — our MT4/MT5 license management product
Code Snippet to Bond a Program to a Single MT4 Forex Account
Here’s a simple way to limit the use of your MT4 program to a single MetaTrader account. By following these steps, you can send your Expert Advisor, Indicator, or Script to anyone you want, knowing that it won’t work on any other account except the one you authorized:
1. Open your MT4 program in MetaEditor by pressing F4 in the MetaTrader platform.

2. Copy the following code.
//--- code begin
int account_number = 0;
if(account_number > 0 && account_number != AccountNumber())
{
Print("Account not allowed");
return(-1);
}
//--- code end3. Modify the second line of the code by replacing ‘0’ with the number of the account authorized to use your MT4 program:
//--- code begin
int account_number = 52068425; // <-- WRITE HERE THE AUTHORIZED ACCOUNT NUMBER
if(account_number > 0 && account_number != AccountNumber())
{
Print("Account not allowed");
return(-1);
}
//--- code end4. Paste the code above between the brackets of ‘OnInit’ or ‘init’ (in old files) function of your MT4 program.

5. Ensure that the line of code (#property strict) is at the start of the program. If it’s missing, add it — this property enforces modern compiler standards on MT4 builds 600 and above. Without it, the account check may not compile correctly in newer MetaEditor versions.

6. Compile the MT4 program.

7. Share only the executable file (‘.ex4’).

Download a Sample Expert Advisor with Account Protection
The EA is linked by default to the fictitious MetaTrader 4 forex account 52068425. Change this number to the account you want to allow use of the program.
MACD SAMPLE – ACCOUNT PROTECTION
Code Snippet to Set a Trial Period
You can limit the use of your MT4 trading tool until a specific expiry date. The EA or indicator will run normally until that date, then stop executing permanently.
Follow the same steps above but replace steps 2 and 3 with the following:
2. Copy the following code.
//--- code begin
datetime trial_end_date = 0;
if(TimeCurrent() > trial_end_date)
{
Print("Trial period has expired!");
return(-1);
}
//--- code end3. Replace `0` with your desired expiry date:
//--- code begin
datetime trial_end_date = D'31.12.2028'; // <-- SET PROGRAM EXPIRATION DATE
if(TimeCurrent() > trial_end_date)
{
Print("Trial period has expired!");
return(-1);
}
//--- code endDownload a Sample Indicator with a Trial Period
The indicator sample uses December 31, 2020 as its default expiry date — you must open the `.mq4` source file, update the `trial_end_date` line to your desired date, and recompile before distributing.
Limiting Use of Your MT4 Program to Demo Accounts Only
If you want to distribute a free trial version that cannot run on live accounts, use this snippet to block live and contest account modes:
//--- code begin
if(AccountInfoInteger(ACCOUNT_TRADE_MODE) != ACCOUNT_TRADE_MODE_DEMO)
{
Print("This program only works on demo accounts!");
return(-1);
}
//--- code endBinding to Multiple MT4 Accounts
If you need to authorize more than one account — for example, a client who runs both a personal and a funded account — use a combined condition. The key is the logic: deny access only when the current account matches neither of the authorized numbers.
//--- code begin
int AcctNbr1 = 52068425; // <-- authorized account 1
int AcctNbr2 = 26341416; // <-- authorized account 2
if(AcctNbr1 != AccountNumber() && AcctNbr2 != AccountNumber())
{
Print("Account not allowed");
return(-1);
}
//--- code endTo authorize a third account, add `int AcctNbr3 = XXXXXXXX;` and extend the condition: `&& AcctNbr3 != AccountNumber()`.
Common mistake: Using two separate `if` blocks (one per account) will deny access to every account — no single account can simultaneously equal both numbers. The single combined condition above is the correct approach.
Restricting Your MT4 Program to Specific Currency Pairs
If you built an EA or indicator for a specific instrument and want to prevent misuse on incompatible pairs, add a symbol check inside `OnInit`.
For a single symbol:
//--- code begin
string allowed_symbol = "EURUSD";
if(Symbol() != allowed_symbol)
{
Print("This program only works on EURUSD");
return(-1);
}
//--- code endFor multiple symbols:
//--- code begin
string sym = Symbol();
if(sym != "EURUSD" && sym != "GBPUSD")
{
Print("This program only works on EURUSD and GBPUSD");
return(-1);
}
//--- code endBroker naming note: Symbol names are broker-specific. “EURUSD” on one broker may appear as “EURUSDm” or “EURUSD.” on another. Verify the exact symbol name shown in your broker’s Market Watch before hardcoding it.
Combining Multiple Restrictions
You can stack any of the methods above inside the same `OnInit` function. The program checks each condition in sequence and stops at the first failure.
Example — require a specific account and enforce a trial period:
//--- code begin
int account_number = 52068425; // <-- authorized account
datetime trial_end = D'31.12.2028'; // <-- expiry date
if(account_number > 0 && account_number != AccountNumber())
{
Print("Account not allowed");
return(-1);
}
if(TimeCurrent() > trial_end)
{
Print("Trial period has expired!");
return(-1);
}
//--- code endPlace all restriction blocks consecutively at the top of `OnInit`, before any initialization logic.
The Same Methods in MQL5
The code above is MQL4-specific. For MetaTrader 5 programs, three things change:
- Remove `#property strict` — this property does not exist in MQL5.
- Replace `AccountNumber()` with `AccountInfoInteger(ACCOUNT_LOGIN)` — this is the MQL5 equivalent.
- Declare account numbers as `long` (not `int`) — MT5 account numbers can exceed the 32-bit integer range.
- Return `INIT_FAILED` instead of `-1` — this is the MQL5 constant for initialization failure.
MQL5 — account binding:
//--- code begin
long account_number = 52068425; // use long, not int
if(account_number > 0 && account_number != AccountInfoInteger(ACCOUNT_LOGIN))
{
Print("Account not allowed");
return(INIT_FAILED);
}
//--- code endMQL5 — trial period:
//--- code begin
datetime trial_end_date = D'31.12.2028';
if(TimeCurrent() > trial_end_date)
{
Print("Trial period has expired!");
return(INIT_FAILED);
}
//--- code endMQL5 — demo-only restriction:
The `AccountInfoInteger(ACCOUNT_TRADE_MODE)` function and the `ACCOUNT_TRADE_MODE_DEMO` constant exist in both MQL4 and MQL5. No changes are needed for that snippet — paste it as-is.

Adding an Extra Layer of Protection with MQL5 Cloud Protector
The MetaTrader platform includes a tool that adds an additional layer of protection: MQL5 Cloud Protector. This free service encrypts your compiled `.ex4` or `.ex5` file at a level comparable to programs listed in the MQL5 Market.

It is advisable to use Cloud Protector whenever a high degree of protection is needed — particularly for programs you intend to sell commercially. From our experience building MT4/MT5 systems since 2011, Cloud Protector significantly reduces the practical need to use DLL-based protection schemes.
MQL5 Cloud Protector was introduced in MT5 build 1700 (late 2017) and was later made available for MT4.
Advanced Protection: When Simple Measures Aren’t Enough
Code-level restrictions work well for personal EAs and small-scale distribution. As your program gains more users or commercial value, you’ll reach limitations that embedded snippets cannot solve:

- You’re distributing to many clients and need centralized license control.
- You want to issue updates only to licensed users.
- You need usage analytics — who is running your program, on which account, and when.
- You need to revoke access remotely without recompiling and redistributing.
These requirements are beyond what embedded MQL code can provide. Code-level protection cannot communicate with a server, cannot be updated without recompilation, and cannot be revoked without redistribution.
Enterprise License Management for MT4 & MT5
If you face those challenges, our Enterprise License Management for MetaTrader 4 & 5 service provides a server-side protection layer:
| Feature | Benefit |
|---|---|
| Secure access control & account binding | Prevent unauthorized usage; bind licenses to authorized users or devices |
| Real-time usage analytics & dashboards | See exactly who is using what — detect anomalies, make data-driven decisions |
| Flexible license models | Per-user, per-instance, subscription-based, or custom enterprise licensing |
| Secure update pipelines | Only licensed installations receive updates and patches |
For an automated, code-integrated licensing product, see LicenseShield — our MT4/MT5 license management SaaS built specifically for commercial EA and indicator distribution.
Choosing the Right Level of Protection
| Scenario | Recommended Approach |
|---|---|
| Personal EA or a handful of trusted clients | Code-level snippets (this guide) |
| Wider commercial distribution, up to ~20 clients | Code-level snippets + MQL5 Cloud Protector |
| Enterprise distribution: subscriptions, analytics, remote revocation | Enterprise License Management |
Frequently Asked Questions
Account Binding
Can I bind my MT4 program to multiple accounts at the same time?
Yes. Use a combined condition that denies access only when the current account matches neither of the authorized numbers:
int AcctNbr1 = 52068425;
int AcctNbr2 = 26341416;
if(AcctNbr1 != AccountNumber() && AcctNbr2 != AccountNumber())
{
Print("Account not allowed");
return(-1);
}Do not use two separate `if` blocks — that logic denies every account unconditionally.
Can I combine account restriction with a trial period?
Yes. Paste both restriction blocks consecutively at the top of `OnInit`. The program checks each condition in sequence — the first failure stops initialization. See the Combining Multiple Restrictions section above for the complete snippet.
MQL5 and Platform Compatibility
Does this code work in MQL5 / MetaTrader 5?
Yes, with three changes: remove `#property strict`, replace `AccountNumber()` with `AccountInfoInteger(ACCOUNT_LOGIN)`, and declare the account number as `long` instead of `int`. Return `INIT_FAILED` instead of `-1`. The MQL5 Equivalents section above has complete code for each method.
How do I restrict my program to specific currency pairs?
Use `Symbol()` inside `OnInit` to check the chart instrument against an allowed list. To restrict to one pair: `if(Symbol() != “EURUSD”) { Print(“Wrong symbol”); return(-1); }`. For multiple pairs, use a combined condition: `if(sym != “EURUSD” && sym != “GBPUSD”)`. Make sure the symbol string matches your broker’s exact naming.
Troubleshooting
Why does AccountNumber() return 0?
`AccountNumber()` returns 0 when the MetaTrader terminal is not yet connected to an account. This typically happens immediately after reopening MT4 before the connection is established. Wait for the terminal to reconnect and reload the indicator or EA — the account number will return correctly once the session is active.
Compilation gives errors after adding #property strict — why?
`#property strict` enforces modern compiler rules on code that previously compiled without them. It can surface type conversion warnings and undeclared identifier errors in older programs. Warnings labeled “possible loss of data due to type conversion” are generally safe to ignore and do not prevent compilation. Errors labeled as actual errors must be corrected before the file will compile.
If you cannot resolve the errors, omit `#property strict` and cast `AccountNumber()` explicitly:
extern int account_number = 52068425;
if(account_number > 0 && account_number != (int)AccountNumber())
{
Print("Account not allowed");
return(-1);
}Can I use Alert() instead of Print() for the error message?
Yes. Replacing `Print(“Account not allowed”)` with `Alert(“Account not allowed”)` will show a visible popup dialog in MetaTrader instead of writing silently to the Journal tab. Both stop the program’s initialization — the difference is only in how the user is notified.
Is MQL5 Cloud Protector actually secure?
MQL5 Cloud Protector adds meaningful encryption, but no compiled binary is permanently unbreakable. Cloud Protector raises the difficulty significantly and makes casual reverse-engineering impractical. For commercial distribution requiring hard enforcement, combine Cloud Protector with a server-side licensing system — the code snippets in this guide alone provide modest protection that can be bypassed by anyone with a hex editor and patience.
Protect Your MT4 Programs and Keep Them Safe
Your MT4 programs are valuable intellectual property. Protecting them is critical to safeguarding both your business and your reputation.
The code methods in this guide cover the most common use cases: single-account binding, trial periods, demo restrictions, symbol restrictions, and any combination of these. When your distribution grows beyond a handful of clients, Enterprise License Management for MT4/5 provides infrastructure that embedded code cannot — remote revocation, usage analytics, and automated license control.
For professional help implementing any of these methods or building custom protection tailored to your setup, visit our MT4 programming services.
Alongside these measures: keep regular backups of your source `.mq4` files, share programs only with trusted partners, and never distribute the source file — only the compiled `.ex4`.

Leave a Reply